18 #include <ndb_global.h>
20 #include <ConfigRetriever.hpp>
22 #include <SocketServer.hpp>
26 #include <mgmapi_config_parameters.h>
27 #include <mgmapi_configuration.hpp>
28 #include <mgmapi_internal.h>
29 #include <ConfigValues.hpp>
35 ConfigRetriever::ConfigRetriever(
const char * _connect_string,
39 const char * _bindaddress,
43 m_node_type(node_type)
45 DBUG_ENTER(
"ConfigRetriever::ConfigRetriever");
46 DBUG_PRINT(
"enter", (
"connect_string: '%s', force_nodeid: %d",
47 _connect_string, force_nodeid));
48 DBUG_PRINT(
"enter", (
"version: %d, node_type: %d, bind: %s, timeout: %d",
49 version, node_type,_bindaddress, timeout_ms));
54 setError(
CR_ERROR,
"Unable to allocate mgm handle");
70 ndb_mgm_set_configuration_nodeid(m_handle, force_nodeid))
72 setError(
CR_ERROR,
"Failed to set forced nodeid");
88 ConfigRetriever::~ConfigRetriever()
90 DBUG_ENTER(
"ConfigRetriever::~ConfigRetriever");
103 ConfigRetriever::get_configuration_nodeid()
const
108 Uint32 ConfigRetriever::get_mgmd_port()
const
113 const char *ConfigRetriever::get_mgmd_host()
const
118 const char *ConfigRetriever::get_connectstring(
char *
buf,
int buf_sz)
const
127 ConfigRetriever::do_connect(
int no_retries,
128 int retry_delay_in_seconds,
int verbose)
131 (
ndb_mgm_connect(m_handle,no_retries,retry_delay_in_seconds,verbose)==0) ?
136 ConfigRetriever::disconnect()
142 ConfigRetriever::is_connected(
void)
173 const int from_node = 0;
175 ndb_mgm_get_configuration2(mgm_handle,
193 if (access(filename, F_OK))
196 err.
assfmt(
"Could not find file: '%s'", filename);
201 FILE * f = fopen(filename,
"rb");
204 setError(
CR_ERROR,
"Failed to open file");
211 while ((read_sz = fread(read_buf, 1,
sizeof(read_buf), f)) != 0)
213 if (config_buf.append(read_buf, read_sz) != 0)
215 setError(
CR_ERROR,
"Out of memory when appending read data");
223 if(!cvf.unpack(config_buf))
225 setError(
CR_ERROR,
"Error while unpacking");
232 ConfigRetriever::setError(ErrorType et,
const char * s){
233 errorString.
assign(s ? s :
"");
234 latestErrorType = et;
235 DBUG_PRINT(
"info", (
"latestErrorType: %u, '%s'",
236 latestErrorType, errorString.
c_str()));
240 ConfigRetriever::setError(ErrorType et,
BaseString err){
241 setError(et, err.
c_str());
245 ConfigRetriever::resetError(){
246 setError(CR_NO_ERROR,0);
250 ConfigRetriever::hasError()
252 return latestErrorType != CR_NO_ERROR;
256 ConfigRetriever::getErrorString(){
257 return errorString.
c_str();
268 if(it.find(CFG_NODE_ID, nodeid)){
274 const char * hostname;
275 if(it.get(CFG_NODE_HOST, &hostname)){
282 if (hostname && hostname[0] != 0 &&
285 "The hostname this node should have according "
286 "to the configuration does not match a local "
287 "interface. Attempt to bind '%s' "
288 "failed with error: %d '%s'",
289 hostname, errno, strerror(errno));
295 if(it.get(CFG_TYPE_OF_SECTION, &_type)){
297 CFG_TYPE_OF_SECTION);
302 if(_type != (
unsigned int)m_node_type){
303 const char *type_s, *alias_s, *type_s2, *alias_s2;
311 "This node type %s(%s) and config "
312 "node type %s(%s) don't match for nodeid %d",
313 alias_s, type_s, alias_s2, type_s2, nodeid);
322 for(iter.first(); iter.valid(); iter.next()){
324 Uint32
type = CONNECTION_TYPE_TCP + 1;
325 if(iter.get(CFG_TYPE_OF_SECTION, &type))
continue;
326 if(type != CONNECTION_TYPE_TCP)
continue;
328 Uint32 nodeId1, nodeId2, remoteNodeId;
329 if(iter.get(CFG_CONNECTION_NODE_1, &nodeId1))
continue;
330 if(iter.get(CFG_CONNECTION_NODE_2, &nodeId2))
continue;
332 if(nodeId1 != nodeid && nodeId2 != nodeid)
continue;
333 remoteNodeId = (nodeid == nodeId1 ? nodeId2 : nodeId1);
338 if(!iter.get(CFG_CONNECTION_HOSTNAME_1, &name) && strlen(name)){
339 if(Ndb_getInAddr(&addr, name) != 0){
340 tmp.assfmt(
"Unable to lookup/illegal hostname %s, "
341 "connection from node %d to node %d",
342 name, nodeid, remoteNodeId);
348 if(!iter.get(CFG_CONNECTION_HOSTNAME_2, &name) && strlen(name)){
349 if(Ndb_getInAddr(&addr, name) != 0){
350 tmp.assfmt(
"Unable to lookup/illegal hostname %s, "
351 "connection from node %d to node %d",
352 name, nodeid, remoteNodeId);
363 ConfigRetriever::setNodeId(Uint32 nodeid)
365 return ndb_mgm_set_configuration_nodeid(m_handle, nodeid);
380 res= ndb_mgm_alloc_nodeid(m_handle, m_version, m_node_type,
387 if (no_retries == 0 || error == NDB_MGM_ALLOCID_CONFIG_MISMATCH)
390 NdbSleep_SecSleep(retry_delay_in_seconds);
397 setError(
CR_ERROR,
"management server handle not initialized");