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

Commit 0c20dd3

Browse files
author
Amit Kapila
committed
Add wait_for_subscription_sync for TAP tests.
The TAP tests for logical replication in src/test/subscription are using the following code in many places to make sure that the subscription is synchronized with the publisher: $node_publisher->wait_for_catchup('tap_sub'); $node_subscriber->poll_query_until('postgres', qq[SELECT count(1) = 0 FROM pg_subscription_rel WHERE srsubstate NOT IN ('r', 's')]); The new function wait_for_subscription_sync() can be used to replace the above code. This eliminates duplicated code and makes it easier to write future tests. Author: Masahiko Sawada Reviewed by: Amit Kapila, Shi yu Discussion: https://postgr.es/m/CAD21AoC-fvAkaKHa4t1urupwL8xbAcWRePeETvshvy80f6WV1A@mail.gmail.com
1 parent c67c2e2 commit 0c20dd3

27 files changed

+129
-260
lines changed

src/test/perl/PostgreSQL/Test/Cluster.pm

+44
Original file line numberDiff line numberDiff line change
@@ -2648,6 +2648,50 @@ sub wait_for_slot_catchup
26482648

26492649
=pod
26502650
2651+
=item $node->wait_for_subscription_sync(publisher, subname, dbname)
2652+
2653+
Wait for all tables in pg_subscription_rel to complete the initial
2654+
synchronization (i.e to be either in 'syncdone' or 'ready' state).
2655+
2656+
If the publisher node is given, additionally, check if the subscriber has
2657+
caught up to what has been committed on the primary. This is useful to
2658+
ensure that the initial data synchronization has been completed after
2659+
creating a new subscription.
2660+
2661+
If there is no active replication connection from this peer, wait until
2662+
poll_query_until timeout.
2663+
2664+
This is not a test. It die()s on failure.
2665+
2666+
=cut
2667+
2668+
sub wait_for_subscription_sync
2669+
{
2670+
my ($self, $publisher, $subname, $dbname) = @_;
2671+
my $name = $self->name;
2672+
2673+
$dbname = defined($dbname) ? $dbname : 'postgres';
2674+
2675+
# Wait for all tables to finish initial sync.
2676+
print "Waiting for all subscriptions in \"$name\" to synchronize data\n";
2677+
my $query =
2678+
qq[SELECT count(1) = 0 FROM pg_subscription_rel WHERE srsubstate NOT IN ('r', 's');];
2679+
$self->poll_query_until($dbname, $query)
2680+
or croak "timed out waiting for subscriber to synchronize data";
2681+
2682+
# Then, wait for the replication to catchup if required.
2683+
if (defined($publisher))
2684+
{
2685+
croak 'subscription name must be specified' unless defined($subname);
2686+
$publisher->wait_for_catchup($subname);
2687+
}
2688+
2689+
print "done\n";
2690+
return;
2691+
}
2692+
2693+
=pod
2694+
26512695
=item $node->wait_for_log(regexp, offset)
26522696
26532697
Waits for the contents of the server log file, starting at the given offset, to

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

+4-14
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,8 @@
102102
"CREATE SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr' PUBLICATION tap_pub, tap_pub_ins_only"
103103
);
104104

105-
$node_publisher->wait_for_catchup('tap_sub');
106-
107-
# Also wait for initial table sync to finish
108-
my $synced_query =
109-
"SELECT count(1) = 0 FROM pg_subscription_rel WHERE srsubstate NOT IN ('r', 's');";
110-
$node_subscriber->poll_query_until('postgres', $synced_query)
111-
or die "Timed out while waiting for subscriber to synchronize data";
105+
# Wait for initial table sync to finish
106+
$node_subscriber->wait_for_subscription_sync($node_publisher, 'tap_sub');
112107

113108
my $result =
114109
$node_subscriber->safe_psql('postgres', "SELECT count(*) FROM tab_notrep");
@@ -237,13 +232,8 @@
237232
"CREATE SUBSCRIPTION tap_sub_temp1 CONNECTION '$publisher_connstr' PUBLICATION tap_pub_temp1, tap_pub_temp2"
238233
);
239234

240-
$node_publisher->wait_for_catchup('tap_sub_temp1');
241-
242-
# Also wait for initial table sync to finish
243-
$synced_query =
244-
"SELECT count(1) = 0 FROM pg_subscription_rel WHERE srsubstate NOT IN ('r', 's');";
245-
$node_subscriber->poll_query_until('postgres', $synced_query)
246-
or die "Timed out while waiting for subscriber to synchronize data";
235+
# Wait for initial table sync to finish
236+
$node_subscriber->wait_for_subscription_sync($node_publisher, 'tap_sub_temp1');
247237

248238
# Subscriber table will have no rows initially
249239
$result =

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

+1-6
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,8 @@
114114
"CREATE SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr' PUBLICATION tap_pub WITH (slot_name = tap_sub_slot)"
115115
);
116116

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

125120
# Insert initial test data
126121
$node_publisher->safe_psql(

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

+5-13
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,8 @@
3939
"CREATE SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr' PUBLICATION tap_pub"
4040
);
4141

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

5045
my $result =
5146
$node_subscriber->safe_psql('postgres', "SELECT count(*) FROM tab_rep");
@@ -71,8 +66,7 @@
7166
$node_subscriber->safe_psql('postgres', "DELETE FROM tab_rep;");
7267

7368
# wait for sync to finish this time
74-
$node_subscriber->poll_query_until('postgres', $synced_query)
75-
or die "Timed out while waiting for subscriber to synchronize data";
69+
$node_subscriber->wait_for_subscription_sync;
7670

7771
# check that all data is synced
7872
$result =
@@ -107,8 +101,7 @@
107101
);
108102

109103
# and wait for data sync to finish again
110-
$node_subscriber->poll_query_until('postgres', $synced_query)
111-
or die "Timed out while waiting for subscriber to synchronize data";
104+
$node_subscriber->wait_for_subscription_sync;
112105

113106
# check that all data is synced
114107
$result =
@@ -133,8 +126,7 @@
133126
"ALTER SUBSCRIPTION tap_sub REFRESH PUBLICATION");
134127

135128
# wait for sync to finish
136-
$node_subscriber->poll_query_until('postgres', $synced_query)
137-
or die "Timed out while waiting for subscriber to synchronize data";
129+
$node_subscriber->wait_for_subscription_sync;
138130

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

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

+2-7
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,8 @@
3232
"CREATE SUBSCRIPTION mysub CONNECTION '$publisher_connstr' PUBLICATION mypub;"
3333
);
3434

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

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

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

+2-7
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,8 @@
2828
"CREATE SUBSCRIPTION mysub CONNECTION '$publisher_connstr' PUBLICATION mypub;"
2929
);
3030

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

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

src/test/subscription/t/007_ddl.pl

+2-7
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,8 @@
4949
m/WARNING: publication "non_existent_pub" does not exist in the publisher/,
5050
"Create subscription throws warning for non-existent publication");
5151

52-
$node_publisher->wait_for_catchup('mysub1');
53-
54-
# Also wait for initial table sync to finish.
55-
my $synced_query =
56-
"SELECT count(1) = 0 FROM pg_subscription_rel WHERE srsubstate NOT IN ('r', 's');";
57-
$node_subscriber->poll_query_until('postgres', $synced_query)
58-
or die "Timed out while waiting for subscriber to synchronize data";
52+
# Wait for initial table sync to finish.
53+
$node_subscriber->wait_for_subscription_sync($node_publisher, 'mysub1');
5954

6055
# Specifying non-existent publication along with add publication.
6156
($ret, $stdout, $stderr) = $node_subscriber->psql('postgres',

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

+3-9
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,8 @@
3838
"CREATE SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr' PUBLICATION tap_pub"
3939
);
4040

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

4944
my $result =
5045
$node_subscriber->safe_psql('postgres',
@@ -105,8 +100,7 @@
105100
$node_subscriber->safe_psql('postgres',
106101
"ALTER SUBSCRIPTION tap_sub REFRESH PUBLICATION");
107102

108-
$node_subscriber->poll_query_until('postgres', $synced_query)
109-
or die "Timed out while waiting for subscriber to synchronize data";
103+
$node_subscriber->wait_for_subscription_sync;
110104

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

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

+2-6
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,7 @@
6767
);
6868

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

7572
# insert data to truncate
7673

@@ -211,8 +208,7 @@
211208
);
212209

213210
# wait for initial data sync
214-
$node_subscriber->poll_query_until('postgres', $synced_query)
215-
or die "Timed out while waiting for subscriber to synchronize data";
211+
$node_subscriber->wait_for_subscription_sync;
216212

217213
# insert data to truncate
218214

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

+1-4
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,7 @@
4040
);
4141

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

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

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

+6-14
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,8 @@ BEGIN
153153
});
154154

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

163159
# Tests for replication using leaf partition identity and schema
164160

@@ -490,10 +486,8 @@ BEGIN
490486
"ALTER SUBSCRIPTION sub2 SET PUBLICATION pub_lower_level, pub_all");
491487

492488
# Wait for initial sync of all subscriptions
493-
$node_subscriber1->poll_query_until('postgres', $synced_query)
494-
or die "Timed out while waiting for subscriber to synchronize data";
495-
$node_subscriber2->poll_query_until('postgres', $synced_query)
496-
or die "Timed out while waiting for subscriber to synchronize data";
489+
$node_subscriber1->wait_for_subscription_sync;
490+
$node_subscriber2->wait_for_subscription_sync;
497491

498492
# check that data is synced correctly
499493
$result = $node_subscriber1->safe_psql('postgres', "SELECT c, a FROM tab2");
@@ -568,8 +562,7 @@ BEGIN
568562

569563
# make sure the subscription on the second subscriber is synced, before
570564
# continuing
571-
$node_subscriber2->poll_query_until('postgres', $synced_query)
572-
or die "Timed out while waiting for subscriber to synchronize data";
565+
$node_subscriber2->wait_for_subscription_sync;
573566

574567
# Insert a change into the leaf partition, should be replicated through
575568
# the partition root (thanks to the FOR ALL TABLES partition).
@@ -824,8 +817,7 @@ BEGIN
824817
$node_subscriber2->safe_psql('postgres',
825818
"ALTER SUBSCRIPTION sub2 REFRESH PUBLICATION");
826819

827-
$node_subscriber2->poll_query_until('postgres', $synced_query)
828-
or die "Timed out while waiting for subscriber to synchronize data";
820+
$node_subscriber2->wait_for_subscription_sync;
829821

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

src/test/subscription/t/014_binary.pl

+1-4
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,7 @@
4646
. "PUBLICATION tpub WITH (slot_name = tpub_slot, binary = true)");
4747

4848
# Ensure nodes are in sync with each other
49-
$node_publisher->wait_for_catchup('tsub');
50-
$node_subscriber->poll_query_until('postgres',
51-
"SELECT count(1) = 0 FROM pg_subscription_rel WHERE srsubstate NOT IN ('s', 'r');"
52-
) or die "Timed out while waiting for subscriber to synchronize data";
49+
$node_subscriber->wait_for_subscription_sync($node_publisher, 'tsub');
5350

5451
# Insert some content and make sure it's replicated across
5552
$node_publisher->safe_psql(

src/test/subscription/t/015_stream.pl

+2-7
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,8 @@
4141
"CREATE SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr application_name=$appname' PUBLICATION tap_pub WITH (streaming = on)"
4242
);
4343

44-
$node_publisher->wait_for_catchup($appname);
45-
46-
# Also wait for initial table sync to finish
47-
my $synced_query =
48-
"SELECT count(1) = 0 FROM pg_subscription_rel WHERE srsubstate NOT IN ('r', 's');";
49-
$node_subscriber->poll_query_until('postgres', $synced_query)
50-
or die "Timed out while waiting for subscriber to synchronize data";
44+
# Wait for initial table sync to finish
45+
$node_subscriber->wait_for_subscription_sync($node_publisher, $appname);
5146

5247
my $result =
5348
$node_subscriber->safe_psql('postgres',

src/test/subscription/t/016_stream_subxact.pl

+2-7
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,8 @@
4141
"CREATE SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr application_name=$appname' PUBLICATION tap_pub WITH (streaming = on)"
4242
);
4343

44-
$node_publisher->wait_for_catchup($appname);
45-
46-
# Also wait for initial table sync to finish
47-
my $synced_query =
48-
"SELECT count(1) = 0 FROM pg_subscription_rel WHERE srsubstate NOT IN ('r', 's');";
49-
$node_subscriber->poll_query_until('postgres', $synced_query)
50-
or die "Timed out while waiting for subscriber to synchronize data";
44+
# Wait for initial table sync to finish
45+
$node_subscriber->wait_for_subscription_sync($node_publisher, $appname);
5146

5247
my $result =
5348
$node_subscriber->safe_psql('postgres',

src/test/subscription/t/017_stream_ddl.pl

+2-7
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,8 @@
4141
"CREATE SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr application_name=$appname' PUBLICATION tap_pub WITH (streaming = on)"
4242
);
4343

44-
$node_publisher->wait_for_catchup($appname);
45-
46-
# Also wait for initial table sync to finish
47-
my $synced_query =
48-
"SELECT count(1) = 0 FROM pg_subscription_rel WHERE srsubstate NOT IN ('r', 's');";
49-
$node_subscriber->poll_query_until('postgres', $synced_query)
50-
or die "Timed out while waiting for subscriber to synchronize data";
44+
# Wait for initial table sync to finish
45+
$node_subscriber->wait_for_subscription_sync($node_publisher, $appname);
5146

5247
my $result =
5348
$node_subscriber->safe_psql('postgres',

src/test/subscription/t/018_stream_subxact_abort.pl

+2-7
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,8 @@
4040
"CREATE SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr application_name=$appname' PUBLICATION tap_pub WITH (streaming = on)"
4141
);
4242

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

5146
my $result =
5247
$node_subscriber->safe_psql('postgres',

src/test/subscription/t/019_stream_subxact_ddl_abort.pl

+2-7
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,8 @@
4141
"CREATE SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr application_name=$appname' PUBLICATION tap_pub WITH (streaming = on)"
4242
);
4343

44-
$node_publisher->wait_for_catchup($appname);
45-
46-
# Also wait for initial table sync to finish
47-
my $synced_query =
48-
"SELECT count(1) = 0 FROM pg_subscription_rel WHERE srsubstate NOT IN ('r', 's');";
49-
$node_subscriber->poll_query_until('postgres', $synced_query)
50-
or die "Timed out while waiting for subscriber to synchronize data";
44+
# Wait for initial table sync to finish
45+
$node_subscriber->wait_for_subscription_sync($node_publisher, $appname);
5146

5247
my $result =
5348
$node_subscriber->safe_psql('postgres',

0 commit comments

Comments
 (0)