Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit cc38c6a

Browse files
kvapkelvich
authored andcommitted
Add the Cluster perl module for tap tests.
1 parent 3a908ad commit cc38c6a

File tree

2 files changed

+155
-91
lines changed

2 files changed

+155
-91
lines changed

Cluster.pm

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
package Cluster;
2+
3+
use strict;
4+
use warnings;
5+
6+
use PostgresNode;
7+
use TestLib;
8+
use Test::More;
9+
use Cwd;
10+
11+
my %allocated_ports = ();
12+
sub allocate_ports
13+
{
14+
my @allocated_now = ();
15+
my ($host, $ports_to_alloc) = @_;
16+
17+
while ($ports_to_alloc > 0)
18+
{
19+
my $port = int(rand() * 16384) + 49152;
20+
next if $allocated_ports{$port};
21+
diag("checking for port $port\n");
22+
if (!TestLib::run_log(['pg_isready', '-h', $host, '-p', $port]))
23+
{
24+
$allocated_ports{$port} = 1;
25+
push(@allocated_now, $port);
26+
$ports_to_alloc--;
27+
}
28+
}
29+
30+
return @allocated_now;
31+
}
32+
33+
sub new
34+
{
35+
my ($class, $nodenum) = @_;
36+
37+
my $nodes = [];
38+
39+
foreach my $i (1..$nodenum)
40+
{
41+
my $host = "127.0.0.1";
42+
my ($pgport, $raftport) = allocate_ports($host, 2);
43+
my $node = new PostgresNode("node$i", $host, $pgport);
44+
$node->{id} = $i;
45+
$node->{raftport} = $raftport;
46+
push(@$nodes, $node);
47+
}
48+
49+
my $self = {
50+
nodenum => $nodenum,
51+
nodes => $nodes,
52+
};
53+
54+
bless $self, $class;
55+
return $self;
56+
}
57+
58+
sub init
59+
{
60+
my ($self) = @_;
61+
my $nodes = $self->{nodes};
62+
63+
foreach my $node (@$nodes)
64+
{
65+
$node->init(hba_permit_replication => 0);
66+
}
67+
}
68+
69+
sub configure
70+
{
71+
my ($self) = @_;
72+
my $nodes = $self->{nodes};
73+
74+
my $connstr = join(',', map { "${ \$_->connstr('postgres') }" } @$nodes);
75+
my $raftpeers = join(',', map { join(':', $_->{id}, $_->host, $_->{raftport}) } @$nodes);
76+
77+
foreach my $node (@$nodes)
78+
{
79+
my $id = $node->{id};
80+
my $host = $node->host;
81+
my $pgport = $node->port;
82+
my $raftport = $node->{raftport};
83+
84+
$node->append_conf("postgresql.conf", qq(
85+
listen_addresses = '$host'
86+
unix_socket_directories = ''
87+
port = $pgport
88+
max_prepared_transactions = 200
89+
max_connections = 200
90+
max_worker_processes = 100
91+
wal_level = logical
92+
fsync = off
93+
max_wal_senders = 10
94+
wal_sender_timeout = 0
95+
max_replication_slots = 10
96+
shared_preload_libraries = 'raftable,multimaster'
97+
multimaster.workers = 10
98+
multimaster.queue_size = 10485760 # 10mb
99+
multimaster.node_id = $id
100+
multimaster.conn_strings = '$connstr'
101+
multimaster.use_raftable = true
102+
multimaster.ignore_tables_without_pk = true
103+
raftable.id = $id
104+
raftable.peers = '$raftpeers'
105+
));
106+
107+
$node->append_conf("pg_hba.conf", qq(
108+
local replication all trust
109+
host replication all 127.0.0.1/32 trust
110+
host replication all ::1/128 trust
111+
));
112+
}
113+
}
114+
115+
sub start
116+
{
117+
my ($self) = @_;
118+
my $nodes = $self->{nodes};
119+
120+
foreach my $node (@$nodes)
121+
{
122+
$node->start();
123+
}
124+
}
125+
126+
sub stop
127+
{
128+
my ($self) = @_;
129+
my $nodes = $self->{nodes};
130+
131+
foreach my $node (@$nodes)
132+
{
133+
$node->stop();
134+
}
135+
}
136+
137+
sub psql
138+
{
139+
my ($self, $index, @args) = @_;
140+
my $node = $self->{nodes}->[$index];
141+
return $node->psql(@args);
142+
}
143+
144+
1;

t/003_pgbench.pl

Lines changed: 11 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,116 +1,36 @@
11
use strict;
22
use warnings;
33

4-
use PostgresNode;
4+
use Cluster;
55
use TestLib;
66
use Test::More tests => 2;
77
use IPC::Run qw(start finish);
88
use 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-
3210
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);
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

9717
my ($rc, $out, $err);
9818

9919
diag("sleeping 10");
10020
sleep(10);
10121

10222
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)"))
10424
{
10525
BAIL_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
{
11030
BAIL_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
{
11535
BAIL_OUT('failed to create reader_log');
11636
}
@@ -169,7 +89,7 @@ sub writer
16989
my $in = '';
17090
my $out = '';
17191
my @benches = ();
172-
foreach my $node (@nodes)
92+
foreach my $node (@{$cluster->{nodes}})
17393
{
17494
push(@benches, writer($node, \$in, \$out));
17595
push(@benches, reader($node, \$in, \$out));
@@ -184,8 +104,8 @@ sub writer
184104

185105
diag("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;");
188108
is($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;");
191111
isnt($out, 0, "there are some zeros in reader_log");

0 commit comments

Comments
 (0)