@@ -10,30 +10,11 @@ use Cwd;
1010use IPC::Run;
1111use Socket;
1212
13- # 127.0.0.1 is not ok because we pick up random ephemeral port which might be
14- # occupied by client connection while node is down -- and then it would refuse
15- # to start later. At least on my linux the kernel binds clients to 127.0.0.1,
16- # so this works. Alternatively we could
17- # 1) bind to the socket manually immediately after shutdown and release before
18- # the start, but this obviously only (greatly) reduces the danger;
19- # 2) forget we are a distributed thing and use unix sockets like vanilla tests
20- #
21- # UPD: well, bf member msvs-6-3 decided to take 127.0.0.2 for local connections,
22- # so 1) was also implemented for such cases.
23- # I don't like falling back to 2) because a) it is slightly easier to connect to
24- # ports b) Windows uses TCP anyway.
25- our $mm_listen_address = ' 127.0.0.2' ;
26-
2713sub new
2814{
2915 my ($class , $n_nodes , $referee ) = @_ ;
3016
31- # ask PostgresNode to use tcp and listen on mm_listen_address
32- $PostgresNode::use_tcp = 1;
33- $PostgresNode::test_pghost = $mm_listen_address ;
34-
3517 my @nodes = map { get_new_node(" node$_ " ) } (1..$n_nodes );
36- $_ -> hold_port() foreach @nodes ;
3718
3819 my $self = {
3920 nodes => \@nodes ,
@@ -42,7 +23,6 @@ sub new
4223 if (defined $referee && $referee )
4324 {
4425 $self -> {referee } = get_new_node(" referee" );
45- $self -> {referee }-> hold_port();
4626 }
4727
4828 bless $self , $class ;
@@ -63,8 +43,7 @@ sub init
6343 }
6444
6545 my $hba = qq{
66- host all all ${mm_listen_address} /32 trust
67- host replication all ${mm_listen_address} /32 trust
46+ local all all trust
6847 } ;
6948
7049 # binary protocol doesn't tolerate strict alignment currently, so use it
@@ -169,7 +148,7 @@ sub init
169148
170149sub create_mm
171150{
172- my ($self , $dbname , $connect_timeout ) = @_ ;
151+ my ($self , $dbname ) = @_ ;
173152 my $nodes = $self -> {nodes };
174153
175154 $self -> await_nodes([0..$# {$self -> {nodes }}], 0);
@@ -227,11 +206,8 @@ sub create_mm
227206 }
228207 }
229208
230- # identity_func uses connect_timeout as it tries connecting to shutdown
231- # node which hangs infinitely otherwise due to our hold_socket hack.
232209 (my $my_connstr , my @peers ) = map {
233- $_ -> connstr($_ -> {dbname }) . ((defined $connect_timeout ) ?
234- " connect_timeout=${connect_timeout} " : " " );
210+ $_ -> connstr($_ -> {dbname });
235211 } @{$self -> {nodes }};
236212
237213 my $node1 = $self -> {nodes }-> [0];
@@ -247,16 +223,17 @@ sub create_mm
247223sub start
248224{
249225 my ($self ) = @_ ;
226+ my $hosts = join ' , ' , map { $_ -> {_host }} @{$self -> {nodes }};
250227 my $ports = join ' , ' , map { $_ -> {_port }} @{$self -> {nodes }};
251- note( " starting cluster on ports: $ports " );
228+ note( " starting cluster on hosts: $hosts , ports: $ports " );
252229 $_ -> start() foreach @{$self -> {nodes }};
253230 if (defined $self -> {referee })
254231 {
232+ my $rhost = $self -> {referee }-> host;
255233 my $rport = $self -> {referee }-> port;
256- note(" starting referee on port $rport " );
234+ note(" starting referee on host $rhost port $rport " );
257235 $self -> {referee }-> start();
258236 }
259-
260237}
261238
262239sub stop
@@ -292,7 +269,6 @@ sub add_node()
292269 my ($self ) = @_ ;
293270
294271 my $new_node = get_new_node(" node@{[$# {$self ->{nodes}} + 2]}" );
295- $new_node -> hold_port();
296272 push (@{$self -> {nodes }}, $new_node );
297273
298274 return $# {$self -> {nodes }};
@@ -316,12 +292,13 @@ sub backup_and_init()
316292 my $node = $self -> {nodes }-> [$from ];
317293 my $backup_name = " backup_for_$to " ;
318294 my $backup_path = $node -> backup_dir . ' /' . $backup_name ;
295+ my $host = $node -> host;
319296 my $port = $node -> port;
320297 my $name = $node -> name;
321298
322299 print " # Taking pg_basebackup $backup_name from node \" $name \"\n " ;
323300 my $dumpres = command_output([' pg_basebackup' , ' -D' , $backup_path , ' -p' , $port ,
324- ' -h' , $mm_listen_address , ' --no-sync' , ' -v' ]);
301+ ' -h' , $host , ' --no-sync' , ' -v' ]);
325302
326303 print " # Backup finished\n " ;
327304
@@ -462,65 +439,5 @@ sub is_ee
462439 return ($stdout =~ m / enterprise/ );
463440}
464441
465- # Override PostgresNode::start|stop to reserve the port while node is down
466- {
467- # otherwise perl whines, thinking our override is redefine
468- no warnings ' redefine' ;
469- # that's how perl people call super from monkey-patch-overridden method
470- # https://stackoverflow.com/a/575873/4014587
471- my $postgres_node_start_super = \&PostgresNode::start;
472- *PostgresNode::start = sub {
473- my $self = shift ;
474- $self -> release_port();
475- $postgres_node_start_super -> ( $self , @_ );
476- };
477-
478- my $postgres_node_stop_super = \&PostgresNode::stop;
479- *PostgresNode::stop = sub {
480- my $self = shift ;
481- $postgres_node_stop_super -> ( $self , @_ );
482- $self -> hold_port();
483- };
484- }
485-
486- # Bind to socket with our port so no one could use it while node is down.
487- sub PostgresNode ::hold_port {
488- my $self = shift ;
489-
490- # do nothing if this is not mm node, it most probably uses unix socket
491- return unless $self -> host eq $mm_listen_address ;
492-
493- # stop() might be called several times without start(), don't bind if
494- # already did so
495- return if defined $self -> {held_socket };
496-
497- my $iaddr = inet_aton($self -> host);
498- my $paddr = sockaddr_in($self -> port, $iaddr );
499- my $proto = getprotobyname (" tcp" );
500-
501- socket (my $sock , PF_INET, SOCK_STREAM, $proto )
502- or die " socket failed: $! " ;
503-
504- # As in postmaster, don't use SO_REUSEADDR on Windows
505- setsockopt ($sock , SOL_SOCKET, SO_REUSEADDR, pack (" l" , 1))
506- unless $TestLib::windows_os ;
507- bind ($sock , $paddr ) or die
508- " socket bind failed: $! " ;
509- listen ($sock , SOMAXCONN)
510- or die " socket listen failed: $! " ;
511-
512- $self -> {held_socket } = $sock ;
513- note(" bind to port @{[$self ->port]}" );
514- }
515-
516- sub PostgresNode ::release_port {
517- my $self = shift ;
518- if (defined $self -> {held_socket })
519- {
520- close ($self -> {held_socket });
521- $self -> {held_socket } = undef ;
522- }
523- note(" port released" );
524- }
525442
5264431;
0 commit comments