ConfigRetriever Class Reference

#include <ConfigRetriever.hpp>


Detailed Description

Used by nodes (DB, MGM, API) to get their config from MGM server.

Definition at line 28 of file ConfigRetriever.hpp.

Public Member Functions

Uint32 allocNodeId (int no_retries, int retry_delay_in_seconds)
 ConfigRetriever (const char *_connect_string, Uint32 version, Uint32 nodeType)
int do_connect (int no_retries, int retry_delay_in_seconds, int verbose)
Uint32 get_configuration_nodeid () const
const char * get_connectstring (char *buf, int buf_sz) const
const char * get_mgmd_host () const
Uint32 get_mgmd_port () const
NdbMgmHandle get_mgmHandle ()
NdbMgmHandleget_mgmHandlePtr ()
ndb_mgm_configurationgetConfig (const char *file)
ndb_mgm_configurationgetConfig (NdbMgmHandle handle)
ndb_mgm_configurationgetConfig ()
const char * getErrorString ()
int hasError ()
void resetError ()
int setNodeId (Uint32 nodeid)
bool verifyConfig (const struct ndb_mgm_configuration *, Uint32 nodeid)
 ~ConfigRetriever ()

Private Types

enum  ErrorType { CR_NO_ERROR = 0, CR_ERROR = 1, CR_RETRY = 2 }

Private Member Functions

void setError (ErrorType, const char *errorMsg)

Private Attributes

Uint32 _ownNodeId
BaseString errorString
ErrorType latestErrorType
NdbMgmHandle m_handle
Uint32 m_node_type
Uint32 m_version


Member Enumeration Documentation

enum ConfigRetriever::ErrorType [private]
 

Enumerator:
CR_NO_ERROR 
CR_ERROR 
CR_RETRY 

Definition at line 84 of file ConfigRetriever.hpp.

00084                  {
00085     CR_NO_ERROR = 0,
00086     CR_ERROR = 1,
00087     CR_RETRY = 2
00088   };


Constructor & Destructor Documentation

ConfigRetriever::ConfigRetriever const char *  _connect_string,
Uint32  version,
Uint32  nodeType
 

Definition at line 47 of file ConfigRetriever.cpp.

References _ownNodeId, CR_ERROR, m_handle, m_node_type, m_version, ndb_mgm_create_handle(), ndb_mgm_get_latest_error_desc(), ndb_mgm_set_connectstring(), resetError(), and setError().

00049 {
00050   m_version = version;
00051   m_node_type = node_type;
00052   _ownNodeId= 0;
00053 
00054   m_handle= ndb_mgm_create_handle();
00055 
00056   if (m_handle == 0) {
00057     setError(CR_ERROR, "Unable to allocate mgm handle");
00058     return;
00059   }
00060 
00061   if (ndb_mgm_set_connectstring(m_handle, _connect_string))
00062   {
00063     setError(CR_ERROR, ndb_mgm_get_latest_error_desc(m_handle));
00064     return;
00065   }
00066   resetError();
00067 }

ConfigRetriever::~ConfigRetriever  ) 
 

Definition at line 69 of file ConfigRetriever.cpp.

References m_handle, ndb_mgm_destroy_handle(), and ndb_mgm_disconnect().

00070 {
00071   if (m_handle) {
00072     ndb_mgm_disconnect(m_handle);
00073     ndb_mgm_destroy_handle(&m_handle);
00074   }
00075 }


Member Function Documentation

Uint32 ConfigRetriever::allocNodeId int  no_retries,
int  retry_delay_in_seconds
 

Returns:
Node id of this node (as stated in local config or connectString)

Definition at line 326 of file ConfigRetriever.cpp.

References _ownNodeId, CR_ERROR, m_handle, m_node_type, m_version, ndb_mgm_alloc_nodeid(), ndb_mgm_get_latest_error_desc(), NdbSleep_SecSleep(), and setError().

Referenced by Ndb_cluster_connection::connect(), Configuration::fetch_configuration(), and MgmtSrvr::MgmtSrvr().

00327 {
00328   _ownNodeId= 0;
00329   if(m_handle != 0)
00330   {
00331     while (1)
00332     {
00333       int res= ndb_mgm_alloc_nodeid(m_handle, m_version, m_node_type);
00334       if(res >= 0)
00335         return _ownNodeId= (Uint32)res;
00336       if (no_retries == 0)
00337         break;
00338       no_retries--;
00339       NdbSleep_SecSleep(retry_delay_in_seconds);
00340     }
00341     setError(CR_ERROR, ndb_mgm_get_latest_error_desc(m_handle));
00342   } else
00343     setError(CR_ERROR, "management server handle not initialized");    
00344   return 0;
00345 }

int ConfigRetriever::do_connect int  no_retries,
int  retry_delay_in_seconds,
int  verbose
 

Definition at line 102 of file ConfigRetriever.cpp.

References m_handle, and ndb_mgm_connect().

Referenced by Ndb_cluster_connection::connect(), Configuration::fetch_configuration(), and MgmtSrvr::MgmtSrvr().

00104 {
00105   return
00106     (ndb_mgm_connect(m_handle,no_retries,retry_delay_in_seconds,verbose)==0) ?
00107     0 : -1;
00108 }

Uint32 ConfigRetriever::get_configuration_nodeid  )  const
 

Definition at line 78 of file ConfigRetriever.cpp.

References m_handle, and ndb_mgm_get_configuration_nodeid().

Referenced by MgmtSrvr::MgmtSrvr().

00079 {
00080   return ndb_mgm_get_configuration_nodeid(m_handle);
00081 }

const char * ConfigRetriever::get_connectstring char *  buf,
int  buf_sz
const
 

Definition at line 93 of file ConfigRetriever.cpp.

References m_handle, and ndb_mgm_get_connectstring().

Referenced by Ndb_cluster_connection::get_connectstring().

00094 {
00095   return ndb_mgm_get_connectstring(m_handle, buf, buf_sz);
00096 }

const char * ConfigRetriever::get_mgmd_host  )  const
 

Definition at line 88 of file ConfigRetriever.cpp.

References m_handle, and ndb_mgm_get_connected_host().

Referenced by Configuration::fetch_configuration(), and Ndb_cluster_connection::get_connected_host().

00089 {
00090   return ndb_mgm_get_connected_host(m_handle);
00091 }

Uint32 ConfigRetriever::get_mgmd_port  )  const
 

Definition at line 83 of file ConfigRetriever.cpp.

References m_handle, and ndb_mgm_get_connected_port().

Referenced by Configuration::fetch_configuration(), and Ndb_cluster_connection::get_connected_port().

00084 {
00085   return ndb_mgm_get_connected_port(m_handle);
00086 }

NdbMgmHandle ConfigRetriever::get_mgmHandle  )  [inline]
 

Definition at line 78 of file ConfigRetriever.hpp.

References m_handle.

Referenced by Ndb_cluster_connection::connect(), and MgmtSrvr::set_connect_string().

00078 { return m_handle; };

NdbMgmHandle* ConfigRetriever::get_mgmHandlePtr  )  [inline]
 

Definition at line 79 of file ConfigRetriever.hpp.

References m_handle.

Referenced by main().

00079 { return &m_handle; };

ndb_mgm_configuration * ConfigRetriever::getConfig const char *  file  ) 
 

Get config from file

Definition at line 146 of file ConfigRetriever.cpp.

References buf, CR_ERROR, f(), ConfigValuesFactory::m_cfg, setError(), BaseString::snprintf(), and ConfigValuesFactory::unpack().

00146                                                {
00147 #ifndef NDB_WIN32       
00148 
00149   struct stat sbuf;
00150   const int res = stat(filename, &sbuf);
00151   if(res != 0){
00152     char buf[255];
00153     BaseString::snprintf(buf, sizeof(buf), "Could not find file: \"%s\"", filename);
00154     setError(CR_ERROR, buf);
00155     return 0;
00156   }
00157   const Uint32 bytes = sbuf.st_size;
00158   
00159   Uint32 * buf2 = new Uint32[bytes/4+1];
00160   
00161   FILE * f = fopen(filename, "rb");
00162   if(f == 0){
00163     setError(CR_ERROR, "Failed to open file");
00164     delete []buf2;
00165     return 0;
00166   }
00167   Uint32 sz = fread(buf2, 1, bytes, f);
00168   fclose(f);
00169   if(sz != bytes){
00170     setError(CR_ERROR, "Failed to read file");
00171     delete []buf2;
00172     return 0;
00173   }
00174   
00175   ConfigValuesFactory cvf;
00176   if(!cvf.unpack(buf2, bytes)){
00177     char buf[255];
00178     BaseString::snprintf(buf, sizeof(buf), "Error while unpacking"); 
00179     setError(CR_ERROR, buf);
00180     delete []buf2;
00181     return 0;
00182   }
00183   delete [] buf2;
00184   return (ndb_mgm_configuration*)cvf.m_cfg;
00185 #else
00186   return 0;
00187 #endif
00188 }                          

ndb_mgm_configuration * ConfigRetriever::getConfig NdbMgmHandle  handle  ) 
 

Get config using socket

Definition at line 134 of file ConfigRetriever.cpp.

References CR_ERROR, m_version, ndb_mgm_get_configuration(), ndb_mgm_get_latest_error_desc(), and setError().

00134                                                {
00135   
00136   ndb_mgm_configuration * conf = ndb_mgm_get_configuration(m_handle,m_version);
00137   if(conf == 0){
00138     setError(CR_ERROR, ndb_mgm_get_latest_error_desc(m_handle));
00139     return 0;
00140   }
00141 
00142   return conf;
00143 }

struct ndb_mgm_configuration * ConfigRetriever::getConfig  ) 
 

Get configuration for current node.

Configuration is fetched from one MGM server configured in local config file. The method loops over all the configured MGM servers and tries to establish a connection. This is repeated until a connection is established, so the function hangs until a connection is established.

Returns:
ndb_mgm_configuration object if succeeded, NULL if erroneous local config file or configuration error.

Definition at line 115 of file ConfigRetriever.cpp.

References _ownNodeId, free, m_handle, p, and verifyConfig().

Referenced by Ndb_cluster_connection::connect(), Configuration::fetch_configuration(), and MgmtSrvr::fetchConfig().

00115                            {
00116 
00117   struct ndb_mgm_configuration * p = 0;
00118 
00119   if(m_handle != 0)
00120     p = getConfig(m_handle);
00121 
00122   if(p == 0)
00123     return 0;
00124   
00125   if(!verifyConfig(p, _ownNodeId)){
00126     free(p);
00127     p= 0;
00128   }
00129   
00130   return p;
00131 }

const char * ConfigRetriever::getErrorString  ) 
 

Definition at line 208 of file ConfigRetriever.cpp.

References BaseString::c_str(), and errorString.

Referenced by Ndb_cluster_connection::connect(), Configuration::fetch_configuration(), MgmtSrvr::MgmtSrvr(), and Ndb_cluster_connection_impl::Ndb_cluster_connection_impl().

00208                                {
00209   return errorString.c_str();
00210 }

int ConfigRetriever::hasError  ) 
 

Definition at line 202 of file ConfigRetriever.cpp.

References CR_NO_ERROR, and latestErrorType.

Referenced by Configuration::fetch_configuration(), and Ndb_cluster_connection_impl::Ndb_cluster_connection_impl().

00203 {
00204   return latestErrorType != CR_NO_ERROR;
00205 }

void ConfigRetriever::resetError  ) 
 

Definition at line 197 of file ConfigRetriever.cpp.

References CR_NO_ERROR, and setError().

Referenced by ConfigRetriever().

00197                            {
00198   setError(CR_NO_ERROR,0);
00199 }

void ConfigRetriever::setError ErrorType  ,
const char *  errorMsg
[private]
 

Definition at line 191 of file ConfigRetriever.cpp.

References BaseString::assign(), errorString, and latestErrorType.

Referenced by allocNodeId(), ConfigRetriever(), getConfig(), resetError(), and verifyConfig().

00191                                                      {
00192   errorString.assign(s ? s : "");
00193   latestErrorType = et;
00194 }

int ConfigRetriever::setNodeId Uint32  nodeid  ) 
 

Definition at line 320 of file ConfigRetriever.cpp.

References m_handle, and ndb_mgm_set_configuration_nodeid().

Referenced by Configuration::fetch_configuration().

00321 {
00322   return ndb_mgm_set_configuration_nodeid(m_handle, nodeid);
00323 }

bool ConfigRetriever::verifyConfig const struct ndb_mgm_configuration ,
Uint32  nodeid
 

Verify config

Definition at line 213 of file ConfigRetriever.cpp.

References _type, buf, CFG_CONNECTION_HOSTNAME_1, CFG_CONNECTION_HOSTNAME_2, CFG_CONNECTION_NODE_1, CFG_CONNECTION_NODE_2, CFG_NODE_DATADIR, CFG_NODE_HOST, CFG_NODE_ID, CFG_SECTION_CONNECTION, CFG_SECTION_NODE, CFG_TYPE_OF_SECTION, CONNECTION_TYPE_TCP, CR_ERROR, datadir, errno, ndb_mgm_configuration_iterator::first(), ndb_mgm_configuration_iterator::get(), hostname, m_node_type, name, Ndb_getInAddr(), ndb_mgm_create_configuration_iterator(), ndb_mgm_find(), ndb_mgm_get_int_parameter(), ndb_mgm_get_node_type_alias_string(), ndb_mgm_get_string_parameter(), NdbConfig_SetPath(), ndb_mgm_configuration_iterator::next(), setError(), BaseString::snprintf(), strerror(), SocketServer::tryBind(), and ndb_mgm_configuration_iterator::valid().

Referenced by getConfig(), and MgmtSrvr::MgmtSrvr().

00213                                                                                      {
00214 
00215   char buf[255];
00216   ndb_mgm_configuration_iterator * it;
00217   it = ndb_mgm_create_configuration_iterator((struct ndb_mgm_configuration *)conf,
00218                                              CFG_SECTION_NODE);
00219 
00220   if(it == 0){
00221     BaseString::snprintf(buf, 255, "Unable to create config iterator");
00222     setError(CR_ERROR, buf);
00223     return false;
00224     
00225   }
00226   NdbAutoPtr<ndb_mgm_configuration_iterator> ptr(it);
00227   
00228   if(ndb_mgm_find(it, CFG_NODE_ID, nodeid) != 0){
00229     BaseString::snprintf(buf, 255, "Unable to find node with id: %d", nodeid);
00230     setError(CR_ERROR, buf);
00231     return false;
00232   }
00233      
00234   const char * hostname;
00235   if(ndb_mgm_get_string_parameter(it, CFG_NODE_HOST, &hostname)){
00236     BaseString::snprintf(buf, 255, "Unable to get hostname(%d) from config",CFG_NODE_HOST);
00237     setError(CR_ERROR, buf);
00238     return false;
00239   }
00240 
00241   const char * datadir;
00242   if(!ndb_mgm_get_string_parameter(it, CFG_NODE_DATADIR, &datadir)){
00243     NdbConfig_SetPath(datadir);
00244   }
00245 
00246   if (hostname && hostname[0] != 0 &&
00247       !SocketServer::tryBind(0,hostname)) {
00248     BaseString::snprintf(buf, 255, "Config hostname(%s) don't match a local interface,"
00249              " tried to bind, error = %d - %s",
00250              hostname, errno, strerror(errno));
00251     setError(CR_ERROR, buf);
00252     return false;
00253   }
00254 
00255   unsigned int _type;
00256   if(ndb_mgm_get_int_parameter(it, CFG_TYPE_OF_SECTION, &_type)){
00257     BaseString::snprintf(buf, 255, "Unable to get type of node(%d) from config",
00258              CFG_TYPE_OF_SECTION);
00259     setError(CR_ERROR, buf);
00260     return false;
00261   }
00262   
00263   if(_type != m_node_type){
00264     const char *type_s, *alias_s, *type_s2, *alias_s2;
00265     alias_s= ndb_mgm_get_node_type_alias_string((enum ndb_mgm_node_type)m_node_type,
00266                                                 &type_s);
00267     alias_s2= ndb_mgm_get_node_type_alias_string((enum ndb_mgm_node_type)_type,
00268                                                  &type_s2);
00269     BaseString::snprintf(buf, 255, "This node type %s(%s) and config "
00270                          "node type %s(%s) don't match for nodeid %d", 
00271                          alias_s, type_s, alias_s2, type_s2, nodeid);
00272     setError(CR_ERROR, buf);
00273     return false;
00274   }
00275 
00279   ndb_mgm_configuration_iterator iter(* conf, CFG_SECTION_CONNECTION);
00280   for(iter.first(); iter.valid(); iter.next()){
00281 
00282     Uint32 type = CONNECTION_TYPE_TCP + 1;
00283     if(iter.get(CFG_TYPE_OF_SECTION, &type)) continue;
00284     if(type != CONNECTION_TYPE_TCP) continue;
00285     
00286     Uint32 nodeId1, nodeId2, remoteNodeId;
00287     if(iter.get(CFG_CONNECTION_NODE_1, &nodeId1)) continue;
00288     if(iter.get(CFG_CONNECTION_NODE_2, &nodeId2)) continue;
00289     
00290     if(nodeId1 != nodeid && nodeId2 != nodeid) continue;
00291     remoteNodeId = (nodeid == nodeId1 ? nodeId2 : nodeId1);
00292 
00293     const char * name;
00294     struct in_addr addr;
00295     BaseString tmp;
00296     if(!iter.get(CFG_CONNECTION_HOSTNAME_1, &name) && strlen(name)){
00297       if(Ndb_getInAddr(&addr, name) != 0){
00298         tmp.assfmt("Unable to lookup/illegal hostname %s, "
00299                    "connection from node %d to node %d",
00300                    name, nodeid, remoteNodeId);
00301         setError(CR_ERROR, tmp.c_str());
00302         return false;
00303       }
00304     }
00305 
00306     if(!iter.get(CFG_CONNECTION_HOSTNAME_2, &name) && strlen(name)){
00307       if(Ndb_getInAddr(&addr, name) != 0){
00308         tmp.assfmt("Unable to lookup/illegal hostname %s, "
00309                    "connection from node %d to node %d",
00310                    name, nodeid, remoteNodeId);
00311         setError(CR_ERROR, tmp.c_str());
00312         return false;
00313       }
00314     }
00315   }
00316   return true;
00317 }


Field Documentation

Uint32 ConfigRetriever::_ownNodeId [private]
 

Definition at line 93 of file ConfigRetriever.hpp.

Referenced by allocNodeId(), ConfigRetriever(), and getConfig().

BaseString ConfigRetriever::errorString [private]
 

Definition at line 83 of file ConfigRetriever.hpp.

Referenced by getErrorString(), and setError().

ErrorType ConfigRetriever::latestErrorType [private]
 

Definition at line 89 of file ConfigRetriever.hpp.

Referenced by hasError(), and setError().

NdbMgmHandle ConfigRetriever::m_handle [private]
 

Definition at line 101 of file ConfigRetriever.hpp.

Referenced by allocNodeId(), ConfigRetriever(), do_connect(), get_configuration_nodeid(), get_connectstring(), get_mgmd_host(), get_mgmd_port(), get_mgmHandle(), get_mgmHandlePtr(), getConfig(), setNodeId(), and ~ConfigRetriever().

Uint32 ConfigRetriever::m_node_type [private]
 

Definition at line 100 of file ConfigRetriever.hpp.

Referenced by allocNodeId(), ConfigRetriever(), and verifyConfig().

Uint32 ConfigRetriever::m_version [private]
 

Definition at line 99 of file ConfigRetriever.hpp.

Referenced by allocNodeId(), ConfigRetriever(), and getConfig().


The documentation for this class was generated from the following files:
Generated on Wed Jul 20 21:11:05 2005 for MySQL 5.0.9 Beta by  doxygen 1.4.3