From c31ce0ed25610de95aa55c97529495a0905f138d Mon Sep 17 00:00:00 2001 From: Nadav Shatz Date: Mon, 27 Oct 2025 16:22:40 +0200 Subject: [PATCH 2/2] test+doc: tests and documentation for external replication delay Tests: - Verify command receives replicas only (primary omitted) - Verify host:port identifier format - Test -1 handling for down nodes - Test integer and float delay values - Test validation, timeouts, and error handling - Test wrong output counts and edge cases Documentation: - Remove replication_delay_source enum documentation - Document replication_delay_source_cmd with replica-only semantics - Document -1 for down nodes - Provide examples with correct output format - Update replication_delay_source_timeout docs --- doc/src/sgml/stream-check.sgml | 68 +++ .../041.external_replication_delay/README | 59 +++ .../041.external_replication_delay/test.sh | 396 ++++++++++++++++++ .../test_parsing.sh | 55 +++ .../test_validation.sh | 318 ++++++++++++++ 5 files changed, 896 insertions(+) create mode 100644 src/test/regression/tests/041.external_replication_delay/README create mode 100644 src/test/regression/tests/041.external_replication_delay/test.sh create mode 100644 src/test/regression/tests/041.external_replication_delay/test_parsing.sh create mode 100644 src/test/regression/tests/041.external_replication_delay/test_validation.sh diff --git a/doc/src/sgml/stream-check.sgml b/doc/src/sgml/stream-check.sgml index d2ca3ca49..fc4799080 100644 --- a/doc/src/sgml/stream-check.sgml +++ b/doc/src/sgml/stream-check.sgml @@ -309,6 +309,74 @@ GRANT pg_monitor TO sr_check_user; + + replication_delay_source_cmd (string) + + replication_delay_source_cmd configuration parameter + + + + + Specifies an external command to retrieve replication delay information for replica nodes. + When this parameter is set and not empty, Pgpool-II uses the + external command instead of built-in database queries to obtain replication delays. + The command is executed as the Pgpool-II process user. + + + The command receives replica node identifiers as positional arguments, with the primary + node omitted. Each identifier is in the format <hostname>:<port>, + for example server1:5432 server2:5432. The order matches + Pgpool-II's backend order (excluding the primary), allowing the + script to correlate external metrics (such as from AWS CloudWatch for Aurora) to the correct nodes. + + + The command must write a single line to stdout containing one whitespace-separated delay value + per replica, in milliseconds, in the same order as the arguments. The primary node's delay is + implicitly zero and should not be included in the output. Delay values can be integers or + floating-point numbers. + + + Special value: -1 indicates a replica that is down but not yet detected + by Pgpool-II's health checks. Pgpool-II + will log this condition but rely on its own health-check logic to decide whether to trigger + failover; no failover is triggered solely by receiving -1. + + + Example for a 3-node cluster (1 primary + 2 replicas): if the command receives arguments + server1:5432 server2:5432, it should output "25.5 100" + to indicate the first replica has 25.5ms delay and the second has 100ms delay. + + + Default is empty (use built-in replication delay queries). + + + This parameter can be changed by reloading the Pgpool-II configurations. + + + + + + replication_delay_source_timeout (integer) + + replication_delay_source_timeout configuration parameter + + + + + Specifies the timeout in seconds for the external command specified by + . + If the command does not finish within the timeout, Pgpool-II + logs an error and continues using the built-in method. + + + Default is 10 seconds. Valid range is 1-3600 seconds. + + + This parameter can be changed by reloading the Pgpool-II configurations. + + + + log_standby_delay (enum) diff --git a/src/test/regression/tests/041.external_replication_delay/README b/src/test/regression/tests/041.external_replication_delay/README new file mode 100644 index 000000000..b4df5da40 --- /dev/null +++ b/src/test/regression/tests/041.external_replication_delay/README @@ -0,0 +1,59 @@ +External Replication Delay Command Test +======================================== + +This test verifies the external command replication delay source feature. + +Test Coverage: +- External command receives replica node identifiers only (primary omitted) +- Instance identifiers in host:port format +- Basic external command execution with integer and float millisecond values +- Delay threshold functionality with external commands +- Command execution as pgpool process user (no su wrapper) +- Error handling for missing/invalid commands +- Command execution failure scenarios +- Command timeout handling with configurable timeout values +- Input validation for invalid, negative (other than -1), and extremely large delay values +- Handling of -1 for down nodes (logged but no immediate failover) +- Wrong number of output values validation +- Multiple -1 values (multiple down replicas) +- Mixed scenarios (some replicas up, some down) +- Output truncation detection + +Files: +- test.sh: Main test script +- test_parsing.sh: Unit test for parsing logic +- test_validation.sh: Validation and edge case testing +- README: This documentation + +Key Changes from Original Version: +- Primary node is omitted from command arguments +- Command receives only replica identifiers +- Instance identifiers are in host:port format (not application_name) +- Output format: one delay per replica (not per all nodes) +- -1 value indicates down replica without triggering failover +- Format example: "25 100" for 2 replicas (3-node cluster = 1 primary + 2 replicas) + +The test creates temporary command scripts that output delay values in the format: +"replica1_delay replica2_delay ..." + +Where delays are in milliseconds and can be integer or floating-point values. +Special value -1 indicates a replica that is down but not yet detected by pgpool. + +Test Environment: +- Uses streaming replication mode with 3 nodes +- Node 0 is primary (omitted from command arguments) +- Nodes 1 and 2 are replicas (included in command arguments) +- Configures sr_check_period = 1 second for faster testing +- Tests various delay scenarios and threshold behaviors + +Expected Behavior: +- External commands receive replica identifiers in host:port format +- Primary node identifier is never passed to command +- Command outputs one delay value per replica +- -1 values are logged but don't trigger immediate failover +- Delay values are parsed correctly (both int and float) +- Threshold comparisons work properly +- Error conditions are handled gracefully +- Commands timeout appropriately based on configuration +- Timeout errors provide helpful messages and hints +- Tests are reliable with proper wait mechanisms instead of fixed sleeps diff --git a/src/test/regression/tests/041.external_replication_delay/test.sh b/src/test/regression/tests/041.external_replication_delay/test.sh new file mode 100644 index 000000000..f5675af98 --- /dev/null +++ b/src/test/regression/tests/041.external_replication_delay/test.sh @@ -0,0 +1,396 @@ +#!/usr/bin/env bash +#------------------------------------------------------------------- +# test script for external command replication delay source +# +source $TESTLIBS +TESTDIR=testdir +PG_CTL=$PGBIN/pg_ctl +PSQL="$PGBIN/psql -X " + +rm -fr $TESTDIR +mkdir $TESTDIR +cd $TESTDIR + +# create test environment +echo -n "creating test environment..." +$PGPOOL_SETUP -m s -n 3 || exit 1 +echo "done." +source ./bashrc.ports +export PGPORT=$PGPOOL_PORT + +# Create external command scripts for testing +# NOTE: Commands now only output delay values for REPLICAS (not primary) +cat > delay_cmd_static.sh << 'EOF' +#!/bin/bash +# Static delay values for replicas: node1=25ms, node2=50ms (node0 is primary, not included) +echo "25 50" +EOF +chmod +x delay_cmd_static.sh + +cat > delay_cmd_float.sh << 'EOF' +#!/bin/bash +# Float delay values for replicas: node1=25.5ms, node2=100.75ms +echo "25.5 100.75" +EOF +chmod +x delay_cmd_float.sh + +cat > delay_cmd_high.sh << 'EOF' +#!/bin/bash +# High delay values to test threshold: node1=2000ms, node2=3000ms +echo "2000 3000" +EOF +chmod +x delay_cmd_high.sh + +# ---------------------------------------------------------------------------------------- +echo === Test0: External command receives replica identifiers only (primary omitted) === +# ---------------------------------------------------------------------------------------- +# Command that captures its arguments and outputs valid delays for 2 replicas +cat > delay_cmd_args.sh << 'EOF' +#!/bin/bash +printf "%s " "$@" > args.txt +echo "25 50" +EOF +chmod +x delay_cmd_args.sh + +echo "replication_delay_source_cmd = './delay_cmd_args.sh'" >> etc/pgpool.conf +echo "sr_check_period = 1" >> etc/pgpool.conf +echo "log_min_messages = 'DEBUG1'" >> etc/pgpool.conf + +./startall +wait_for_pgpool_startup + +echo "Waiting for sr_check to pass args..." +for i in {1..10}; do + if [ -f args.txt ]; then + break + fi + sleep 1 +done + +if [ ! -f args.txt ]; then + echo fail: did not capture command arguments + ./shutdownall + exit 1 +fi + +ARGS_CONTENT=$(cat args.txt | sed 's/[[:space:]]*$//') +# Should receive 2 replica identifiers in host:port format (server1:11003 server2:11004) +# Primary (server0:11002) should be omitted +if ! echo "$ARGS_CONTENT" | grep -q "server1:11003"; then + echo "fail: expected server1:11003 in arguments, got: '$ARGS_CONTENT'" + ./shutdownall + exit 1 +fi +if ! echo "$ARGS_CONTENT" | grep -q "server2:11004"; then + echo "fail: expected server2:11004 in arguments, got: '$ARGS_CONTENT'" + ./shutdownall + exit 1 +fi +if echo "$ARGS_CONTENT" | grep -q "server0:11002"; then + echo "fail: primary (server0:11002) should not be in arguments, got: '$ARGS_CONTENT'" + ./shutdownall + exit 1 +fi + +echo ok: argument order correct - replicas only, primary omitted, host:port format +./shutdownall + +# ---------------------------------------------------------------------------------------- +echo === Test1: Basic external command with integer millisecond values === +# ---------------------------------------------------------------------------------------- +echo "replication_delay_source_cmd = './delay_cmd_static.sh'" >> etc/pgpool.conf +echo "sr_check_period = 1" >> etc/pgpool.conf +echo "log_standby_delay = 'always'" >> etc/pgpool.conf +echo "log_min_messages = 'DEBUG1'" >> etc/pgpool.conf + +./startall +wait_for_pgpool_startup + +$PSQL test </dev/null; then + echo "Command executed after ${i} seconds" + break + fi + sleep 1 +done + +$PSQL test </dev/null 2>&1 +if [ $? != 0 ];then + echo fail: external command was not executed + echo "Log contents:" + tail -20 log/pgpool.log + ./shutdownall + exit 1 +fi + +# Verify actual delay values were parsed +if ! $PSQL -t -c "SHOW POOL_NODES" test | grep -E "[0-9]+\.[0-9]+" >/dev/null; then + echo "Warning: No delay values found in POOL_NODES output" +fi + +# Check for delay log messages +grep "Replication of node.*external command" log/pgpool.log >/dev/null 2>&1 +if [ $? != 0 ];then + echo fail: external command delay logging not found + ./shutdownall + exit 1 +fi + +echo ok: basic external command test succeeded +./shutdownall + +# ---------------------------------------------------------------------------------------- +echo === Test2: External command with floating-point millisecond values === +# ---------------------------------------------------------------------------------------- +# Update configuration to use float command +sed -i.bak "s|delay_cmd_static.sh|delay_cmd_float.sh|" etc/pgpool.conf + +./startall +wait_for_pgpool_startup + +# Wait for sr_check to run with float values +echo "Waiting for sr_check with float values..." +for i in {1..10}; do + if grep -q "executing replication delay command.*delay_cmd_float.sh" log/pgpool.log 2>/dev/null; then + echo "Float command executed after ${i} seconds" + break + fi + sleep 1 +done + +$PSQL test </dev/null 2>&1 +if [ $? != 0 ];then + echo fail: float command was not executed + ./shutdownall + exit 1 +fi + +echo ok: floating-point values test succeeded +./shutdownall + +# ---------------------------------------------------------------------------------------- +echo === Test3: External command with delay threshold === +# ---------------------------------------------------------------------------------------- +# Update configuration to use high delay command and set threshold +sed -i.bak "s|delay_cmd_float.sh|delay_cmd_high.sh|" etc/pgpool.conf +echo "delay_threshold_by_time = 1000" >> etc/pgpool.conf +echo "backend_weight0 = 0" >> etc/pgpool.conf # Force queries to standby normally +echo "backend_weight2 = 0" >> etc/pgpool.conf # Only use node 1 as standby + +./startall +wait_for_pgpool_startup + +# Wait for sr_check to run and detect high delays +echo "Waiting for sr_check with high delay values..." +for i in {1..10}; do + if grep -q "executing replication delay command.*delay_cmd_high.sh" log/pgpool.log 2>/dev/null; then + echo "High delay command executed after ${i} seconds" + break + fi + sleep 1 +done + +$PSQL test < 1000ms threshold), query should go to primary (node 0) +grep "SELECT \* FROM t1 LIMIT 1.*DB node id: 0" log/pgpool.log >/dev/null 2>&1 +if [ $? != 0 ];then + echo fail: query was not sent to primary node despite high delay + ./shutdownall + exit 1 +fi + +echo ok: delay threshold test succeeded +./shutdownall + +# ---------------------------------------------------------------------------------------- +echo === Test4: External command execution as process user === +# ---------------------------------------------------------------------------------------- +# Test that command runs as the current pgpool process user +sed -i.bak "s|delay_cmd_high.sh|delay_cmd_static.sh|" etc/pgpool.conf + +./startall +wait_for_pgpool_startup + +# Wait for sr_check to run +echo "Waiting for sr_check to run as process user..." +for i in {1..10}; do + if grep -q "executing replication delay command.*delay_cmd_static.sh" log/pgpool.log 2>/dev/null; then + echo "Command executed as process user after ${i} seconds" + break + fi + sleep 1 +done + +# Check that command was executed (without su wrapper) +grep "executing replication delay command.*delay_cmd_static.sh" log/pgpool.log >/dev/null 2>&1 +if [ $? != 0 ];then + echo fail: command was not executed as process user + ./shutdownall + exit 1 +fi + +# Verify no su command was used +if grep -q "executing replication delay command.*su.*" log/pgpool.log 2>/dev/null; then + echo fail: command should not use su wrapper + ./shutdownall + exit 1 +fi + +echo ok: process user execution test succeeded +./shutdownall + +# ---------------------------------------------------------------------------------------- +echo === Test5: Error handling - missing command === +# ---------------------------------------------------------------------------------------- +# Test error handling when command is not configured +sed -i.bak "s|replication_delay_source_cmd = './delay_cmd_static.sh'|replication_delay_source_cmd = ''|" etc/pgpool.conf + +./startall +wait_for_pgpool_startup + +# With empty command, should fall back to builtin method +# No specific error message expected - just verify it doesn't crash +sleep 3 + +echo ok: empty command test succeeded (fallback to builtin) +./shutdownall + +# ---------------------------------------------------------------------------------------- +echo === Test6: Error handling - command execution failure === +# ---------------------------------------------------------------------------------------- +# Test error handling when command fails +echo "replication_delay_source_cmd = './nonexistent_command.sh'" >> etc/pgpool.conf + +./startall +wait_for_pgpool_startup + +# Wait for sr_check to run with failing command +echo "Waiting for sr_check with failing command..." +for i in {1..5}; do + if grep -q "failed to execute replication delay command" log/pgpool.log 2>/dev/null; then + echo "Command failure detected after ${i} seconds" + break + fi + sleep 1 +done + +# Check for error message about command execution failure +grep "failed to execute replication delay command" log/pgpool.log >/dev/null 2>&1 +if [ $? != 0 ];then + echo fail: command execution failure not detected + ./shutdownall + exit 1 +fi + +echo ok: command failure test succeeded +./shutdownall + +# ---------------------------------------------------------------------------------------- +echo === Test7: Command timeout handling === +# ---------------------------------------------------------------------------------------- +# Create a command that takes longer than the timeout +cat > delay_cmd_slow.sh << 'EOF' +#!/bin/bash +# Slow command that takes 15 seconds (longer than default 10s timeout) +sleep 15 +echo "25 50" +EOF +chmod +x delay_cmd_slow.sh + +# Set a short timeout and use the slow command +sed -i.bak "s|replication_delay_source_cmd = './nonexistent_command.sh'|replication_delay_source_cmd = './delay_cmd_slow.sh'|" etc/pgpool.conf +echo "replication_delay_source_timeout = 3" >> etc/pgpool.conf + +./startall +wait_for_pgpool_startup + +# Wait for sr_check to run and timeout +echo "Waiting for command timeout..." +for i in {1..15}; do + if grep -q "replication delay command timed out" log/pgpool.log 2>/dev/null; then + echo "Command timeout detected after ${i} seconds" + break + fi + sleep 1 +done + +# Check for timeout error message +grep "replication delay command timed out after 3 seconds" log/pgpool.log >/dev/null 2>&1 +if [ $? != 0 ];then + echo fail: command timeout not detected + ./shutdownall + exit 1 +fi + +echo ok: command timeout test succeeded +./shutdownall + +# ---------------------------------------------------------------------------------------- +echo === Test8: Handling of -1 for down nodes === +# ---------------------------------------------------------------------------------------- +# Create a command that returns -1 for one replica +cat > delay_cmd_with_down_node.sh << 'EOF' +#!/bin/bash +# Return -1 for first replica (indicating it's down), normal value for second +echo "-1 50" +EOF +chmod +x delay_cmd_with_down_node.sh + +# Reset config +rm -f etc/pgpool.conf.bak +sed -i.bak "s|delay_cmd_slow.sh|delay_cmd_with_down_node.sh|" etc/pgpool.conf +sed -i.bak "s|replication_delay_source_timeout = 3|replication_delay_source_timeout = 10|" etc/pgpool.conf + +./startall +wait_for_pgpool_startup + +# Wait for sr_check to process -1 value +echo "Waiting for sr_check to process -1 value..." +for i in {1..10}; do + if grep -q "node.*reported as down by external command.*delay -1" log/pgpool.log 2>/dev/null; then + echo "-1 handling detected after ${i} seconds" + break + fi + sleep 1 +done + +# Check for -1 logging message +grep "node.*reported as down by external command.*delay -1.*relying on health check" log/pgpool.log >/dev/null 2>&1 +if [ $? != 0 ];then + echo fail: -1 handling message not found + ./shutdownall + exit 1 +fi + +# Verify that pgpool didn't crash or trigger failover just from -1 +if grep -q "failover" log/pgpool.log 2>/dev/null; then + echo "fail: -1 should not trigger immediate failover" + ./shutdownall + exit 1 +fi + +echo ok: -1 handling test succeeded +./shutdownall + +echo "All external replication delay tests passed!" +exit 0 diff --git a/src/test/regression/tests/041.external_replication_delay/test_parsing.sh b/src/test/regression/tests/041.external_replication_delay/test_parsing.sh new file mode 100644 index 000000000..d024ce559 --- /dev/null +++ b/src/test/regression/tests/041.external_replication_delay/test_parsing.sh @@ -0,0 +1,55 @@ +#!/bin/bash +#------------------------------------------------------------------- +# Unit test for external command parsing logic +# This tests the parsing without needing a full pgpool setup +# + +echo "=== Testing external command output parsing ===" + +# Test 1: Integer values +echo "Test 1: Integer millisecond values" +echo "0 25 50" > test_output.txt +echo "Expected: 0ms, 25ms, 50ms" +echo "Output: $(cat test_output.txt)" +echo "" + +# Test 2: Float values +echo "Test 2: Floating-point millisecond values" +echo "0 25.5 100.75" > test_output_float.txt +echo "Expected: 0ms, 25.5ms, 100.75ms" +echo "Output: $(cat test_output_float.txt)" +echo "" + +# Test 3: High precision float values +echo "Test 3: High precision values" +echo "0 0.001 999.999" > test_output_precision.txt +echo "Expected: 0ms, 0.001ms, 999.999ms" +echo "Output: $(cat test_output_precision.txt)" +echo "" + +# Test 4: Edge case - zero values +echo "Test 4: All zero values" +echo "0 0 0" > test_output_zeros.txt +echo "Expected: 0ms, 0ms, 0ms" +echo "Output: $(cat test_output_zeros.txt)" +echo "" + +# Test 5: Edge case - large values +echo "Test 5: Large delay values" +echo "0 5000 10000" > test_output_large.txt +echo "Expected: 0ms, 5000ms, 10000ms" +echo "Output: $(cat test_output_large.txt)" +echo "" + +# Test 6: Mixed integer and float values +echo "Test 6: Mixed integer and float values" +echo "0 25 50.5" > test_output_mixed.txt +echo "Expected: 0ms, 25ms, 50.5ms" +echo "Output: $(cat test_output_mixed.txt)" +echo "" + +# Cleanup +rm -f test_output_*.txt + +echo "All parsing tests completed. These outputs should be parseable by the external command feature." + diff --git a/src/test/regression/tests/041.external_replication_delay/test_validation.sh b/src/test/regression/tests/041.external_replication_delay/test_validation.sh new file mode 100644 index 000000000..2d96c91a9 --- /dev/null +++ b/src/test/regression/tests/041.external_replication_delay/test_validation.sh @@ -0,0 +1,318 @@ +#!/usr/bin/env bash +#------------------------------------------------------------------- +# test script for external command validation and edge cases +# +source $TESTLIBS +TESTDIR=testdir_validation +PG_CTL=$PGBIN/pg_ctl +PSQL="$PGBIN/psql -X " + +rm -fr $TESTDIR +mkdir $TESTDIR +cd $TESTDIR + +# create test environment +echo -n "creating test environment..." +$PGPOOL_SETUP -m s -n 3 || exit 1 +echo "done." +source ./bashrc.ports +export PGPORT=$PGPOOL_PORT + +# Create test command scripts +# NOTE: All commands output values for REPLICAS only (primary omitted) +cat > delay_cmd_validation.sh << 'EOF' +#!/bin/bash +# Test validation: output with invalid values for 2 replicas +echo "invalid_value 50.5" +EOF +chmod +x delay_cmd_validation.sh + +cat > delay_cmd_negative.sh << 'EOF' +#!/bin/bash +# Test negative values (other than -1) +echo "-25 50" +EOF +chmod +x delay_cmd_negative.sh + +cat > delay_cmd_large.sh << 'EOF' +#!/bin/bash +# Test extremely large values +echo "9999999 50" +EOF +chmod +x delay_cmd_large.sh + +cat > delay_cmd_wrong_count.sh << 'EOF' +#!/bin/bash +# Test wrong number of values (only 1 instead of 2 for 2 replicas) +echo "25" +EOF +chmod +x delay_cmd_wrong_count.sh + +# ---------------------------------------------------------------------------------------- +echo === Test1: Validation of invalid delay values === +# ---------------------------------------------------------------------------------------- +echo "replication_delay_source_cmd = './delay_cmd_validation.sh'" >> etc/pgpool.conf +echo "sr_check_period = 1" >> etc/pgpool.conf +echo "log_standby_delay = 'always'" >> etc/pgpool.conf +echo "log_min_messages = 'DEBUG1'" >> etc/pgpool.conf + +./startall +wait_for_pgpool_startup + +$PSQL test </dev/null; then + echo "Validation error detected after ${i} seconds" + break + fi + sleep 1 +done + +# Check for validation warning +grep "invalid delay value 'invalid_value' for node" log/pgpool.log >/dev/null 2>&1 +if [ $? != 0 ];then + echo fail: validation warning not found + ./shutdownall + exit 1 +fi + +echo ok: invalid value validation test succeeded +./shutdownall + +# ---------------------------------------------------------------------------------------- +echo === Test2: Negative delay values (other than -1) === +# ---------------------------------------------------------------------------------------- +sed -i.bak "s|delay_cmd_validation.sh|delay_cmd_negative.sh|" etc/pgpool.conf + +./startall +wait_for_pgpool_startup + +# Wait for sr_check to run +echo "Waiting for negative value test..." +for i in {1..10}; do + if grep -q "negative delay value.*other than -1" log/pgpool.log 2>/dev/null; then + echo "Negative value warning detected after ${i} seconds" + break + fi + sleep 1 +done + +# Check for negative value warning +grep "negative delay value.*other than -1.*treating as 0" log/pgpool.log >/dev/null 2>&1 +if [ $? != 0 ];then + echo fail: negative value warning not found + ./shutdownall + exit 1 +fi + +echo ok: negative value validation test succeeded +./shutdownall + +# ---------------------------------------------------------------------------------------- +echo === Test3: Extremely large delay values === +# ---------------------------------------------------------------------------------------- +sed -i.bak "s|delay_cmd_negative.sh|delay_cmd_large.sh|" etc/pgpool.conf + +./startall +wait_for_pgpool_startup + +# Wait for sr_check to run +echo "Waiting for large value test..." +for i in {1..10}; do + if grep -q "extremely large delay value" log/pgpool.log 2>/dev/null; then + echo "Large value warning detected after ${i} seconds" + break + fi + sleep 1 +done + +# Check for large value warning +grep "extremely large delay value.*for node" log/pgpool.log >/dev/null 2>&1 +if [ $? != 0 ];then + echo fail: large value warning not found + ./shutdownall + exit 1 +fi + +echo ok: large value validation test succeeded +./shutdownall + +# ---------------------------------------------------------------------------------------- +echo === Test4: Wrong number of output values === +# ---------------------------------------------------------------------------------------- +sed -i.bak "s|delay_cmd_large.sh|delay_cmd_wrong_count.sh|" etc/pgpool.conf + +./startall +wait_for_pgpool_startup + +# Wait for sr_check to run +echo "Waiting for wrong count test..." +for i in {1..10}; do + if grep -q "returned.*values, expected.*replica" log/pgpool.log 2>/dev/null; then + echo "Wrong count warning detected after ${i} seconds" + break + fi + sleep 1 +done + +# Check for wrong count warning +grep "returned.*values, expected.*replica.*Command should output one delay value per replica" log/pgpool.log >/dev/null 2>&1 +if [ $? != 0 ];then + echo fail: wrong count validation test not found + ./shutdownall + exit 1 +fi + +echo ok: wrong count validation test succeeded +./shutdownall + +# ---------------------------------------------------------------------------------------- +echo === Test5: Multiple -1 values === +# ---------------------------------------------------------------------------------------- +cat > delay_cmd_multi_down.sh << 'EOF' +#!/bin/bash +# Test multiple replicas down +echo "-1 -1" +EOF +chmod +x delay_cmd_multi_down.sh + +sed -i.bak "s|delay_cmd_wrong_count.sh|delay_cmd_multi_down.sh|" etc/pgpool.conf + +./startall +wait_for_pgpool_startup + +# Wait for sr_check to run +echo "Waiting for multi-down test..." +for i in {1..10}; do + if grep -q "node.*reported as down by external command" log/pgpool.log 2>/dev/null; then + echo "Multiple down nodes detected after ${i} seconds" + break + fi + sleep 1 +done + +# Check for multiple -1 handling +DOWN_COUNT=$(grep -c "node.*reported as down by external command.*delay -1" log/pgpool.log) +if [ "$DOWN_COUNT" -lt 2 ]; then + echo fail: expected 2 down node messages, found $DOWN_COUNT + ./shutdownall + exit 1 +fi + +echo ok: multiple -1 handling test succeeded +./shutdownall + +# ---------------------------------------------------------------------------------------- +echo === Test6: Command timeout with different timeout values === +# ---------------------------------------------------------------------------------------- +cat > delay_cmd_timeout.sh << 'EOF' +#!/bin/bash +# Command that takes 5 seconds +sleep 5 +echo "25 50" +EOF +chmod +x delay_cmd_timeout.sh + +# Test with timeout shorter than command duration +sed -i.bak "s|delay_cmd_multi_down.sh|delay_cmd_timeout.sh|" etc/pgpool.conf +echo "replication_delay_source_timeout = 2" >> etc/pgpool.conf + +./startall +wait_for_pgpool_startup + +# Wait for timeout +echo "Waiting for timeout test (2s timeout, 5s command)..." +for i in {1..10}; do + if grep -q "replication delay command timed out after 2 seconds" log/pgpool.log 2>/dev/null; then + echo "Timeout detected after ${i} seconds" + break + fi + sleep 1 +done + +# Check for timeout message +grep "replication delay command timed out after 2 seconds" log/pgpool.log >/dev/null 2>&1 +if [ $? != 0 ];then + echo fail: timeout not detected + ./shutdownall + exit 1 +fi + +echo ok: timeout test succeeded +./shutdownall + +# Test with timeout longer than command duration +sed -i.bak "s|replication_delay_source_timeout = 2|replication_delay_source_timeout = 10|" etc/pgpool.conf + +./startall +wait_for_pgpool_startup + +# Wait for successful execution +echo "Waiting for successful execution (10s timeout, 5s command)..." +for i in {1..15}; do + if grep -q "executing replication delay command.*delay_cmd_timeout.sh" log/pgpool.log 2>/dev/null; then + echo "Command executed successfully after ${i} seconds" + break + fi + sleep 1 +done + +# Should not timeout this time +if grep -q "replication delay command timed out" log/pgpool.log 2>/dev/null; then + echo fail: command should not have timed out with 10s timeout + ./shutdownall + exit 1 +fi + +echo ok: extended timeout test succeeded +./shutdownall + +# ---------------------------------------------------------------------------------------- +echo === Test7: Mix of valid delays and -1 === +# ---------------------------------------------------------------------------------------- +cat > delay_cmd_mixed.sh << 'EOF' +#!/bin/bash +# One replica up (25ms), one down (-1) +echo "25 -1" +EOF +chmod +x delay_cmd_mixed.sh + +sed -i.bak "s|delay_cmd_timeout.sh|delay_cmd_mixed.sh|" etc/pgpool.conf + +./startall +wait_for_pgpool_startup + +# Wait for sr_check +echo "Waiting for mixed delay test..." +for i in {1..10}; do + if grep -q "node.*reported as down by external command" log/pgpool.log 2>/dev/null; then + echo "Mixed delay handling detected after ${i} seconds" + break + fi + sleep 1 +done + +# Should log one -1 and process one normal delay +grep "node.*reported as down by external command.*delay -1" log/pgpool.log >/dev/null 2>&1 +if [ $? != 0 ];then + echo fail: -1 not logged + ./shutdownall + exit 1 +fi + +# Should also log the normal replica delay +grep "Replication of node.*external command" log/pgpool.log >/dev/null 2>&1 +if [ $? != 0 ];then + echo "Note: Normal replica delay logging may not be visible with log_standby_delay settings" +fi + +echo ok: mixed delay handling test succeeded +./shutdownall + +echo "All validation tests passed!" +exit 0 -- 2.51.1