From 703bee238db9dbee5be394e8e263982698b9505a Mon Sep 17 00:00:00 2001 From: Yingchun Lai Date: Fri, 8 Dec 2023 15:41:34 +0800 Subject: [PATCH] 4 --- src/shell/commands/debugger.cpp | 10 +++---- src/shell/main.cpp | 3 ++- src/tools/mutation_log_tool.cpp | 46 +++++++++++++++++++-------------- 3 files changed, 34 insertions(+), 25 deletions(-) diff --git a/src/shell/commands/debugger.cpp b/src/shell/commands/debugger.cpp index bbab395900f..45f3f522e41 100644 --- a/src/shell/commands/debugger.cpp +++ b/src/shell/commands/debugger.cpp @@ -281,9 +281,9 @@ bool local_get(command_executor *e, shell_context *sc, arguments args) dsn::blob user_data; pegasus::pegasus_extract_user_data(0, std::move(value), user_data); fmt::print(stderr, - "{} : \"{}\"\n", - expire_ts, - pegasus::utils::c_escape_string(user_data, sc->escape_all)); + "{} : \"{}\"\n", + expire_ts, + pegasus::utils::c_escape_string(user_data, sc->escape_all)); } delete db; @@ -335,7 +335,7 @@ bool rdb_value_hex2str(command_executor *e, shell_context *sc, arguments args) dsn::blob user_data; pegasus::pegasus_extract_user_data(0, std::move(pegasus_value), user_data); fmt::print(stderr, - "user_data:\n \"{}\"\n", - pegasus::utils::c_escape_string(user_data.to_string(), sc->escape_all)); + "user_data:\n \"{}\"\n", + pegasus::utils::c_escape_string(user_data.to_string(), sc->escape_all)); return true; } diff --git a/src/shell/main.cpp b/src/shell/main.cpp index e7836a5f9c9..75a52abc63b 100644 --- a/src/shell/main.cpp +++ b/src/shell/main.cpp @@ -392,7 +392,8 @@ static command_executor commands[] = { { "mlog_dump", "dump mutation log dir", - "<-i|--input log_dir(e.g. '/path/to/replica/reps/2.1.pegasus/plog/')> [-o|--output file_name] [-d|--detailed]", + "<-i|--input log_dir(e.g. '/path/to/replica/reps/2.1.pegasus/plog/')> [-o|--output " + "file_name] [-d|--detailed]", mlog_dump, }, { diff --git a/src/tools/mutation_log_tool.cpp b/src/tools/mutation_log_tool.cpp index 74f2a5bea34..72ac90d8955 100644 --- a/src/tools/mutation_log_tool.cpp +++ b/src/tools/mutation_log_tool.cpp @@ -41,6 +41,7 @@ #include "runtime/task/task_spec.h" #include "utils/autoref_ptr.h" #include "utils/blob.h" +#include "utils/defer.h" #include "utils/error_code.h" #include "utils/filesystem.h" #include "utils/flags.h" @@ -60,38 +61,46 @@ bool mutation_log_tool::dump( { std::string absolute_path; if (!utils::filesystem::get_absolute_path(log_dir, absolute_path)) { - output << "ERROR: get absolute path failed" << std::endl; + output << fmt::format("ERROR: get absolute path failed\n"); return false; } std::string norm_path; utils::filesystem::get_normalized_path(absolute_path, norm_path); - auto dn = std::make_shared("", norm_path); + auto dn = std::make_unique(/* tag_ */ "", norm_path); app_info ai; ai.__set_app_type("pegasus"); - auto stub = std::make_shared(); - auto *rep = new replica(stub.get(), pid, ai, dn.get(), false, false); + auto stub = std::make_unique(); + // Constructor of replica is private which can not be accessed by std::make_unique, so use raw + // pointer here. + auto *rep = new replica(stub.get(), + pid, + ai, + dn.get(), + /* need_restore */ false, + /* is_duplication_follower */ false); + auto cleanup = dsn::defer([rep]() { delete rep; }); auto mlog = std::make_shared(log_dir, FLAGS_log_private_file_size_mb, pid, rep); error_code err = mlog->open( [mlog, &output, callback](int log_length, mutation_ptr &mu) -> bool { - std::cout << "1" << std::endl; if (mlog->max_decree(mu->data.header.pid) == 0) { mlog->set_valid_start_offset_on_open(mu->data.header.pid, 0); } char timestamp_buf[32] = {0}; utils::time_ms_to_string(mu->data.header.timestamp / 1000, timestamp_buf); - output << "mutation [" << mu->name() << "]: " - << "gpid=" << mu->data.header.pid.get_app_id() << "." - << mu->data.header.pid.get_partition_index() << ", " - << "ballot=" << mu->data.header.ballot << ", decree=" << mu->data.header.decree - << ", " - << "timestamp=" << timestamp_buf - << ", last_committed_decree=" << mu->data.header.last_committed_decree << ", " - << "log_offset=" << mu->data.header.log_offset << ", log_length=" << log_length - << ", " - << "update_count=" << mu->data.updates.size(); - if (callback && mu->data.updates.size() > 0) { - + output << fmt::format("mutation [{}]: gpid={}, ballot={}, decree={}, timestamp={}, " + "last_committed_decree={}, log_offset={}, log_length={}, " + "update_count={}\n", + mu->name(), + mu->data.header.pid, + mu->data.header.ballot, + mu->data.header.decree, + timestamp_buf, + mu->data.header.last_committed_decree, + mu->data.header.log_offset, + log_length, + mu->data.updates.size()); + if (callback && !mu->data.updates.empty()) { dsn::message_ex **batched_requests = (dsn::message_ex **)alloca(sizeof(dsn::message_ex *) * mu->data.updates.size()); int batched_count = 0; @@ -115,9 +124,8 @@ bool mutation_log_tool::dump( }, nullptr); mlog->close(); - delete rep; if (err != dsn::ERR_OK) { - output << "ERROR: dump mutation log failed, err = " << err.to_string() << std::endl; + output << fmt::format("ERROR: dump mutation log failed, err = {}\n", err); return false; } else { return true;