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