This is an automated email from the ASF dual-hosted git repository.

wangdan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-pegasus.git


The following commit(s) were added to refs/heads/master by this push:
     new 53dd218d5 feat(shell): add JSON format data output to some 
backup_policy commands (#2030)
53dd218d5 is described below

commit 53dd218d5a23ff2c808e2e90696055459cee34be
Author: Samunroyu <[email protected]>
AuthorDate: Thu May 30 11:32:05 2024 +0800

    feat(shell): add JSON format data output to some backup_policy commands 
(#2030)
    
    Add JSON output to some backup policy commands to facilitate the writing of 
automation scripts.
    
    Backup policy commands including:
    - ls_backup_policy
    - query_backup_policy
    
    ls_backup_policy Output example by Tabler format
    ```
    [p1]
    backup_provider_type  : hdfs_service
    backup_interval       : 86400s
    app_ids               : {3}
    start_time            : 03:36
    status                : enabled
    backup_history_count  : 1
    
    [p2]
    backup_provider_type  : hdfs_service
    backup_interval       : 86400s
    app_ids               : {3}
    start_time            : 20:25
    status                : enabled
    backup_history_count  : 1
    ```
    ls_backup_policy Output example by JSON format
    ```
    {
        "p1": {
            "backup_provider_type": "hdfs_service",
            "backup_interval": "86400s",
            "app_ids": "{3}",
            "start_time": "03:36",
            "status": "enabled",
            "backup_history_count": "1"
        },
        "p2": {
            "backup_provider_type": "hdfs_service",
            "backup_interval": "86400s",
            "app_ids": "{3}",
            "start_time": "20:25",
            "status": "enabled",
            "backup_history_count": "1"
        }
    }
    ```
    
    query_backup_policy Output example by Tabler format
    ```
    [p1]
    backup_provider_type  : hdfs_service
    backup_interval       : 86400s
    app_ids               : {3}
    start_time            : 03:36
    status                : enabled
    backup_history_count  : 1
    
    [backup_info]
    id             start_time           end_time  app_ids
    1716781003199  2024-05-27 03:36:43  -         {3}
    
    [p2]
    backup_provider_type  : hdfs_service
    backup_interval       : 86400s
    app_ids               : {3}
    start_time            : 20:25
    status                : enabled
    backup_history_count  : 1
    
    [backup_info]
    id             start_time           end_time  app_ids
    1716840160297  2024-05-27 20:02:40  -         {3}
    ```
    query_backup_policy Output example by JSON format
       ```
       {
            "p1": {
                "backup_provider_type": "hdfs_service",
                "backup_interval": "86400s",
                "app_ids": "{3}",
                "start_time": "03:36",
                "status": "enabled",
                "backup_history_count": "1"
            },
            "p1_backup_info": {
                "1716781003199": {
                    "id": "1716781003199",
                    "start_time": "2024-05-27 03:36:43",
                    "end_time": "-",
                    "app_ids": "{3}"
                }
            },
            "p2": {
                "backup_provider_type": "hdfs_service",
                "backup_interval": "86400s",
                "app_ids": "{3}",
                "start_time": "20:25",
                "status": "enabled",
                "backup_history_count": "1"
            },
            "p2_backup_info": {
                "1716840160297": {
                    "id": "1716840160297",
                    "start_time": "2024-05-27 20:02:40",
                    "end_time": "-",
                    "app_ids": "{3}"
                }
            }
        }
    ```
---
 src/client/replication_ddl_client.cpp | 76 ++++++++++++++++++++---------------
 src/client/replication_ddl_client.h   |  5 ++-
 src/meta/meta_backup_service.cpp      |  3 +-
 src/shell/commands/cold_backup.cpp    | 67 ++++++++++--------------------
 src/shell/main.cpp                    |  9 ++++-
 5 files changed, 77 insertions(+), 83 deletions(-)

diff --git a/src/client/replication_ddl_client.cpp 
b/src/client/replication_ddl_client.cpp
index a71241362..13a2e5181 100644
--- a/src/client/replication_ddl_client.cpp
+++ b/src/client/replication_ddl_client.cpp
@@ -38,6 +38,7 @@
 
 #include "backup_types.h"
 #include "common//duplication_common.h"
+#include "common/backup_common.h"
 #include "common/bulk_load_common.h"
 #include "common/gpid.h"
 #include "common/manual_compact.h"
@@ -1139,20 +1140,19 @@ dsn::error_code 
replication_ddl_client::enable_backup_policy(const std::string &
     }
 }
 
-static void print_policy_entry(const policy_entry &entry)
+static dsn::utils::table_printer print_policy_entry(const policy_entry &entry)
 {
-    dsn::utils::table_printer tp;
-    tp.add_row_name_and_data("    name", entry.policy_name);
-    tp.add_row_name_and_data("    backup_provider_type", 
entry.backup_provider_type);
-    tp.add_row_name_and_data("    backup_interval", 
entry.backup_interval_seconds + "s");
-    tp.add_row_name_and_data("    app_ids", fmt::format("{{{}}}", 
fmt::join(entry.app_ids, ", ")));
-    tp.add_row_name_and_data("    start_time", entry.start_time);
-    tp.add_row_name_and_data("    status", entry.is_disable ? "disabled" : 
"enabled");
-    tp.add_row_name_and_data("    backup_history_count", 
entry.backup_history_count_to_keep);
-    tp.output(std::cout);
+    dsn::utils::table_printer tp(entry.policy_name);
+    tp.add_row_name_and_data("backup_provider_type", 
entry.backup_provider_type);
+    tp.add_row_name_and_data("backup_interval", entry.backup_interval_seconds 
+ "s");
+    tp.add_row_name_and_data("app_ids", fmt::format("{{{}}}", 
fmt::join(entry.app_ids, ", ")));
+    tp.add_row_name_and_data("start_time", entry.start_time);
+    tp.add_row_name_and_data("status", entry.is_disable ? "disabled" : 
"enabled");
+    tp.add_row_name_and_data("backup_history_count", 
entry.backup_history_count_to_keep);
+    return tp;
 }
 
-static void print_backup_entry(const backup_entry &bentry)
+static void print_backup_entry(dsn::utils::table_printer &tp, const 
backup_entry &bentry)
 {
     char start_time[30] = {'\0'};
     char end_time[30] = {'\0'};
@@ -1164,15 +1164,13 @@ static void print_backup_entry(const backup_entry 
&bentry)
         ::dsn::utils::time_ms_to_date_time(bentry.end_time_ms, end_time, 30);
     }
 
-    dsn::utils::table_printer tp;
-    tp.add_row_name_and_data("    id", bentry.backup_id);
-    tp.add_row_name_and_data("    start_time", start_time);
-    tp.add_row_name_and_data("    end_time", end_time);
-    tp.add_row_name_and_data("    app_ids", fmt::format("{{{}}}", 
fmt::join(bentry.app_ids, ", ")));
-    tp.output(std::cout);
+    tp.add_row(bentry.backup_id);
+    tp.append_data(start_time);
+    tp.append_data(end_time);
+    tp.append_data(fmt::format("{{{}}}", fmt::join(bentry.app_ids, ", ")));
 }
 
-dsn::error_code replication_ddl_client::ls_backup_policy()
+dsn::error_code replication_ddl_client::ls_backup_policy(bool json)
 {
     auto req = std::make_shared<configuration_query_backup_policy_request>();
     req->policy_names.clear();
@@ -1187,21 +1185,26 @@ dsn::error_code 
replication_ddl_client::ls_backup_policy()
     configuration_query_backup_policy_response resp;
     ::dsn::unmarshall(resp_task->get_response(), resp);
 
+    std::streambuf *buf;
+    std::ofstream of;
+    buf = std::cout.rdbuf();
+    std::ostream out(buf);
+
     if (resp.err != ERR_OK) {
         return resp.err;
     } else {
+        dsn::utils::multi_table_printer mtp;
         for (int32_t idx = 0; idx < resp.policys.size(); idx++) {
-            std::cout << "[" << idx + 1 << "]" << std::endl;
-            print_policy_entry(resp.policys[idx]);
-            std::cout << std::endl;
+            dsn::utils::table_printer tp = 
print_policy_entry(resp.policys[idx]);
+            mtp.add(std::move(tp));
         }
+        mtp.output(out, json ? tp_output_format::kJsonPretty : 
tp_output_format::kTabular);
     }
     return ERR_OK;
 }
 
-dsn::error_code
-replication_ddl_client::query_backup_policy(const std::vector<std::string> 
&policy_names,
-                                            int backup_info_cnt)
+dsn::error_code replication_ddl_client::query_backup_policy(
+    const std::vector<std::string> &policy_names, int backup_info_cnt, bool 
json)
 {
     auto req = std::make_shared<configuration_query_backup_policy_request>();
     req->policy_names = policy_names;
@@ -1217,23 +1220,32 @@ replication_ddl_client::query_backup_policy(const 
std::vector<std::string> &poli
     configuration_query_backup_policy_response resp;
     ::dsn::unmarshall(resp_task->get_response(), resp);
 
+    std::streambuf *buf;
+    std::ofstream of;
+    buf = std::cout.rdbuf();
+    std::ostream out(buf);
+
     if (resp.err != ERR_OK) {
         return resp.err;
     } else {
+        dsn::utils::multi_table_printer mtp;
         for (int32_t idx = 0; idx < resp.policys.size(); idx++) {
-            if (idx != 0) {
-                std::cout << "************************" << std::endl;
-            }
             const policy_entry &pentry = resp.policys[idx];
-            std::cout << "policy_info:" << std::endl;
-            print_policy_entry(pentry);
-            std::cout << std::endl << "backup_infos:" << std::endl;
+            dsn::utils::table_printer tp_policy = print_policy_entry(pentry);
+            mtp.add(std::move(tp_policy));
             const std::vector<backup_entry> &backup_infos = 
resp.backup_infos[idx];
+            dsn::utils::table_printer tp_backup(pentry.policy_name + "_" +
+                                                
cold_backup_constant::BACKUP_INFO);
+            tp_backup.add_title("id");
+            tp_backup.add_column("start_time");
+            tp_backup.add_column("end_time");
+            tp_backup.add_column("app_ids");
             for (int bi_idx = 0; bi_idx < backup_infos.size(); bi_idx++) {
-                std::cout << "[" << (bi_idx + 1) << "]" << std::endl;
-                print_backup_entry(backup_infos[bi_idx]);
+                print_backup_entry(tp_backup, backup_infos[bi_idx]);
             }
+            mtp.add(std::move(tp_backup));
         }
+        mtp.output(out, json ? tp_output_format::kJsonPretty : 
tp_output_format::kTabular);
     }
     return ERR_OK;
 }
diff --git a/src/client/replication_ddl_client.h 
b/src/client/replication_ddl_client.h
index 12710e785..b36b2cf32 100644
--- a/src/client/replication_ddl_client.h
+++ b/src/client/replication_ddl_client.h
@@ -179,14 +179,15 @@ public:
 
     error_with<query_backup_status_response> query_backup(int32_t app_id, 
int64_t backup_id);
 
-    dsn::error_code ls_backup_policy();
+    dsn::error_code ls_backup_policy(bool json);
 
     dsn::error_code disable_backup_policy(const std::string &policy_name);
 
     dsn::error_code enable_backup_policy(const std::string &policy_name);
 
     dsn::error_code query_backup_policy(const std::vector<std::string> 
&policy_names,
-                                        int backup_info_cnt);
+                                        int backup_info_cnt,
+                                        bool json);
 
     dsn::error_code update_backup_policy(const std::string &policy_name,
                                          const std::vector<int32_t> 
&add_appids,
diff --git a/src/meta/meta_backup_service.cpp b/src/meta/meta_backup_service.cpp
index 3e924635a..6423e40b1 100644
--- a/src/meta/meta_backup_service.cpp
+++ b/src/meta/meta_backup_service.cpp
@@ -1468,8 +1468,7 @@ bool backup_service::is_valid_policy_name_unlocked(const 
std::string &policy_nam
     // BACKUP_INFO and policy_name should not be the same, because they are in 
the same level in the
     // output when query the policy details, use different names to 
distinguish the respective
     // contents.
-    static const std::set<std::string> kReservedNames = 
{cold_backup_constant::BACKUP_INFO};
-    if (kReservedNames.count(policy_name) == 1) {
+    if (policy_name.find(cold_backup_constant::BACKUP_INFO) != 
std::string::npos) {
         hint_message = "policy name is reserved";
         return false;
     }
diff --git a/src/shell/commands/cold_backup.cpp 
b/src/shell/commands/cold_backup.cpp
index 4dfd41473..f235234c6 100644
--- a/src/shell/commands/cold_backup.cpp
+++ b/src/shell/commands/cold_backup.cpp
@@ -32,7 +32,9 @@
 #include <vector>
 
 #include "client/replication_ddl_client.h"
+#include "shell/argh.h"
 #include "shell/command_executor.h"
+#include "shell/command_helper.h"
 #include "shell/commands.h"
 #include "shell/sds/sds.h"
 #include "utils/error_code.h"
@@ -143,64 +145,39 @@ bool add_backup_policy(command_executor *e, shell_context 
*sc, arguments args)
 
 bool ls_backup_policy(command_executor *e, shell_context *sc, arguments args)
 {
-    ::dsn::error_code err = sc->ddl_client->ls_backup_policy();
+    argh::parser cmd(args.argc, args.argv);
+    const bool json = cmd[{"-j", "--json"}];
+
+    ::dsn::error_code err = sc->ddl_client->ls_backup_policy(json);
     if (err != ::dsn::ERR_OK) {
         std::cout << "ls backup policy failed" << std::endl;
-    } else {
-        std::cout << std::endl << "ls backup policy succeed" << std::endl;
     }
     return true;
 }
 
 bool query_backup_policy(command_executor *e, shell_context *sc, arguments 
args)
 {
-    static struct option long_options[] = {{"policy_name", required_argument, 
0, 'p'},
-                                           {"backup_info_cnt", 
required_argument, 0, 'b'},
-                                           {0, 0, 0, 0}};
+    const std::string query_backup_policy_help = "<policy_name> 
[-b|--backup_info_cnt] [-j|--json]";
+    argh::parser cmd(args.argc, args.argv, 
argh::parser::PREFER_PARAM_FOR_UNREG_OPTION);
+    RETURN_FALSE_IF_NOT(cmd.pos_args().size() > 1,
+                        "invalid command, should be in the form of '{}'",
+                        query_backup_policy_help);
+
+    int param_index = 1;
     std::vector<std::string> policy_names;
-    int backup_info_cnt = 3;
+    PARSE_STRS(policy_names);
 
-    optind = 0;
-    while (true) {
-        int option_index = 0;
-        int c;
-        c = getopt_long(args.argc, args.argv, "p:b:", long_options, 
&option_index);
-        if (c == -1)
-            break;
-        switch (c) {
-        case 'p': {
-            std::vector<std::string> names;
-            ::dsn::utils::split_args(optarg, names, ',');
-            for (const auto &policy_name : names) {
-                if (policy_name.empty()) {
-                    fprintf(stderr, "invalid, empty policy_name, just 
ignore\n");
-                    continue;
-                } else {
-                    policy_names.emplace_back(policy_name);
-                }
-            }
-        } break;
-        case 'b':
-            backup_info_cnt = atoi(optarg);
-            if (backup_info_cnt <= 0) {
-                fprintf(stderr, "invalid backup_info_cnt %s\n", optarg);
-                return false;
-            }
-            break;
-        default:
-            return false;
-        }
-    }
-    if (policy_names.empty()) {
-        fprintf(stderr, "empty policy_name, please assign policy_name you want 
to query\n");
-        return false;
-    }
-    ::dsn::error_code ret = sc->ddl_client->query_backup_policy(policy_names, 
backup_info_cnt);
+    uint32_t backup_info_cnt;
+    PARSE_OPT_UINT(backup_info_cnt, 3, {"-b", "--backup_info_cnt"});
+
+    const bool json = cmd[{"-j", "--json"}];
+
+    ::dsn::error_code ret =
+        sc->ddl_client->query_backup_policy(policy_names, backup_info_cnt, 
json);
     if (ret != ::dsn::ERR_OK) {
         fprintf(stderr, "query backup policy failed, err = %s\n", 
ret.to_string());
-    } else {
-        std::cout << std::endl << "query backup policy succeed" << std::endl;
     }
+
     return true;
 }
 
diff --git a/src/shell/main.cpp b/src/shell/main.cpp
index 4b4934eed..34bddc4c6 100644
--- a/src/shell/main.cpp
+++ b/src/shell/main.cpp
@@ -413,11 +413,16 @@ static command_executor commands[] = {
         "<-c|--backup_history_cnt num>",
         add_backup_policy,
     },
-    {"ls_backup_policy", "list the names of the subsistent backup policies", 
"", ls_backup_policy},
+    {
+        "ls_backup_policy",
+        "list the names of the subsistent backup policies",
+        "[-j|--json]",
+        ls_backup_policy,
+    },
     {
         "query_backup_policy",
         "query subsistent backup policy and last backup infos",
-        "<-p|--policy_name p1,p2...> [-b|--backup_info_cnt num]",
+        "<-p|--policy_name p1,p2...> [-b|--backup_info_cnt num] [-j|--json]",
         query_backup_policy,
     },
     {


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to