From: Date: November 17 2008 3:21pm Subject: bzr commit into mysql-5.1 branch (tomas.ulin:2748) Bug#38473 List-Archive: http://lists.mysql.com/commits/58959 X-Bug: 38473 Message-Id: <20081117142102.3952A440D2@linux.local> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit #At file:///home/tomas/mysql_src/mysql-5.1-telco-6.3/ 2748 Tomas Ulin 2008-11-17 Bug #38473 The MGM API function ndb_mgm_listen_event does not use bind-address parameter. + extend ndb connectstring to take bind address parameter modified: sql/ha_ndbcluster_connection.cc storage/ndb/include/mgmapi/mgmapi.h storage/ndb/src/mgmapi/LocalConfig.cpp storage/ndb/src/mgmapi/LocalConfig.hpp storage/ndb/src/mgmapi/mgmapi.cpp === modified file 'sql/ha_ndbcluster_connection.cc' --- a/sql/ha_ndbcluster_connection.cc 2008-10-02 16:11:03 +0000 +++ b/sql/ha_ndbcluster_connection.cc 2008-11-17 14:20:48 +0000 @@ -138,7 +138,8 @@ int ndbcluster_connect(int (*connect_cal connect_callback(); for (unsigned i= 0; i < g_ndb_cluster_connection_pool_alloc; i++) { - if (g_ndb_cluster_connection_pool[i]->node_id() == 0) + int node_id= g_ndb_cluster_connection_pool[i]->node_id(); + if (node_id == 0) { // not connected to mgmd yet, try again g_ndb_cluster_connection_pool[i]->connect(0,0,0); @@ -148,6 +149,7 @@ int ndbcluster_connect(int (*connect_cal g_ndb_cluster_connection_pool[i]->start_connect_thread(); continue; } + node_id= g_ndb_cluster_connection_pool[i]->node_id(); } DBUG_PRINT("info", ("NDBCLUSTER storage engine (%u) at %s on port %d", i, @@ -161,18 +163,21 @@ int ndbcluster_connect(int (*connect_cal gettimeofday(&now_time, 0); } while (res != 0 && end_time.tv_sec > now_time.tv_sec); + const char *msg; if (res == 0) { - sql_print_information("NDB[%u]: all storage nodes connected", i); + msg= "all storage nodes connected"; } else if (res > 0) { - sql_print_information("NDB[%u]: some storage nodes connected", i); + msg= "some storage nodes connected"; } else if (res < 0) { - sql_print_information("NDB[%u]: no storage nodes connected (timed out)", i); + msg= "no storage nodes connected (timed out)"; } + sql_print_information("NDB[%u]: NodeID: %d, %s", + i, node_id, msg); } } else if (res == 1) === modified file 'storage/ndb/include/mgmapi/mgmapi.h' --- a/storage/ndb/include/mgmapi/mgmapi.h 2008-08-07 03:45:20 +0000 +++ b/storage/ndb/include/mgmapi/mgmapi.h 2008-11-17 14:20:48 +0000 @@ -554,6 +554,15 @@ extern "C" { */ const char *ndb_mgm_get_connected_host(NdbMgmHandle handle); + /** + * Gets connection bind address + * + * @param handle Management handle + * + * @return hostname + */ + const char *ndb_mgm_get_connected_bind_address(NdbMgmHandle handle); + #ifndef DOXYGEN_SHOULD_SKIP_INTERNAL /** @} *********************************************************************/ /** === modified file 'storage/ndb/src/mgmapi/LocalConfig.cpp' --- a/storage/ndb/src/mgmapi/LocalConfig.cpp 2008-10-24 11:00:37 +0000 +++ b/storage/ndb/src/mgmapi/LocalConfig.cpp 2008-11-17 14:20:48 +0000 @@ -22,6 +22,7 @@ LocalConfig::LocalConfig(){ error_line = 0; error_msg[0] = 0; _ownNodeId= 0; + bind_address_port= 0; } bool @@ -153,6 +154,11 @@ const char *hostNameTokens[] = { 0 }; +const char *bindAddressTokens[] = { + "bind-address=%[^:]:%i", + 0 +}; + const char *fileNameTokens[] = { "file://%s", "file=%s", @@ -179,6 +185,10 @@ LocalConfig::parseHostName(const char * mgmtSrvrId.type = MgmId_TCP; mgmtSrvrId.name.assign(tempString); mgmtSrvrId.port = port; + /* assign default bind_address if available */ + if (bind_address.length()) + mgmtSrvrId.bind_address.assign(bind_address); + mgmtSrvrId.bind_address_port = bind_address_port; ids.push_back(mgmtSrvrId); return true; } @@ -193,6 +203,41 @@ LocalConfig::parseHostName(const char * } bool +LocalConfig::parseBindAddress(const char * buf) +{ + char tempString[1024]; + char tempString2[1024]; + int port; + do + { + for(int i = 0; bindAddressTokens[i] != 0; i++) + { + if (sscanf(buf, bindAddressTokens[i], tempString, &port) == 2) + { + if (ids.size() == 0) + { + /* assign default bind_address */ + bind_address.assign(tempString); + bind_address_port = port; + return true; + } + /* override bind_address on latest mgmd */ + MgmtSrvrId &mgmtSrvrId= ids[ids.size()-1]; + mgmtSrvrId.bind_address.assign(tempString); + mgmtSrvrId.bind_address_port = port; + return true; + } + } + if (buf == tempString2) + break; + // try to add port 0 to see if it works + snprintf(tempString2, sizeof(tempString2),"%s:0", buf); + buf= tempString2; + } while(1); + return false; +} + +bool LocalConfig::parseFileName(const char * buf){ char tempString[1024]; for(int i = 0; fileNameTokens[i] != 0; i++) { @@ -221,6 +266,8 @@ LocalConfig::parseString(const char * co if (parseNodeId(tok)) continue; if (parseHostName(tok)) + continue; + if (parseBindAddress(tok)) continue; if (parseFileName(tok)) continue; === modified file 'storage/ndb/src/mgmapi/LocalConfig.hpp' --- a/storage/ndb/src/mgmapi/LocalConfig.hpp 2006-12-23 19:20:40 +0000 +++ b/storage/ndb/src/mgmapi/LocalConfig.hpp 2008-11-17 14:20:48 +0000 @@ -33,6 +33,8 @@ struct MgmtSrvrId { MgmtSrvrId_Type type; BaseString name; unsigned int port; + BaseString bind_address; + unsigned int bind_address_port; }; struct LocalConfig { @@ -43,6 +45,9 @@ struct LocalConfig { int error_line; char error_msg[256]; + BaseString bind_address; + unsigned int bind_address_port; + LocalConfig(); ~LocalConfig(); bool init(const char *connectString = 0, @@ -58,6 +63,7 @@ struct LocalConfig { bool parseNodeId(const char *buf); bool parseHostName(const char *buf); + bool parseBindAddress(const char *buf); bool parseFileName(const char *buf); bool parseString(const char *buf, BaseString &err); char * makeConnectString(char *buf, int sz); === modified file 'storage/ndb/src/mgmapi/mgmapi.cpp' --- a/storage/ndb/src/mgmapi/mgmapi.cpp 2008-10-31 15:22:39 +0000 +++ b/storage/ndb/src/mgmapi/mgmapi.cpp 2008-11-17 14:20:48 +0000 @@ -106,6 +106,7 @@ struct ndb_mgm_handle { int mgmd_version_minor; int mgmd_version_build; char * m_bindaddress; + int m_bindaddress_port; }; #define SET_ERROR(h, e, s) setError(h, e, __LINE__, s) @@ -189,6 +190,7 @@ ndb_mgm_create_handle() h->errstream = stdout; h->m_name = 0; h->m_bindaddress = 0; + h->m_bindaddress_port = 0; strncpy(h->last_error_desc, "No error", NDB_MGM_MAX_ERR_DESC_SIZE); @@ -245,10 +247,22 @@ ndb_mgm_set_bindaddress(NdbMgmHandle han free(handle->m_bindaddress); if (arg) + { handle->m_bindaddress = strdup(arg); + char *port = strchr(handle->m_bindaddress, ':'); + if (port != 0) + { + handle->m_bindaddress_port = atoi(port+1); + *port = 0; + } + else + handle->m_bindaddress_port = 0; + } else + { handle->m_bindaddress = 0; - + handle->m_bindaddress_port = 0; + } DBUG_RETURN(0); } @@ -522,50 +536,6 @@ ndb_mgm_connect(NdbMgmHandle handle, int LocalConfig &cfg= handle->cfg; NDB_SOCKET_TYPE sockfd= NDB_INVALID_SOCKET; Uint32 i; - SocketClient s(0, 0); - s.set_connect_timeout((handle->timeout+999)/1000); - if (!s.init()) - { - fprintf(handle->errstream, - "Unable to create socket, " - "while trying to connect with connect string: %s\n", - cfg.makeConnectString(buf,sizeof(buf))); - - setError(handle, NDB_MGM_COULD_NOT_CONNECT_TO_SOCKET, __LINE__, - "Unable to create socket, " - "while trying to connect with connect string: %s\n", - cfg.makeConnectString(buf,sizeof(buf))); - DBUG_RETURN(-1); - } - - if (handle->m_bindaddress) - { - BaseString::snprintf(buf, sizeof(buf), handle->m_bindaddress); - unsigned short portno = 0; - char * port = strchr(buf, ':'); - if (port != 0) - { - portno = atoi(port+1); - * port = 0; - } - int err; - if ((err = s.bind(buf, portno)) != 0) - { - fprintf(handle->errstream, - "Unable to bind local address %s errno: %d, " - "while trying to connect with connect string: %s\n", - handle->m_bindaddress, err, - cfg.makeConnectString(buf,sizeof(buf))); - - setError(handle, NDB_MGM_BIND_ADDRESS, __LINE__, - "Unable to bind local address %s errno: %d, " - "while trying to connect with connect string: %s\n", - handle->m_bindaddress, err, - cfg.makeConnectString(buf,sizeof(buf))); - DBUG_RETURN(-1); - } - } - while (sockfd == NDB_INVALID_SOCKET) { // do all the mgmt servers @@ -573,6 +543,58 @@ ndb_mgm_connect(NdbMgmHandle handle, int { if (cfg.ids[i].type != MgmId_TCP) continue; + + SocketClient s(0, 0); + const char *bind_address= NULL; + unsigned short bind_address_port= 0; + s.set_connect_timeout((handle->timeout+999)/1000); + if (!s.init()) + { + fprintf(handle->errstream, + "Unable to create socket, " + "while trying to connect with connect string: %s\n", + cfg.makeConnectString(buf,sizeof(buf))); + + setError(handle, NDB_MGM_COULD_NOT_CONNECT_TO_SOCKET, __LINE__, + "Unable to create socket, " + "while trying to connect with connect string: %s\n", + cfg.makeConnectString(buf,sizeof(buf))); + DBUG_RETURN(-1); + } + if (handle->m_bindaddress) + { + bind_address= handle->m_bindaddress; + bind_address_port= handle->m_bindaddress_port; + } + else if (cfg.ids[i].bind_address.length()) + { + bind_address= cfg.ids[i].bind_address.c_str(); + bind_address_port= cfg.ids[i].bind_address_port; + } + if (bind_address) + { + int err; + if ((err = s.bind(bind_address, bind_address_port)) != 0) + { + if (!handle->m_bindaddress) + { + // retry with next mgmt server + continue; + } + fprintf(handle->errstream, + "Unable to bind local address '%s:%d' errno: %d, " + "while trying to connect with connect string: '%s'\n", + bind_address, (int)bind_address_port, err, + cfg.makeConnectString(buf,sizeof(buf))); + + setError(handle, NDB_MGM_BIND_ADDRESS, __LINE__, + "Unable to bind local address '%s:%d' errno: %d, " + "while trying to connect with connect string: '%s'\n", + bind_address, (int)bind_address_port, err, + cfg.makeConnectString(buf,sizeof(buf))); + DBUG_RETURN(-1); + } + } sockfd = s.connect(cfg.ids[i].name.c_str(), cfg.ids[i].port); if (sockfd != NDB_INVALID_SOCKET) break; @@ -1712,8 +1734,33 @@ ndb_mgm_listen_event_internal(NdbMgmHand const char *hostname= ndb_mgm_get_connected_host(handle); int port= ndb_mgm_get_connected_port(handle); - SocketClient s(hostname, port); - const NDB_SOCKET_TYPE sockfd = s.connect(); + const char *bind_address= ndb_mgm_get_connected_bind_address(handle); + SocketClient s(0, 0); + s.set_connect_timeout((handle->timeout+999)/1000); + if (!s.init()) + { + fprintf(handle->errstream, "Unable to create socket"); + setError(handle, NDB_MGM_COULD_NOT_CONNECT_TO_SOCKET, __LINE__, + "Unable to create socket"); + return -1; + } + if (bind_address) + { + int err; + if ((err = s.bind(bind_address, 0)) != 0) + { + fprintf(handle->errstream, + "Unable to bind local address '%s:0' err: %d, errno: %d, " + "while trying to connect with connect string: '%s:%d'\n", + bind_address, err, errno, hostname, port); + setError(handle, NDB_MGM_BIND_ADDRESS, __LINE__, + "Unable to bind local address '%s:0' errno: %d, errno: %d, " + "while trying to connect with connect string: '%s:%d'\n", + bind_address, err, errno, hostname, port); + return -1; + } + } + const NDB_SOCKET_TYPE sockfd = s.connect(hostname, port); if (sockfd == NDB_INVALID_SOCKET) { setError(handle, NDB_MGM_COULD_NOT_CONNECT_TO_SOCKET, __LINE__, "Unable to connect to"); @@ -2318,6 +2365,19 @@ extern "C" const char *ndb_mgm_get_connectstring(NdbMgmHandle handle, char *buf, int buf_sz) { return handle->cfg.makeConnectString(buf,buf_sz); +} + +extern "C" +const char *ndb_mgm_get_connected_bind_address(NdbMgmHandle handle) +{ + if (handle->cfg_i >= 0) + { + if (handle->m_bindaddress) + return handle->m_bindaddress; + if (handle->cfg.ids[handle->cfg_i].bind_address.length()) + return handle->cfg.ids[handle->cfg_i].bind_address.c_str(); + } + return 0; } extern "C"