Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
empiredan committed Dec 18, 2024
1 parent 871b004 commit 6329ecb
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 22 deletions.
26 changes: 15 additions & 11 deletions src/replica/replica_stub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -486,11 +486,11 @@ std::vector<replica_stub::disk_replicas_info> replica_stub::get_all_disk_dirs()
// TaskCode: LPC_REPLICATION_INIT_LOAD
// ThreadPool: THREAD_POOL_LOCAL_APP
void replica_stub::load_replica(dir_node *dn,
const std::string &dir,
const std::string &replica_dir,
utils::ex_lock &reps_lock,
replicas &reps)
{
LOG_INFO("loading replica: tag={}, dir={}", dn->tag, dir);
LOG_INFO("loading replica: tag={}, replica_dir={}", dn->tag, replica_dir);

const auto *const worker = task::get_current_worker2();
if (worker != nullptr) {
Expand All @@ -500,17 +500,17 @@ void replica_stub::load_replica(dir_node *dn,
"among multiple threads");
}

auto rep = load_replica(dn, dir.c_str());
auto rep = load_replica(dn, replica_dir);
if (rep == nullptr) {
return;
}

LOG_INFO("{}@{}: load replica successfully, tag={}, dir={}, last_durable_decree={}, "
LOG_INFO("{}@{}: load replica successfully, tag={}, replica_dir={}, last_durable_decree={}, "
"last_committed_decree={}, last_prepared_decree={}",
rep->get_gpid(),
dsn_primary_host_port(),
dn->tag,
dir,
replica_dir,
rep->last_durable_decree(),
rep->last_committed_decree(),
rep->last_prepared_decree());
Expand Down Expand Up @@ -1972,7 +1972,7 @@ void replica_stub::open_replica(
_primary_host_port_cache,
group_check ? "with" : "without",
dir);
rep = load_replica(dn, dir.c_str());
rep = load_replica(dn, dir);

// if load data failed, re-open the `*.ori` folder which is the origin replica dir of disk
// migration
Expand All @@ -1997,7 +1997,7 @@ void replica_stub::open_replica(
boost::replace_first(
origin_dir, replica_disk_migrator::kReplicaDirOriginSuffix, "");
dsn::utils::filesystem::rename_path(origin_tmp_dir, origin_dir);
rep = load_replica(origin_dn, origin_dir.c_str());
rep = load_replica(origin_dn, origin_dir);

FAIL_POINT_INJECT_F("mock_replica_load", [&](std::string_view) -> void {});
}
Expand Down Expand Up @@ -2225,7 +2225,7 @@ bool replica_stub::validate_replica_dir(const std::string &dir,
return true;
}

replica_ptr replica_stub::load_replica(dir_node *dn, const char *replica_dir)
replica_ptr replica_stub::load_replica(dir_node *dn, const std::string &replica_dir)
{
FAIL_POINT_INJECT_F("mock_replica_load",
[&](std::string_view) -> replica * { return nullptr; });
Expand All @@ -2234,7 +2234,7 @@ replica_ptr replica_stub::load_replica(dir_node *dn, const char *replica_dir)
gpid pid;
std::string hint_message;
if (!validate_replica_dir(replica_dir, ai, pid, hint_message)) {
LOG_ERROR("invalid replica dir '{}', hint: {}", replica_dir, hint_message);
LOG_ERROR("invalid replica dir '{}', hint={}", replica_dir, hint_message);
return nullptr;
}

Expand All @@ -2244,7 +2244,11 @@ replica_ptr replica_stub::load_replica(dir_node *dn, const char *replica_dir)
auto *rep = new replica(this, pid, ai, dn, false);
const auto err = rep->initialize_on_load();
if (err != ERR_OK) {
LOG_ERROR("{}: load replica failed, err = {}", rep->name(), err);
LOG_ERROR("{}: load replica failed, tag={}, replica_dir={}, err={}",
rep->name(),
dn->tag,
replica_dir,
err);
delete rep;
rep = nullptr;

Expand All @@ -2258,7 +2262,7 @@ replica_ptr replica_stub::load_replica(dir_node *dn, const char *replica_dir)
return nullptr;
}

LOG_INFO("{}: load replica succeed", rep->name());
LOG_INFO("{}: load replica succeed, tag={}, replica_dir={}", rep->name(), dn->tag, replica_dir);
return rep;
}

Expand Down
12 changes: 7 additions & 5 deletions src/replica/replica_stub.h
Original file line number Diff line number Diff line change
Expand Up @@ -393,14 +393,16 @@ class replica_stub : public serverlet<replica_stub>, public ref_counter
parse_replica_dir_name(const std::string &dir_name, gpid &pid, std::string &app_type);

// Load an existing replica which is located in `dn` with `replica_dir`. Usually each
// different `dn` represents a unique disk. `dir` is the absolute path of the directory
// for a replica.
virtual replica_ptr load_replica(dir_node *dn, const char *replica_dir);
// different `dn` represents a unique disk. `replica_dir` is the absolute path of the
// directory for a replica.
virtual replica_ptr load_replica(dir_node *dn, const std::string &replica_dir);

// The same as the above `load_replica` function, except that this function is to load
// each replica to `reps` with protection from `reps_lock`.
void
load_replica(dir_node *dn, const std::string &dir, utils::ex_lock &reps_lock, replicas &reps);
void load_replica(dir_node *dn,
const std::string &replica_dir,
utils::ex_lock &reps_lock,
replicas &reps);

// Load all replicas simultaneously from all disks to `reps`.
void load_replicas(replicas &reps);
Expand Down
12 changes: 6 additions & 6 deletions src/replica/test/load_replicas_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,11 @@ class mock_load_replica : public replica_stub
}

private:
void load_replica_for_test(dir_node *dn, const char *dir, replica_ptr &rep)
void load_replica_for_test(dir_node *dn, const std::string &replica_dir, replica_ptr &rep)
{
ASSERT_TRUE(utils::filesystem::directory_exists(dir));
ASSERT_TRUE(utils::filesystem::directory_exists(replica_dir));

const auto &dir_name = get_replica_dir_name(dir);
const auto &dir_name = get_replica_dir_name(replica_dir);

gpid pid;
std::string app_type;
Expand Down Expand Up @@ -165,7 +165,7 @@ class mock_load_replica : public replica_stub
}

// Check the absolute dir of this replica.
ASSERT_EQ(dn->replica_dir("pegasus", pid), dir);
ASSERT_EQ(dn->replica_dir("pegasus", pid), replica_dir);

app_info ai;
ai.app_type = "pegasus";
Expand All @@ -180,10 +180,10 @@ class mock_load_replica : public replica_stub
}

// Mock the process of loading a replica.
replica_ptr load_replica(dir_node *dn, const char *dir) override
replica_ptr load_replica(dir_node *dn, const std::string &replica_dir) override
{
replica_ptr rep;
load_replica_for_test(dn, dir, rep);
load_replica_for_test(dn, replica_dir, rep);
return rep;
}

Expand Down

0 comments on commit 6329ecb

Please sign in to comment.