From 2fd258cc46649fddafd031954116ff6cc68e12a6 Mon Sep 17 00:00:00 2001 From: gjw2284740 Date: Thu, 9 Sep 2021 11:05:30 +0800 Subject: [PATCH] [opensource] add usec_tool --- tools/ob_admin/CMakeLists.txt | 2 + tools/ob_admin/main.cpp | 16 ++- tools/ob_admin/ob_admin_executor.cpp | 2 +- tools/ob_admin/ob_admin_executor.h | 8 +- .../usec_tool/ob_admin_usec_executor.cpp | 117 ++++++++++++++++++ .../usec_tool/ob_admin_usec_executor.h | 45 +++++++ 6 files changed, 180 insertions(+), 10 deletions(-) create mode 100644 tools/ob_admin/usec_tool/ob_admin_usec_executor.cpp create mode 100644 tools/ob_admin/usec_tool/ob_admin_usec_executor.h diff --git a/tools/ob_admin/CMakeLists.txt b/tools/ob_admin/CMakeLists.txt index e83e39134c..2341755f7c 100644 --- a/tools/ob_admin/CMakeLists.txt +++ b/tools/ob_admin/CMakeLists.txt @@ -10,6 +10,8 @@ add_executable(ob_admin clog_tool/ob_log_entry_filter.h clog_tool/ob_log_entry_parser.cpp clog_tool/ob_log_entry_parser.h + usec_tool/ob_admin_usec_executor.cpp + usec_tool/ob_admin_usec_executor.h ob_admin_executor.h ob_admin_executor.cpp main.cpp) diff --git a/tools/ob_admin/main.cpp b/tools/ob_admin/main.cpp index 944bdf8874..fb4d4a1007 100644 --- a/tools/ob_admin/main.cpp +++ b/tools/ob_admin/main.cpp @@ -17,34 +17,40 @@ #include "share/ob_define.h" #include "ob_admin_executor.h" #include "clog_tool/ob_admin_clog_v2_executor.h" +#include "usec_tool/ob_admin_usec_executor.h" using namespace oceanbase::common; using namespace oceanbase::tools; void print_usage() { - fprintf(stderr, "\nUsage: ob_admin clog_tool\n"); + fprintf(stderr, + "\nUSAGE:\n" + " ob_admin clog_tool\n" + " ob_admin usec_tool\n"); } -int main(int argc, char* argv[]) +int main(int argc, char *argv[]) { int ret = 0; OB_LOGGER.set_log_level("INFO"); OB_LOGGER.set_file_name("ob_admin.log", true, false); - const char* log_level = getenv("OB_ADMIN_LOG_LEVEL"); + const char *log_level = getenv("OB_ADMIN_LOG_LEVEL"); if (NULL != log_level) { OB_LOGGER.set_log_level(log_level); } std::ostringstream ss; - copy(argv, argv + argc, std::ostream_iterator(ss, " ")); + copy(argv, argv + argc, std::ostream_iterator(ss, " ")); _OB_LOG(INFO, "cmd: [%s]", ss.str().c_str()); - ObAdminExecutor* executor = NULL; + ObAdminExecutor *executor = NULL; if (argc < 2) { print_usage(); } else { if (0 == strcmp("clog_tool", argv[1])) { executor = new ObAdminClogV2Executor(); + } else if (0 == strcmp("usec_tool", argv[1])) { + executor = new ObAdminUsecExecutor(); } else { print_usage(); } diff --git a/tools/ob_admin/ob_admin_executor.cpp b/tools/ob_admin/ob_admin_executor.cpp index eebc920d0a..6d4d00fe21 100644 --- a/tools/ob_admin/ob_admin_executor.cpp +++ b/tools/ob_admin/ob_admin_executor.cpp @@ -15,7 +15,7 @@ namespace oceanbase { using namespace common; namespace tools { -int ObAdminExecutor::parse_options(int argc, char* argv[]) +int ObAdminExecutor::parse_options(int argc, char *argv[]) { int ret = OB_SUCCESS; int option_index = 0; diff --git a/tools/ob_admin/ob_admin_executor.h b/tools/ob_admin/ob_admin_executor.h index ad3ce929d7..39d40e57e3 100644 --- a/tools/ob_admin/ob_admin_executor.h +++ b/tools/ob_admin/ob_admin_executor.h @@ -25,17 +25,17 @@ public: {} virtual ~ObAdminExecutor() {} - virtual int execute(int argc, char* argv[]) = 0; + virtual int execute(int argc, char *argv[]) = 0; protected: - int parse_options(int argc, char* argv[]); + int parse_options(int argc, char *argv[]); protected: common::ObString DB_host_; int32_t DB_port_; uint64_t tenant_id_; - const char* config_file_; - const char* wallet_file_; + const char *config_file_; + const char *wallet_file_; }; } // namespace tools } // namespace oceanbase diff --git a/tools/ob_admin/usec_tool/ob_admin_usec_executor.cpp b/tools/ob_admin/usec_tool/ob_admin_usec_executor.cpp new file mode 100644 index 0000000000..9b65486f0a --- /dev/null +++ b/tools/ob_admin/usec_tool/ob_admin_usec_executor.cpp @@ -0,0 +1,117 @@ +/** + * Copyright (c) 2021 OceanBase + * OceanBase CE is licensed under Mulan PubL v2. + * You can use this software according to the terms and conditions of the Mulan PubL v2. + * You may obtain a copy of Mulan PubL v2 at: + * http://license.coscl.org.cn/MulanPubL-2.0 + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, + * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, + * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + * See the Mulan PubL v2 for more details. + */ + +#define USING_LOG_PREFIX COMMON +#include "ob_admin_usec_executor.h" + +using namespace oceanbase::share; +using namespace oceanbase::common; + +namespace oceanbase { +namespace tools { + +ObAdminUsecExecutor::ObAdminUsecExecutor() : cmd_(ObAdminUsecCmd::MAX_CMD), usec_(0) +{} + +int ObAdminUsecExecutor::execute(int argc, char *argv[]) +{ + int ret = OB_SUCCESS; + reset(); + + if (OB_FAIL(parse_cmd(argc - 1, argv + 1))) { + LOG_WARN("fail to parse cmd", K(ret)); + } else if (ObAdminUsecCmd::TO_TIME == cmd_) { + ObObj result; + int32_t offset = 0; + char buf[10] = {0}; + if (time_zone_.empty()) { + // if the member time_zone is empty, we use UTC+8 by default. + time_zone_.assign_buffer(buf, 10); + strcpy(buf, "+8:00"); + time_zone_.set_length(static_cast(strlen(buf))); + LOG_INFO("use default time zone", K_(time_zone)); + } + if (OB_FAIL(tz_info_.set_timezone(time_zone_))) { + LOG_WARN("fail to set time zone", K(ret)); + } else if (OB_FAIL(tz_info_.get_timezone_offset(USEC_TO_SEC(usec_), offset))) { + LOG_WARN("fail to get offset between utc and local", K(ret)); + } else { + usec_ += SEC_TO_USEC(offset); + if (!ObTimeConverter::is_valid_datetime(usec_)) { + ret = OB_DATETIME_FUNCTION_OVERFLOW; + LOG_WARN("datetime overflow", K(ret), K(usec_)); + } else { + result.set_timestamp(usec_); + LOG_INFO("usec to time", K(result), K_(usec), K_(time_zone), K(offset)); + fprintf(stdout, "\n%s, UTC%s\n", to_cstring(result), to_cstring(time_zone_)); + } + } + if (OB_FAIL(ret)) { + print_usage(); + } + } else { + print_usage(); + } + + return ret; +} + +void ObAdminUsecExecutor::reset() +{ + cmd_ = ObAdminUsecCmd::MAX_CMD; + usec_ = 0; +} + +int ObAdminUsecExecutor::parse_cmd(int argc, char *argv[]) +{ + int ret = OB_SUCCESS; + int opt = 0; + const char *opt_string = "ht:z:"; + struct option longopts[] = {{"help", 0, NULL, 'h'}, {"to_time", 1, NULL, 't'}, {"time_zone", 1, NULL, 'z'}}; + + while ((opt = getopt_long(argc, argv, opt_string, longopts, NULL)) != -1) { + switch (opt) { + case 'h': { + print_usage(); + break; + } + case 't': { + cmd_ = ObAdminUsecCmd::TO_TIME; + usec_ = static_cast(strtol(optarg, NULL, 10)); + break; + } + case 'z': { + time_zone_.assign_ptr(optarg, strlen(optarg)); + break; + } + default: { + print_usage(); + ret = OB_INVALID_ARGUMENT; + } + } + } + return ret; +} + +void ObAdminUsecExecutor::print_usage() +{ + fprintf(stderr, + "\nUSAGE:\n" + " ob_admin usec_tool -t usec [-z time_zone]\n" + "EXAMPLE:\n" + " ob_admin usec_tool -t 1625104800000000\n" + " ob_admin usec_tool -t 1625104800000000 -z +8:00\n" + " ob_admin usec_tool -t 1625104800000000 -z -8:00\n"); +} + +} // namespace tools +} // namespace oceanbase diff --git a/tools/ob_admin/usec_tool/ob_admin_usec_executor.h b/tools/ob_admin/usec_tool/ob_admin_usec_executor.h new file mode 100644 index 0000000000..97085c1425 --- /dev/null +++ b/tools/ob_admin/usec_tool/ob_admin_usec_executor.h @@ -0,0 +1,45 @@ +/** + * Copyright (c) 2021 OceanBase + * OceanBase CE is licensed under Mulan PubL v2. + * You can use this software according to the terms and conditions of the Mulan PubL v2. + * You may obtain a copy of Mulan PubL v2 at: + * http://license.coscl.org.cn/MulanPubL-2.0 + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, + * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, + * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + * See the Mulan PubL v2 for more details. + */ + +#ifndef OB_ADMIN_USEC_EXECUTOR_H_ +#define OB_ADMIN_USEC_EXECUTOR_H_ +#include "../ob_admin_executor.h" + +namespace oceanbase { +namespace tools { + +enum ObAdminUsecCmd { + TO_TIME, + MAX_CMD, +}; + +class ObAdminUsecExecutor : public ObAdminExecutor { +public: + ObAdminUsecExecutor(); + virtual ~ObAdminUsecExecutor() = default; + virtual int execute(int argc, char *argv[]); + void reset(); + +private: + int parse_cmd(int argc, char *argv[]); + void print_usage(); + +private: + ObAdminUsecCmd cmd_; + int64_t usec_; + common::ObString time_zone_; + ObTimeZoneInfo tz_info_; +}; +} // namespace tools +} // namespace oceanbase + +#endif /* OB_ADMIN_USEC_EXECUTOR_H_ */ -- GitLab