[opensource] add usec_tool
This commit is contained in:
parent
751ce2ac1b
commit
2fd258cc46
@ -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)
|
||||
|
@ -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<char*>(ss, " "));
|
||||
copy(argv, argv + argc, std::ostream_iterator<char *>(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();
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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
|
||||
|
117
tools/ob_admin/usec_tool/ob_admin_usec_executor.cpp
Normal file
117
tools/ob_admin/usec_tool/ob_admin_usec_executor.cpp
Normal file
@ -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<int32_t>(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<int64_t>(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
|
45
tools/ob_admin/usec_tool/ob_admin_usec_executor.h
Normal file
45
tools/ob_admin/usec_tool/ob_admin_usec_executor.h
Normal file
@ -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_ */
|
Loading…
x
Reference in New Issue
Block a user