mgmapi.cpp

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

CPP
2,561
字号
    free(tmp_data);    if (res < 0)    {      fprintf(handle->errstream, "Failed to decode buffer\n");      break;    }    ConfigValuesFactory cvf;    const int res2 = cvf.unpack(tmp);    if(!res2){      fprintf(handle->errstream, "Failed to unpack buffer\n");      break;    }    delete prop;    return (ndb_mgm_configuration*)cvf.getConfigValues();  } while(0);  delete prop;  return 0;}extern "C"voidndb_mgm_destroy_configuration(struct ndb_mgm_configuration *cfg){  if (cfg) {    ((ConfigValues *)cfg)->~ConfigValues();    free((void *)cfg);  }}extern "C"intndb_mgm_set_configuration_nodeid(NdbMgmHandle handle, int nodeid){  CHECK_HANDLE(handle, -1);  handle->cfg._ownNodeId= nodeid;  return 0;}extern "C"intndb_mgm_get_configuration_nodeid(NdbMgmHandle handle){  CHECK_HANDLE(handle, 0);  return handle->cfg._ownNodeId;}extern "C"int ndb_mgm_get_connected_port(NdbMgmHandle handle){  if (handle->cfg_i >= 0)    return handle->cfg.ids[handle->cfg_i].port;  else    return 0;}extern "C"const char *ndb_mgm_get_connected_host(NdbMgmHandle handle){  if (handle->cfg_i >= 0)    return handle->cfg.ids[handle->cfg_i].name.c_str();  else    return 0;}extern "C"const char *ndb_mgm_get_connectstring(NdbMgmHandle handle, char *buf, int buf_sz){  return handle->cfg.makeConnectString(buf,buf_sz);}extern "C"intndb_mgm_alloc_nodeid(NdbMgmHandle handle, unsigned int version, int nodetype,                     int log_event){  CHECK_HANDLE(handle, 0);  CHECK_CONNECTED(handle, 0);  union { long l; char c[sizeof(long)]; } endian_check;  endian_check.l = 1;  int nodeid= handle->cfg._ownNodeId;  Properties args;  args.put("version", version);  args.put("nodetype", nodetype);  args.put("nodeid", nodeid);  args.put("user", "mysqld");  args.put("password", "mysqld");  args.put("public key", "a public key");  args.put("endian", (endian_check.c[sizeof(long)-1])?"big":"little");  if (handle->m_name)    args.put("name", handle->m_name);  args.put("log_event", log_event);  const ParserRow<ParserDummy> reply[]= {    MGM_CMD("get nodeid reply", NULL, ""),      MGM_ARG("error_code", Int, Optional, "Error code"),      MGM_ARG("nodeid", Int, Optional, "Error message"),      MGM_ARG("result", String, Mandatory, "Error message"),    MGM_END()  };    const Properties *prop;  prop= ndb_mgm_call(handle, reply, "get nodeid", &args);  CHECK_REPLY(prop, -1);  nodeid= -1;  do {    const char * buf;    if (!prop->get("result", &buf) || strcmp(buf, "Ok") != 0)    {      const char *hostname= ndb_mgm_get_connected_host(handle);      unsigned port=  ndb_mgm_get_connected_port(handle);      BaseString err;      Uint32 error_code= NDB_MGM_ALLOCID_ERROR;      err.assfmt("Could not alloc node id at %s port %d: %s",		 hostname, port, buf);      prop->get("error_code", &error_code);      setError(handle, error_code, __LINE__, err.c_str());      break;    }    Uint32 _nodeid;    if(!prop->get("nodeid", &_nodeid) != 0){      fprintf(handle->errstream, "ERROR Message: <nodeid Unspecified>\n");      break;    }    nodeid= _nodeid;  }while(0);  delete prop;  return nodeid;}/***************************************************************************** * Global Replication ******************************************************************************/extern "C"int ndb_mgm_rep_command(NdbMgmHandle handle, unsigned int request,		    unsigned int* replication_id,		    struct ndb_mgm_reply* /*reply*/) {  SET_ERROR(handle, NDB_MGM_NO_ERROR, "Executing: ndb_mgm_rep_command");  const ParserRow<ParserDummy> replication_reply[] = {    MGM_CMD("global replication reply", NULL, ""),    MGM_ARG("result", String, Mandatory, "Error message"),    MGM_ARG("id", Int, Optional, "Id of global replication"),    MGM_END()  };  CHECK_HANDLE(handle, -1);  CHECK_CONNECTED(handle, -1);  Properties args;  args.put("request", request);  const Properties *reply;  reply = ndb_mgm_call(handle, replication_reply, "rep", &args);  CHECK_REPLY(reply, -1);    const char * result;  reply->get("result", &result);  reply->get("id", replication_id);  if(strcmp(result,"Ok")!=0) {    delete reply;    return -1;  }  delete reply;  return 0;}extern "C"intndb_mgm_set_int_parameter(NdbMgmHandle handle,			  int node, 			  int param,			  unsigned value,			  struct ndb_mgm_reply*){  CHECK_HANDLE(handle, 0);  CHECK_CONNECTED(handle, 0);    Properties args;  args.put("node", node);  args.put("param", param);  args.put("value", value);    const ParserRow<ParserDummy> reply[]= {    MGM_CMD("set parameter reply", NULL, ""),    MGM_ARG("result", String, Mandatory, "Error message"),    MGM_END()  };    const Properties *prop;  prop= ndb_mgm_call(handle, reply, "set parameter", &args);  CHECK_REPLY(prop, -1);  int res= -1;  do {    const char * buf;    if(!prop->get("result", &buf) || strcmp(buf, "Ok") != 0){      fprintf(handle->errstream, "ERROR Message: %s\n", buf);      break;    }    res= 0;  } while(0);    delete prop;  return res;}extern "C"int ndb_mgm_set_int64_parameter(NdbMgmHandle handle,			    int node, 			    int param,			    unsigned long long value,			    struct ndb_mgm_reply*){  CHECK_HANDLE(handle, 0);  CHECK_CONNECTED(handle, 0);    Properties args;  args.put("node", node);  args.put("param", param);  args.put("value", value);    const ParserRow<ParserDummy> reply[]= {    MGM_CMD("set parameter reply", NULL, ""),    MGM_ARG("result", String, Mandatory, "Error message"),    MGM_END()  };    const Properties *prop;  prop= ndb_mgm_call(handle, reply, "set parameter", &args);    if(prop == NULL) {    SET_ERROR(handle, EIO, "Unable set parameter");    return -1;  }  int res= -1;  do {    const char * buf;    if(!prop->get("result", &buf) || strcmp(buf, "Ok") != 0){      fprintf(handle->errstream, "ERROR Message: %s\n", buf);      break;    }    res= 0;  } while(0);    delete prop;  return res;}extern "C"intndb_mgm_set_string_parameter(NdbMgmHandle handle,			     int node, 			     int param,			     const char * value,			     struct ndb_mgm_reply*){  CHECK_HANDLE(handle, 0);  CHECK_CONNECTED(handle, 0);    Properties args;  args.put("node", node);  args.put("parameter", param);  args.put("value", value);    const ParserRow<ParserDummy> reply[]= {    MGM_CMD("set parameter reply", NULL, ""),    MGM_ARG("result", String, Mandatory, "Error message"),    MGM_END()  };    const Properties *prop;  prop= ndb_mgm_call(handle, reply, "set parameter", &args);    if(prop == NULL) {    SET_ERROR(handle, EIO, "Unable set parameter");    return -1;  }  int res= -1;  do {    const char * buf;    if(!prop->get("result", &buf) || strcmp(buf, "Ok") != 0){      fprintf(handle->errstream, "ERROR Message: %s\n", buf);      break;    }    res= 0;  } while(0);    delete prop;  return res;}extern "C"intndb_mgm_purge_stale_sessions(NdbMgmHandle handle, char **purged){  CHECK_HANDLE(handle, 0);  CHECK_CONNECTED(handle, 0);    Properties args;    const ParserRow<ParserDummy> reply[]= {    MGM_CMD("purge stale sessions reply", NULL, ""),    MGM_ARG("purged", String, Optional, ""),    MGM_ARG("result", String, Mandatory, "Error message"),    MGM_END()  };    const Properties *prop;  prop= ndb_mgm_call(handle, reply, "purge stale sessions", &args);    if(prop == NULL) {    SET_ERROR(handle, EIO, "Unable to purge stale sessions");    return -1;  }  int res= -1;  do {    const char * buf;    if(!prop->get("result", &buf) || strcmp(buf, "Ok") != 0){      fprintf(handle->errstream, "ERROR Message: %s\n", buf);      break;    }    if (purged) {      if (prop->get("purged", &buf))	*purged= strdup(buf);      else	*purged= 0;    }    res= 0;  } while(0);  delete prop;  return res;}extern "C"intndb_mgm_check_connection(NdbMgmHandle handle){  CHECK_HANDLE(handle, 0);  CHECK_CONNECTED(handle, 0);  SocketOutputStream out(handle->socket);  SocketInputStream in(handle->socket, handle->read_timeout);  char buf[32];  if (out.println("check connection"))    goto ndb_mgm_check_connection_error;  if (out.println(""))    goto ndb_mgm_check_connection_error;  in.gets(buf, sizeof(buf));  if(strcmp("check connection reply\n", buf))    goto ndb_mgm_check_connection_error;  in.gets(buf, sizeof(buf));  if(strcmp("result: Ok\n", buf))    goto ndb_mgm_check_connection_error;  in.gets(buf, sizeof(buf));  if(strcmp("\n", buf))    goto ndb_mgm_check_connection_error;  return 0;ndb_mgm_check_connection_error:  ndb_mgm_disconnect(handle);  return -1;}extern "C"intndb_mgm_set_connection_int_parameter(NdbMgmHandle handle,				     int node1,				     int node2,				     int param,				     int value,				     struct ndb_mgm_reply* mgmreply){  DBUG_ENTER("ndb_mgm_set_connection_int_parameter");  CHECK_HANDLE(handle, 0);  CHECK_CONNECTED(handle, 0);    Properties args;  args.put("node1", node1);  args.put("node2", node2);  args.put("param", param);  args.put("value", (Uint32)value);    const ParserRow<ParserDummy> reply[]= {    MGM_CMD("set connection parameter reply", NULL, ""),    MGM_ARG("message", String, Mandatory, "Error Message"),    MGM_ARG("result", String, Mandatory, "Status Result"),    MGM_END()  };    const Properties *prop;  prop= ndb_mgm_call(handle, reply, "set connection parameter", &args);  CHECK_REPLY(prop, -1);  int res= -1;  do {    const char * buf;    if(!prop->get("result", &buf) || strcmp(buf, "Ok") != 0){      fprintf(handle->errstream, "ERROR Message: %s\n", buf);      break;    }    res= 0;  } while(0);    delete prop;  DBUG_RETURN(res);}extern "C"intndb_mgm_get_connection_int_parameter(NdbMgmHandle handle,				     int node1,				     int node2,				     int param,				     int *value,				     struct ndb_mgm_reply* mgmreply){  DBUG_ENTER("ndb_mgm_get_connection_int_parameter");  CHECK_HANDLE(handle, -1);  CHECK_CONNECTED(handle, -2);    Properties args;  args.put("node1", node1);  args.put("node2", node2);  args.put("param", param);  const ParserRow<ParserDummy> reply[]= {    MGM_CMD("get connection parameter reply", NULL, ""),    MGM_ARG("value", Int, Mandatory, "Current Value"),    MGM_ARG("result", String, Mandatory, "Result"),    MGM_END()  };    const Properties *prop;  prop = ndb_mgm_call(handle, reply, "get connection parameter", &args);  CHECK_REPLY(prop, -3);  int res= -1;  do {    const char * buf;    if(!prop->get("result", &buf) || strcmp(buf, "Ok") != 0){      fprintf(handle->errstream, "ERROR Message: %s\n", buf);      break;    }    res= 0;  } while(0);  if(!prop->get("value",(Uint32*)value)){    fprintf(handle->errstream, "Unable to get value\n");    res = -4;  }  delete prop;  DBUG_RETURN(res);}extern "C"NDB_SOCKET_TYPEndb_mgm_convert_to_transporter(NdbMgmHandle *handle){  NDB_SOCKET_TYPE s;  CHECK_HANDLE((*handle), NDB_INVALID_SOCKET);  CHECK_CONNECTED((*handle), NDB_INVALID_SOCKET);  (*handle)->connected= 0;   // we pretend we're disconnected  s= (*handle)->socket;  SocketOutputStream s_output(s);  s_output.println("transporter connect");  s_output.println("");  ndb_mgm_destroy_handle(handle); // set connected=0, so won't disconnect  return s;}extern "C"Uint32ndb_mgm_get_mgmd_nodeid(NdbMgmHandle handle){  Uint32 nodeid=0;  DBUG_ENTER("ndb_mgm_get_mgmd_nodeid");  CHECK_HANDLE(handle, 0);  CHECK_CONNECTED(handle, 0);    Properties args;  const ParserRow<ParserDummy> reply[]= {    MGM_CMD("get mgmd nodeid reply", NULL, ""),    MGM_ARG("nodeid", Int, Mandatory, "Node ID"),    MGM_END()  };    const Properties *prop;  prop = ndb_mgm_call(handle, reply, "get mgmd nodeid", &args);  CHECK_REPLY(prop, 0);  if(!prop->get("nodeid",&nodeid)){    fprintf(handle->errstream, "Unable t

⌨️ 快捷键说明

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