|
| 1 | +use strict; |
| 2 | +use warnings; |
| 3 | + |
| 4 | +use PostgresNode; |
| 5 | +use TestLib; |
| 6 | +use Test::Moretests=> 2; |
| 7 | +use IPC::Runqw(start finish); |
| 8 | +use Cwd; |
| 9 | + |
| 10 | +my%allocated_ports = (); |
| 11 | +suballocate_ports |
| 12 | +{ |
| 13 | +my@allocated_now = (); |
| 14 | +my ($host,$ports_to_alloc) =@_; |
| 15 | + |
| 16 | +while ($ports_to_alloc > 0) |
| 17 | +{ |
| 18 | +my$port =int(rand() * 16384) + 49152; |
| 19 | +nextif$allocated_ports{$port}; |
| 20 | +diag("checking for port$port\n"); |
| 21 | +if (!TestLib::run_log(['pg_isready','-h',$host,'-p',$port])) |
| 22 | +{ |
| 23 | +$allocated_ports{$port} = 1; |
| 24 | +push(@allocated_now,$port); |
| 25 | +$ports_to_alloc--; |
| 26 | +} |
| 27 | +} |
| 28 | + |
| 29 | +return@allocated_now; |
| 30 | +} |
| 31 | + |
| 32 | +my$nnodes = 2; |
| 33 | +my@nodes = (); |
| 34 | + |
| 35 | +diag("creating nodes"); |
| 36 | +foreachmy$i (1..$nnodes) |
| 37 | +{ |
| 38 | +my$host ="127.0.0.1"; |
| 39 | +my ($pgport,$raftport) = allocate_ports($host, 2); |
| 40 | +my$node = new PostgresNode("node$i",$host,$pgport); |
| 41 | +$node->{id} =$i; |
| 42 | +$node->{raftport} =$raftport; |
| 43 | +push(@nodes,$node); |
| 44 | +} |
| 45 | + |
| 46 | +my$mm_connstr =join(',',map {"${\$_->connstr('postgres') }" }@nodes); |
| 47 | +my$raft_peers =join(',',map {join(':',$_->{id},$_->host,$_->{raftport}) }@nodes); |
| 48 | + |
| 49 | +diag("mm_connstr =$mm_connstr\n"); |
| 50 | +diag("raft_peers =$raft_peers\n"); |
| 51 | + |
| 52 | +diag("initting and configuring nodes"); |
| 53 | +foreachmy$node (@nodes) |
| 54 | +{ |
| 55 | +my$id =$node->{id}; |
| 56 | +my$host =$node->host; |
| 57 | +my$pgport =$node->port; |
| 58 | +my$raftport =$node->{raftport}; |
| 59 | + |
| 60 | +$node->init(hba_permit_replication=> 0); |
| 61 | +$node->append_conf("postgresql.conf",qq( |
| 62 | +listen_addresses = '$host' |
| 63 | +unix_socket_directories = '' |
| 64 | +port =$pgport |
| 65 | +max_prepared_transactions = 10 |
| 66 | +max_worker_processes = 10 |
| 67 | +wal_level = logical |
| 68 | +fsync = off |
| 69 | +max_wal_senders = 10 |
| 70 | +wal_sender_timeout = 0 |
| 71 | +max_replication_slots = 10 |
| 72 | +shared_preload_libraries = 'raftable,multimaster' |
| 73 | +multimaster.workers = 4 |
| 74 | +multimaster.queue_size = 10485760 # 10mb |
| 75 | +multimaster.node_id =$id |
| 76 | +multimaster.conn_strings = '$mm_connstr' |
| 77 | +multimaster.use_raftable = true |
| 78 | +multimaster.ignore_tables_without_pk = true |
| 79 | +raftable.id =$id |
| 80 | +raftable.peers = '$raft_peers' |
| 81 | +)); |
| 82 | + |
| 83 | +$node->append_conf("pg_hba.conf",qq( |
| 84 | +local replication all trust |
| 85 | +host replication all 127.0.0.1/32 trust |
| 86 | +host replication all ::1/128 trust |
| 87 | +)); |
| 88 | +} |
| 89 | + |
| 90 | +diag("starting nodes"); |
| 91 | +foreachmy$node (@nodes) |
| 92 | +{ |
| 93 | +$node->start(); |
| 94 | +} |
| 95 | + |
| 96 | +my ($rc,$out,$err); |
| 97 | + |
| 98 | +diag("sleeping 10"); |
| 99 | +sleep(10); |
| 100 | + |
| 101 | +diag("preparing the tables"); |
| 102 | +if ($nodes[0]->psql('postgres',"create table t (k int primary key, v int)")) |
| 103 | +{ |
| 104 | +BAIL_OUT('failed to create t'); |
| 105 | +} |
| 106 | + |
| 107 | +if ($nodes[0]->psql('postgres',"insert into t (select generate_series(0, 999), 0)")) |
| 108 | +{ |
| 109 | +BAIL_OUT('failed to fill t'); |
| 110 | +} |
| 111 | + |
| 112 | +if ($nodes[0]->psql('postgres',"create table reader_log (v int)")) |
| 113 | +{ |
| 114 | +BAIL_OUT('failed to create reader_log'); |
| 115 | +} |
| 116 | + |
| 117 | +subreader |
| 118 | +{ |
| 119 | +my ($node,$inref,$outref) =@_; |
| 120 | + |
| 121 | +my$clients = 1; |
| 122 | +my$jobs = 1; |
| 123 | +my$seconds = 30; |
| 124 | +my$tps = 10; |
| 125 | +my@argv = ( |
| 126 | +'pgbench', |
| 127 | +'-n', |
| 128 | +-c=>$clients, |
| 129 | +-j=>$jobs, |
| 130 | +-T=>$seconds, |
| 131 | +-h=>$node->host(), |
| 132 | +-p=>$node->port(), |
| 133 | +-f=>'tests/writer.pgb', |
| 134 | +-R=>$tps, |
| 135 | +'postgres', |
| 136 | +); |
| 137 | + |
| 138 | +diag("running[" . getcwd() ."]:" .join('',@argv)); |
| 139 | + |
| 140 | +return start(\@argv,$inref,$outref); |
| 141 | +} |
| 142 | + |
| 143 | +subwriter |
| 144 | +{ |
| 145 | +my ($node,$inref,$outref) =@_; |
| 146 | + |
| 147 | +my$clients = 10; |
| 148 | +my$jobs = 10; |
| 149 | +my$seconds = 30; |
| 150 | +my@argv = ( |
| 151 | +'pgbench', |
| 152 | +'-n', |
| 153 | +-c=>$clients, |
| 154 | +-j=>$jobs, |
| 155 | +-T=>$seconds, |
| 156 | +-h=>$node->host(), |
| 157 | +-p=>$node->port(), |
| 158 | +-f=>'tests/reader.pgb', |
| 159 | +'postgres', |
| 160 | +); |
| 161 | + |
| 162 | +diag("running[" . getcwd() ."]:" .join('',@argv)); |
| 163 | + |
| 164 | +return start(\@argv,$inref,$outref); |
| 165 | +} |
| 166 | + |
| 167 | +diag("starting benches"); |
| 168 | +my$in =''; |
| 169 | +my$out =''; |
| 170 | +my@benches = (); |
| 171 | +foreachmy$node (@nodes) |
| 172 | +{ |
| 173 | +push(@benches, writer($node, \$in, \$out)); |
| 174 | +push(@benches, reader($node, \$in, \$out)); |
| 175 | +} |
| 176 | + |
| 177 | +diag("finishing benches"); |
| 178 | +foreachmy$bench (@benches) |
| 179 | +{ |
| 180 | +finish($bench) || BAIL_OUT("pgbench exited with$?"); |
| 181 | +} |
| 182 | +diag($out); |
| 183 | + |
| 184 | +diag("checking readers' logs"); |
| 185 | + |
| 186 | +($rc,$out,$err) =$nodes[0]->psql('postgres',"select count(*) from reader_log where v != 0;"); |
| 187 | +is($out, 0,"there is nothing except zeros in reader_log"); |
| 188 | + |
| 189 | +($rc,$out,$err) =$nodes[0]->psql('postgres',"select count(*) from reader_log where v = 0;"); |
| 190 | +isnt($out, 0,"there are some zeros in reader_log"); |