Commit 1add7651 authored by joreland@mysql.com's avatar joreland@mysql.com

bug#4529

parent ad325516
......@@ -19,6 +19,8 @@
#include <ndb_types.h>
#include <mgmapi.h>
#include <BaseString.hpp>
#include <LocalConfig.hpp>
/**
* @class ConfigRetriever
......@@ -26,15 +28,16 @@
*/
class ConfigRetriever {
public:
ConfigRetriever();
ConfigRetriever(const int id, const char* remoteHost, const int port);
ConfigRetriever(Uint32 version, Uint32 nodeType);
~ConfigRetriever();
/**
* Read local config
* @return Own node id, -1 means fail
*/
int init(bool onlyNodeId = false);
int init();
int do_connect();
/**
* Get configuration for current (nodeId given in local config file) node.
......@@ -47,7 +50,7 @@ public:
* @return ndb_mgm_configuration object if succeeded,
* NULL if erroneous local config file or configuration error.
*/
struct ndb_mgm_configuration * getConfig(int versionId, int nodeType);
struct ndb_mgm_configuration * getConfig();
const char * getErrorString();
......@@ -61,29 +64,22 @@ public:
*/
void setLocalConfigFileName(const char * connectString);
/**
* Sets connectstring which can be used instead of local config file
* environment variables and Ndb.cfg has precidence over this
*/
void setDefaultConnectString(const char * defaultConnectString);
/**
* @return Node id of this node (as stated in local config or connectString)
*/
inline Uint32 getOwnNodeId() { return _ownNodeId; }
Uint32 allocNodeId();
/**
* Get config using socket
*/
struct ndb_mgm_configuration * getConfig(const char * mgmhost, short port,
int versionId, int nodetype);
struct ndb_mgm_configuration * getConfig(NdbMgmHandle handle);
/**
* Get config from file
*/
struct ndb_mgm_configuration * getConfig(const char * file, int versionId);
struct ndb_mgm_configuration * getConfig(const char * file);
private:
char * errorString;
BaseString errorString;
enum ErrorType {
CR_ERROR = 0,
CR_RETRY = 1
......@@ -91,20 +87,21 @@ private:
ErrorType latestErrorType;
void setError(ErrorType, const char * errorMsg);
char * _localConfigFileName;
struct LocalConfig * _localConfig;
BaseString _localConfigFileName;
struct LocalConfig _localConfig;
int _ownNodeId;
char * m_connectString;
char * m_defaultConnectString;
BaseString m_connectString;
Uint32 m_version;
Uint32 m_node_type;
NdbMgmHandle m_handle;
/**
* Verify config
*/
bool verifyConfig(const struct ndb_mgm_configuration *, int type);
bool verifyConfig(const struct ndb_mgm_configuration *);
};
#endif
......
......@@ -32,49 +32,35 @@ enum MgmtSrvrId_Type {
struct MgmtSrvrId {
MgmtSrvrId_Type type;
union {
struct {
char * remoteHost;
unsigned int port;
} tcp;
struct {
char * filename;
} file;
} data;
BaseString name;
unsigned int port;
};
struct LocalConfig {
int _ownNodeId;
int size;
int items;
MgmtSrvrId ** ids;
Vector<MgmtSrvrId> ids;
int error_line;
char error_msg[256];
LocalConfig();
~LocalConfig();
bool init(bool onlyNodeId = false,
const char *connectString = 0,
const char *fileName = 0,
const char *defaultConnectString = 0);
void add(MgmtSrvrId *i);
bool init(const char *connectString = 0,
const char *fileName = 0);
void printError() const;
void printUsage() const;
void setError(int lineNumber, const char * _msg);
bool readConnectString(const char * connectString, bool onlyNodeId = false);
bool readFile(const char * filename, bool &fopenError, bool onlyNodeId = false);
bool readConnectString(const char *);
bool readFile(const char * file, bool &fopenError);
bool parseLine(char * line, int lineNumber);
bool parseNodeId(const char *buf);
bool parseHostName(const char *buf);
bool parseFileName(const char *buf);
bool parseString(const char *buf, bool onlyNodeId, char *line);
bool parseString(const char *buf, char *line);
};
#endif // LocalConfig_H
......
......@@ -43,33 +43,14 @@
//****************************************************************************
//****************************************************************************
ConfigRetriever::ConfigRetriever() {
ConfigRetriever::ConfigRetriever(Uint32 version, Uint32 node_type) {
_localConfigFileName = 0;
m_defaultConnectString = 0;
errorString = 0;
_localConfig = new LocalConfig();
m_connectString = 0;
m_handle= 0;
m_version = version;
m_node_type = node_type;
}
ConfigRetriever::~ConfigRetriever(){
if(_localConfigFileName != 0)
free(_localConfigFileName);
if(m_defaultConnectString != 0)
free(m_defaultConnectString);
if(m_connectString != 0)
free(m_connectString);
if(errorString != 0)
free(errorString);
delete _localConfig;
if (m_handle) {
ndb_mgm_disconnect(m_handle);
......@@ -82,68 +63,51 @@ ConfigRetriever::~ConfigRetriever(){
//****************************************************************************
int
ConfigRetriever::init(bool onlyNodeId) {
if (_localConfig->init(onlyNodeId, m_connectString, _localConfigFileName, m_defaultConnectString)) {
return _ownNodeId = (*_localConfig)._ownNodeId;
ConfigRetriever::init() {
if (!_localConfig.init(m_connectString.c_str(),
_localConfigFileName.c_str())){
setError(CR_ERROR, "error in retrieving contact info for mgmtsrvr");
_localConfig.printError();
_localConfig.printUsage();
return -1;
}
setError(CR_ERROR, "error in retrieving contact info for mgmtsrvr");
_localConfig->printError();
_localConfig->printUsage();
return -1;
}
//****************************************************************************
//****************************************************************************
//****************************************************************************
//****************************************************************************
struct ndb_mgm_configuration*
ConfigRetriever::getConfig(int verId, int nodeType) {
return _ownNodeId = _localConfig._ownNodeId;
}
int res = init();
if (res == -1) {
return 0;
}
int
ConfigRetriever::do_connect(){
if (_localConfig->items == 0){
setError(CR_ERROR,"No Management Servers configured in local config file");
return 0;
if(!m_handle)
m_handle= ndb_mgm_create_handle();
if (m_handle == 0) {
setError(CR_ERROR, "Unable to allocate mgm handle");
return -1;
}
int retry = 1;
int retry_max = 12; // Max number of retry attempts
int retry_interval= 5; // Seconds between each retry
do {
while(retry < retry_max){
Uint32 type = CR_ERROR;
for (int i = 0; i<_localConfig->items; i++){
MgmtSrvrId * m = _localConfig->ids[i];
struct ndb_mgm_configuration * p = 0;
BaseString tmp;
for (int i = 0; i<_localConfig.ids.size(); i++){
MgmtSrvrId * m = &_localConfig.ids[i];
switch(m->type){
case MgmId_TCP:
p = getConfig(m->data.tcp.remoteHost, m->data.tcp.port,
verId, nodeType);
break;
case MgmId_File:
p = getConfig(m->data.file.filename, verId);
break;
default:
setError(CR_ERROR, "Unknown error type");
break;
}
if (p != 0) {
if(!verifyConfig(p, nodeType)){
free(p);
tmp.assfmt("%s:%d", m->name.c_str(), m->port);
if (ndb_mgm_connect(m_handle, tmp.c_str()) == 0) {
return 0;
}
return p;
setError(CR_RETRY, ndb_mgm_get_latest_error_desc(m_handle));
case MgmId_File:
break;
}
if(latestErrorType == CR_RETRY)
type = CR_RETRY;
} // for
if(type == CR_RETRY){
}
if(latestErrorType == CR_RETRY){
REPORT_WARNING("Failed to retrieve cluster configuration");
ndbout << "(Cause of failure: " << getErrorString() << ")" << endl;
ndbout << "Attempt " << retry << " of " << retry_max << ". "
......@@ -154,82 +118,63 @@ ConfigRetriever::getConfig(int verId, int nodeType) {
break;
}
retry++;
} while (retry <= retry_max);
}
return 0;
ndb_mgm_destroy_handle(&m_handle);
m_handle= 0;
return -1;
}
ndb_mgm_configuration *
ConfigRetriever::getConfig(const char * mgmhost,
short port,
int versionId,
int nodetype){
if (m_handle) {
ndb_mgm_disconnect(m_handle);
ndb_mgm_destroy_handle(&m_handle);
}
//****************************************************************************
//****************************************************************************
//****************************************************************************
//****************************************************************************
struct ndb_mgm_configuration*
ConfigRetriever::getConfig() {
m_handle = ndb_mgm_create_handle();
struct ndb_mgm_configuration * p = 0;
if (m_handle == 0) {
setError(CR_ERROR, "Unable to allocate mgm handle");
return 0;
if(m_handle != 0){
p = getConfig(m_handle);
} else {
for (int i = 0; i<_localConfig.ids.size(); i++){
MgmtSrvrId * m = &_localConfig.ids[i];
switch(m->type){
case MgmId_File:
p = getConfig(m->name.c_str());
break;
case MgmId_TCP:
break;
}
if(p)
break;
}
}
BaseString tmp;
tmp.assfmt("%s:%d", mgmhost, port);
if (ndb_mgm_connect(m_handle, tmp.c_str()) != 0) {
setError(CR_RETRY, ndb_mgm_get_latest_error_desc(m_handle));
ndb_mgm_destroy_handle(&m_handle);
m_handle= 0;
if(p == 0)
return 0;
if(!verifyConfig(p)){
free(p);
p= 0;
}
return p;
}
ndb_mgm_configuration * conf = ndb_mgm_get_configuration(m_handle, versionId);
ndb_mgm_configuration *
ConfigRetriever::getConfig(NdbMgmHandle m_handle){
ndb_mgm_configuration * conf = ndb_mgm_get_configuration(m_handle,m_version);
if(conf == 0){
setError(CR_ERROR, ndb_mgm_get_latest_error_desc(m_handle));
ndb_mgm_disconnect(m_handle);
ndb_mgm_destroy_handle(&m_handle);
m_handle= 0;
return 0;
}
{
unsigned nodeid= getOwnNodeId();
int res= ndb_mgm_alloc_nodeid(m_handle, versionId, &nodeid, nodetype);
if(res != 0) {
setError(CR_ERROR, ndb_mgm_get_latest_error_desc(m_handle));
ndb_mgm_disconnect(m_handle);
ndb_mgm_destroy_handle(&m_handle);
m_handle= 0;
return 0;
}
_ownNodeId= nodeid;
}
return conf;
#if 0
bool compatible;
if (global_ndb_check)
compatible = ndbCompatible_ndb_mgmt(versionId, version);
else
compatible = ndbCompatible_api_mgmt(versionId, version);
if(!compatible){ // if(version != versionId){
NDB_CLOSE_SOCKET(sockfd);
snprintf(err_buf, sizeof(err_buf), "Management Server: Invalid version. "
"Version from server: %d Own version: %d", version, versionId);
setError(CR_ERROR, err_buf);
return 0;
}
#endif
}
ndb_mgm_configuration *
ConfigRetriever::getConfig(const char * filename, int versionId){
ConfigRetriever::getConfig(const char * filename){
struct stat sbuf;
const int res = stat(filename, &sbuf);
......@@ -272,60 +217,29 @@ ConfigRetriever::getConfig(const char * filename, int versionId){
void
ConfigRetriever::setError(ErrorType et, const char * s){
if(errorString != 0){
free(errorString);
}
if(s == 0)
errorString = 0;
else
errorString = strdup(s);
errorString.assign(s ? s : "");
latestErrorType = et;
}
const char *
ConfigRetriever::getErrorString(){
return errorString;
return errorString.c_str();
}
void
ConfigRetriever::setLocalConfigFileName(const char * localConfigFileName) {
if(_localConfigFileName != 0)
free(_localConfigFileName);
if(localConfigFileName != 0)
_localConfigFileName = strdup(localConfigFileName);
else
_localConfigFileName = 0;
_localConfigFileName.assign(localConfigFileName ? localConfigFileName : "");
}
void
ConfigRetriever::setConnectString(const char * connectString) {
if(m_connectString != 0)
free(m_connectString);
if (connectString != 0) {
m_connectString = strdup(connectString);
} else {
m_connectString = 0;
}
}
/**
* @note Do not use! Use the one above if possible. /elathal
*/
void
ConfigRetriever::setDefaultConnectString(const char * defaultConnectString) {
if(m_defaultConnectString != 0)
free(m_defaultConnectString);
if (defaultConnectString != 0) {
m_defaultConnectString = strdup(defaultConnectString);
} else {
m_defaultConnectString = 0;
}
m_connectString.assign(connectString ? connectString : "");
}
bool
ConfigRetriever::verifyConfig(const struct ndb_mgm_configuration * conf,
int type){
ConfigRetriever::verifyConfig(const struct ndb_mgm_configuration * conf){
char buf[255];
ndb_mgm_configuration_iterator * it;
it = ndb_mgm_create_configuration_iterator((struct ndb_mgm_configuration *)conf, CFG_SECTION_NODE);
......@@ -338,8 +252,8 @@ ConfigRetriever::verifyConfig(const struct ndb_mgm_configuration * conf,
}
NdbAutoPtr<ndb_mgm_configuration_iterator> ptr(it);
if(ndb_mgm_find(it, CFG_NODE_ID, getOwnNodeId()) != 0){
snprintf(buf, 255, "Unable to find node with id: %d", getOwnNodeId());
if(ndb_mgm_find(it, CFG_NODE_ID, _ownNodeId) != 0){
snprintf(buf, 255, "Unable to find node with id: %d", _ownNodeId);
setError(CR_ERROR, buf);
return false;
}
......@@ -396,11 +310,27 @@ ConfigRetriever::verifyConfig(const struct ndb_mgm_configuration * conf,
return false;
}
if(_type != type){
if(_type != m_node_type){
snprintf(buf, 255, "Supplied node type(%d) and config node type(%d) "
" don't match", type, _type);
" don't match", m_node_type, _type);
setError(CR_ERROR, buf);
return false;
}
return true;
}
Uint32
ConfigRetriever::allocNodeId(){
unsigned nodeid= _ownNodeId;
if(m_handle != 0){
int res= ndb_mgm_alloc_nodeid(m_handle, m_version, &nodeid, m_node_type);
if(res != 0) {
setError(CR_ERROR, ndb_mgm_get_latest_error_desc(m_handle));
return 0;
}
}
return _ownNodeId= nodeid;
}
......@@ -20,16 +20,13 @@
#include <NdbAutoPtr.hpp>
LocalConfig::LocalConfig(){
ids = 0; size = 0; items = 0;
error_line = 0; error_msg[0] = 0;
_ownNodeId= 0;
}
bool
LocalConfig::init(bool onlyNodeId,
const char *connectString,
const char *fileName,
const char *defaultConnectString) {
LocalConfig::init(const char *connectString,
const char *fileName) {
/**
* Escalation:
* 1. Check connectString
......@@ -41,8 +38,8 @@ LocalConfig::init(bool onlyNodeId,
*/
//1. Check connectString
if(connectString != 0) {
if(readConnectString(connectString, onlyNodeId)){
if(connectString != 0 && connectString[0] != 0){
if(readConnectString(connectString)){
return true;
}
return false;
......@@ -51,7 +48,7 @@ LocalConfig::init(bool onlyNodeId,
//2. Check given filename
if (fileName && strlen(fileName) > 0) {
bool fopenError;
if(readFile(fileName, fopenError, onlyNodeId)){
if(readFile(fileName, fopenError)){
return true;
}
return false;
......@@ -61,7 +58,7 @@ LocalConfig::init(bool onlyNodeId,
char buf[255];
if(NdbEnv_GetEnv("NDB_CONNECTSTRING", buf, sizeof(buf)) &&
strlen(buf) != 0){
if(readConnectString(buf, onlyNodeId)){
if(readConnectString(buf)){
return true;
}
return false;
......@@ -72,7 +69,7 @@ LocalConfig::init(bool onlyNodeId,
bool fopenError;
char *buf= NdbConfig_NdbCfgName(1 /*true*/);
NdbAutoPtr<char> tmp_aptr(buf);
if(readFile(buf, fopenError, onlyNodeId))
if(readFile(buf, fopenError))
return true;
if (!fopenError)
return false;
......@@ -83,24 +80,17 @@ LocalConfig::init(bool onlyNodeId,
bool fopenError;
char *buf= NdbConfig_NdbCfgName(0 /*false*/);
NdbAutoPtr<char> tmp_aptr(buf);
if(readFile(buf, fopenError, onlyNodeId))
if(readFile(buf, fopenError))
return true;
if (!fopenError)
return false;
}
//6. Check defaultConnectString
if(defaultConnectString != 0) {
if(readConnectString(defaultConnectString, onlyNodeId))
return true;
return false;
}
//7. Check
{
char buf[256];
snprintf(buf, sizeof(buf), "host=localhost:%u", NDB_BASE_PORT);
if(readConnectString(buf, onlyNodeId))
if(readConnectString(buf))
return true;
}
......@@ -110,30 +100,8 @@ LocalConfig::init(bool onlyNodeId,
}
LocalConfig::~LocalConfig(){
for(int i = 0; i<items; i++){
if(ids[i]->type == MgmId_TCP)
free(ids[i]->data.tcp.remoteHost);
else if(ids[i]->type == MgmId_File)
free(ids[i]->data.file.filename);
delete ids[i];
}
if(ids != 0)
delete[] ids;
}
void LocalConfig::add(MgmtSrvrId * i){
if(items == size){
MgmtSrvrId ** tmp = new MgmtSrvrId * [size+10];
if(ids != 0){
memcpy(tmp, ids, items*sizeof(MgmtSrvrId *));
delete []ids;
}
ids = tmp;
}
ids[items] = i;
items++;
}
void LocalConfig::setError(int lineNumber, const char * _msg) {
error_line = lineNumber;
strncpy(error_msg, _msg, sizeof(error_msg));
......@@ -162,13 +130,13 @@ void LocalConfig::printUsage() const {
<<endl<<endl;
}
char *nodeIdTokens[] = {
const char *nodeIdTokens[] = {
"OwnProcessId %i",
"nodeid=%i",
0
};
char *hostNameTokens[] = {
const char *hostNameTokens[] = {
"host://%[^:]:%i",
"host=%[^:]:%i",
"%[^:]:%i",
......@@ -176,7 +144,7 @@ char *hostNameTokens[] = {
0
};
char *fileNameTokens[] = {
const char *fileNameTokens[] = {
"file://%s",
"file=%s",
0
......@@ -196,11 +164,11 @@ LocalConfig::parseHostName(const char * buf){
int port;
for(int i = 0; hostNameTokens[i] != 0; i++) {
if (sscanf(buf, hostNameTokens[i], tempString, &port) == 2) {
MgmtSrvrId* mgmtSrvrId = new MgmtSrvrId();
mgmtSrvrId->type = MgmId_TCP;
mgmtSrvrId->data.tcp.remoteHost = strdup(tempString);
mgmtSrvrId->data.tcp.port = port;
add(mgmtSrvrId);
MgmtSrvrId mgmtSrvrId;
mgmtSrvrId.type = MgmId_TCP;
mgmtSrvrId.name.assign(tempString);
mgmtSrvrId.port = port;
ids.push_back(mgmtSrvrId);
return true;
}
}
......@@ -212,10 +180,10 @@ LocalConfig::parseFileName(const char * buf){
char tempString[1024];
for(int i = 0; fileNameTokens[i] != 0; i++) {
if (sscanf(buf, fileNameTokens[i], tempString) == 1) {
MgmtSrvrId* mgmtSrvrId = new MgmtSrvrId();
mgmtSrvrId->type = MgmId_File;
mgmtSrvrId->data.file.filename = strdup(tempString);
add(mgmtSrvrId);
MgmtSrvrId mgmtSrvrId;
mgmtSrvrId.type = MgmId_File;
mgmtSrvrId.name.assign(tempString);
ids.push_back(mgmtSrvrId);
return true;
}
}
......@@ -223,7 +191,7 @@ LocalConfig::parseFileName(const char * buf){
}
bool
LocalConfig::parseString(const char * connectString, bool onlyNodeId, char *line){
LocalConfig::parseString(const char * connectString, char *line){
char * for_strtok;
char * copy = strdup(connectString);
NdbAutoPtr<char> tmp_aptr(copy);
......@@ -231,8 +199,7 @@ LocalConfig::parseString(const char * connectString, bool onlyNodeId, char *line
bool b_nodeId = false;
bool found_other = false;
for (char *tok = strtok_r(copy,";",&for_strtok);
tok != 0 && !(onlyNodeId && b_nodeId);
for (char *tok = strtok_r(copy,";",&for_strtok); tok != 0;
tok = strtok_r(NULL, ";", &for_strtok)) {
if (tok[0] == '#') continue;
......@@ -240,8 +207,6 @@ LocalConfig::parseString(const char * connectString, bool onlyNodeId, char *line
if (!b_nodeId) // only one nodeid definition allowed
if (b_nodeId = parseNodeId(tok))
continue;
if (onlyNodeId)
continue;
if (found_other = parseHostName(tok))
continue;
if (found_other = parseFileName(tok))
......@@ -252,16 +217,17 @@ LocalConfig::parseString(const char * connectString, bool onlyNodeId, char *line
return false;
}
if (!onlyNodeId && !found_other) {
if (!found_other) {
if (line)
snprintf(line, 150, "Missing host/file name extry in \"%s\"", connectString);
snprintf(line, 150, "Missing host/file name extry in \"%s\"",
connectString);
return false;
}
return true;
}
bool LocalConfig::readFile(const char * filename, bool &fopenError, bool onlyNodeId)
bool LocalConfig::readFile(const char * filename, bool &fopenError)
{
char line[150], line2[150];
......@@ -292,7 +258,7 @@ bool LocalConfig::readFile(const char * filename, bool &fopenError, bool onlyNod
strcat(theString, line);
}
bool return_value = parseString(theString, onlyNodeId, line);
bool return_value = parseString(theString, line);
if (!return_value) {
snprintf(line2, 150, "Reading %s: %s", filename, line);
......@@ -305,9 +271,9 @@ bool LocalConfig::readFile(const char * filename, bool &fopenError, bool onlyNod
}
bool
LocalConfig::readConnectString(const char * connectString, bool onlyNodeId){
LocalConfig::readConnectString(const char * connectString){
char line[150], line2[150];
bool return_value = parseString(connectString, onlyNodeId, line);
bool return_value = parseString(connectString, line);
if (!return_value) {
snprintf(line2, 150, "Reading NDB_CONNECTSTRING \"%s\": %s", connectString, line);
setError(0,line2);
......
......@@ -69,9 +69,10 @@ NDB_MAIN(ndb_kernel){
}
{ // Do configuration
theConfig->setupConfiguration();
signal(SIGPIPE, SIG_IGN);
theConfig->fetch_configuration();
}
if (theConfig->getDaemonMode()) {
// Become a daemon
char *lockfile= NdbConfig_PidFileName(globalData.ownId);
......@@ -88,8 +89,6 @@ NDB_MAIN(ndb_kernel){
/**
* Parent
*/
theConfig->closeConfiguration();
catchsigs(true);
int status = 0;
......@@ -132,11 +131,13 @@ NDB_MAIN(ndb_kernel){
exit(0);
}
g_eventLogger.info("Ndb has terminated (pid %d) restarting", child);
theConfig->fetch_configuration();
}
g_eventLogger.info("Angel pid: %d ndb pid: %d", getppid(), getpid());
theConfig->setupConfiguration();
systemInfo(* theConfig, * theConfig->m_logLevel);
// Load blocks
globalEmulatorData.theSimBlockList->load(* theConfig);
......
......@@ -35,6 +35,7 @@
#include <ndb_limits.h>
#include "pc.hpp"
#include <LogLevel.hpp>
#include <NdbSleep.h>
extern "C" {
void ndbSetOwnVersion();
......@@ -153,39 +154,82 @@ Configuration::closeConfiguration(){
}
void
Configuration::setupConfiguration(){
Configuration::fetch_configuration(){
/**
* Fetch configuration from management server
*/
if (m_config_retriever) {
delete m_config_retriever;
}
m_config_retriever= new ConfigRetriever();
ConfigRetriever &cr= *m_config_retriever;
cr.setConnectString(_connectString);
stopOnError(true);
ndb_mgm_configuration * p = cr.getConfig(NDB_VERSION, NODE_TYPE_DB);
m_config_retriever= new ConfigRetriever(NDB_VERSION, NODE_TYPE_DB);
m_config_retriever->setConnectString(_connectString ? _connectString : "");
if(m_config_retriever->init() == -1 ||
m_config_retriever->do_connect() == -1){
const char * s = m_config_retriever->getErrorString();
if(s == 0)
s = "No error given!";
/* Set stop on error to true otherwise NDB will
go into an restart loop...
*/
ERROR_SET(fatal, ERR_INVALID_CONFIG, "Could connect to ndb_mgmd", s);
}
ConfigRetriever &cr= *m_config_retriever;
if((globalData.ownId = cr.allocNodeId()) == 0){
for(Uint32 i = 0; i<3; i++){
NdbSleep_SecSleep(3);
if(globalData.ownId = cr.allocNodeId())
break;
}
}
if(globalData.ownId == 0){
ERROR_SET(fatal, ERR_INVALID_CONFIG,
"Unable to alloc node id", m_config_retriever->getErrorString());
}
ndb_mgm_configuration * p = cr.getConfig();
if(p == 0){
const char * s = cr.getErrorString();
if(s == 0)
s = "No error given!";
/* Set stop on error to true otherwise NDB will
go into an restart loop...
*/
*/
ERROR_SET(fatal, ERR_INVALID_CONFIG, "Could not fetch configuration"
"/invalid configuration", s);
}
if(m_clusterConfig)
free(m_clusterConfig);
m_clusterConfig = p;
ndb_mgm_configuration_iterator iter(* p, CFG_SECTION_NODE);
if (iter.find(CFG_NODE_ID, globalData.ownId)){
ERROR_SET(fatal, ERR_INVALID_CONFIG, "Invalid configuration fetched", "DB missing");
}
if(iter.get(CFG_DB_STOP_ON_ERROR, &_stopOnError)){
ERROR_SET(fatal, ERR_INVALID_CONFIG, "Invalid configuration fetched",
"StopOnError missing");
}
}
Uint32 nodeId = globalData.ownId = cr.getOwnNodeId();
void
Configuration::setupConfiguration(){
ndb_mgm_configuration * p = m_clusterConfig;
/**
* Configure transporters
*/
{
int res = IPCConfig::configureTransporters(nodeId,
int res = IPCConfig::configureTransporters(globalData.ownId,
* p,
globalTransporterRegistry);
if(res <= 0){
......@@ -247,11 +291,6 @@ Configuration::setupConfiguration(){
}
}
if(iter.get(CFG_DB_STOP_ON_ERROR, &_stopOnError)){
ERROR_SET(fatal, ERR_INVALID_CONFIG, "Invalid configuration fetched",
"StopOnError missing");
}
if(iter.get(CFG_DB_STOP_ON_ERROR_INSERT, &m_restartOnErrorInsert)){
ERROR_SET(fatal, ERR_INVALID_CONFIG, "Invalid configuration fetched",
"RestartOnErrorInsert missing");
......@@ -268,7 +307,6 @@ Configuration::setupConfiguration(){
ConfigValues* cf = ConfigValuesFactory::extractCurrentSection(iter.m_config);
m_clusterConfig = p;
m_clusterConfigIter = ndb_mgm_create_configuration_iterator
(p, CFG_SECTION_NODE);
......
......@@ -32,6 +32,7 @@ public:
*/
bool init(int argc, const char** argv);
void fetch_configuration();
void setupConfiguration();
void closeConfiguration();
......
......@@ -1438,11 +1438,7 @@ ndb_mgm_get_configuration(NdbMgmHandle handle, unsigned int version) {
const Properties *prop;
prop = ndb_mgm_call(handle, reply, "get config", &args);
if(prop == NULL) {
SET_ERROR(handle, EIO, "Unable to fetch config");
return 0;
}
CHECK_REPLY(prop, 0);
do {
const char * buf;
......@@ -1537,17 +1533,14 @@ ndb_mgm_alloc_nodeid(NdbMgmHandle handle, unsigned int version, unsigned *pnodei
const Properties *prop;
prop= ndb_mgm_call(handle, reply, "get nodeid", &args);
if(prop == NULL) {
SET_ERROR(handle, EIO, "Unable to alloc nodeid");
return -1;
}
CHECK_REPLY(prop, -1);
int res= -1;
do {
const char * buf;
if(!prop->get("result", &buf) || strcmp(buf, "Ok") != 0){
ndbout_c("ERROR Message: %s\n", buf);
setError(handle, NDB_MGM_COULD_NOT_CONNECT_TO_SOCKET, __LINE__,
"Could not alloc node id: %s",buf);
break;
}
if(!prop->get("nodeid", pnodeid) != 0){
......@@ -1621,11 +1614,7 @@ ndb_mgm_set_int_parameter(NdbMgmHandle handle,
const Properties *prop;
prop= ndb_mgm_call(handle, reply, "set parameter", &args);
if(prop == NULL) {
SET_ERROR(handle, EIO, "Unable set parameter");
return -1;
}
CHECK_REPLY(prop, -1);
int res= -1;
do {
......
......@@ -47,10 +47,6 @@ handler(int sig){
int main(int argc, const char** argv){
int optind = 0;
char _default_connectstring_buf[256];
snprintf(_default_connectstring_buf, sizeof(_default_connectstring_buf),
"host=localhost:%u", NDB_BASE_PORT);
const char *_default_connectstring= _default_connectstring_buf;
const char *_host = 0;
int _port = 0;
int _help = 0;
......@@ -79,9 +75,9 @@ int main(int argc, const char** argv){
_port = atoi(argv[1]);
}
} else {
if(cfg.init(false, 0, 0, _default_connectstring) && cfg.items > 0 && cfg.ids[0]->type == MgmId_TCP){
_host = cfg.ids[0]->data.tcp.remoteHost;
_port = cfg.ids[0]->data.tcp.port;
if(cfg.init(0, 0) && cfg.ids.size() > 0 && cfg.ids[0].type == MgmId_TCP){
_host = cfg.ids[0].name.c_str();
_port = cfg.ids[0].port;
} else {
cfg.printError();
cfg.printUsage();
......
......@@ -584,18 +584,11 @@ MgmtSrvr::MgmtSrvr(NodeId nodeId,
_ownNodeId= 0;
NodeId tmp= nodeId;
if (getFreeNodeId(&tmp, NDB_MGM_NODE_TYPE_MGM, 0, 0)){
_ownNodeId= tmp;
if (nodeId != 0 && nodeId != tmp) {
ndbout << "Unable to obtain requested nodeid " << nodeId
<< " nodeid " << tmp << " available\n";
_ownNodeId= 0; // did not get nodeid requested
}
m_allocated_resources.reserve_node(_ownNodeId);
} else {
ndbout_c("Unable to retrieve own node id");
if (!alloc_node_id(&tmp, NDB_MGM_NODE_TYPE_MGM, 0, 0)){
ndbout << "Unable to obtain requested nodeid " << nodeId;
exit(-1);
}
_ownNodeId = tmp;
}
......@@ -2301,10 +2294,19 @@ MgmtSrvr::getNodeType(NodeId nodeId) const
return nodeTypes[nodeId];
}
#ifdef NDB_WIN32
static NdbMutex & f_node_id_mutex = * NdbMutex_Create();
#else
static NdbMutex f_node_id_mutex = NDB_MUTEX_INITIALIZER;
#endif
bool
MgmtSrvr::getFreeNodeId(NodeId * nodeId, enum ndb_mgm_node_type type,
struct sockaddr *client_addr, socklen_t *client_addr_len) const
MgmtSrvr::alloc_node_id(NodeId * nodeId,
enum ndb_mgm_node_type type,
struct sockaddr *client_addr,
socklen_t *client_addr_len)
{
Guard g(&f_node_id_mutex);
#if 0
ndbout << "MgmtSrvr::getFreeNodeId type=" << type
<< " *nodeid=" << *nodeId << endl;
......@@ -2365,6 +2367,7 @@ MgmtSrvr::getFreeNodeId(NodeId * nodeId, enum ndb_mgm_node_type type,
}
}
*nodeId= tmp;
m_reserved_nodes.set(tmp);
#if 0
ndbout << "MgmtSrvr::getFreeNodeId found type=" << type
<< " *nodeid=" << *nodeId << endl;
......@@ -2769,6 +2772,7 @@ MgmtSrvr::Allocated_resources::Allocated_resources(MgmtSrvr &m)
MgmtSrvr::Allocated_resources::~Allocated_resources()
{
Guard g(&f_node_id_mutex);
m_mgmsrv.m_reserved_nodes.bitANDC(m_reserved_nodes);
}
......@@ -2776,7 +2780,6 @@ void
MgmtSrvr::Allocated_resources::reserve_node(NodeId id)
{
m_reserved_nodes.set(id);
m_mgmsrv.m_reserved_nodes.set(id);
}
int
......
......@@ -78,6 +78,7 @@ public:
// methods to reserve/allocate resources which
// will be freed when running destructor
void reserve_node(NodeId id);
bool is_reserved(NodeId nodeId) { return m_reserved_nodes.get(nodeId);}
private:
MgmtSrvr &m_mgmsrv;
NodeBitmask m_reserved_nodes;
......@@ -465,8 +466,8 @@ public:
* @return false if none found
*/
bool getNextNodeId(NodeId * _nodeId, enum ndb_mgm_node_type type) const ;
bool getFreeNodeId(NodeId * _nodeId, enum ndb_mgm_node_type type,
struct sockaddr *client_addr, socklen_t *client_addr_len) const ;
bool alloc_node_id(NodeId * _nodeId, enum ndb_mgm_node_type type,
struct sockaddr *client_addr, socklen_t *client_addr_len);
/**
*
......
......@@ -288,16 +288,15 @@ MgmtSrvr::readConfig() {
Config *
MgmtSrvr::fetchConfig() {
ConfigRetriever cr;
ConfigRetriever cr(NDB_VERSION, NODE_TYPE_MGM);
cr.setLocalConfigFileName(m_localNdbConfigFilename.c_str());
struct ndb_mgm_configuration * tmp = cr.getConfig(NDB_VERSION,
NODE_TYPE_MGM);
struct ndb_mgm_configuration * tmp = cr.getConfig();
if(tmp != 0){
Config * conf = new Config();
conf->m_configValues = tmp;
return conf;
}
return 0;
}
......
......@@ -401,34 +401,26 @@ MgmApiSession::get_nodeid(Parser_t::Context &,
struct sockaddr addr;
socklen_t addrlen= sizeof(addr);
int r;
if (r= getpeername(m_socket, &addr, &addrlen)) {
int r = getpeername(m_socket, &addr, &addrlen);
if (r != 0 ) {
m_output->println(cmd);
m_output->println("result: getpeername(%d) failed, err= %d", m_socket, r);
m_output->println("");
return;
}
NodeId free_id= 0;
NodeId tmp= nodeid;
if (m_mgmsrv.getFreeNodeId(&tmp, (enum ndb_mgm_node_type)nodetype, &addr, &addrlen))
free_id= tmp;
if (nodeid != 0 && free_id != nodeid){
m_output->println(cmd);
m_output->println("result: no free nodeid %d for nodetype %d",
nodeid, nodetype);
m_output->println("");
return;
}
if(tmp == 0 || !m_allocated_resources->is_reserved(tmp)){
if (!m_mgmsrv.alloc_node_id(&tmp, (enum ndb_mgm_node_type)nodetype,
&addr, &addrlen)){
m_output->println(cmd);
m_output->println("result: no free nodeid %d for nodetype %d",
nodeid, nodetype);
m_output->println("");
return;
}
}
if (free_id == 0){
m_output->println(cmd);
m_output->println("result: no free nodeid for nodetype %d", nodetype);
m_output->println("");
return;
}
#if 0
if (!compatible){
m_output->println(cmd);
......@@ -438,14 +430,13 @@ MgmApiSession::get_nodeid(Parser_t::Context &,
return;
}
#endif
m_output->println(cmd);
m_output->println("nodeid: %u", free_id);
m_output->println("nodeid: %u", tmp);
m_output->println("result: Ok");
m_output->println("");
m_allocated_resources->reserve_node(free_id);
m_allocated_resources->reserve_node(tmp);
return;
}
......
......@@ -16,8 +16,6 @@
#include <ndb_global.h>
#include <signal.h>
#include "MgmtSrvr.hpp"
#include "EventLogger.hpp"
#include <Config.hpp>
......@@ -229,6 +227,7 @@ NDB_MAIN(mgmsrv){
}
}
signal(SIGPIPE, SIG_IGN);
if(!glob.mgmObject->start()){
ndbout_c("Unable to start management server.");
ndbout_c("Probably caused by illegal initial configuration file.");
......@@ -312,14 +311,13 @@ MgmGlobals::~MgmGlobals(){
static bool
readLocalConfig(){
// Read local config file
ConfigRetriever cr;
cr.setLocalConfigFileName(glob.local_config_filename);
int nodeid = cr.init(true);
LocalConfig lc;
int nodeid = lc.init(glob.local_config_filename);
if(nodeid == -1){
return false;
}
glob.localNodeId = (NodeId)nodeid;
glob.localNodeId = nodeid;
return true;
}
......@@ -342,18 +340,7 @@ readGlobalConfig() {
InitConfigFileParser parser;
glob.cluster_config = parser.parseConfig(glob.config_filename);
if(glob.cluster_config == 0){
/**
* Try to get configuration from other MGM server
* Note: Only new format
*/
glob.cluster_config = new Config();
ConfigRetriever cr;
cr.setLocalConfigFileName(glob.local_config_filename);
glob.cluster_config->m_configValues = cr.getConfig(NDB_VERSION,
NODE_TYPE_MGM);
if (glob.cluster_config->m_configValues == NULL)
return false;
return false;
}
return true;
}
......@@ -343,27 +343,39 @@ TransporterFacade*
TransporterFacade::start_instance(const char * connectString){
// TransporterFacade used from API get config from mgmt srvr
s_config_retriever= new ConfigRetriever;
ConfigRetriever &configRetriever= *s_config_retriever;
configRetriever.setConnectString(connectString);
ndb_mgm_configuration * props = configRetriever.getConfig(NDB_VERSION,
NODE_TYPE_API);
if (props == 0) {
ndbout << "Configuration error: ";
const char* erString = configRetriever.getErrorString();
if (erString == 0) {
erString = "No error specified!";
}
ndbout << erString << endl;
return 0;
}
const int nodeId = configRetriever.getOwnNodeId();
TransporterFacade * tf = start_instance(nodeId, props);
s_config_retriever= new ConfigRetriever(NDB_VERSION, NODE_TYPE_API);
s_config_retriever->setConnectString(connectString);
const char* error = 0;
do {
if(s_config_retriever->init() == -1)
break;
if(s_config_retriever->do_connect() == -1)
break;
const Uint32 nodeId = s_config_retriever->allocNodeId();
if(nodeId == 0)
break;
ndb_mgm_configuration * props = s_config_retriever->getConfig();
if(props == 0)
break;
TransporterFacade * tf = start_instance(nodeId, props);
free(props);
return tf;
} while(0);
free(props);
return tf;
ndbout << "Configuration error: ";
const char* erString = s_config_retriever->getErrorString();
if (erString == 0) {
erString = "No error specified!";
}
ndbout << erString << endl;
return 0;
}
TransporterFacade*
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment