11use strict;
22use warnings;
33
4- use PostgresNode ;
4+ use Cluster ;
55use TestLib;
66use Test::Moretests => 2;
77use IPC::Runqw( start finish) ;
88use Cwd;
99
10- my %allocated_ports = ();
11- sub allocate_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- next if $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-
3210my $nnodes = 2;
33- my @nodes = ();
34-
35- diag(" creating nodes" );
36- foreach my $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 );
11+ my $cluster = new Cluster($nnodes );
4812
49- diag(" mm_connstr =$mm_connstr \n " );
50- diag(" raft_peers =$raft_peers \n " );
51-
52- diag(" initting and configuring nodes" );
53- foreach my $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 = 200
66- max_connections = 200
67- max_worker_processes = 100
68- wal_level = logical
69- fsync = off
70- max_wal_senders = 10
71- wal_sender_timeout = 0
72- max_replication_slots = 10
73- shared_preload_libraries = 'raftable,multimaster'
74- multimaster.workers = 10
75- multimaster.queue_size = 10485760 # 10mb
76- multimaster.node_id =$id
77- multimaster.conn_strings = '$mm_connstr '
78- multimaster.use_raftable = true
79- multimaster.ignore_tables_without_pk = true
80- raftable.id =$id
81- raftable.peers = '$raft_peers '
82- ) );
83-
84- $node -> append_conf(" pg_hba.conf" ,qq(
85- local replication all trust
86- host replication all 127.0.0.1/32 trust
87- host replication all ::1/128 trust
88- ) );
89- }
90-
91- diag(" starting nodes" );
92- foreach my $node (@nodes )
93- {
94- $node -> start();
95- }
13+ $cluster -> init();
14+ $cluster -> configure();
15+ $cluster -> start();
9616
9717my ($rc ,$out ,$err );
9818
9919diag(" sleeping 10" );
10020sleep (10);
10121
10222diag(" preparing the tables" );
103- if ($nodes [0] -> psql(' postgres' ," create table t (k int primary key, v int)" ))
23+ if ($cluster -> psql(0, ' postgres' ," create table t (k int primary key, v int)" ))
10424{
10525BAIL_OUT(' failed to create t' );
10626}
10727
108- if ($nodes [0] -> psql(' postgres' ," insert into t (select generate_series(0, 999), 0)" ))
28+ if ($cluster -> psql(0, ' postgres' ," insert into t (select generate_series(0, 999), 0)" ))
10929{
11030BAIL_OUT(' failed to fill t' );
11131}
11232
113- if ($nodes [0] -> psql(' postgres' ," create table reader_log (v int)" ))
33+ if ($cluster -> psql(0, ' postgres' ," create table reader_log (v int)" ))
11434{
11535BAIL_OUT(' failed to create reader_log' );
11636}
@@ -169,7 +89,7 @@ sub writer
16989my $in =' ' ;
17090my $out =' ' ;
17191my @benches = ();
172- foreach my $node (@nodes )
92+ foreach my $node (@{ $cluster -> { nodes }} )
17393{
17494push (@benches , writer($node , \$in , \$out ));
17595push (@benches , reader($node , \$in , \$out ));
@@ -184,8 +104,8 @@ sub writer
184104
185105diag(" checking readers' logs" );
186106
187- ($rc ,$out ,$err ) =$nodes [0] -> psql(' postgres' ," select count(*) from reader_log where v != 0;" );
107+ ($rc ,$out ,$err ) =$cluster -> psql(0, ' postgres' ," select count(*) from reader_log where v != 0;" );
188108is($out , 0," there is nothing except zeros in reader_log" );
189109
190- ($rc ,$out ,$err ) =$nodes [0] -> psql(' postgres' ," select count(*) from reader_log where v = 0;" );
110+ ($rc ,$out ,$err ) =$cluster -> psql(0, ' postgres' ," select count(*) from reader_log where v = 0;" );
191111isnt($out , 0," there are some zeros in reader_log" );