|
3 | 3 |
|
4 | 4 | use PostgresNode;
|
5 | 5 | use TestLib;
|
6 |
| -use Test::More tests => 1; |
| 6 | +use Test::More tests => 2; |
7 | 7 |
|
8 | 8 | my $master = get_new_node("master");
|
9 | 9 | $master->init;
|
|
18 | 18 | $shard1->init;
|
19 | 19 | $shard1->append_conf('postgresql.conf', qq(
|
20 | 20 | max_prepared_transactions = 30
|
21 |
| - log_checkpoints = true |
22 |
| - # shared_preload_libraries = 'pg_tsdtm' |
23 | 21 | ));
|
24 | 22 | $shard1->start;
|
25 | 23 |
|
26 | 24 | my $shard2 = get_new_node("shard2");
|
27 | 25 | $shard2->init;
|
28 | 26 | $shard2->append_conf('postgresql.conf', qq(
|
29 | 27 | max_prepared_transactions = 30
|
30 |
| - log_checkpoints = true |
31 |
| - # shared_preload_libraries = 'pg_tsdtm' |
32 | 28 | ));
|
33 | 29 | $shard2->start;
|
34 | 30 |
|
| 31 | +############################################################################### |
| 32 | +# Prepare nodes |
35 | 33 | ###############################################################################
|
36 | 34 |
|
37 | 35 | $master->psql('postgres', "CREATE EXTENSION postgres_fdw");
|
38 | 36 | $master->psql('postgres', "CREATE TABLE accounts(id integer primary key, amount integer)");
|
| 37 | +$master->psql('postgres', "CREATE TABLE global_transactions(tx_time timestamp)"); |
39 | 38 |
|
40 | 39 | foreach my $node ($shard1, $shard2)
|
41 | 40 | {
|
42 | 41 | my $port = $node->port;
|
43 | 42 | my $host = $node->host;
|
44 | 43 |
|
45 |
| - # $node->psql('postgres', "CREATE EXTENSION pg_tsdtm"); |
46 | 44 | $node->psql('postgres', "CREATE TABLE accounts(id integer primary key, amount integer)");
|
47 | 45 |
|
48 | 46 | $master->psql('postgres', "CREATE SERVER shard_$port FOREIGN DATA WRAPPER postgres_fdw options(dbname 'postgres', host '$host', port '$port')");
|
49 | 47 | $master->psql('postgres', "CREATE FOREIGN TABLE accounts_fdw_$port() inherits (accounts) server shard_$port options(table_name 'accounts')");
|
50 | 48 | $master->psql('postgres', "CREATE USER MAPPING for stas SERVER shard_$port options (user 'stas')");
|
51 |
| - |
52 |
| - # diag("done $host $port"); |
53 | 49 | }
|
54 | 50 |
|
55 | 51 | $shard1->psql('postgres', "insert into accounts select 2*id-1, 0 from generate_series(1, 10010) as id;");
|
| 52 | +$shard1->psql('postgres', "CREATE TABLE local_transactions(tx_time timestamp)"); |
| 53 | + |
56 | 54 | $shard2->psql('postgres', "insert into accounts select 2*id, 0 from generate_series(1, 10010) as id;");
|
| 55 | +$shard2->psql('postgres', "CREATE TABLE local_transactions(tx_time timestamp)"); |
| 56 | + |
| 57 | +$master->pgbench(-n, -c => 20, -t => 30, -f => "$TestLib::log_path/../../t/bank.sql", 'postgres' ); |
57 | 58 |
|
58 | 59 | # diag( $master->connstr() );
|
59 | 60 | # sleep(3600);
|
60 | 61 |
|
| 62 | +############################################################################### |
| 63 | +# Helpers |
| 64 | +############################################################################### |
| 65 | + |
| 66 | +sub count_and_delete_rows |
| 67 | +{ |
| 68 | + my ($node, $table) = @_; |
| 69 | + my ($rc, $count, $err); |
| 70 | + |
| 71 | + ($rc, $count, $err) = $node->psql('postgres',"select count(*) from $table", |
| 72 | + on_error_die => 1); |
| 73 | + |
| 74 | + die "count_rows: $err" if ($err ne ''); |
| 75 | + |
| 76 | + $node->psql('postgres',"delete from $table", on_error_die => 1); |
| 77 | + |
| 78 | + diag($node->name, ": completed $count transactions"); |
| 79 | + |
| 80 | + return $count; |
| 81 | +} |
| 82 | + |
| 83 | +############################################################################### |
| 84 | +# Concurrent global transactions |
61 | 85 | ###############################################################################
|
62 | 86 |
|
63 | 87 | my ($err, $rc);
|
| 88 | +my $started; |
64 | 89 | my $seconds = 30;
|
| 90 | +my $selects; |
65 | 91 | my $total = '0';
|
66 | 92 | my $oldtotal = '0';
|
67 |
| -my $isolation_error = 0; |
| 93 | +my $isolation_errors = 0; |
68 | 94 |
|
69 | 95 |
|
70 |
| -$master->pgbench(-n, -c => 20, -t => 30, -f => "$TestLib::log_path/../../t/bank.pgb", 'postgres' ); |
| 96 | +my $pgb_handle; |
71 | 97 |
|
72 |
| -my $pgb_handle = $master->pgbench_async(-n, -c => 5, -T => $seconds, -f => "$TestLib::log_path/../../t/bank.pgb", 'postgres' ); |
| 98 | +$pgb_handle = $master->pgbench_async(-n, -c => 5, -T => $seconds, -f => "$TestLib::log_path/../../t/bank.sql", 'postgres' ); |
73 | 99 |
|
74 |
| -my $started = time(); |
| 100 | +$started = time(); |
| 101 | +$selects = 0; |
75 | 102 | while (time() - $started < $seconds)
|
76 | 103 | {
|
77 | 104 | ($rc, $total, $err) = $master->psql('postgres', "select sum(amount) from accounts");
|
78 | 105 | if ( ($total ne $oldtotal) and ($total ne '') )
|
79 | 106 | {
|
80 |
| - $isolation_error = 1; |
| 107 | + $isolation_errors++; |
81 | 108 | $oldtotal = $total;
|
82 | 109 | diag("Isolation error. Total = $total");
|
83 | 110 | }
|
84 |
| - # diag("Total = $total"); |
| 111 | + if (($err eq '') and ($total ne '') ) { $selects++; } |
85 | 112 | }
|
86 | 113 |
|
87 | 114 | $master->pgbench_await($pgb_handle);
|
88 | 115 |
|
89 |
| -is($isolation_error, 0, 'check proper isolation'); |
| 116 | +# sanity check |
| 117 | +diag("completed $selects selects"); |
| 118 | +die "no actual transactions happend" unless ( $selects > 0 && |
| 119 | + count_and_delete_rows($master, 'global_transactions') > 0); |
| 120 | + |
| 121 | +is($isolation_errors, 0, 'isolation between concurrent global transaction'); |
| 122 | + |
| 123 | +############################################################################### |
| 124 | +# Concurrent global and local transactions |
| 125 | +############################################################################### |
| 126 | + |
| 127 | +my ($pgb_handle1, $pgb_handle2, $pgb_handle3); |
| 128 | + |
| 129 | +# global txses |
| 130 | +$pgb_handle1 = $master->pgbench_async(-n, -c => 5, -T => $seconds, -f => "$TestLib::log_path/../../t/bank.sql", 'postgres' ); |
| 131 | + |
| 132 | +# concurrent local |
| 133 | +$pgb_handle2 = $shard1->pgbench_async(-n, -c => 5, -T => $seconds, -f => "$TestLib::log_path/../../t/bank1.sql", 'postgres' ); |
| 134 | +$pgb_handle3 = $shard2->pgbench_async(-n, -c => 5, -T => $seconds, -f => "$TestLib::log_path/../../t/bank2.sql", 'postgres' ); |
| 135 | + |
| 136 | +$started = time(); |
| 137 | +$selects = 0; |
| 138 | +$oldtotal = 0; |
| 139 | +while (time() - $started < $seconds) |
| 140 | +{ |
| 141 | + ($rc, $total, $err) = $master->psql('postgres', "select sum(amount) from accounts"); |
| 142 | + if ( ($total ne $oldtotal) and ($total ne '') ) |
| 143 | + { |
| 144 | + $isolation_errors++; |
| 145 | + $oldtotal = $total; |
| 146 | + diag("Isolation error. Total = $total"); |
| 147 | + } |
| 148 | + if (($err eq '') and ($total ne '') ) { $selects++; } |
| 149 | +} |
| 150 | + |
| 151 | +diag("selects = $selects"); |
| 152 | +$master->pgbench_await($pgb_handle1); |
| 153 | +$shard1->pgbench_await($pgb_handle2); |
| 154 | +$shard2->pgbench_await($pgb_handle3); |
| 155 | + |
| 156 | +diag("completed $selects selects"); |
| 157 | +die "" unless ( $selects > 0 && |
| 158 | + count_and_delete_rows($master, 'global_transactions') > 0 && |
| 159 | + count_and_delete_rows($shard1, 'local_transactions') > 0 && |
| 160 | + count_and_delete_rows($shard2, 'local_transactions') > 0); |
| 161 | + |
| 162 | +is($isolation_errors, 0, 'isolation between concurrent global and local transactions'); |
| 163 | + |
| 164 | + |
| 165 | +# diag( $master->connstr('postgres'), "\n" ); |
| 166 | +# diag( $shard1->connstr('postgres'), "\n" ); |
| 167 | +# diag( $shard2->connstr('postgres'), "\n" ); |
| 168 | +# sleep(3600); |
90 | 169 |
|
91 | 170 | $master->stop;
|
92 | 171 | $shard1->stop;
|
|
0 commit comments