1
1
use strict;
2
2
use warnings;
3
3
4
- use PostgresNode ;
4
+ use Cluster ;
5
5
use TestLib;
6
6
use Test::More tests => 2;
7
7
use IPC::Run qw( start finish) ;
8
8
use Cwd;
9
9
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
-
32
10
my $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 );
48
12
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();
96
16
97
17
my ($rc , $out , $err );
98
18
99
19
diag(" sleeping 10" );
100
20
sleep (10);
101
21
102
22
diag(" 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)" ))
104
24
{
105
25
BAIL_OUT(' failed to create t' );
106
26
}
107
27
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)" ))
109
29
{
110
30
BAIL_OUT(' failed to fill t' );
111
31
}
112
32
113
- if ($nodes [0] -> psql(' postgres' , " create table reader_log (v int)" ))
33
+ if ($cluster -> psql(0, ' postgres' , " create table reader_log (v int)" ))
114
34
{
115
35
BAIL_OUT(' failed to create reader_log' );
116
36
}
@@ -169,7 +89,7 @@ sub writer
169
89
my $in = ' ' ;
170
90
my $out = ' ' ;
171
91
my @benches = ();
172
- foreach my $node (@nodes )
92
+ foreach my $node (@{ $cluster -> { nodes }} )
173
93
{
174
94
push (@benches , writer($node , \$in , \$out ));
175
95
push (@benches , reader($node , \$in , \$out ));
@@ -184,8 +104,8 @@ sub writer
184
104
185
105
diag(" checking readers' logs" );
186
106
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;" );
188
108
is($out , 0, " there is nothing except zeros in reader_log" );
189
109
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;" );
191
111
isnt($out , 0, " there are some zeros in reader_log" );
0 commit comments