📄 mgmapi.cpp
字号:
extern "C"ndb_mgm_node_statusndb_mgm_match_node_status(const char * status){ if(status == 0) return NDB_MGM_NODE_STATUS_UNKNOWN; for(int i = 0; i<no_of_status_values; i++) if(strcmp(status, status_values[i].str) == 0) return status_values[i].value; return NDB_MGM_NODE_STATUS_UNKNOWN;}extern "C"const char * ndb_mgm_get_node_status_string(enum ndb_mgm_node_status status){ int i; for(i = 0; i<no_of_status_values; i++) if(status_values[i].value == status) return status_values[i].str; for(i = 0; i<no_of_status_values; i++) if(status_values[i].value == NDB_MGM_NODE_STATUS_UNKNOWN) return status_values[i].str; return 0;}static intstatus_ackumulate(struct ndb_mgm_node_state * state, const char * field, const char * value){ if(strcmp("type", field) == 0){ state->node_type = ndb_mgm_match_node_type(value); } else if(strcmp("status", field) == 0){ state->node_status = ndb_mgm_match_node_status(value); } else if(strcmp("startphase", field) == 0){ state->start_phase = atoi(value); } else if(strcmp("dynamic_id", field) == 0){ state->dynamic_id = atoi(value); } else if(strcmp("node_group", field) == 0){ state->node_group = atoi(value); } else if(strcmp("version", field) == 0){ state->version = atoi(value); } else if(strcmp("connect_count", field) == 0){ state->connect_count = atoi(value); } else if(strcmp("address", field) == 0){ strncpy(state->connect_address, value, sizeof(state->connect_address)); state->connect_address[sizeof(state->connect_address)-1]= 0; } else { ndbout_c("Unknown field: %s", field); } return 0;}/** * Compare function for qsort() that sorts ndb_mgm_node_state in * node_id order */static intcmp_state(const void *_a, const void *_b) { struct ndb_mgm_node_state *a, *b; a = (struct ndb_mgm_node_state *)_a; b = (struct ndb_mgm_node_state *)_b; if (a->node_id > b->node_id) return 1; return -1;}extern "C"struct ndb_mgm_cluster_state * ndb_mgm_get_status(NdbMgmHandle handle){ SET_ERROR(handle, NDB_MGM_NO_ERROR, "Executing: ndb_mgm_get_status"); CHECK_HANDLE(handle, NULL); CHECK_CONNECTED(handle, NULL); SocketOutputStream out(handle->socket); SocketInputStream in(handle->socket, handle->read_timeout); out.println("get status"); out.println(""); char buf[1024]; in.gets(buf, sizeof(buf)); if(buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = '\0'; if(strcmp("node status", buf) != 0) { SET_ERROR(handle, NDB_MGM_ILLEGAL_NODE_STATUS, buf); return NULL; } in.gets(buf, sizeof(buf)); if(buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = '\0'; BaseString tmp(buf); Vector<BaseString> split; tmp.split(split, ":"); if(split.size() != 2){ return NULL; } if(!(split[0].trim() == "nodes")){ return NULL; } const int noOfNodes = atoi(split[1].c_str()); ndb_mgm_cluster_state *state = (ndb_mgm_cluster_state*) malloc(sizeof(ndb_mgm_cluster_state)+ noOfNodes*(sizeof(ndb_mgm_node_state)+sizeof("000.000.000.000#"))); state->no_of_nodes= noOfNodes; ndb_mgm_node_state * ptr = &state->node_states[0]; int nodeId = 0; int i; for (i= 0; i < noOfNodes; i++) { state->node_states[i].connect_address[0]= 0; } i = -1; ptr--; for(; i<noOfNodes; ){ in.gets(buf, sizeof(buf)); tmp.assign(buf); if(tmp.trim() == ""){ break; } Vector<BaseString> split; tmp.split(split, ":.", 4); if(split.size() != 4) break; const int id = atoi(split[1].c_str()); if(id != nodeId){ ptr++; i++; nodeId = id; ptr->node_id = id; } split[3].trim(" \t\n"); if(status_ackumulate(ptr,split[2].c_str(), split[3].c_str()) != 0) { break; } } if(i+1 != noOfNodes){ free(state); return NULL; } qsort(state->node_states, state->no_of_nodes, sizeof(state->node_states[0]), cmp_state); return state;}extern "C"int ndb_mgm_enter_single_user(NdbMgmHandle handle, unsigned int nodeId, struct ndb_mgm_reply* /*reply*/) { SET_ERROR(handle, NDB_MGM_NO_ERROR, "Executing: ndb_mgm_enter_single_user"); const ParserRow<ParserDummy> enter_single_reply[] = { MGM_CMD("enter single user reply", NULL, ""), MGM_ARG("result", String, Mandatory, "Error message"), MGM_END() }; CHECK_HANDLE(handle, -1); CHECK_CONNECTED(handle, -1); Properties args; args.put("nodeId", nodeId); const Properties *reply; reply = ndb_mgm_call(handle, enter_single_reply, "enter single user", &args); CHECK_REPLY(reply, -1); BaseString result; reply->get("result", result); if(strcmp(result.c_str(), "Ok") != 0) { SET_ERROR(handle, NDB_MGM_COULD_NOT_ENTER_SINGLE_USER_MODE, result.c_str()); delete reply; return -1; } delete reply; return 0;}extern "C"int ndb_mgm_exit_single_user(NdbMgmHandle handle, struct ndb_mgm_reply* /*reply*/) { SET_ERROR(handle, NDB_MGM_NO_ERROR, "Executing: ndb_mgm_exit_single_user"); const ParserRow<ParserDummy> exit_single_reply[] = { MGM_CMD("exit single user reply", NULL, ""), MGM_ARG("result", String, Mandatory, "Error message"), MGM_END() }; CHECK_HANDLE(handle, -1); CHECK_CONNECTED(handle, -1); const Properties *reply; reply = ndb_mgm_call(handle, exit_single_reply, "exit single user", 0); CHECK_REPLY(reply, -1); const char * buf; reply->get("result", &buf); if(strcmp(buf,"Ok")!=0) { SET_ERROR(handle, NDB_MGM_COULD_NOT_EXIT_SINGLE_USER_MODE, buf); delete reply; return -1; } delete reply; return 0;}extern "C"int ndb_mgm_stop(NdbMgmHandle handle, int no_of_nodes, const int * node_list){ SET_ERROR(handle, NDB_MGM_NO_ERROR, "Executing: ndb_mgm_stop"); return ndb_mgm_stop2(handle, no_of_nodes, node_list, 0);}extern "C"int ndb_mgm_stop2(NdbMgmHandle handle, int no_of_nodes, const int * node_list, int abort){ SET_ERROR(handle, NDB_MGM_NO_ERROR, "Executing: ndb_mgm_stop2"); const ParserRow<ParserDummy> stop_reply[] = { MGM_CMD("stop reply", NULL, ""), MGM_ARG("stopped", Int, Optional, "No of stopped nodes"), MGM_ARG("result", String, Mandatory, "Error message"), MGM_END() }; CHECK_HANDLE(handle, -1); CHECK_CONNECTED(handle, -1); if(no_of_nodes < 0){ SET_ERROR(handle, NDB_MGM_ILLEGAL_NUMBER_OF_NODES, "Negative number of nodes requested to stop"); return -1; } Uint32 stoppedNoOfNodes = 0; if(no_of_nodes == 0){ /** * All database nodes should be stopped */ Properties args; args.put("abort", abort); const Properties *reply; reply = ndb_mgm_call(handle, stop_reply, "stop all", &args); CHECK_REPLY(reply, -1); 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; } 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; reply = ndb_mgm_call(handle, stop_reply, "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; } 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_restart2(NdbMgmHandle handle, int no_of_nodes, const int * node_list, int initial, int nostart, int abort){ SET_ERROR(handle, NDB_MGM_NO_ERROR, "Executing: ndb_mgm_restart2"); Uint32 restarted = 0; const ParserRow<ParserDummy> restart_reply[] = { MGM_CMD("restart reply", NULL, ""), MGM_ARG("result", String, Mandatory, "Error message"), MGM_ARG("restarted", Int, Optional, "No of restarted nodes"), MGM_END() }; CHECK_HANDLE(handle, -1); CHECK_CONNECTED(handle, -1); 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, "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 reply = ndb_mgm_call(handle, restart_reply, "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); delete reply; } return restarted;}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);}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;}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);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -