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