|
| 1 | +use strict; |
| 2 | +use warnings; |
| 3 | + |
| 4 | +use PostgresNode; |
| 5 | +use TestLib; |
| 6 | +use Test::More tests => 2; |
| 7 | + |
| 8 | +use DBI; |
| 9 | +use DBD::Pg ':async'; |
| 10 | + |
| 11 | +sub query_row |
| 12 | +{ |
| 13 | + my ($dbi, $sql, @keys) = @_; |
| 14 | + my $sth = $dbi->prepare($sql) || die; |
| 15 | + $sth->execute(@keys) || die; |
| 16 | + my $ret = $sth->fetchrow_array || undef; |
| 17 | + print "query_row('$sql') -> $ret \n"; |
| 18 | + return $ret; |
| 19 | +} |
| 20 | + |
| 21 | +sub query_exec |
| 22 | +{ |
| 23 | + my ($dbi, $sql) = @_; |
| 24 | + my $rv = $dbi->do($sql) || die; |
| 25 | + print "query_exec('$sql')\n"; |
| 26 | + return $rv; |
| 27 | +} |
| 28 | + |
| 29 | +sub query_exec_async |
| 30 | +{ |
| 31 | + my ($dbi, $sql) = @_; |
| 32 | + my $rv = $dbi->do($sql, {pg_async => PG_ASYNC}) || die; |
| 33 | + print "query_exec('$sql')\n"; |
| 34 | + return $rv; |
| 35 | +} |
| 36 | + |
| 37 | +my %allocated_ports = (); |
| 38 | +sub allocate_ports |
| 39 | +{ |
| 40 | + my @allocated_now = (); |
| 41 | + my ($host, $ports_to_alloc) = @_; |
| 42 | + |
| 43 | + while ($ports_to_alloc > 0) |
| 44 | + { |
| 45 | + my $port = int(rand() * 16384) + 49152; |
| 46 | + next if $allocated_ports{$port}; |
| 47 | + print "# Checking for port $port\n"; |
| 48 | + if (!TestLib::run_log(['pg_isready', '-h', $host, '-p', $port])) |
| 49 | + { |
| 50 | + $allocated_ports{$port} = 1; |
| 51 | + push(@allocated_now, $port); |
| 52 | + $ports_to_alloc--; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + return @allocated_now; |
| 57 | +} |
| 58 | + |
| 59 | +my $nnodes = 2; |
| 60 | +my @nodes = (); |
| 61 | + |
| 62 | +# Create nodes and allocate ports |
| 63 | +foreach my $i (1..$nnodes) |
| 64 | +{ |
| 65 | + my $host = "127.0.0.1"; |
| 66 | + my ($pgport, $raftport) = allocate_ports($host, 2); |
| 67 | + my $node = new PostgresNode("node$i", $host, $pgport); |
| 68 | + $node->{id} = $i; |
| 69 | + $node->{raftport} = $raftport; |
| 70 | + push(@nodes, $node); |
| 71 | +} |
| 72 | + |
| 73 | +my $mm_connstr = join(',', map { "${ \$_->connstr('postgres') }" } @nodes); |
| 74 | +my $raft_peers = join(',', map { join(':', $_->{id}, $_->host, $_->{raftport}) } @nodes); |
| 75 | + |
| 76 | +print("# mm_connstr = $mm_connstr\n"); |
| 77 | +print("# raft_peers = $raft_peers\n"); |
| 78 | + |
| 79 | +# Init and Configure |
| 80 | +foreach my $node (@nodes) |
| 81 | +{ |
| 82 | + my $id = $node->{id}; |
| 83 | + my $host = $node->host; |
| 84 | + my $pgport = $node->port; |
| 85 | + my $raftport = $node->{raftport}; |
| 86 | + |
| 87 | + $node->init(hba_permit_replication => 0); |
| 88 | + $node->append_conf("postgresql.conf", qq( |
| 89 | + listen_addresses = '$host' |
| 90 | + unix_socket_directories = '' |
| 91 | + port = $pgport |
| 92 | + max_connections = 200 |
| 93 | + shared_buffers = 1GB |
| 94 | + max_prepared_transactions = 200 |
| 95 | + max_worker_processes = 100 |
| 96 | + wal_level = logical |
| 97 | + fsync = off |
| 98 | + max_wal_size = 100GB |
| 99 | + min_wal_size = 1GB |
| 100 | + max_wal_senders = 10 |
| 101 | + wal_sender_timeout = 0 |
| 102 | + max_replication_slots = 10 |
| 103 | + shared_preload_libraries = 'raftable,multimaster' |
| 104 | + multimaster.workers = 8 |
| 105 | + multimaster.queue_size = 104857600 # 100mb |
| 106 | + multimaster.node_id = $id |
| 107 | + multimaster.conn_strings = '$mm_connstr' |
| 108 | + raftable.id = $id |
| 109 | + raftable.peers = '$raft_peers' |
| 110 | + )); |
| 111 | + |
| 112 | + $node->append_conf("pg_hba.conf", qq( |
| 113 | + local replication all trust |
| 114 | + host replication all 127.0.0.1/32 trust |
| 115 | + host replication all ::1/128 trust |
| 116 | + )); |
| 117 | +} |
| 118 | + |
| 119 | +# Start |
| 120 | +foreach my $node (@nodes) |
| 121 | +{ |
| 122 | + $node->start(); |
| 123 | +} |
| 124 | + |
| 125 | +$nodes[0]->psql("create table t(k int primary key, v text)"); |
| 126 | +$nodes[0]->psql("insert into t values (1, 'hello'), (2, 'world')"); |
| 127 | + |
| 128 | +#my @conns = map { DBI->connect('DBI:Pg:' . $_->connstr()) } @nodes; |
| 129 | +# |
| 130 | +#query_exec($conns[0], "begin"); |
| 131 | +#query_exec($conns[1], "begin"); |
| 132 | +# |
| 133 | +#query_exec($conns[0], "update t set v = 'foo' where k = 1"); |
| 134 | +#query_exec($conns[1], "update t set v = 'bar' where k = 2"); |
| 135 | +# |
| 136 | +#query_exec($conns[0], "update t set v = 'bar' where k = 2"); |
| 137 | +#query_exec($conns[1], "update t set v = 'foo' where k = 1"); |
| 138 | +# |
| 139 | +#query_exec_async($conns[0], "commit"); |
| 140 | +#query_exec_async($conns[1], "commit"); |
| 141 | +# |
| 142 | +#my $ready = 0; |
| 143 | +#$ready++ if $conns[0]->pg_ready; |
| 144 | +#$ready++ if $conns[1]->pg_ready; |
| 145 | +# |
| 146 | +#is($ready, 1, "one of the connections is deadlocked"); |
| 147 | +# |
| 148 | +#sleep(2); |
0 commit comments