Add stderr log level for ldb backup commands

Summary:
Also extracted the common logic into a base class, BackupableCommand.
Closes https://github.com/facebook/rocksdb/pull/1939

Differential Revision: D4630121

Pulled By: ajkr

fbshipit-source-id: 04bb067
main
Andrew Kryczka 8 years ago committed by Facebook Github Bot
parent 4561275c2d
commit 2a5daa06f0
  1. 106
      tools/ldb_cmd.cc
  2. 48
      tools/ldb_cmd_impl.h

@ -2588,43 +2588,71 @@ void RepairCommand::DoCommand() {
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
const std::string BackupCommand::ARG_THREAD = "num_threads"; const std::string BackupableCommand::ARG_NUM_THREADS = "num_threads";
const std::string BackupCommand::ARG_BACKUP_ENV = "backup_env_uri"; const std::string BackupableCommand::ARG_BACKUP_ENV_URI = "backup_env_uri";
const std::string BackupCommand::ARG_BACKUP_DIR = "backup_dir"; const std::string BackupableCommand::ARG_BACKUP_DIR = "backup_dir";
const std::string BackupableCommand::ARG_STDERR_LOG_LEVEL = "stderr_log_level";
BackupCommand::BackupCommand(const std::vector<std::string>& params, BackupableCommand::BackupableCommand(
const std::map<std::string, std::string>& options, const std::vector<std::string>& params,
const std::vector<std::string>& flags) const std::map<std::string, std::string>& options,
: LDBCommand( const std::vector<std::string>& flags)
options, flags, false, : LDBCommand(options, flags, false /* is_read_only */,
BuildCmdLineOptions({ARG_BACKUP_ENV, ARG_BACKUP_DIR, ARG_THREAD})), BuildCmdLineOptions({ARG_BACKUP_ENV_URI, ARG_BACKUP_DIR,
thread_num_(1) { ARG_NUM_THREADS, ARG_STDERR_LOG_LEVEL})),
auto itr = options.find(ARG_THREAD); num_threads_(1) {
auto itr = options.find(ARG_NUM_THREADS);
if (itr != options.end()) { if (itr != options.end()) {
thread_num_ = std::stoi(itr->second); num_threads_ = std::stoi(itr->second);
} }
itr = options.find(ARG_BACKUP_ENV); itr = options.find(ARG_BACKUP_ENV_URI);
if (itr != options.end()) { if (itr != options.end()) {
test_cluster_ = itr->second; backup_env_uri_ = itr->second;
} }
itr = options.find(ARG_BACKUP_DIR); itr = options.find(ARG_BACKUP_DIR);
if (itr == options.end()) { if (itr == options.end()) {
exec_state_ = LDBCommandExecuteResult::Failed("--" + ARG_BACKUP_DIR + exec_state_ = LDBCommandExecuteResult::Failed("--" + ARG_BACKUP_DIR +
": missing backup directory"); ": missing backup directory");
} else { } else {
test_path_ = itr->second; backup_dir_ = itr->second;
}
itr = options.find(ARG_STDERR_LOG_LEVEL);
if (itr != options.end()) {
int stderr_log_level = std::stoi(itr->second);
if (stderr_log_level < 0 ||
stderr_log_level >= InfoLogLevel::NUM_INFO_LOG_LEVELS) {
exec_state_ = LDBCommandExecuteResult::Failed(
ARG_STDERR_LOG_LEVEL + " must be >= 0 and < " +
std::to_string(InfoLogLevel::NUM_INFO_LOG_LEVELS) + ".");
} else {
logger_.reset(
new StderrLogger(static_cast<InfoLogLevel>(stderr_log_level)));
}
} }
} }
void BackupCommand::Help(std::string& ret) { void BackupableCommand::Help(const std::string& name, std::string& ret) {
ret.append(" "); ret.append(" ");
ret.append(BackupCommand::Name()); ret.append(name);
ret.append(" [--" + ARG_BACKUP_ENV + "] "); ret.append(" [--" + ARG_BACKUP_ENV_URI + "] ");
ret.append(" [--" + ARG_BACKUP_DIR + "] "); ret.append(" [--" + ARG_BACKUP_DIR + "] ");
ret.append(" [--" + ARG_THREAD + "] "); ret.append(" [--" + ARG_NUM_THREADS + "] ");
ret.append(" [--" + ARG_STDERR_LOG_LEVEL + "=<int (InfoLogLevel)>] ");
ret.append("\n"); ret.append("\n");
} }
// ----------------------------------------------------------------------------
BackupCommand::BackupCommand(const std::vector<std::string>& params,
const std::map<std::string, std::string>& options,
const std::vector<std::string>& flags)
: BackupableCommand(params, options, flags) {}
void BackupCommand::Help(std::string& ret) {
BackupableCommand::Help(Name(), ret);
}
void BackupCommand::DoCommand() { void BackupCommand::DoCommand() {
BackupEngine* backup_engine; BackupEngine* backup_engine;
Status status; Status status;
@ -2634,10 +2662,11 @@ void BackupCommand::DoCommand() {
} }
printf("open db OK\n"); printf("open db OK\n");
std::unique_ptr<Env> custom_env_guard; std::unique_ptr<Env> custom_env_guard;
Env* custom_env = NewCustomObject<Env>(test_cluster_, &custom_env_guard); Env* custom_env = NewCustomObject<Env>(backup_env_uri_, &custom_env_guard);
BackupableDBOptions backup_options = BackupableDBOptions backup_options =
BackupableDBOptions(test_path_, custom_env); BackupableDBOptions(backup_dir_, custom_env);
backup_options.max_background_operations = thread_num_; backup_options.info_log = logger_.get();
backup_options.max_background_operations = num_threads_;
status = BackupEngine::Open(Env::Default(), backup_options, &backup_engine); status = BackupEngine::Open(Env::Default(), backup_options, &backup_engine);
if (status.ok()) { if (status.ok()) {
printf("open backup engine OK\n"); printf("open backup engine OK\n");
@ -2656,42 +2685,14 @@ void BackupCommand::DoCommand() {
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
const std::string RestoreCommand::ARG_BACKUP_ENV_URI = "backup_env_uri";
const std::string RestoreCommand::ARG_BACKUP_DIR = "backup_dir";
const std::string RestoreCommand::ARG_NUM_THREADS = "num_threads";
RestoreCommand::RestoreCommand( RestoreCommand::RestoreCommand(
const std::vector<std::string>& params, const std::vector<std::string>& params,
const std::map<std::string, std::string>& options, const std::map<std::string, std::string>& options,
const std::vector<std::string>& flags) const std::vector<std::string>& flags)
: LDBCommand(options, flags, false /* is_read_only */, : BackupableCommand(params, options, flags) {}
BuildCmdLineOptions(
{ARG_BACKUP_ENV_URI, ARG_BACKUP_DIR, ARG_NUM_THREADS})),
num_threads_(1) {
auto itr = options.find(ARG_NUM_THREADS);
if (itr != options.end()) {
num_threads_ = std::stoi(itr->second);
}
itr = options.find(ARG_BACKUP_ENV_URI);
if (itr != options.end()) {
backup_env_uri_ = itr->second;
}
itr = options.find(ARG_BACKUP_DIR);
if (itr == options.end()) {
exec_state_ = LDBCommandExecuteResult::Failed("--" + ARG_BACKUP_DIR +
": missing backup directory");
} else {
backup_dir_ = itr->second;
}
}
void RestoreCommand::Help(std::string& ret) { void RestoreCommand::Help(std::string& ret) {
ret.append(" "); BackupableCommand::Help(Name(), ret);
ret.append(RestoreCommand::Name());
ret.append(" [--" + ARG_BACKUP_ENV_URI + "] ");
ret.append(" [--" + ARG_BACKUP_DIR + "] ");
ret.append(" [--" + ARG_NUM_THREADS + "] ");
ret.append("\n");
} }
void RestoreCommand::DoCommand() { void RestoreCommand::DoCommand() {
@ -2701,6 +2702,7 @@ void RestoreCommand::DoCommand() {
Status status; Status status;
{ {
BackupableDBOptions opts(backup_dir_, custom_env); BackupableDBOptions opts(backup_dir_, custom_env);
opts.info_log = logger_.get();
opts.max_background_operations = num_threads_; opts.max_background_operations = num_threads_;
BackupEngineReadOnly* raw_restore_engine_ptr; BackupEngineReadOnly* raw_restore_engine_ptr;
status = BackupEngineReadOnly::Open(Env::Default(), opts, status = BackupEngineReadOnly::Open(Env::Default(), opts,

@ -463,48 +463,44 @@ class RepairCommand : public LDBCommand {
static void Help(std::string& ret); static void Help(std::string& ret);
}; };
class BackupCommand : public LDBCommand { class BackupableCommand : public LDBCommand {
public: public:
static std::string Name() { return "backup"; } BackupableCommand(const std::vector<std::string>& params,
const std::map<std::string, std::string>& options,
const std::vector<std::string>& flags);
protected:
static void Help(const std::string& name, std::string& ret);
std::string backup_env_uri_;
std::string backup_dir_;
int num_threads_;
std::unique_ptr<Logger> logger_;
private:
static const std::string ARG_BACKUP_DIR;
static const std::string ARG_BACKUP_ENV_URI;
static const std::string ARG_NUM_THREADS;
static const std::string ARG_STDERR_LOG_LEVEL;
};
class BackupCommand : public BackupableCommand {
public:
static std::string Name() { return "backup"; }
BackupCommand(const std::vector<std::string>& params, BackupCommand(const std::vector<std::string>& params,
const std::map<std::string, std::string>& options, const std::map<std::string, std::string>& options,
const std::vector<std::string>& flags); const std::vector<std::string>& flags);
virtual void DoCommand() override; virtual void DoCommand() override;
static void Help(std::string& ret); static void Help(std::string& ret);
private:
std::string test_cluster_;
std::string test_path_;
int thread_num_;
static const std::string ARG_BACKUP_DIR;
static const std::string ARG_BACKUP_ENV;
static const std::string ARG_THREAD;
}; };
class RestoreCommand : public LDBCommand { class RestoreCommand : public BackupableCommand {
public: public:
static std::string Name() { return "restore"; } static std::string Name() { return "restore"; }
RestoreCommand(const std::vector<std::string>& params, RestoreCommand(const std::vector<std::string>& params,
const std::map<std::string, std::string>& options, const std::map<std::string, std::string>& options,
const std::vector<std::string>& flags); const std::vector<std::string>& flags);
virtual void DoCommand() override; virtual void DoCommand() override;
virtual bool NoDBOpen() override { return true; } virtual bool NoDBOpen() override { return true; }
static void Help(std::string& ret); static void Help(std::string& ret);
private:
std::string backup_env_uri_;
std::string backup_dir_;
int num_threads_;
static const std::string ARG_BACKUP_DIR;
static const std::string ARG_BACKUP_ENV_URI;
static const std::string ARG_NUM_THREADS;
}; };
} // namespace rocksdb } // namespace rocksdb

Loading…
Cancel
Save