From 0f7cb6795f40aadfc793f04f66e044e85d9b72e1 Mon Sep 17 00:00:00 2001 From: Heikki Linnakangas Date: Tue, 14 Feb 2017 09:13:48 +0200 Subject: [PATCH] Move function to more logical place. SendDummyPacket() is completely specific to the UDP interconnect implementation. Along the way, I couldn't resist some cosmetic cleanup: use %m rather than strerror(errno), avoid unnecessary variable initializations, and pgindent. --- src/backend/cdb/motion/ic_common.c | 108 ----------------------------- src/backend/cdb/motion/ic_udpifc.c | 107 ++++++++++++++++++++++++++++ src/include/cdb/ml_ipc.h | 5 -- 3 files changed, 107 insertions(+), 113 deletions(-) diff --git a/src/backend/cdb/motion/ic_common.c b/src/backend/cdb/motion/ic_common.c index 159ef78f7a..5612a6308f 100644 --- a/src/backend/cdb/motion/ic_common.c +++ b/src/backend/cdb/motion/ic_common.c @@ -793,114 +793,6 @@ void adjustMasterRouting(Slice *recvSlice) } } -void -SendDummyPacket(void) -{ - int sockfd = -1; - int ret = -1; - struct addrinfo* addrs = NULL; - struct addrinfo* rp = NULL; - struct addrinfo hint; - uint16 udp_listener; - char port_str[32] = {0}; - char* dummy_pkt = "stop it"; - - /* - * Get address info from interconnect udp listener port - */ - udp_listener = Gp_listener_port; - snprintf(port_str, sizeof(port_str), "%d", udp_listener); - - MemSet(&hint, 0, sizeof(hint)); - hint.ai_socktype = SOCK_DGRAM; - hint.ai_family = AF_UNSPEC; /* Allow for IPv4 or IPv6 */ - -#ifdef AI_NUMERICSERV - hint.ai_flags = AI_NUMERICHOST | AI_NUMERICSERV; /* Never do name resolution */ -#else - hint.ai_flags = AI_NUMERICHOST; /* Never do name resolution */ -#endif - - ret = pg_getaddrinfo_all(NULL, port_str, &hint, &addrs); - if (ret || !addrs) - { - elog(LOG, "Send dummy packet failed, pg_getaddrinfo_all(): %s", strerror(errno)); - goto send_error; - } - - for (rp = addrs; rp != NULL; rp = rp->ai_next) - { - /* Create socket according to pg_getaddrinfo_all() */ - sockfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); - if (sockfd < 0) - { - continue; - } - - if (!pg_set_noblock(sockfd)) - { - if (sockfd >= 0) - { - closesocket(sockfd); - sockfd = -1; - } - continue; - } - break; - } - - if (rp == NULL) - { - elog(LOG, "Send dummy packet failed, create socket failed: %s", strerror(errno)); - goto send_error; - } - - /* - * Send a dummy package to the interconnect listener, try 10 times - */ - int counter = 0; - while (counter < 10) - { - counter++; - ret = sendto(sockfd, dummy_pkt, strlen(dummy_pkt), 0, rp->ai_addr, rp->ai_addrlen); - if (ret < 0) - { - if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) - { - continue; - } - else - { - elog(LOG, "Send dummy packet failed, sendto failed: %s", strerror(errno)); - goto send_error; - } - } - break; - } - - if (counter >= 10) - { - elog(LOG, "Send dummy packet failed, sendto failed: %s", strerror(errno)); - goto send_error; - } - - pg_freeaddrinfo_all(hint.ai_family, addrs); - closesocket(sockfd); - return; - -send_error: - - if (addrs) - { - pg_freeaddrinfo_all(hint.ai_family, addrs); - } - if (sockfd != -1) - { - closesocket(sockfd); - } - return; -} - /* * WaitInterconnectQuit * diff --git a/src/backend/cdb/motion/ic_udpifc.c b/src/backend/cdb/motion/ic_udpifc.c index f1ba5e6d68..50a939dbf6 100644 --- a/src/backend/cdb/motion/ic_udpifc.c +++ b/src/backend/cdb/motion/ic_udpifc.c @@ -643,6 +643,7 @@ static void setMainThreadWaiting(ThreadWaitingState *state, int motNodeId, int r static void checkRxThreadError(void); static void setRxThreadError(int eno); static void resetRxThreadError(void); +static void SendDummyPacket(void); static void getSockAddr(struct sockaddr_storage * peer, socklen_t * peer_len, const char * listenerAddr, int listenerPort); static void setXmitSocketOptions(int txfd); @@ -6686,3 +6687,109 @@ WaitInterconnectQuitUDPIFC(void) } ic_control_info.threadCreated = false; } + +/* + * Send a dummy packet to interconnect thread to exit poll() immediately + */ +static void +SendDummyPacket(void) +{ + int sockfd = -1; + int ret; + struct addrinfo *addrs = NULL; + struct addrinfo *rp; + struct addrinfo hint; + uint16 udp_listener; + char port_str[32] = {0}; + char *dummy_pkt = "stop it"; + int counter; + + /* + * Get address info from interconnect udp listener port + */ + udp_listener = Gp_listener_port; + snprintf(port_str, sizeof(port_str), "%d", udp_listener); + + MemSet(&hint, 0, sizeof(hint)); + hint.ai_socktype = SOCK_DGRAM; + hint.ai_family = AF_UNSPEC; /* Allow for IPv4 or IPv6 */ + + /* Never do name resolution */ +#ifdef AI_NUMERICSERV + hint.ai_flags = AI_NUMERICHOST | AI_NUMERICSERV; +#else + hint.ai_flags = AI_NUMERICHOST; +#endif + + ret = pg_getaddrinfo_all(NULL, port_str, &hint, &addrs); + if (ret || !addrs) + { + elog(LOG, "send dummy packet failed, pg_getaddrinfo_all(): %m"); + goto send_error; + } + + for (rp = addrs; rp != NULL; rp = rp->ai_next) + { + /* Create socket according to pg_getaddrinfo_all() */ + sockfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); + if (sockfd < 0) + continue; + + if (!pg_set_noblock(sockfd)) + { + if (sockfd >= 0) + { + closesocket(sockfd); + sockfd = -1; + } + continue; + } + break; + } + + if (rp == NULL) + { + elog(LOG, "send dummy packet failed, create socket failed: %m"); + goto send_error; + } + + /* + * Send a dummy package to the interconnect listener, try 10 times + */ + + counter = 0; + while (counter < 10) + { + counter++; + ret = sendto(sockfd, dummy_pkt, strlen(dummy_pkt), 0, rp->ai_addr, rp->ai_addrlen); + if (ret < 0) + { + if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) + continue; + else + { + elog(LOG, "send dummy packet failed, sendto failed: %m"); + goto send_error; + } + } + break; + } + + if (counter >= 10) + { + elog(LOG, "send dummy packet failed, sendto failed: %m"); + goto send_error; + } + + pg_freeaddrinfo_all(hint.ai_family, addrs); + closesocket(sockfd); + return; + +send_error: + + if (addrs) + pg_freeaddrinfo_all(hint.ai_family, addrs); + if (sockfd != -1) + closesocket(sockfd); + return; +} diff --git a/src/include/cdb/ml_ipc.h b/src/include/cdb/ml_ipc.h index 46095c1248..ea68f05a1b 100644 --- a/src/include/cdb/ml_ipc.h +++ b/src/include/cdb/ml_ipc.h @@ -50,11 +50,6 @@ extern void CleanUpMotionLayerIPC(void); */ extern void WaitInterconnectQuit(void); -/* -* Send a dummy packet to interconnect thread to exit poll() immediately -*/ -extern void SendDummyPacket(void); - /* Returns the fd of the socket that connects to the seqserver. This value * is -1 if it has not been setup. */ -- GitLab