Skip to content

Commit 54a16df

Browse files
committed
Make the pg_rewind regression tests more robust on slow systems.
There were a couple of hard-coded sleeps in the tests: to wait for standby to catch up with master, and to wait for promotion with "pg_ctl promote" to complete. Instead of a fixed, hard-coded sleep, poll the server with a query once a second. This isn't ideal either, and I wish we had a better solution for real-world applications too, but this should fix the immediate problem. Patch by Michael Paquier, with some editing by me.
1 parent cef939c commit 54a16df

File tree

1 file changed

+41
-5
lines changed

1 file changed

+41
-5
lines changed

src/bin/pg_rewind/RewindTest.pm

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,37 @@ sub check_query
125125
}
126126
}
127127

128+
# Run a query once a second, until it returns 't' (i.e. SQL boolean true).
129+
sub poll_query_until
130+
{
131+
my ($query, $connstr) = @_;
132+
133+
my $max_attempts = 30;
134+
my $attempts = 0;
135+
my ($stdout, $stderr);
136+
137+
while ($attempts < $max_attempts)
138+
{
139+
my $cmd = ['psql', '-At', '-c', "$query", '-d', "$connstr" ];
140+
my $result = run $cmd, '>', \$stdout, '2>', \$stderr;
141+
142+
chomp($stdout);
143+
if ($stdout eq "t")
144+
{
145+
return 1;
146+
}
147+
148+
# Wait a second before retrying.
149+
sleep 1;
150+
$attempts++;
151+
}
152+
153+
# The query result didn't change in 30 seconds. Give up. Print the stderr
154+
# from the last attempt, hopefully that's useful for debugging.
155+
diag $stderr;
156+
return 0;
157+
}
158+
128159
sub append_to_file
129160
{
130161
my($filename, $str) = @_;
@@ -185,16 +216,19 @@ sub create_standby
185216
# Base backup is taken with xlog files included
186217
system_or_bail("pg_basebackup -D $test_standby_datadir -p $port_master -x >>$log_path 2>&1");
187218
append_to_file("$test_standby_datadir/recovery.conf", qq(
188-
primary_conninfo='$connstr_master'
219+
primary_conninfo='$connstr_master application_name=rewind_standby'
189220
standby_mode=on
190221
recovery_target_timeline='latest'
191222
));
192223

193224
# Start standby
194225
system_or_bail("pg_ctl -w -D $test_standby_datadir -o \"-k $tempdir_short --listen-addresses='' -p $port_standby\" start >>$log_path 2>&1");
195226

196-
# sleep a bit to make sure the standby has caught up.
197-
sleep 1;
227+
# Wait until the standby has caught up with the primary, by polling
228+
# pg_stat_replication.
229+
my $caughtup_query = "SELECT pg_current_xlog_location() = replay_location FROM pg_stat_replication WHERE application_name = 'rewind_standby';";
230+
poll_query_until($caughtup_query, $connstr_master)
231+
or die "Timed out while waiting for standby to catch up";
198232
}
199233

200234
sub promote_standby
@@ -203,9 +237,11 @@ sub promote_standby
203237
# up standby
204238

205239
# Now promote slave and insert some new data on master, this will put
206-
# the master out-of-sync with the standby.
240+
# the master out-of-sync with the standby. Wait until the standby is
241+
# out of recovery mode, and is ready to accept read-write connections.
207242
system_or_bail("pg_ctl -w -D $test_standby_datadir promote >>$log_path 2>&1");
208-
sleep 2;
243+
poll_query_until("SELECT NOT pg_is_in_recovery()", $connstr_standby)
244+
or die "Timed out while waiting for promotion of standby";
209245
}
210246

211247
sub run_pg_rewind

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy