/* Copyright (c) 2003-2007 MySQL AB This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ #include #include #include #include #include #include #include #include #include #define MGMERR(h) \ ndbout << "latest_error="< 0 && attempts > _timeout){ /** * Timeout has expired waiting for the nodes to enter * the state we want */ bool waitMore = false; /** * Make special check if we are waiting for * cluster to become started */ if(_status == NDB_MGM_NODE_STATUS_STARTED){ waitMore = true; /** * First check if any node is not starting * then it's no idea to wait anymore */ for (size_t n = 0; n < ndbNodes.size(); n++){ if (ndbNodes[n].node_status != NDB_MGM_NODE_STATUS_STARTED && ndbNodes[n].node_status != NDB_MGM_NODE_STATUS_STARTING) waitMore = false; } } if (!waitMore || resetAttempts > MAX_RESET_ATTEMPTS){ g_err << "waitNodeState(" << ndb_mgm_get_node_status_string(_status) <<", "<<_startphase<<")" << " timeout after " << attempts <<" attemps" << endl; return -1; } g_err << "waitNodeState(" << ndb_mgm_get_node_status_string(_status) <<", "<<_startphase<<")" << " resetting number of attempts " << resetAttempts << endl; attempts = 0; resetAttempts++; } allInState = true; if (getStatus() != 0){ g_err << "getStatus != 0" << endl; return -1; } // ndbout << "waitNodeState; _num_nodes = " << _num_nodes << endl; // for (int i = 0; i < _num_nodes; i++) // ndbout << " node["<node_id << " " << ndb_mgm_get_node_status_string(ndbNode->node_status); if (ndbNode->node_status == NDB_MGM_NODE_STATUS_STARTING) g_info<< ", start_phase=" << ndbNode->start_phase; g_info << endl; assert(ndbNode != NULL); if(_status == NDB_MGM_NODE_STATUS_STARTING && ((ndbNode->node_status == NDB_MGM_NODE_STATUS_STARTING && ndbNode->start_phase >= _startphase) || (ndbNode->node_status == NDB_MGM_NODE_STATUS_STARTED))) continue; if (_status == NDB_MGM_NODE_STATUS_STARTING){ g_info << "status = " << ndb_mgm_get_node_status_string(ndbNode->node_status) <<", start_phase="<start_phase<node_status != _status) { if (ndbNode->node_status < _status) allInState = false; else g_info << "node_status(" << ndbNode->node_status <<") != _status("<<_status<<")"<start_phase < _startphase) allInState = false; } else { if (ndbNode->node_status != _status) allInState = false; } } g_info << "Waiting for cluster enter state" << ndb_mgm_get_node_status_string(_status)<< endl; NdbSleep_SecSleep(1); attempts++; } return 0; } int NdbRestarter::waitNodesStarted(const int * _nodes, int _num_nodes, unsigned int _timeout){ return waitNodesState(_nodes, _num_nodes, NDB_MGM_NODE_STATUS_STARTED, _timeout); } int NdbRestarter::waitNodesStartPhase(const int * _nodes, int _num_nodes, int _startphase, unsigned int _timeout){ return waitNodesState(_nodes, _num_nodes, NDB_MGM_NODE_STATUS_STARTING, _timeout, _startphase); } int NdbRestarter::waitNodesNoStart(const int * _nodes, int _num_nodes, unsigned int _timeout){ return waitNodesState(_nodes, _num_nodes, NDB_MGM_NODE_STATUS_NOT_STARTED, _timeout); } bool NdbRestarter::isConnected(){ if (connected == true) return true; return connect() == 0; } int NdbRestarter::connect(){ disconnect(); handle = ndb_mgm_create_handle(); if (handle == NULL){ g_err << "handle == NULL" << endl; return -1; } g_info << "Connecting to mgmsrv at " << addr.c_str() << endl; if (ndb_mgm_set_connectstring(handle,addr.c_str())) { MGMERR(handle); g_err << "Connection to " << addr.c_str() << " failed" << endl; return -1; } if (ndb_mgm_connect(handle, 0, 0, 0) == -1) { MGMERR(handle); g_err << "Connection to " << addr.c_str() << " failed" << endl; return -1; } connected = true; return 0; } void NdbRestarter::disconnect(){ if (handle != NULL){ ndb_mgm_disconnect(handle); ndb_mgm_destroy_handle(&handle); } connected = false; } int NdbRestarter::getStatus(){ int retries = 0; struct ndb_mgm_cluster_state * status; struct ndb_mgm_node_state * node; ndbNodes.clear(); mgmNodes.clear(); apiNodes.clear(); if (!isConnected()) return -1; while(retries < 10){ status = ndb_mgm_get_status(handle); if (status == NULL){ ndbout << "status==NULL, retries="<no_of_nodes; i++){ node = &status->node_states[i]; switch(node->node_type){ case NDB_MGM_NODE_TYPE_NDB: ndbNodes.push_back(*node); break; case NDB_MGM_NODE_TYPE_MGM: mgmNodes.push_back(*node); break; case NDB_MGM_NODE_TYPE_API: apiNodes.push_back(*node); break; default: if(node->node_status == NDB_MGM_NODE_STATUS_UNKNOWN || node->node_status == NDB_MGM_NODE_STATUS_NO_CONTACT){ retries++; ndbNodes.clear(); mgmNodes.clear(); apiNodes.clear(); free(status); status = NULL; i = status->no_of_nodes; ndbout << "kalle"<< endl; break; } abort(); break; } } if(status == 0){ ndbout << "status == 0" << endl; continue; } free(status); return 0; } g_err << "getStatus failed" << endl; return -1; } int NdbRestarter::getNumDbNodes(){ if (!isConnected()) return -1; if (getStatus() != 0) return -1; return ndbNodes.size(); } int NdbRestarter::restartAll(bool initial, bool nostart, bool abort){ if (!isConnected()) return -1; if (ndb_mgm_restart2(handle, 0, NULL, initial, 1, abort) == -1) { MGMERR(handle); g_err << "Could not restart(stop) all nodes " << endl; // return -1; Continue anyway - Magnus } if (waitClusterNoStart(60) != 0){ g_err << "Cluster didnt enter STATUS_NOT_STARTED within 60s" << endl; return -1; } if(nostart){ g_debug << "restartAll: nostart == true" << endl; return 0; } if (ndb_mgm_start(handle, 0, NULL) == -1) { MGMERR(handle); g_err << "Could not restart(start) all nodes " << endl; return -1; } return 0; } int NdbRestarter::startAll(){ if (!isConnected()) return -1; if (ndb_mgm_start(handle, 0, NULL) == -1) { MGMERR(handle); g_err << "Could not start all nodes " << endl; return -1; } return 0; } int NdbRestarter::startNodes(const int * nodes, int num_nodes){ if (!isConnected()) return -1; if (ndb_mgm_start(handle, num_nodes, nodes) != num_nodes) { MGMERR(handle); g_err << "Could not start all nodes " << endl; return -1; } return 0; } int NdbRestarter::insertErrorInNode(int _nodeId, int _error){ if (!isConnected()) return -1; ndb_mgm_reply reply; reply.return_code = 0; if (ndb_mgm_insert_error(handle, _nodeId, _error, &reply) == -1){ MGMERR(handle); g_err << "Could not insert error in node with id = "<< _nodeId << endl; } if(reply.return_code != 0){ g_err << "Error: " << reply.message << endl; } return 0; } int NdbRestarter::insertErrorInAllNodes(int _error){ if (!isConnected()) return -1; if (getStatus() != 0) return -1; int result = 0; for(size_t i = 0; i < ndbNodes.size(); i++){ g_debug << "inserting error in node " << ndbNodes[i].node_id << endl; if (insertErrorInNode(ndbNodes[i].node_id, _error) == -1) result = -1; } return result; } int NdbRestarter::dumpStateOneNode(int _nodeId, const int * _args, int _num_args){ if (!isConnected()) return -1; ndb_mgm_reply reply; reply.return_code = 0; if (ndb_mgm_dump_state(handle, _nodeId, _args, _num_args, &reply) == -1){ MGMERR(handle); g_err << "Could not dump state in node with id = "<< _nodeId << endl; } if(reply.return_code != 0){ g_err << "Error: " << reply.message << endl; } return reply.return_code; } int NdbRestarter::dumpStateAllNodes(const int * _args, int _num_args){ if (!isConnected()) return -1; if (getStatus() != 0) return -1; int result = 0; for(size_t i = 0; i < ndbNodes.size(); i++){ g_debug << "dumping state in node " << ndbNodes[i].node_id << endl; if (dumpStateOneNode(ndbNodes[i].node_id, _args, _num_args) == -1) result = -1; } return result; } int NdbRestarter::enterSingleUserMode(int _nodeId){ if (!isConnected()) return -1; ndb_mgm_reply reply; reply.return_code = 0; if (ndb_mgm_enter_single_user(handle, _nodeId, &reply) == -1){ MGMERR(handle); g_err << "Could not enter single user mode api node = "<< _nodeId << endl; } if(reply.return_code != 0){ g_err << "Error: " << reply.message << endl; } return reply.return_code; } int NdbRestarter::exitSingleUserMode(){ if (!isConnected()) return -1; ndb_mgm_reply reply; reply.return_code = 0; if (ndb_mgm_exit_single_user(handle, &reply) == -1){ MGMERR(handle); g_err << "Could not exit single user mode " << endl; } if(reply.return_code != 0){ g_err << "Error: " << reply.message << endl; } return reply.return_code; } ndb_mgm_configuration* NdbRestarter::getConfig(){ if(m_config) return m_config; if (!isConnected()) return 0; m_config = ndb_mgm_get_configuration(handle, 0); return m_config; } template class Vector;