From 5bc4d0914853aacbcf96b9d40c0fb12dcb9b275e Mon Sep 17 00:00:00 2001 From: T_WRLD <983415676@qq.com> Date: Sun, 9 Jul 2023 22:39:12 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9C=A8query=E4=B8=AD=E6=B7=BB=E5=8A=A0w?= =?UTF-8?q?=E5=8F=82=E6=95=B0=EF=BC=8C=E7=94=A8-Cvw=E8=A7=84=E8=8C=83CM?= =?UTF-8?q?=E5=92=8CDN=E7=BB=9F=E4=B8=80=E4=B8=BA=E5=88=86=E8=A1=8C?= =?UTF-8?q?=E8=BE=93=E5=87=BA=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/cm_ctl/cm_ctl.cpp | 35 ++++++++++++--- src/cm_ctl/ctl_help.cpp | 5 ++- src/cm_ctl/ctl_query.cpp | 18 +++++--- src/cm_ctl/ctl_query_base.cpp | 84 ++++++++++++++++++++++------------- 4 files changed, 96 insertions(+), 46 deletions(-) diff --git a/src/cm_ctl/cm_ctl.cpp b/src/cm_ctl/cm_ctl.cpp index b8ab67a..444d519 100644 --- a/src/cm_ctl/cm_ctl.cpp +++ b/src/cm_ctl/cm_ctl.cpp @@ -57,6 +57,8 @@ char* g_bin_name = NULL; char* g_bin_path = NULL; +// Need to change the options here, if options of the commands are added or modified +static const char* g_allowedOptions = "aAb:B:cCD:dE:fFgil:I:j:k:L:m:M:n:NP:pqrRsSt:T:vwxz:"; extern char sys_log_path[MAXPGPATH]; extern const char* prefix_name; @@ -67,6 +69,7 @@ static bool lc_operation = false; bool got_stop = false; bool g_detailQuery = false; +bool g_formatQuery = false; bool g_coupleQuery = false; bool backup_process_query = false; bool g_balanceQuery = false; @@ -1034,13 +1037,13 @@ static int CheckCommandQueryLcOperation(void) if (ctl_command == CM_QUERY_COMMAND && lc_operation) { if (g_detailQuery) { write_runlog2(ERROR, errcode(ERRCODE_PARAMETER_FAILURE), errmsg("-C is needed."), errdetail("N/A"), - errmodule(MOD_CMCTL), errcause("%s: The cmdline entered by the user is incorrect.", g_progname), - erraction("Please check the cmdline entered by the user(%s).", g_cmdLine)); + errmodule(MOD_CMCTL), errcause("%s: The cmdline entered by the user is incorrect.", g_progname), + erraction("Please check the cmdline entered by the user(%s).", g_cmdLine)); } else { write_runlog2(ERROR, errcode(ERRCODE_PARAMETER_FAILURE), errmsg("-Cv is needed."), - errdetail("N/A"), errmodule(MOD_CMCTL), - errcause("%s: The cmdline entered by the user is incorrect.", g_progname), - erraction("Please check the cmdline entered by the user(%s).", g_cmdLine)); + errdetail("N/A"), errmodule(MOD_CMCTL), + errcause("%s: The cmdline entered by the user is incorrect.", g_progname), + erraction("Please check the cmdline entered by the user(%s).", g_cmdLine)); } return 1; } @@ -1067,6 +1070,22 @@ static int CheckCommandQuery(void) return 1; } + cond = (g_formatQuery && !g_coupleQuery && !g_detailQuery); + if (cond) { + write_runlog2(ERROR, errcode(ERRCODE_PARAMETER_FAILURE), errmsg("-Cv is needed."), errdetail("N/A"), + errmodule(MOD_CMCTL), errcause("%s: The cmdline entered by the user is incorrect.", g_progname), + erraction("Please check the cmdline entered by the user(%s).", g_cmdLine)); + return 1; + } + + cond = (g_formatQuery && !g_coupleQuery && g_detailQuery); + if (cond) { + write_runlog2(ERROR, errcode(ERRCODE_PARAMETER_FAILURE), errmsg("-C is needed."), errdetail("N/A"), + errmodule(MOD_CMCTL), errcause("%s: The cmdline entered by the user is incorrect.", g_progname), + erraction("Please check the cmdline entered by the user(%s).", g_cmdLine)); + return 1; + } + CM_RETURN_INT_IFERR(CheckCommandQueryLcOperation()); cond = (ctl_command == CM_QUERY_COMMAND) && !logic_cluster_query && @@ -1810,6 +1829,9 @@ static void ParseCmdArgsCore(int cmd, bool *setDataPath, CtlOption *ctlCtx) case 'v': g_detailQuery = true; break; + case 'w': + g_formatQuery = true; + break; case 'c': ctlCtx->build.isNeedCmsBuild = true; coordinator_dynamic_view = true; @@ -2370,8 +2392,7 @@ int main(int argc, char** argv) /* process command-line options */ while (optind < argc) { - while ((c = getopt_long(argc, argv, "aAb:B:cCD:dE:fFgil:I:j:k:L:m:M:n:NP:pqrRsSt:T:vxz:", longOptions, - &optionIndex)) != -1) { + while ((c = getopt_long(argc, argv, g_allowedOptions, longOptions, &optionIndex)) != -1) { /* parse command type */ ParseCmdArgsCore(c, &set_data_path, &ctlCtx); } diff --git a/src/cm_ctl/ctl_help.cpp b/src/cm_ctl/ctl_help.cpp index 0dcd1e7..3038ff4 100644 --- a/src/cm_ctl/ctl_help.cpp +++ b/src/cm_ctl/ctl_help.cpp @@ -61,7 +61,7 @@ static void UsageHelp(const char *projectName) #ifdef ENABLE_MULTIPLE_NODES (void)printf(_(" %s stop [[-z AVAILABILITY_ZONE] | [-n NODEID [-D DATADIR [-R] | -I RESOURCE_INSTANCE_ID]]] " "[-t SECS] [-m SHUTDOWN-MODE]\n"), projectName); - (void)printf(_(" %s query [-z ALL] [-n NODEID [-D DATADIR -R]] [-l FILENAME] [-v [-C [-s] [-S] [-d] [-i] [-F] " + (void)printf(_(" %s query [-z ALL] [-n NODEID [-D DATADIR -R]] [-l FILENAME] [-v [-C [-w] [-s] [-S] [-d] [-i] [-F] " "[-L ALL] [-x] [-p]] | [-r]] [-t SECS] [--minorityAz=AZ_NAME]\n"), projectName); (void)printf(_(" %s restart [-L LCNAME]\n"), projectName); (void)printf(_(" %s view [-v | -N | -n NODEID | -c] [-l FILENAME]\n"), projectName); @@ -69,7 +69,7 @@ static void UsageHelp(const char *projectName) #else (void)printf(_(" %s stop [[-z AVAILABILITY_ZONE] | [-n NODEID [-D DATADIR]] | [-I RESOURCE_INSTANCE_ID [-n NODEID]]] [-t SECS] " "[-m SHUTDOWN-MODE]\n"), projectName); - (void)printf(_(" %s query [-z ALL] [-l FILENAME] [-v [-C [-s] [-S] [-d] [-i] [-F] [-x] [-p]] | [-r]] [-t SECS] " + (void)printf(_(" %s query [-z ALL] [-l FILENAME] [-v [-C [-w] [-s] [-S] [-d] [-i] [-F] [-x] [-p]] | [-r]] [-t SECS] " "[--minorityAz=AZ_NAME]\n"), projectName); (void)printf(_(" %s view [-v | -N | -n NODEID] [-l FILENAME]\n"), projectName); #endif @@ -182,6 +182,7 @@ static void QueryHelp() (void)printf(_(" -s show instances that need to switchover\n")); (void)printf(_(" -C show query result by HA relation\n")); (void)printf(_(" -v show detail query result\n")); + (void)printf(_(" -w show detail query result in vertical format\n")); (void)printf(_(" -d show instance datapath\n")); (void)printf(_(" -i show physical node ip\n")); (void)printf(_(" -F show all fenced UDF master process status\n")); diff --git a/src/cm_ctl/ctl_query.cpp b/src/cm_ctl/ctl_query.cpp index 5c836e7..ae8eeb0 100644 --- a/src/cm_ctl/ctl_query.cpp +++ b/src/cm_ctl/ctl_query.cpp @@ -46,6 +46,7 @@ static status_t QueryResourceStatus(CM_Conn *pCmsCon); static bool hasFindEtcdL = false; extern bool g_detailQuery; extern bool g_coupleQuery; +extern bool g_formatQuery; extern bool g_balanceQuery; extern bool g_startStatusQuery; extern bool g_abnormalQuery; @@ -1896,12 +1897,17 @@ static void print_simple_DN_result(uint32 node_index, cm_to_ctl_instance_status cm_to_ctl_instance_status_ptr->data_node_member.local_status.buildReason)); } - if (g_multi_az_cluster && ((uint32)cm_to_ctl_instance_status_ptr->member_index < (g_dn_replication_num - 1))) { - (void)fprintf(g_logFilePtr, " | "); - } else if (!g_single_node_cluster && !g_multi_az_cluster && - (cm_to_ctl_instance_status_ptr->member_index == 0 || cm_to_ctl_instance_status_ptr->member_index == 1)) { - (void)fprintf(g_logFilePtr, " | "); - } else { + if (g_formatQuery) { (void)fprintf(g_logFilePtr, "\n"); + } else { + if (g_multi_az_cluster && ((uint32)cm_to_ctl_instance_status_ptr->member_index < (g_dn_replication_num - 1))) { + (void)fprintf(g_logFilePtr, " | "); + } else if (!g_single_node_cluster && !g_multi_az_cluster && + (cm_to_ctl_instance_status_ptr->member_index == 0 || + cm_to_ctl_instance_status_ptr->member_index == 1)) { + (void)fprintf(g_logFilePtr, " | "); + } else { + (void)fprintf(g_logFilePtr, "\n"); + } } } diff --git a/src/cm_ctl/ctl_query_base.cpp b/src/cm_ctl/ctl_query_base.cpp index 2b92ea6..a38ef68 100644 --- a/src/cm_ctl/ctl_query_base.cpp +++ b/src/cm_ctl/ctl_query_base.cpp @@ -30,6 +30,7 @@ extern bool g_detailQuery; extern bool g_coupleQuery; +extern bool g_formatQuery; extern bool g_balanceQuery; extern bool g_startStatusQuery; extern bool g_portQuery; @@ -453,36 +454,47 @@ void CalcDnHeaderSize(uint32 *nodeLen, uint32 *instanceLen, uint32 *stateLen) void PrintDnHeaderLine(uint32 nodeLen, uint32 instanceLen, uint32 tmpInstanceLen, uint32 stateLen) { - if (g_ipQuery) { - if (g_multi_az_cluster) { - for (uint32 jj = 0; jj < g_dn_replication_num - 1; jj++) { - (void)fprintf(g_logFilePtr, "%-*s%-*s%-*s%-*s| ", - nodeLen, "node", MAX_IP_LEN + 1, "node_ip", - tmpInstanceLen, "instance", stateLen, "state"); - } - } else if (!g_single_node_cluster) { - (void)fprintf(g_logFilePtr, "%-*s%-*s%-*s%-*s| ", nodeLen, "node", MAX_IP_LEN + 1, - "node_ip", tmpInstanceLen, "instance", stateLen, "state"); - (void)fprintf(g_logFilePtr, "%-*s%-*s%-*s%-*s| ", nodeLen, "node", MAX_IP_LEN + 1, - "node_ip", tmpInstanceLen, "instance", stateLen, "state"); + if (g_formatQuery) { + if (g_ipQuery) { + (void)fprintf(g_logFilePtr, "%-*s%-*s%-*s%s\n", nodeLen, "node", MAX_IP_LEN + 1, + "node_ip", tmpInstanceLen, "instance", "state"); + } else { + (void)fprintf(g_logFilePtr, "%-*s%-*s%s\n", nodeLen, "node", + g_single_node_cluster ? tmpInstanceLen : instanceLen, + "instance", "state"); } - (void)fprintf(g_logFilePtr, "%-*s%-*s%-*s%s\n", nodeLen, "node", MAX_IP_LEN + 1, - "node_ip", tmpInstanceLen, "instance", "state"); } else { - if (g_multi_az_cluster) { - for (uint32 jj = 0; jj < g_dn_replication_num - 1; jj++) { - (void)fprintf(g_logFilePtr, "%-*s%-*s%-*s| ", nodeLen, "node", - tmpInstanceLen, "instance", stateLen, "state"); + if (g_ipQuery) { + if (g_multi_az_cluster) { + for (uint32 jj = 0; jj < g_dn_replication_num - 1; jj++) { + (void)fprintf(g_logFilePtr, "%-*s%-*s%-*s%-*s| ", + nodeLen, "node", MAX_IP_LEN + 1, "node_ip", + tmpInstanceLen, "instance", stateLen, "state"); + } + } else if (!g_single_node_cluster) { + (void)fprintf(g_logFilePtr, "%-*s%-*s%-*s%-*s| ", nodeLen, "node", MAX_IP_LEN + 1, + "node_ip", tmpInstanceLen, "instance", stateLen, "state"); + (void)fprintf(g_logFilePtr, "%-*s%-*s%-*s%-*s| ", nodeLen, "node", MAX_IP_LEN + 1, + "node_ip", tmpInstanceLen, "instance", stateLen, "state"); } - } else if (!g_single_node_cluster) { - (void)fprintf(g_logFilePtr, "%-*s%-*s%-*s| ", nodeLen, "node", - tmpInstanceLen, "instance", stateLen, "state"); - (void)fprintf(g_logFilePtr, "%-*s%-*s%-*s| ", - nodeLen, "node", tmpInstanceLen, "instance", stateLen, "state"); + (void)fprintf(g_logFilePtr, "%-*s%-*s%-*s%s\n", nodeLen, "node", MAX_IP_LEN + 1, + "node_ip", tmpInstanceLen, "instance", "state"); + } else { + if (g_multi_az_cluster) { + for (uint32 jj = 0; jj < g_dn_replication_num - 1; jj++) { + (void)fprintf(g_logFilePtr, "%-*s%-*s%-*s| ", nodeLen, "node", + tmpInstanceLen, "instance", stateLen, "state"); + } + } else if (!g_single_node_cluster) { + (void)fprintf(g_logFilePtr, "%-*s%-*s%-*s| ", nodeLen, "node", + tmpInstanceLen, "instance", stateLen, "state"); + (void)fprintf(g_logFilePtr, "%-*s%-*s%-*s| ", + nodeLen, "node", tmpInstanceLen, "instance", stateLen, "state"); + } + (void)fprintf(g_logFilePtr, "%-*s%-*s%s\n", nodeLen, "node", + g_single_node_cluster ? tmpInstanceLen : instanceLen, + "instance", "state"); } - (void)fprintf(g_logFilePtr, "%-*s%-*s%s\n", nodeLen, "node", - g_single_node_cluster ? tmpInstanceLen : instanceLen, - "instance", "state"); } } @@ -507,12 +519,22 @@ void PrintDnStatusLine() SECONDARY_DYNAMIC_ROLE_LEN + SPACE_LEN + INSTANCE_DB_STATE_LEN; if (g_multi_az_cluster || g_single_node_cluster) { - maxLen = g_dn_replication_num * - (nodeLen + tmpInstanceLen + (g_ipQuery ? (MAX_IP_LEN + 1) : 0)) + - g_dn_replication_num * (stateLen + SEPERATOR_LEN + SPACE_LEN); + if (g_formatQuery) { + maxLen = (nodeLen + tmpInstanceLen + (g_ipQuery ? (MAX_IP_LEN + 1) : 0)) + + (stateLen + SEPERATOR_LEN + SPACE_LEN); + } else { + maxLen = g_dn_replication_num * + (nodeLen + tmpInstanceLen + (g_ipQuery ? (MAX_IP_LEN + 1) : 0)) + + g_dn_replication_num * (stateLen + SEPERATOR_LEN + SPACE_LEN); + } } else { - maxLen = NODE_NUM * (nodeLen + tmpInstanceLen + (g_ipQuery ? (MAX_IP_LEN + 1) : 0)) + - SPACE_NUM * (stateLen + SEPERATOR_LEN + SPACE_LEN) + secondryStateLen; + if (g_formatQuery) { + maxLen = (nodeLen + tmpInstanceLen + (g_ipQuery ? (MAX_IP_LEN + 1) : 0)) + + (stateLen + SEPERATOR_LEN + SPACE_LEN) + secondryStateLen; + } else { + maxLen = NODE_NUM * (nodeLen + tmpInstanceLen + (g_ipQuery ? (MAX_IP_LEN + 1) : 0)) + + SPACE_NUM * (stateLen + SEPERATOR_LEN + SPACE_LEN) + secondryStateLen; + } } for (uint32 i = 0; i < maxLen; i++) { (void)fprintf(g_logFilePtr, "-");