Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 5afa63f

Browse files
author
Amit Kapila
committed
Back-Patch "Add wait_for_subscription_sync for TAP tests."
This was originally done in commit 0c20dd3 for 16 only, to eliminate duplicate code and as an infrastructure that makes it easier to write future tests. However, it has been suggested that it would be good to back-patch this testing infrastructure to aid future tests in back-branches. Backpatch to all supported versions. Author: Masahiko Sawada Reviewed by: Amit Kapila, Shi yu Discussion: https://postgr.es/m/CAD21AoC-fvAkaKHa4t1urupwL8xbAcWRePeETvshvy80f6WV1A@mail.gmail.com Discussion: https://postgr.es/m/E1oJBIf-0006sw-SA@gemulon.postgresql.org
1 parent 547b963 commit 5afa63f

11 files changed

+72
-85
lines changed

src/test/perl/PostgresNode.pm

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2195,6 +2195,50 @@ sub wait_for_slot_catchup
21952195

21962196
=pod
21972197
2198+
=item $node->wait_for_subscription_sync(publisher, subname, dbname)
2199+
2200+
Wait for all tables in pg_subscription_rel to complete the initial
2201+
synchronization (i.e to be either in 'syncdone' or 'ready' state).
2202+
2203+
If the publisher node is given, additionally, check if the subscriber has
2204+
caught up to what has been committed on the primary. This is useful to
2205+
ensure that the initial data synchronization has been completed after
2206+
creating a new subscription.
2207+
2208+
If there is no active replication connection from this peer, wait until
2209+
poll_query_until timeout.
2210+
2211+
This is not a test. It die()s on failure.
2212+
2213+
=cut
2214+
2215+
sub wait_for_subscription_sync
2216+
{
2217+
my ($self, $publisher, $subname, $dbname) = @_;
2218+
my $name = $self->name;
2219+
2220+
$dbname = defined($dbname) ? $dbname : 'postgres';
2221+
2222+
# Wait for all tables to finish initial sync.
2223+
print "Waiting for all subscriptions in \"$name\" to synchronize data\n";
2224+
my $query =
2225+
qq[SELECT count(1) = 0 FROM pg_subscription_rel WHERE srsubstate NOT IN ('r', 's');];
2226+
$self->poll_query_until($dbname, $query)
2227+
or croak "timed out waiting for subscriber to synchronize data";
2228+
2229+
# Then, wait for the replication to catchup if required.
2230+
if (defined($publisher))
2231+
{
2232+
croak 'subscription name must be specified' unless defined($subname);
2233+
$publisher->wait_for_catchup($subname);
2234+
}
2235+
2236+
print "done\n";
2237+
return;
2238+
}
2239+
2240+
=pod
2241+
21982242
=item $node->wait_for_log(regexp, offset)
21992243
22002244
Waits for the contents of the server log file, starting at the given offset, to

src/test/subscription/t/001_rep_changes.pl

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,8 @@
8585
"CREATE SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr' PUBLICATION tap_pub, tap_pub_ins_only"
8686
);
8787

88-
$node_publisher->wait_for_catchup('tap_sub');
89-
90-
# Also wait for initial table sync to finish
91-
my $synced_query =
92-
"SELECT count(1) = 0 FROM pg_subscription_rel WHERE srsubstate NOT IN ('r', 's');";
93-
$node_subscriber->poll_query_until('postgres', $synced_query)
94-
or die "Timed out while waiting for subscriber to synchronize data";
88+
# Wait for initial table sync to finish
89+
$node_subscriber->wait_for_subscription_sync($node_publisher, 'tap_sub');
9590

9691
my $result =
9792
$node_subscriber->safe_psql('postgres', "SELECT count(*) FROM tab_notrep");

src/test/subscription/t/002_types.pl

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,8 @@
111111
"CREATE SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr' PUBLICATION tap_pub WITH (slot_name = tap_sub_slot)"
112112
);
113113

114-
$node_publisher->wait_for_catchup('tap_sub');
115-
116-
# Wait for initial sync to finish as well
117-
my $synced_query =
118-
"SELECT count(1) = 0 FROM pg_subscription_rel WHERE srsubstate NOT IN ('s', 'r');";
119-
$node_subscriber->poll_query_until('postgres', $synced_query)
120-
or die "Timed out while waiting for subscriber to synchronize data";
114+
# Wait for initial sync to finish
115+
$node_subscriber->wait_for_subscription_sync($node_publisher, 'tap_sub');
121116

122117
# Insert initial test data
123118
$node_publisher->safe_psql(

src/test/subscription/t/004_sync.pl

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,8 @@
3636
"CREATE SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr' PUBLICATION tap_pub"
3737
);
3838

39-
$node_publisher->wait_for_catchup('tap_sub');
40-
41-
# Also wait for initial table sync to finish
42-
my $synced_query =
43-
"SELECT count(1) = 0 FROM pg_subscription_rel WHERE srsubstate NOT IN ('r', 's');";
44-
$node_subscriber->poll_query_until('postgres', $synced_query)
45-
or die "Timed out while waiting for subscriber to synchronize data";
39+
# Wait for initial table sync to finish
40+
$node_subscriber->wait_for_subscription_sync($node_publisher, 'tap_sub');
4641

4742
my $result =
4843
$node_subscriber->safe_psql('postgres', "SELECT count(*) FROM tab_rep");
@@ -68,8 +63,7 @@
6863
$node_subscriber->safe_psql('postgres', "DELETE FROM tab_rep;");
6964

7065
# wait for sync to finish this time
71-
$node_subscriber->poll_query_until('postgres', $synced_query)
72-
or die "Timed out while waiting for subscriber to synchronize data";
66+
$node_subscriber->wait_for_subscription_sync;
7367

7468
# check that all data is synced
7569
$result =
@@ -104,8 +98,7 @@
10498
);
10599

106100
# and wait for data sync to finish again
107-
$node_subscriber->poll_query_until('postgres', $synced_query)
108-
or die "Timed out while waiting for subscriber to synchronize data";
101+
$node_subscriber->wait_for_subscription_sync;
109102

110103
# check that all data is synced
111104
$result =
@@ -130,8 +123,7 @@
130123
"ALTER SUBSCRIPTION tap_sub REFRESH PUBLICATION");
131124

132125
# wait for sync to finish
133-
$node_subscriber->poll_query_until('postgres', $synced_query)
134-
or die "Timed out while waiting for subscriber to synchronize data";
126+
$node_subscriber->wait_for_subscription_sync;
135127

136128
$result = $node_subscriber->safe_psql('postgres',
137129
"SELECT count(*) FROM tab_rep_next");

src/test/subscription/t/005_encoding.pl

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,8 @@
2929
"CREATE SUBSCRIPTION mysub CONNECTION '$publisher_connstr' PUBLICATION mypub;"
3030
);
3131

32-
$node_publisher->wait_for_catchup('mysub');
33-
34-
# Wait for initial sync to finish as well
35-
my $synced_query =
36-
"SELECT count(1) = 0 FROM pg_subscription_rel WHERE srsubstate NOT IN ('s', 'r');";
37-
$node_subscriber->poll_query_until('postgres', $synced_query)
38-
or die "Timed out while waiting for subscriber to synchronize data";
32+
# Wait for initial sync to finish
33+
$node_subscriber->wait_for_subscription_sync($node_publisher, 'mysub');
3934

4035
$node_publisher->safe_psql('postgres',
4136
q{INSERT INTO test1 VALUES (1, E'Mot\xc3\xb6rhead')}); # hand-rolled UTF-8

src/test/subscription/t/006_rewrite.pl

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,8 @@
2525
"CREATE SUBSCRIPTION mysub CONNECTION '$publisher_connstr' PUBLICATION mypub;"
2626
);
2727

28-
$node_publisher->wait_for_catchup('mysub');
29-
30-
# Wait for initial sync to finish as well
31-
my $synced_query =
32-
"SELECT count(1) = 0 FROM pg_subscription_rel WHERE srsubstate NOT IN ('s', 'r');";
33-
$node_subscriber->poll_query_until('postgres', $synced_query)
34-
or die "Timed out while waiting for subscriber to synchronize data";
28+
# Wait for initial sync to finish
29+
$node_subscriber->wait_for_subscription_sync($node_publisher, 'mysub');
3530

3631
$node_publisher->safe_psql('postgres',
3732
q{INSERT INTO test1 (a, b) VALUES (1, 'one'), (2, 'two');});

src/test/subscription/t/008_diff_schema.pl

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,8 @@
3535
"CREATE SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr' PUBLICATION tap_pub"
3636
);
3737

38-
$node_publisher->wait_for_catchup('tap_sub');
39-
40-
# Also wait for initial table sync to finish
41-
my $synced_query =
42-
"SELECT count(1) = 0 FROM pg_subscription_rel WHERE srsubstate NOT IN ('r', 's');";
43-
$node_subscriber->poll_query_until('postgres', $synced_query)
44-
or die "Timed out while waiting for subscriber to synchronize data";
38+
# Wait for initial table sync to finish
39+
$node_subscriber->wait_for_subscription_sync($node_publisher, 'tap_sub');
4540

4641
my $result =
4742
$node_subscriber->safe_psql('postgres',
@@ -102,8 +97,7 @@
10297
$node_subscriber->safe_psql('postgres',
10398
"ALTER SUBSCRIPTION tap_sub REFRESH PUBLICATION");
10499

105-
$node_subscriber->poll_query_until('postgres', $synced_query)
106-
or die "Timed out while waiting for subscriber to synchronize data";
100+
$node_subscriber->wait_for_subscription_sync;
107101

108102
# Add replica identity column. (The serial is not necessary, but it's
109103
# a convenient way to get a default on the new column so that rows

src/test/subscription/t/010_truncate.pl

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,7 @@
6464
);
6565

6666
# Wait for initial sync of all subscriptions
67-
my $synced_query =
68-
"SELECT count(1) = 0 FROM pg_subscription_rel WHERE srsubstate NOT IN ('r', 's');";
69-
$node_subscriber->poll_query_until('postgres', $synced_query)
70-
or die "Timed out while waiting for subscriber to synchronize data";
67+
$node_subscriber->wait_for_subscription_sync;
7168

7269
# insert data to truncate
7370

@@ -180,8 +177,7 @@
180177
);
181178

182179
# wait for initial data sync
183-
$node_subscriber->poll_query_until('postgres', $synced_query)
184-
or die "Timed out while waiting for subscriber to synchronize data";
180+
$node_subscriber->wait_for_subscription_sync;
185181

186182
# insert data to truncate
187183

src/test/subscription/t/011_generated.pl

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,7 @@
3737
);
3838

3939
# Wait for initial sync of all subscriptions
40-
my $synced_query =
41-
"SELECT count(1) = 0 FROM pg_subscription_rel WHERE srsubstate NOT IN ('r', 's');";
42-
$node_subscriber->poll_query_until('postgres', $synced_query)
43-
or die "Timed out while waiting for subscriber to synchronize data";
40+
$node_subscriber->wait_for_subscription_sync;
4441

4542
my $result = $node_subscriber->safe_psql('postgres', "SELECT a, b FROM tab1");
4643
is( $result, qq(1|22

src/test/subscription/t/013_partition.pl

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,8 @@ BEGIN
150150
});
151151

152152
# Wait for initial sync of all subscriptions
153-
my $synced_query =
154-
"SELECT count(1) = 0 FROM pg_subscription_rel WHERE srsubstate NOT IN ('r', 's');";
155-
$node_subscriber1->poll_query_until('postgres', $synced_query)
156-
or die "Timed out while waiting for subscriber to synchronize data";
157-
$node_subscriber2->poll_query_until('postgres', $synced_query)
158-
or die "Timed out while waiting for subscriber to synchronize data";
153+
$node_subscriber1->wait_for_subscription_sync;
154+
$node_subscriber2->wait_for_subscription_sync;
159155

160156
# Tests for replication using leaf partition identity and schema
161157

@@ -480,10 +476,8 @@ BEGIN
480476
"ALTER SUBSCRIPTION sub2 SET PUBLICATION pub_lower_level, pub_all");
481477

482478
# Wait for initial sync of all subscriptions
483-
$node_subscriber1->poll_query_until('postgres', $synced_query)
484-
or die "Timed out while waiting for subscriber to synchronize data";
485-
$node_subscriber2->poll_query_until('postgres', $synced_query)
486-
or die "Timed out while waiting for subscriber to synchronize data";
479+
$node_subscriber1->wait_for_subscription_sync;
480+
$node_subscriber2->wait_for_subscription_sync;
487481

488482
# check that data is synced correctly
489483
$result = $node_subscriber1->safe_psql('postgres',
@@ -554,8 +548,7 @@ BEGIN
554548

555549
# make sure the subscription on the second subscriber is synced, before
556550
# continuing
557-
$node_subscriber2->poll_query_until('postgres', $synced_query)
558-
or die "Timed out while waiting for subscriber to synchronize data";
551+
$node_subscriber2->wait_for_subscription_sync;
559552

560553
# Insert a change into the leaf partition, should be replicated through
561554
# the partition root (thanks to the FOR ALL TABLES partition).
@@ -810,8 +803,7 @@ BEGIN
810803
$node_subscriber2->safe_psql('postgres',
811804
"ALTER SUBSCRIPTION sub2 REFRESH PUBLICATION");
812805

813-
$node_subscriber2->poll_query_until('postgres', $synced_query)
814-
or die "Timed out while waiting for subscriber to synchronize data";
806+
$node_subscriber2->wait_for_subscription_sync;
815807

816808
# Make partition map cache
817809
$node_publisher->safe_psql('postgres', "INSERT INTO tab5 VALUES (1, 1)");

src/test/subscription/t/100_bugs.pl

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,7 @@
142142
# We cannot rely solely on wait_for_catchup() here; it isn't sufficient
143143
# when tablesync workers might still be running. So in addition to that,
144144
# verify that tables are synced.
145-
# XXX maybe this should be integrated in wait_for_catchup() itself.
146-
$node_twoways->wait_for_catchup('testsub');
147-
my $synced_query =
148-
"SELECT count(1) = 0 FROM pg_subscription_rel WHERE srsubstate NOT IN ('r', 's');";
149-
$node_twoways->poll_query_until('d2', $synced_query)
150-
or die "Timed out while waiting for subscriber to synchronize data";
145+
$node_twoways->wait_for_subscription_sync($node_twoways, 'testsub', 'd2');
151146

152147
is($node_twoways->safe_psql('d2', "SELECT count(f) FROM t"),
153148
$rows * 2, "2x$rows rows in t");
@@ -207,11 +202,8 @@
207202
"CREATE SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr' PUBLICATION tap_pub"
208203
);
209204

210-
$node_publisher->wait_for_catchup('tap_sub');
211-
212-
# Also wait for initial table sync to finish
213-
$node_subscriber->poll_query_until('postgres', $synced_query)
214-
or die "Timed out while waiting for subscriber to synchronize data";
205+
# Wait for initial table sync to finish
206+
$node_subscriber->wait_for_subscription_sync($node_publisher, 'tap_sub');
215207

216208
is( $node_subscriber->safe_psql(
217209
'postgres', "SELECT * FROM tab_replidentity_index"),

0 commit comments

Comments
 (0)