mgmapi.cpp

来自「MySQL源码文件5.X系列, 可自已编译到服务器」· C++ 代码 · 共 2,561 行 · 第 1/5 页

CPP
2,561
字号
    else      *disconnect= 0;    BaseString result;    reply->get("result", result);    if(strcmp(result.c_str(), "Ok") != 0) {      SET_ERROR(handle, NDB_MGM_STOP_FAILED, result.c_str());      delete reply;      return -1;    }    delete reply;    return stoppedNoOfNodes;  }  /**   * A list of database nodes should be stopped   */  Properties args;  BaseString node_list_str;  node_list_str.assfmt("%d", node_list[0]);  for(int node = 1; node < no_of_nodes; node++)    node_list_str.appfmt(" %d", node_list[node]);    args.put("node", node_list_str.c_str());  args.put("abort", abort);  const Properties *reply;  if(use_v2)    reply = ndb_mgm_call(handle, stop_reply_v2, "stop v2", &args);  else    reply = ndb_mgm_call(handle, stop_reply_v1, "stop", &args);  CHECK_REPLY(reply, stoppedNoOfNodes);  if(!reply->get("stopped", &stoppedNoOfNodes)){    SET_ERROR(handle, NDB_MGM_STOP_FAILED, 	      "Could not get number of stopped nodes from mgm server");    delete reply;    return -1;  }  if(use_v2)    reply->get("disconnect", (Uint32*)disconnect);  else    *disconnect= 0;  BaseString result;  reply->get("result", result);  if(strcmp(result.c_str(), "Ok") != 0) {    SET_ERROR(handle, NDB_MGM_STOP_FAILED, result.c_str());    delete reply;    return -1;  }  delete reply;  return stoppedNoOfNodes;}extern "C"intndb_mgm_restart(NdbMgmHandle handle, int no_of_nodes, const int *node_list) {  SET_ERROR(handle, NDB_MGM_NO_ERROR, "Executing: ndb_mgm_restart");  return ndb_mgm_restart2(handle, no_of_nodes, node_list, 0, 0, 0);}extern "C"intndb_mgm_restart2(NdbMgmHandle handle, int no_of_nodes, const int * node_list,		 int initial, int nostart, int abort){  int disconnect;  return ndb_mgm_restart3(handle, no_of_nodes, node_list, initial, nostart,                          abort, &disconnect);}extern "C"intndb_mgm_restart3(NdbMgmHandle handle, int no_of_nodes, const int * node_list,		 int initial, int nostart, int abort, int *disconnect){  SET_ERROR(handle, NDB_MGM_NO_ERROR, "Executing: ndb_mgm_restart3");  Uint32 restarted = 0;  const ParserRow<ParserDummy> restart_reply_v1[] = {    MGM_CMD("restart reply", NULL, ""),    MGM_ARG("result", String, Mandatory, "Error message"),    MGM_ARG("restarted", Int, Optional, "No of restarted nodes"),    MGM_END()  };  const ParserRow<ParserDummy> restart_reply_v2[] = {    MGM_CMD("restart reply", NULL, ""),    MGM_ARG("result", String, Mandatory, "Error message"),    MGM_ARG("restarted", Int, Optional, "No of restarted nodes"),    MGM_ARG("disconnect", Int, Optional, "Disconnect to apply"),    MGM_END()  };  CHECK_HANDLE(handle, -1);  CHECK_CONNECTED(handle, -1);  if(handle->mgmd_version_build==-1)  {    char verstr[50];    if(!ndb_mgm_get_version(handle,                        &(handle->mgmd_version_major),                        &(handle->mgmd_version_minor),                        &(handle->mgmd_version_build),                        sizeof(verstr),                            verstr))    {      return -1;    }  }  int use_v2= ((handle->mgmd_version_major==5)    && (        (handle->mgmd_version_minor==0 && handle->mgmd_version_build>=21)        ||(handle->mgmd_version_minor==1 && handle->mgmd_version_build>=12)        ||(handle->mgmd_version_minor>1)        )               )    || (handle->mgmd_version_major>5);  if(no_of_nodes < 0){    SET_ERROR(handle, NDB_MGM_RESTART_FAILED, 	      "Restart requested of negative number of nodes");    return -1;  }    if(no_of_nodes == 0) {    Properties args;        args.put("abort", abort);    args.put("initialstart", initial);    args.put("nostart", nostart);    const Properties *reply;    const int timeout = handle->read_timeout;    handle->read_timeout= 5*60*1000; // 5 minutes    reply = ndb_mgm_call(handle, restart_reply_v1, "restart all", &args);    handle->read_timeout= timeout;    CHECK_REPLY(reply, -1);    BaseString result;    reply->get("result", result);    if(strcmp(result.c_str(), "Ok") != 0) {      SET_ERROR(handle, NDB_MGM_RESTART_FAILED, result.c_str());      delete reply;      return -1;    }    if(!reply->get("restarted", &restarted)){      SET_ERROR(handle, NDB_MGM_RESTART_FAILED, 		"Could not get restarted number of nodes from mgm server");      delete reply;      return -1;    }    delete reply;    return restarted;  }        BaseString node_list_str;  node_list_str.assfmt("%d", node_list[0]);  for(int node = 1; node < no_of_nodes; node++)    node_list_str.appfmt(" %d", node_list[node]);  Properties args;    args.put("node", node_list_str.c_str());  args.put("abort", abort);  args.put("initialstart", initial);  args.put("nostart", nostart);  const Properties *reply;  const int timeout = handle->read_timeout;  handle->read_timeout= 5*60*1000; // 5 minutes  if(use_v2)    reply = ndb_mgm_call(handle, restart_reply_v2, "restart node v2", &args);  else    reply = ndb_mgm_call(handle, restart_reply_v1, "restart node", &args);  handle->read_timeout= timeout;  if(reply != NULL) {    BaseString result;    reply->get("result", result);    if(strcmp(result.c_str(), "Ok") != 0) {      SET_ERROR(handle, NDB_MGM_RESTART_FAILED, result.c_str());      delete reply;      return -1;    }    reply->get("restarted", &restarted);    if(use_v2)      reply->get("disconnect", (Uint32*)disconnect);    else      *disconnect= 0;    delete reply;  }     return restarted;}static const char *clusterlog_severity_names[]=  { "enabled", "debug", "info", "warning", "error", "critical", "alert" };struct ndb_mgm_event_severities {  const char* name;  enum ndb_mgm_event_severity severity;} clusterlog_severities[] = {  { clusterlog_severity_names[0], NDB_MGM_EVENT_SEVERITY_ON },  { clusterlog_severity_names[1], NDB_MGM_EVENT_SEVERITY_DEBUG },  { clusterlog_severity_names[2], NDB_MGM_EVENT_SEVERITY_INFO },  { clusterlog_severity_names[3], NDB_MGM_EVENT_SEVERITY_WARNING },  { clusterlog_severity_names[4], NDB_MGM_EVENT_SEVERITY_ERROR },  { clusterlog_severity_names[5], NDB_MGM_EVENT_SEVERITY_CRITICAL },  { clusterlog_severity_names[6], NDB_MGM_EVENT_SEVERITY_ALERT },  { "all",                        NDB_MGM_EVENT_SEVERITY_ALL },  { 0,                            NDB_MGM_ILLEGAL_EVENT_SEVERITY },};extern "C"ndb_mgm_event_severityndb_mgm_match_event_severity(const char * name){  if(name == 0)    return NDB_MGM_ILLEGAL_EVENT_SEVERITY;    for(int i = 0; clusterlog_severities[i].name !=0 ; i++)    if(strcasecmp(name, clusterlog_severities[i].name) == 0)      return clusterlog_severities[i].severity;  return NDB_MGM_ILLEGAL_EVENT_SEVERITY;}extern "C"const char * ndb_mgm_get_event_severity_string(enum ndb_mgm_event_severity severity){  int i= (int)severity;  if (i >= 0 && i < (int)NDB_MGM_EVENT_SEVERITY_ALL)    return clusterlog_severity_names[i];  for(i = (int)NDB_MGM_EVENT_SEVERITY_ALL; clusterlog_severities[i].name != 0; i++)    if(clusterlog_severities[i].severity == severity)      return clusterlog_severities[i].name;  return 0;}extern "C"const unsigned int *ndb_mgm_get_clusterlog_severity_filter(NdbMgmHandle handle) {  SET_ERROR(handle, NDB_MGM_NO_ERROR, "Executing: ndb_mgm_get_clusterlog_severity_filter");  static unsigned int enabled[(int)NDB_MGM_EVENT_SEVERITY_ALL]=    {0,0,0,0,0,0,0};  const ParserRow<ParserDummy> getinfo_reply[] = {    MGM_CMD("clusterlog", NULL, ""),    MGM_ARG(clusterlog_severity_names[0], Int, Mandatory, ""),    MGM_ARG(clusterlog_severity_names[1], Int, Mandatory, ""),    MGM_ARG(clusterlog_severity_names[2], Int, Mandatory, ""),    MGM_ARG(clusterlog_severity_names[3], Int, Mandatory, ""),    MGM_ARG(clusterlog_severity_names[4], Int, Mandatory, ""),    MGM_ARG(clusterlog_severity_names[5], Int, Mandatory, ""),    MGM_ARG(clusterlog_severity_names[6], Int, Mandatory, ""),  };  CHECK_HANDLE(handle, NULL);  CHECK_CONNECTED(handle, NULL);  Properties args;  const Properties *reply;  reply = ndb_mgm_call(handle, getinfo_reply, "get info clusterlog", &args);  CHECK_REPLY(reply, NULL);    for(int i=0; i < (int)NDB_MGM_EVENT_SEVERITY_ALL; i++) {    reply->get(clusterlog_severity_names[i], &enabled[i]);  }  return enabled;}extern "C"int ndb_mgm_set_clusterlog_severity_filter(NdbMgmHandle handle, 				       enum ndb_mgm_event_severity severity,				       int enable,				       struct ndb_mgm_reply* /*reply*/) {  SET_ERROR(handle, NDB_MGM_NO_ERROR,	    "Executing: ndb_mgm_set_clusterlog_severity_filter");  const ParserRow<ParserDummy> filter_reply[] = {    MGM_CMD("set logfilter reply", NULL, ""),    MGM_ARG("result", String, Mandatory, "Error message"),    MGM_END()  };  int retval = -1;  CHECK_HANDLE(handle, -1);  CHECK_CONNECTED(handle, -1);  Properties args;  args.put("level", severity);  args.put("enable", enable);    const Properties *reply;  reply = ndb_mgm_call(handle, filter_reply, "set logfilter", &args);  CHECK_REPLY(reply, retval);  BaseString result;  reply->get("result", result);  if (strcmp(result.c_str(), "1") == 0)    retval = 1;  else if (strcmp(result.c_str(), "0") == 0)    retval = 0;  else  {    SET_ERROR(handle, EINVAL, result.c_str());  }  delete reply;  return retval;}struct ndb_mgm_event_categories {  const char* name;  enum ndb_mgm_event_category category;} categories[] = {  { "STARTUP", NDB_MGM_EVENT_CATEGORY_STARTUP },  { "SHUTDOWN", NDB_MGM_EVENT_CATEGORY_SHUTDOWN },  { "STATISTICS", NDB_MGM_EVENT_CATEGORY_STATISTIC },  { "NODERESTART", NDB_MGM_EVENT_CATEGORY_NODE_RESTART },  { "CONNECTION", NDB_MGM_EVENT_CATEGORY_CONNECTION },  { "CHECKPOINT", NDB_MGM_EVENT_CATEGORY_CHECKPOINT },  { "DEBUG", NDB_MGM_EVENT_CATEGORY_DEBUG },  { "INFO", NDB_MGM_EVENT_CATEGORY_INFO },  { "ERROR", NDB_MGM_EVENT_CATEGORY_ERROR },  { "BACKUP", NDB_MGM_EVENT_CATEGORY_BACKUP },  { "CONGESTION", NDB_MGM_EVENT_CATEGORY_CONGESTION },  { 0, NDB_MGM_ILLEGAL_EVENT_CATEGORY }};extern "C"ndb_mgm_event_categoryndb_mgm_match_event_category(const char * status){  if(status == 0)    return NDB_MGM_ILLEGAL_EVENT_CATEGORY;    for(int i = 0; categories[i].name !=0 ; i++)    if(strcmp(status, categories[i].name) == 0)      return categories[i].category;  return NDB_MGM_ILLEGAL_EVENT_CATEGORY;}extern "C"const char * ndb_mgm_get_event_category_string(enum ndb_mgm_event_category status){  int i;  for(i = 0; categories[i].name != 0; i++)    if(categories[i].category == status)      return categories[i].name;    return 0;}static const char *clusterlog_names[]=  { "startup", "shutdown", "statistics", "checkpoint", "noderestart", "connection", "info", "warning", "error", "congestion", "debug", "backup" };extern "C"const unsigned int *ndb_mgm_get_clusterlog_loglevel(NdbMgmHandle handle){  SET_ERROR(handle, NDB_MGM_NO_ERROR, "Executing: ndb_mgm_get_clusterlog_loglevel");  int loglevel_count = CFG_MAX_LOGLEVEL - CFG_MIN_LOGLEVEL + 1 ;  static unsigned int loglevel[CFG_MAX_LOGLEVEL - CFG_MIN_LOGLEVEL + 1] = {0,0,0,0,0,0,0,0,0,0,0,0};  const ParserRow<ParserDummy> getloglevel_reply[] = {    MGM_CMD("get cluster loglevel", NULL, ""),    MGM_ARG(clusterlog_names[0], Int, Mandatory, ""),    MGM_ARG(clusterlog_names[1], Int, Mandatory, ""),    MGM_ARG(clusterlog_names[2], Int, Mandatory, ""),    MGM_ARG(clusterlog_names[3], Int, Mandatory, ""),    MGM_ARG(clusterlog_names[4], Int, Mandatory, ""),    MGM_ARG(clusterlog_names[5], Int, Mandatory, ""),    MGM_ARG(clusterlog_names[6], Int, Mandatory, ""),    MGM_ARG(clusterlog_names[7], Int, Mandatory, ""),    MGM_ARG(clusterlog_names[8], Int, Mandatory, ""),    MGM_ARG(clusterlog_names[9], Int, Mandatory, ""),    MGM_ARG(clusterlog_names[10], Int, Mandatory, ""),    MGM_ARG(clusterlog_names[11], Int, Mandatory, ""),  };  CHECK_HANDLE(handle, NULL);  CHECK_CONNECTED(handle, NULL);  Properties args;  const Properties *reply;  reply = ndb_mgm_call(handle, getloglevel_reply, "get cluster loglevel", &args);  CHECK_REPLY(reply, NULL);  for(int i=0; i < loglevel_count; i++) {    reply->get(clusterlog_names[i], &loglevel[i]);  }  return loglevel;}extern "C"int ndb_mgm_set_clusterlog_loglevel(NdbMgmHandle handle, int nodeId,				enum ndb_mgm_event_category cat,				int level,				struct ndb_mgm_reply* /*reply*/) {  SET_ERROR(handle, NDB_MGM_NO_ERROR, 	    "Executing: ndb_mgm_set_clusterlog_loglevel");  const ParserRow<ParserDummy> clusterlog_reply[] = {    MGM_CMD("set cluster loglevel reply", NULL, ""),    MGM_ARG("result", String, Mandatory, "Error message"),    MGM_END()  };  CHECK_HANDLE(handle, -1);  CHECK_CONNECTED(handle, -1);  Properties args;  args.put("node", nodeId);  args.put("category", cat);  args.put("level", level);    const Properties *reply;  reply = ndb_mgm_call(handle, clusterlog_reply, 		       "set cluster loglevel", &args);  CHECK_REPLY(reply, -1);    DBUG_ENTER("ndb_mgm_set_clusterlog_loglevel");  DBUG_PRINT("enter",("node=%d, category=%d, level=%d", nodeId, cat, level));  BaseString result;  reply->get("result", result);  if(strcmp(result.c_str(), "Ok") != 0) {    SET_ERROR(handle, EINVAL, result.c_str());    delete reply;    DBUG_RETURN(-1);  }  delete reply;  DBUG_RETURN(0);}extern "C"int ndb_mgm_set_loglevel_node(NdbMgmHandle handle, int nodeId,			  enum ndb_mgm_event_category category,			  int level,			  struct ndb_mgm_reply* /*reply*/) {  SET_ERROR(handle, NDB_MGM_NO_ERROR, "Executing: ndb_mgm_set_loglevel_node");  const ParserRow<ParserDummy> loglevel_reply[] = {    MGM_CMD("set loglevel reply", NULL, ""),    MGM_ARG("result", String, Mandatory, "Error message"),    MGM_END()  };  CHECK_HANDLE(handle, -1);  CHECK_CONNECTED(handle, -1);  Properties args;  args.put("node", nodeId);  args.put("category", category);  args.put("level", level);  const Properties *reply;  reply = ndb_mgm_call(handle, loglevel_reply, "set loglevel", &args);  CHECK_REPLY(reply, -1);  BaseString result;  reply->get("result", result);  if(strcmp(result.c_str(), "Ok") != 0) {    SET_ERROR(handle, EINVAL, result.c_str());    delete reply;    return -1;  }  delete reply;  return 0;}intndb_mgm_listen_event_internal(NdbMgmHandle handle, const int filter[],			      int parsable){  SET_ERROR(handle, NDB_MGM_NO_ERROR, "Executing: ndb_mgm_listen_event");  const ParserRow<ParserDummy> stat_reply[] = {    MGM_CMD("listen event", NULL, ""),    MGM_ARG("result", Int, Mandatory, "Error message"),    MGM_ARG("msg", String, Optional, "Error message"),    MGM_END()  };  CHECK_HANDLE(handle, -1);  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();  if (sockfd == NDB_INVALID_SOCKET) {    setError(handle, NDB_MGM_COULD_NOT_CONNECT_TO_SOCKET, __LINE__,	     "Unable to connect to");    return -1;  }  Properties args;  if (parsable)    args.put("parsable", parsable);  {    BaseString tmp;    for(int i = 0; filter[i] != 0; i += 2){      tmp.appfmt("%d=%d ", filter[i+1], filter[i]);    }    args.put("filter", tmp.c_str());  }  int tmp = handle->socket;  handle->socket = sockfd;  const Properties *reply;  reply = ndb_mgm_call(handle, stat_reply, "listen event", &args);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?