static int send_packet_4_nodes(WdPacket *packet, WD_SEND_TYPE type) { int rtn; WdInfo * p = WD_List; int i,cnt; int sock; int rc; pthread_attr_t attr; pthread_t thread[MAX_WATCHDOG_NUM]; WdPacketThreadArg thread_arg[MAX_WATCHDOG_NUM]; if (packet == NULL) { return WD_NG; } /* thread init */ pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); /* skip myself */ p++; WD_MYSELF->is_contactable = true; /* send packet to other watchdogs */ cnt = 0; while (p->status != WD_END) { /* don't send packet to pgpool in down */ if (p->status == WD_DOWN || (packet->packet_no != WD_ADD_REQ && p->status == WD_INIT)) { p->is_contactable = false; p++; continue; } if (type == WD_SEND_TO_MASTER ) { if (p->status != WD_MASTER) { p++; continue; } } else if (type == WD_SEND_WITHOUT_MASTER ) { if (p->status == WD_MASTER) { p++; continue; } } sock = wd_create_send_socket(p->hostname, p->wd_port); if (sock == -1) { pool_log("send_packet_4_nodes: packet for %s:%d is canceled", p->hostname, p->wd_port); p->is_contactable = false; p++; continue; } else { p->is_contactable = true; } thread_arg[cnt].sock = sock; thread_arg[cnt].target = p; thread_arg[cnt].packet = packet; rc = pthread_create(&thread[cnt], &attr, wd_thread_negotiation, (void*)&thread_arg[cnt]); cnt ++; p++; } pthread_attr_destroy(&attr); /* no packet is sent */ if (cnt == 0) { return WD_OK; } /* default return value */ if ((packet->packet_no == WD_STAND_FOR_MASTER) || (packet->packet_no == WD_STAND_FOR_LOCK_HOLDER) || (packet->packet_no == WD_DECLARE_LOCK_HOLDER) || (packet->packet_no == WD_START_RECOVERY)) { rtn = WD_OK; } else { rtn = WD_NG; } /* receive the results */ for (i=0; i<cnt; ) { int result; rc = pthread_join(thread[i], (void **)&result); if ((rc != 0) && (errno == EINTR)) { usleep(100); continue; } /* aggregate results according to the packet type */ if ((packet->packet_no == WD_STAND_FOR_MASTER) || (packet->packet_no == WD_STAND_FOR_LOCK_HOLDER) || (packet->packet_no == WD_DECLARE_LOCK_HOLDER) || (packet->packet_no == WD_START_RECOVERY)) { /* if any result is NG then return NG */ if (result == WD_NG) { rtn = WD_NG; } } else { /* if any result is OK then return OK */ if (result == WD_OK) { rtn = WD_OK; } } i++; } return rtn; }
static int send_packet_4_nodes(WdPacket *packet, WD_SEND_TYPE type) { int rtn; WdInfo * p = WD_List; int i,cnt; int sock; int rc; pthread_attr_t attr; pthread_t thread[MAX_WATCHDOG_NUM]; WdPacketThreadArg thread_arg[MAX_WATCHDOG_NUM]; if (packet == NULL) { return WD_NG; } if ((type == WD_SEND_TO_MASTER) && (WD_MYSELF->status == WD_MASTER)) { return WD_OK; } /* thread init */ pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); /* send packet to all watchdogs */ p++; cnt = 0; while (p->status != WD_END) { if (p->status == WD_DOWN) { p++; continue; } if (type == WD_SEND_TO_MASTER ) { if (p->status != WD_MASTER) { p++; continue; } } else if (type == WD_SEND_WITHOUT_MASTER ) { if (p->status == WD_MASTER) { p++; continue; } } sock = wd_create_send_socket(p->hostname, p->wd_port); if (sock == -1) { p++; continue; } thread_arg[cnt].sock = sock; thread_arg[cnt].target = p; thread_arg[cnt].packet = packet; rc = pthread_create(&thread[cnt], &attr, wd_thread_negotiation, (void*)&thread_arg[cnt]); cnt ++; p++; } pthread_attr_destroy(&attr); if (cnt == 0) { return WD_OK; } /* init return value */ if ((packet->packet_no == WD_STAND_FOR_MASTER) || (packet->packet_no == WD_STAND_FOR_LOCK_HOLDER) || (packet->packet_no == WD_START_RECOVERY)) { rtn = WD_OK; } else { rtn = WD_NG; } for (i=0; i<cnt; ) { int result; rc = pthread_join(thread[i], (void **)&result); if ((rc != 0) && (errno == EINTR)) { usleep(100); continue; } if ((packet->packet_no == WD_STAND_FOR_MASTER) || (packet->packet_no == WD_STAND_FOR_LOCK_HOLDER) || (packet->packet_no == WD_START_RECOVERY)) { /* if any result is NG then return NG */ if (result == WD_NG) { rtn = WD_NG; } } else { /* if any result is OK then return OK */ if (result == WD_OK) { rtn = WD_OK; } } i++; } return rtn; }