@@ -91,7 +91,7 @@ static void MtmSender(Datum arg);
91
91
static void MtmReceiver (Datum arg );
92
92
static void MtmMonitor (Datum arg );
93
93
static void MtmSendHeartbeat (void );
94
- static bool MtmSendToNode (int node , void const * buf , int size , time_t reconnectTimeout );
94
+ static bool MtmSendToNode (int node , void const * buf , int size );
95
95
96
96
char const * const MtmMessageKindMnem [] =
97
97
{
@@ -160,7 +160,7 @@ static void MtmRegisterSocket(int fd, int node)
160
160
ev .events = EPOLLIN ;
161
161
ev .data .u32 = node ;
162
162
if (epoll_ctl (epollfd , EPOLL_CTL_ADD , fd , & ev ) < 0 ) {
163
- MTM_ELOG (LOG , "Arbiter failed to add socket to epoll set: %d " , errno );
163
+ MTM_ELOG (LOG , "Arbiter failed to add socket to epoll set: %s " , strerror ( errno ) );
164
164
}
165
165
#else
166
166
FD_SET (fd , & inset );
@@ -174,7 +174,7 @@ static void MtmUnregisterSocket(int fd)
174
174
{
175
175
#if USE_EPOLL
176
176
if (epoll_ctl (epollfd , EPOLL_CTL_DEL , fd , NULL ) < 0 ) {
177
- MTM_ELOG (LOG , "Arbiter failed to unregister socket from epoll set: %d " , errno );
177
+ MTM_ELOG (LOG , "Arbiter failed to unregister socket from epoll set: %s " , strerror ( errno ) );
178
178
}
179
179
#else
180
180
FD_CLR (fd , & inset );
@@ -365,7 +365,7 @@ static void MtmSendHeartbeat()
365
365
|| !BIT_CHECK (Mtm -> disabledNodeMask , i )
366
366
|| BIT_CHECK (Mtm -> reconnectMask , i )))
367
367
{
368
- if (!MtmSendToNode (i , & msg , sizeof (msg ), MtmHeartbeatRecvTimeout )) {
368
+ if (!MtmSendToNode (i , & msg , sizeof (msg ))) {
369
369
MTM_ELOG (LOG , "Arbiter failed to send heartbeat to node %d" , i + 1 );
370
370
} else {
371
371
if (last_heartbeat_to_node [i ] + MSEC_TO_USEC (MtmHeartbeatSendTimeout )* 2 < now ) {
@@ -402,7 +402,7 @@ void MtmCheckHeartbeat()
402
402
}
403
403
404
404
405
- static int MtmConnectSocket (int node , int port , time_t timeout )
405
+ static int MtmConnectSocket (int node , int port )
406
406
{
407
407
struct addrinfo * addrs = NULL ;
408
408
struct addrinfo * addr ;
@@ -411,12 +411,9 @@ static int MtmConnectSocket(int node, int port, time_t timeout)
411
411
MtmHandshakeMessage req ;
412
412
MtmArbiterMessage resp ;
413
413
int sd = -1 ;
414
- int ret ;
415
- timestamp_t start = MtmGetSystemTime ();
414
+ int rc ;
416
415
char const * host = Mtm -> nodes [node ].con .hostName ;
417
416
nodemask_t save_mask = busy_mask ;
418
- timestamp_t afterWait ;
419
- timestamp_t beforeWait ;
420
417
421
418
/* Initialize hint structure */
422
419
MemSet (& hint , 0 , sizeof (hint ));
@@ -425,67 +422,60 @@ static int MtmConnectSocket(int node, int port, time_t timeout)
425
422
426
423
snprintf (portstr , sizeof (portstr ), "%d" , port );
427
424
428
- ret = pg_getaddrinfo_all (host , portstr , & hint , & addrs );
429
- if (ret != 0 )
425
+ rc = pg_getaddrinfo_all (host , portstr , & hint , & addrs );
426
+ if (rc != 0 )
430
427
{
431
- MTM_ELOG (LOG , "Arbiter failed to resolve host '%s' by name: (%d) % s" , host , ret , gai_strerror (ret ));
428
+ MTM_ELOG (LOG , "Arbiter failed to resolve host '%s' by name: % s" , host , gai_strerror (rc ));
432
429
return -1 ;
433
430
}
434
431
BIT_SET (busy_mask , node );
435
432
436
- Retry :
437
- while (1 ) {
438
- int rc = -1 ;
439
- sd = socket (AF_INET , SOCK_STREAM , 0 );
440
- if (sd < 0 ) {
441
- MTM_ELOG (LOG , "Arbiter failed to create socket: %d" , errno );
442
- goto Error ;
443
- }
444
- rc = fcntl (sd , F_SETFL , O_NONBLOCK );
445
- if (rc < 0 ) {
446
- MTM_ELOG (LOG , "Arbiter failed to switch socket to non-blocking mode: %d" , errno );
447
- goto Error ;
448
- }
449
- for (addr = addrs ; addr != NULL ; addr = addr -> ai_next )
450
- {
451
- do {
452
- rc = connect (sd , addr -> ai_addr , addr -> ai_addrlen );
453
- } while (rc < 0 && errno == EINTR );
433
+ Retry :
454
434
455
- if (rc >= 0 || errno == EINPROGRESS ) {
456
- break ;
457
- }
458
- }
459
- if (rc == 0 ) {
435
+ sd = socket (AF_INET , SOCK_STREAM , 0 );
436
+ if (sd < 0 ) {
437
+ MTM_ELOG (LOG , "Arbiter failed to create socket: %s" , strerror (errno ));
438
+ goto Error ;
439
+ }
440
+ rc = fcntl (sd , F_SETFL , O_NONBLOCK );
441
+ if (rc < 0 ) {
442
+ MTM_ELOG (LOG , "Arbiter failed to switch socket to non-blocking mode: %s" , strerror (errno ));
443
+ goto Error ;
444
+ }
445
+ for (addr = addrs ; addr != NULL ; addr = addr -> ai_next )
446
+ {
447
+ do {
448
+ rc = connect (sd , addr -> ai_addr , addr -> ai_addrlen );
449
+ } while (rc < 0 && errno == EINTR );
450
+
451
+ if (rc >= 0 || errno == EINPROGRESS ) {
460
452
break ;
461
453
}
462
- beforeWait = MtmGetSystemTime ();
463
- if (errno != EINPROGRESS || start + MSEC_TO_USEC (timeout ) < beforeWait ) {
464
- MTM_ELOG (WARNING , "Arbiter failed to connect to %s:%d: error=%d" , host , port , errno );
465
- goto Error ;
466
- } else {
467
- rc = MtmWaitSocket (sd , true, MtmHeartbeatSendTimeout );
468
- if (rc == 1 ) {
469
- socklen_t optlen = sizeof (int );
470
- if (getsockopt (sd , SOL_SOCKET , SO_ERROR , (void * )& rc , & optlen ) < 0 ) {
471
- MTM_ELOG (WARNING , "Arbiter failed to getsockopt for %s:%d: error=%d" , host , port , errno );
472
- goto Error ;
473
- }
474
- if (rc == 0 ) {
475
- break ;
476
- } else {
477
- MTM_ELOG (WARNING , "Arbiter trying to connect to %s:%d: rc=%d, error=%d" , host , port , rc , errno );
478
- }
479
- } else {
480
- MTM_ELOG (WARNING , "Arbiter waiting socket to %s:%d: rc=%d, error=%d" , host , port , rc , errno );
454
+ }
455
+
456
+ if (rc != 0 && errno == EINPROGRESS ) {
457
+ rc = MtmWaitSocket (sd , true, MtmHeartbeatSendTimeout );
458
+ if (rc == 1 ) {
459
+ socklen_t optlen = sizeof (int );
460
+ int errcode ;
461
+
462
+ if (getsockopt (sd , SOL_SOCKET , SO_ERROR , (void * )& errcode , & optlen ) < 0 ) {
463
+ MTM_ELOG (WARNING , "Arbiter failed to getsockopt for %s:%d: %s" , host , port , strerror (errcode ));
464
+ goto Error ;
481
465
}
482
- close (sd );
483
- afterWait = MtmGetSystemTime ();
484
- if (afterWait < beforeWait + MSEC_TO_USEC (MtmHeartbeatSendTimeout )) {
485
- MtmSleep (beforeWait + MSEC_TO_USEC (MtmHeartbeatSendTimeout ) - afterWait );
466
+ if (errcode != 0 ) {
467
+ MTM_ELOG (WARNING , "Arbiter trying to connect to %s:%d: %s" , host , port , strerror (errcode ));
468
+ goto Error ;
486
469
}
470
+ } else {
471
+ MTM_ELOG (WARNING , "Arbiter waiting socket to %s:%d: %s" , host , port , strerror (errno ));
487
472
}
488
473
}
474
+ else if (rc != 0 ) {
475
+ MTM_ELOG (WARNING , "Arbiter failed to connect to %s:%d: (%d) %s" , host , port , rc , strerror (errno ));
476
+ goto Error ;
477
+ }
478
+
489
479
MtmSetSocketOptions (sd );
490
480
MtmInitMessage (& req .hdr , MSG_HANDSHAKE );
491
481
req .hdr .node = MtmNodeId ;
@@ -494,12 +484,12 @@ static int MtmConnectSocket(int node, int port, time_t timeout)
494
484
req .hdr .csn = MtmGetCurrentTime ();
495
485
strcpy (req .connStr , Mtm -> nodes [MtmNodeId - 1 ].con .connStr );
496
486
if (!MtmWriteSocket (sd , & req , sizeof req )) {
497
- MTM_ELOG (WARNING , "Arbiter failed to send handshake message to %s:%d: %d " , host , port , errno );
487
+ MTM_ELOG (WARNING , "Arbiter failed to send handshake message to %s:%d: %s " , host , port , strerror ( errno ) );
498
488
close (sd );
499
489
goto Retry ;
500
490
}
501
491
if (MtmReadSocket (sd , & resp , sizeof resp ) != sizeof (resp )) {
502
- MTM_ELOG (WARNING , "Arbiter failed to receive response for handshake message from %s:%d: errno=%d " , host , port , errno );
492
+ MTM_ELOG (WARNING , "Arbiter failed to receive response for handshake message from %s:%d: %s " , host , port , strerror ( errno ) );
503
493
close (sd );
504
494
goto Retry ;
505
495
}
@@ -521,7 +511,7 @@ static int MtmConnectSocket(int node, int port, time_t timeout)
521
511
522
512
return sd ;
523
513
524
- Error :
514
+ Error :
525
515
busy_mask = save_mask ;
526
516
if (sd >= 0 ) {
527
517
close (sd );
@@ -545,7 +535,7 @@ static void MtmOpenConnections()
545
535
}
546
536
for (i = 0 ; i < nNodes ; i ++ ) {
547
537
if (i + 1 != MtmNodeId && i < Mtm -> nAllNodes ) {
548
- sockets [i ] = MtmConnectSocket (i , Mtm -> nodes [i ].con .arbiterPort , MtmConnectTimeout );
538
+ sockets [i ] = MtmConnectSocket (i , Mtm -> nodes [i ].con .arbiterPort );
549
539
if (sockets [i ] < 0 ) {
550
540
MtmOnNodeDisconnect (i + 1 );
551
541
}
@@ -560,7 +550,7 @@ static void MtmOpenConnections()
560
550
}
561
551
562
552
563
- static bool MtmSendToNode (int node , void const * buf , int size , time_t reconnectTimeout )
553
+ static bool MtmSendToNode (int node , void const * buf , int size )
564
554
{
565
555
bool result = true;
566
556
nodemask_t save_mask = busy_mask ;
@@ -583,11 +573,11 @@ static bool MtmSendToNode(int node, void const* buf, int size, time_t reconnectT
583
573
}
584
574
if (sockets [node ] < 0 || !MtmWriteSocket (sockets [node ], buf , size )) {
585
575
if (sockets [node ] >= 0 ) {
586
- MTM_ELOG (WARNING , "Arbiter fail to write to node %d: %d " , node + 1 , errno );
576
+ MTM_ELOG (WARNING , "Arbiter fail to write to node %d: %s " , node + 1 , strerror ( errno ) );
587
577
close (sockets [node ]);
588
578
sockets [node ] = -1 ;
589
579
}
590
- sockets [node ] = MtmConnectSocket (node , Mtm -> nodes [node ].con .arbiterPort , reconnectTimeout );
580
+ sockets [node ] = MtmConnectSocket (node , Mtm -> nodes [node ].con .arbiterPort );
591
581
if (sockets [node ] < 0 ) {
592
582
MtmOnNodeDisconnect (node + 1 );
593
583
result = false;
@@ -607,7 +597,7 @@ static int MtmReadFromNode(int node, void* buf, int buf_size)
607
597
{
608
598
int rc = MtmReadSocket (sockets [node ], buf , buf_size );
609
599
if (rc <= 0 ) {
610
- MTM_ELOG (WARNING , "Arbiter failed to read from node=%d, rc=%d, errno=%d " , node + 1 , rc , errno );
600
+ MTM_ELOG (WARNING , "Arbiter failed to read from node=%d: %s " , node + 1 , strerror ( errno ) );
611
601
MtmDisconnect (node );
612
602
}
613
603
return rc ;
@@ -617,17 +607,17 @@ static void MtmAcceptOneConnection()
617
607
{
618
608
int fd = accept (gateway , NULL , NULL );
619
609
if (fd < 0 ) {
620
- MTM_ELOG (WARNING , "Arbiter failed to accept socket: %d " , errno );
610
+ MTM_ELOG (WARNING , "Arbiter failed to accept socket: %s " , strerror ( errno ) );
621
611
} else {
622
612
MtmHandshakeMessage req ;
623
613
MtmArbiterMessage resp ;
624
614
int rc = fcntl (fd , F_SETFL , O_NONBLOCK );
625
615
if (rc < 0 ) {
626
- MTM_ELOG (ERROR , "Arbiter failed to switch socket to non-blocking mode: %d " , errno );
616
+ MTM_ELOG (ERROR , "Arbiter failed to switch socket to non-blocking mode: %s " , strerror ( errno ) );
627
617
}
628
618
rc = MtmReadSocket (fd , & req , sizeof req );
629
619
if (rc < sizeof (req )) {
630
- MTM_ELOG (WARNING , "Arbiter failed to handshake socket: %d, errno=%d " , rc , errno );
620
+ MTM_ELOG (WARNING , "Arbiter failed to handshake socket: %s " , strerror ( errno ) );
631
621
close (fd );
632
622
} else if (req .hdr .code != MSG_HANDSHAKE && req .hdr .dxid != HANDSHAKE_MAGIC ) {
633
623
MTM_ELOG (WARNING , "Arbiter get unexpected handshake message %d" , req .hdr .code );
@@ -764,7 +754,7 @@ static void MtmSender(Datum arg)
764
754
765
755
for (i = 0 ; i < Mtm -> nAllNodes ; i ++ ) {
766
756
if (txBuffer [i ].used != 0 ) {
767
- MtmSendToNode (i , txBuffer [i ].data , txBuffer [i ].used * sizeof (MtmArbiterMessage ), MtmReconnectTimeout );
757
+ MtmSendToNode (i , txBuffer [i ].data , txBuffer [i ].used * sizeof (MtmArbiterMessage ));
768
758
txBuffer [i ].used = 0 ;
769
759
}
770
760
}
@@ -864,7 +854,7 @@ static void MtmReceiver(Datum arg)
864
854
if (errno == EINTR ) {
865
855
continue ;
866
856
}
867
- MTM_ELOG (ERROR , "Arbiter failed to poll sockets: %d " , errno );
857
+ MTM_ELOG (ERROR , "Arbiter failed to poll sockets: %s " , strerror ( errno ) );
868
858
}
869
859
for (j = 0 ; j < n ; j ++ ) {
870
860
i = events [j ].data .u32 ;
@@ -888,7 +878,7 @@ static void MtmReceiver(Datum arg)
888
878
} while (n < 0 && MtmRecovery ());
889
879
890
880
if (n < 0 ) {
891
- MTM_ELOG (ERROR , "Arbiter failed to select sockets: %d " , errno );
881
+ MTM_ELOG (ERROR , "Arbiter failed to select sockets: %s " , strerror ( errno ) );
892
882
}
893
883
for (i = 0 ; i < nNodes ; i ++ ) {
894
884
if (sockets [i ] >= 0 && FD_ISSET (sockets [i ], & events ))
0 commit comments