main.cpp 15.9 KB
Newer Older
Q
qinzuoyan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
// Copyright (c) 2017, Xiaomi, Inc.  All rights reserved.
// This source code is licensed under the Apache License Version 2.0, which
// can be found in the LICENSE file in the root directory of this source tree.

#include <pegasus/version.h>
#include <setjmp.h>
#include <signal.h>
#include <algorithm>
#include "args.h"
#include "command_executor.h"
#include "commands.h"

std::string s_last_history;
14
const int max_params_count = 10000;
Q
qinzuoyan 已提交
15 16 17 18
std::map<std::string, command_executor *> commands_map;
shell_context global_context;
size_t max_length = 0;

19 20 21 22 23 24 25
void print_help();
bool help_info(command_executor *e, shell_context *sc, arguments args)
{
    print_help();
    return true;
}

Q
qinzuoyan 已提交
26
command_executor commands[] = {
27 28 29
    {
        "help", "print help info", "", help_info,
    },
Q
qinzuoyan 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
    {
        "version", "get the shell version", "", version,
    },
    {
        "cluster_info", "get the informations for the cluster", "", query_cluster_info,
    },
    {
        "app",
        "get the partition information for some specific app",
        "<app_name> [-d|--detailed] [-o|--output <out_file>]",
        query_app,
    },
    {
        "app_disk",
        "get the disk usage information for some specific app",
        "<app_name> [-d|--detailed] [-o|--output <out_file>]",
        app_disk,
    },
    {
        "ls",
        "list all apps",
        "[-a|-all] [-d|--detailed] [-s|--status "
        "<all|available|creating|dropping|dropped>] "
        "[-o|--output FILE_PATH]",
        ls_apps,
    },
    {
        "nodes",
        "get the node status for this cluster",
        "[-d|--detailed] [-s|--status <all|alive|unalive>] [-o|--output "
        "FILE_PATH]",
        ls_nodes,
    },
    {
        "create",
        "create an app",
66 67
        "app_name [--partition_count|-p NUMBER] [--replica_count|-r NUMBER] "
        "[--envs|-e k1=v1,k2=v2...]",
Q
qinzuoyan 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
        create_app,
    },
    {
        "drop", "drop an app", "app_name [--reserve_seconds|-r NUMBER]", drop_app,
    },
    {
        "recall", "recall an app", "<app_id> [new_app_name]", recall_app,
    },
    {
        "set_meta_level",
        "set the meta function level: stopped, blind, freezed, steady, lively",
        "<stopped | blind | freezed | steady | lively>",
        set_meta_level,
    },
    {
        "get_meta_level", "get the meta function level", "", get_meta_level,
    },
    {
        "balance",
        "send explicit balancer request for the cluster",
        "-g|--gpid <appid.pidx> -p|--type <move_pri|copy_pri|copy_sec> -f|--from "
        "<from_address> "
        "-t|--to <to_address>",
        balance,
    },
    {
        "propose",
        "send configuration proposals to cluster",
        "[-f|--force] "
        "-g|--gpid <appid.pidx> -p|--type <ASSIGN_PRIMARY|ADD_SECONDARY|DOWNGRADE_TO_INACTIVE...> "
        "-t|--target <node_exec_command> -n|--node <node_affected> ",
        propose,
    },
    {
        "use",
        "set the current app used for the data access commands",
        "[app_name]",
        use_app_as_current,
    },
Q
qinzuoyan 已提交
107 108 109 110 111 112
    {
        "escape_all",
        "if escape all characters when printing key/value bytes",
        "[true|false]",
        process_escape_all,
    },
113 114 115 116 117 118
    {
        "timeout",
        "default timeout in milliseconds for read/write operations",
        "[time_in_ms]",
        process_timeout,
    },
Q
qinzuoyan 已提交
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
    {
        "hash",
        "calculate the hash result for some hash key",
        "<hash_key> <sort_key>",
        calculate_hash_value,
    },
    {
        "set", "set value", "<hash_key> <sort_key> <value> [ttl_in_seconds]", data_operations,
    },
    {
        "multi_set",
        "set multiple values for a single hash key",
        "<hash_key> <sort_key> <value> [sort_key value...]",
        data_operations,
    },
    {
        "get", "get value", "<hash_key> <sort_key>", data_operations,
    },
    {
        "multi_get",
        "get multiple values for a single hash key",
        "<hash_key> [sort_key...]",
        data_operations,
    },
143 144 145 146 147
    {
        "multi_get_range",
        "get multiple values under sort key range for a single hash key",
        "<hash_key> <start_sort_key> <stop_sort_key> "
        "[--start_inclusive|-a <true|false>] [--stop_inclusive|-b <true|false>] "
148 149
        "[--sort_key_filter_type|-s <anywhere|prefix|postfix>] "
        "[--sort_key_filter_pattern|-y <str>] "
150
        "[--max_count|-n <num>] [--no_value|-i] [--reverse|-r]",
151 152
        data_operations,
    },
Q
qinzuoyan 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
    {
        "multi_get_sortkeys",
        "get multiple sort keys for a single hash key",
        "<hash_key>",
        data_operations,
    },
    {
        "del", "del a key", "<hash_key> <sort_key>", data_operations,
    },
    {
        "multi_del",
        "delete multiple values for a single hash key",
        "<hash_key> <sort_key> [sort_key...]",
        data_operations,
    },
168 169 170 171 172 173 174 175 176 177
    {
        "multi_del_range",
        "delete multiple values under sort key range for a single hash key",
        "<hash_key> <start_sort_key> <stop_sort_key> "
        "[--start_inclusive|-a <true|false>] [--stop_inclusive|-b <true|false>] "
        "[--sort_key_filter_type|-s <anywhere|prefix|postfix>] "
        "[--sort_key_filter_pattern|-y <str>] "
        "[--output|-o <file_name>] [--silent|-i]",
        data_operations,
    },
Q
qinzuoyan 已提交
178 179 180 181 182 183 184 185 186 187
    {
        "exist", "check value exist", "<hash_key> <sort_key>", data_operations,
    },
    {
        "count", "get sort key count for a single hash key", "<hash_key>", data_operations,
    },
    {
        "ttl", "query ttl for a specific key", "<hash_key> <sort_key>", data_operations,
    },
    {
188
        "hash_scan",
Q
qinzuoyan 已提交
189
        "scan all sorted keys for a single hash key",
190 191
        "<hash_key> <start_sort_key> <stop_sort_key> [-d|--detailed] "
        "[-o|--output <file_name>] [-n|--max_count <num>] [-t|--timeout_ms <num>] "
192 193 194
        "[--start_inclusive|-a <true|false>] [--stop_inclusive|-b <true|false>] "
        "[--sort_key_filter_type|-s <anywhere|prefix|postfix>] "
        "[--sort_key_filter_pattern|-y <str>] "
195
        "[--no_value|-i]",
Q
qinzuoyan 已提交
196 197 198
        data_operations,
    },
    {
199 200 201 202
        "full_scan",
        "scan all hash keys",
        "[-d|--detailed] [-p|--partition <num>] [-o|--output <file_name>] "
        "[-n|--max_count <num>] [-t|--timeout_ms <num>] "
203 204 205 206
        "[--hash_key_filter_type|-h <anywhere|prefix|postfix>] "
        "[--hash_key_filter_pattern|-x <str>] "
        "[--sort_key_filter_type|-s <anywhere|prefix|postfix>] "
        "[--sort_key_filter_pattern|-y <str>] "
207
        "[--no_value|-i]",
Q
qinzuoyan 已提交
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
        data_operations,
    },
    {
        "copy_data",
        "copy app data",
        "-c|--target_cluster_name <str> -a|--target_app_name <str> "
        "[-s|--max_split_count <num>] "
        "[-b|--max_batch_count <num>] [-t|--timeout_ms <num>]",
        data_operations,
    },
    {
        "clear_data",
        "clear app data",
        "[-f|--force] [-s|--max_split_count <num>] [-b|--max_batch_count <num>] "
        "[-t|--timeout_ms "
        "<num>]",
        data_operations,
    },
    {
        "count_data",
        "get app row count",
        "[-s|--max_split_count <num>] [-b|--max_batch_count <num>] "
        "[-t|--timeout_ms <num>] "
231
        "[-z|--stat_size] [-c|--top_count <num>]",
Q
qinzuoyan 已提交
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
        data_operations,
    },
    {
        "remote_command",
        "send remote command to servers",
        "[-t all|meta-server|replica-server] [-l ip:port,ip:port,...,ip:port] "
        "command [arguments...]",
        remote_command,
    },
    {
        "server_info",
        "get info of servers",
        "[-t all|meta-server|replica-server] [-l ip:port,ip:port,...,ip:port]",
        server_info,
    },
    {
        "server_stat",
        "get stat of servers",
        "[-t all|meta-server|replica-server] [-l ip:port,ip:port,...,ip:port]",
        server_stat,
    },
    {
        "app_stat", "get stat of apps", "[-a|--app_name <str>] [-o|--output <out_file>]", app_stat,
    },
    {
        "flush_log",
        "flush log of servers",
        "[-t all|meta-server|replica-server] [-l ip:port,ip:port,...,ip:port]",
        flush_log,
    },
    {
        "local_get", "get value from local db", "<db_path> <hash_key> <sort_key>", local_get,
    },
    {
        "sst_dump",
        "dump sstable dir or files",
        "[--command=check|scan|none|raw] --file=data_dir_OR_sst_file "
        "[--from=<user_key>] "
        "[--to=<user_key>] [--read_num=NUM] [--show_properties]",
        sst_dump,
    },
    {
        "mlog_dump",
        "dump mutation log dir",
        "-i|--input log_dir [-o|--output file_name] [-d|--detailed]",
        mlog_dump,
    },
    {
        "recover",
        "control the meta to recover the system from given nodes",
        "[-f|--node_list_file file_name] [-s|--node_list_str node_str] "
        "[-w|--wait_seconds seconds] "
        "[-b|--skip_bad_nodes] [-l|--skip_lost_partitions] [-o|--output "
        "FILE_NAME]",
        recover,
    },
C
cailiuyang 已提交
288 289 290 291 292 293 294 295
    {
        "add_backup_policy",
        "add new cold backup policy",
        "<-p|--policy_name p1> <-b|--backup_provider_type provider> <-a|--app_ids 1,2,3..> "
        "<-i|--backup_interval_seconds sec> <-s|--start_time hour:minute> "
        "<-c|--backup_history_cnt count>",
        add_backup_policy,
    },
296
    {"ls_backup_policy", "list the names of the subsistent backup policies", "", ls_backup_policy},
C
cailiuyang 已提交
297 298
    {
        "query_backup_policy",
299
        "query subsistent backup policy and last backup infos",
C
cailiuyang 已提交
300
        "<-p|--policy_name p1,p2> [-b|--backup_info_cnt cnt]",
C
cailiuyang 已提交
301 302 303 304 305
        query_backup_policy,
    },
    {
        "modify_backup_policy",
        "modify the backup policy",
C
cailiuyang 已提交
306 307 308
        "<-p|--policy_name p1> [-a|--add_app 1,2...] [-r|--remove_app 1,2...] "
        "[-i|--backup_interval_seconds sec] [-c|--backup_history_count cnt] "
        "[-s|--start_time hour:minute]",
C
cailiuyang 已提交
309 310 311 312 313
        modify_backup_policy,
    },
    {
        "disable_backup_policy",
        "stop policy continue backup",
C
cailiuyang 已提交
314
        "<-p|--policy_name p1>",
C
cailiuyang 已提交
315 316 317 318 319
        disable_backup_policy,
    },
    {
        "enable_backup_policy",
        "start backup policy to backup again",
C
cailiuyang 已提交
320
        "<-p|--policy_name p1>",
C
cailiuyang 已提交
321 322 323 324 325
        enable_backup_policy,
    },
    {
        "restore_app",
        "restore app from backup media",
C
cailiuyang 已提交
326 327 328
        "<-c|--old_cluster_name name> <-p|--old_policy_name name> <-a|--old_app_name name> "
        "<-i|--old_app_id id> <-t|--timestamp/backup_id timestamp> "
        "<-b|--backup_provider_type provider> [-n|--new_app_name name] [-s|--skip_bad_partition] ",
C
cailiuyang 已提交
329 330 331
        restore,
    },
    {
332 333
        "query_restore_status",
        "query restore status",
C
cailiuyang 已提交
334
        "<restore_app_id> [-d|--detailed]",
335
        query_restore_status,
C
cailiuyang 已提交
336
    },
337 338 339
    {
        "get_app_envs", "get current app envs", "", get_app_envs,
    },
340 341 342 343 344 345 346 347 348
    {
        "set_app_envs", "set current app envs", "<key1> <value1> <key2> <value2> ...", set_app_envs,
    },
    {
        "del_app_envs", "delete current app envs", "<key1> <key2> ...", del_app_envs,
    },
    {
        "clear_app_envs", "clear current app envs", "<-a|--all> <-p|--prefix str>", clear_app_envs,
    },
Q
qinzuoyan 已提交
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
    {
        "exit", "exit shell", "", exit_shell,
    },
    {
        nullptr, nullptr, nullptr, nullptr,
    }};

void print_help(command_executor *e, size_t length)
{
    int padding = length - strlen(e->name);
    std::cout << "\t" << e->name << ": ";
    for (int i = 0; i < padding; ++i)
        std::cout << " ";
    std::cout << e->name << " " << e->option_usage << std::endl;
}

void print_help()
{
    std::cout << "Usage:" << std::endl;
    for (int i = 0; commands[i].name != nullptr; ++i) {
        print_help(&commands[i], max_length);
    }
}

void register_all_commands()
{
    for (int i = 0; commands[i].name != nullptr; ++i) {
        auto pr = commands_map.emplace(commands[i].name, &commands[i]);
        dassert(pr.second, "the command '%s' is already registered!!!", commands[i].name);
        max_length = std::max(max_length, strlen(commands[i].name));
    }
}

void execute_command(command_executor *e, int argc, std::string str_args[])
{
    static char buffer[max_params_count][512]; // 512*32
    static char *argv[max_params_count];
    for (int i = 0; i < max_params_count; ++i) {
        argv[i] = buffer[i];
    }

    for (int i = 0; i < argc && i < max_params_count; ++i) {
        if (!str_args[i].empty()) {
            strcpy(argv[i], str_args[i].c_str());
        } else {
            memset(argv[i], 0, sizeof(512));
        }
    }

    if (!e->exec(e, &global_context, {argc, argv})) {
        printf("USAGE: ");
        print_help(e, max_length);
    }
}

static sigjmp_buf s_ctrlc_buf;
void handle_signals(int signo)
{
    if (signo == SIGINT) {
        std::cout << std::endl << "Type \"Ctrl-D\" to exit the shell." << std::endl;
        siglongjmp(s_ctrlc_buf, 1);
    }
}

void initialize(int argc, char **argv)
{
    if (signal(SIGINT, handle_signals) == SIG_ERR) {
        std::cout << "ERROR: register signal handler failed" << std::endl;
        dsn_exit(-1);
    }

    std::cout << "Pegasus Shell " << PEGASUS_VERSION << std::endl;
    std::cout << "Type \"help\" for more information." << std::endl;
    std::cout << "Type \"Ctrl-D\" to exit the shell." << std::endl;
    std::cout << std::endl;

    std::string config_file = argc > 1 ? argv[1] : "config.ini";
    if (!pegasus::pegasus_client_factory::initialize(config_file.c_str())) {
        std::cout << "ERROR: init pegasus failed: " << config_file << std::endl;
        dsn_exit(-1);
    } else {
        std::cout << "The config file is: " << config_file << std::endl;
    }

    std::string cluster_name = argc > 2 ? argv[2] : "mycluster";
    std::cout << "The cluster name is: " << cluster_name << std::endl;

    global_context.current_cluster_name = cluster_name;
    std::string section = "uri-resolver.dsn://" + global_context.current_cluster_name;
    std::string key = "arguments";
    std::string server_list = dsn_config_get_value_string(section.c_str(), key.c_str(), "", "");
    std::cout << "The cluster meta list is: " << server_list << std::endl;

    dsn::replication::replica_helper::load_meta_servers(
        global_context.meta_list, section.c_str(), key.c_str());
    global_context.ddl_client =
        new dsn::replication::replication_ddl_client(global_context.meta_list);

    register_all_commands();
}

void run()
{
    while (sigsetjmp(s_ctrlc_buf, 1) != 0)
        ;

    while (true) {
        int arg_count;
        std::string args[max_params_count];
        scanfCommand(arg_count, args, max_params_count);
        if (arg_count > 0) {
            int i = 0;
            for (; i < arg_count; ++i) {
                std::string &s = args[i];
                int j = 0;
                for (; j < s.size(); ++j) {
                    if (!isprint(s.at(j))) {
                        std::cout << "ERROR: found unprintable character in '"
                                  << pegasus::utils::c_escape_string(s) << "'" << std::endl;
                        break;
                    }
                }
                if (j < s.size())
                    break;
            }
            if (i < arg_count)
                continue;
            auto iter = commands_map.find(args[0]);
            if (iter != commands_map.end()) {
                execute_command(iter->second, arg_count, args);
            } else {
                std::cout << "ERROR: invalid subcommand '" << args[0] << "'" << std::endl;
                print_help();
            }
        }
    }
}

int main(int argc, char **argv)
{
    initialize(argc, argv);
    run();
    return 0;
}

#if defined(__linux__)
#include <dsn/git_commit.h>
#include <dsn/version.h>
#include <pegasus/git_commit.h>
#include <pegasus/version.h>
static char const rcsid[] =
    "$Version: Pegasus Shell " PEGASUS_VERSION " (" PEGASUS_GIT_COMMIT ")"
#if defined(DSN_BUILD_TYPE)
    " " STR(DSN_BUILD_TYPE)
#endif
        ", built with rDSN " DSN_CORE_VERSION " (" DSN_GIT_COMMIT ")"
        ", built by gcc " STR(__GNUC__) "." STR(__GNUC_MINOR__) "." STR(__GNUC_PATCHLEVEL__)
#if defined(DSN_BUILD_HOSTNAME)
            ", built on " STR(DSN_BUILD_HOSTNAME)
#endif
                ", built at " __DATE__ " " __TIME__ " $";
const char *pegasus_shell_rcsid() { return rcsid; }
#endif