Skip to content

Commit

Permalink
feat(replica): parameterize-fileddatatype
Browse files Browse the repository at this point in the history
  • Loading branch information
Samunroyu committed Dec 1, 2023
1 parent a049bd3 commit 4bb8d46
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 22 deletions.
3 changes: 2 additions & 1 deletion src/replica/replica_disk_migrator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,8 @@ bool replica_disk_migrator::migrate_replica_app_info(const replica_disk_migrate_
return false;
});
replica_init_info init_info = _replica->get_app()->init_info();
const auto &store_init_info_err = init_info.store(_target_replica_dir, dsn::utils::FileDataType::kSensitive);
const auto &store_init_info_err =
init_info.store(_target_replica_dir, dsn::utils::FileDataType::kSensitive);
if (store_init_info_err != ERR_OK) {
LOG_ERROR_PREFIX("disk migration(origin={}, target={}) stores app init info failed({})",
req.origin_disk,
Expand Down
50 changes: 30 additions & 20 deletions src/replica/replication_app_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,17 @@ namespace replication {
const std::string replica_init_info::kInitInfo = ".init-info";

namespace {
error_code write_blob_to_file(const std::string &fname, const blob &data, const dsn::utils::FileDataType &fileDataType)
error_code write_blob_to_file(const std::string &fname,
const blob &data,
const dsn::utils::FileDataType &fileDataType)
{
// TODO(yingchun): consider not encrypt the meta files.
std::string tmp_fname = fname + ".tmp";
auto cleanup = defer([tmp_fname]() { utils::filesystem::remove_path(tmp_fname); });
auto s =
rocksdb::WriteStringToFile(dsn::utils::PegasusEnv(fileDataType),
rocksdb::Slice(data.data(), data.length()),
tmp_fname,
/* should_sync */ true);
auto s = rocksdb::WriteStringToFile(dsn::utils::PegasusEnv(fileDataType),
rocksdb::Slice(data.data(), data.length()),
tmp_fname,
/* should_sync */ true);
LOG_AND_RETURN_NOT_TRUE(
ERROR, s.ok(), ERR_FILE_OPERATION_FAILED, "write file {} failed", tmp_fname);
LOG_AND_RETURN_NOT_TRUE(ERROR,
Expand All @@ -88,21 +89,25 @@ error_code write_blob_to_file(const std::string &fname, const blob &data, const
}
} // namespace

error_code replica_init_info::load(const std::string &dir, const dsn::utils::FileDataType &fileDataType)
error_code replica_init_info::load(const std::string &dir,
const dsn::utils::FileDataType &fileDataType)
{
std::string info_path = utils::filesystem::path_combine(dir, kInitInfo);
LOG_AND_RETURN_NOT_TRUE(ERROR,
utils::filesystem::path_exists(info_path),
ERR_PATH_NOT_FOUND,
"file({}) not exist",
info_path);
LOG_AND_RETURN_NOT_OK(
ERROR, load_json(info_path, fileDataType), "load replica_init_info from {} failed", info_path);
LOG_AND_RETURN_NOT_OK(ERROR,
load_json(info_path, fileDataType),
"load replica_init_info from {} failed",
info_path);
LOG_INFO("load replica_init_info from {} succeed: {}", info_path, to_string());
return ERR_OK;
}

error_code replica_init_info::store(const std::string &dir, const dsn::utils::FileDataType &fileDataType)
error_code replica_init_info::store(const std::string &dir,
const dsn::utils::FileDataType &fileDataType)
{
uint64_t start = dsn_now_ns();
std::string info_path = utils::filesystem::path_combine(dir, kInitInfo);
Expand All @@ -118,11 +123,11 @@ error_code replica_init_info::store(const std::string &dir, const dsn::utils::Fi
return ERR_OK;
}

error_code replica_init_info::load_json(const std::string &fname, const dsn::utils::FileDataType &fileDataType)
error_code replica_init_info::load_json(const std::string &fname,
const dsn::utils::FileDataType &fileDataType)
{
std::string data;
auto s = rocksdb::ReadFileToString(
dsn::utils::PegasusEnv(fileDataType), fname, &data);
auto s = rocksdb::ReadFileToString(dsn::utils::PegasusEnv(fileDataType), fname, &data);
LOG_AND_RETURN_NOT_TRUE(ERROR, s.ok(), ERR_FILE_OPERATION_FAILED, "read file {} failed", fname);
LOG_AND_RETURN_NOT_TRUE(ERROR,
json::json_forwarder<replica_init_info>::decode(
Expand All @@ -133,9 +138,11 @@ error_code replica_init_info::load_json(const std::string &fname, const dsn::uti
return ERR_OK;
}

error_code replica_init_info::store_json(const std::string &fname, const dsn::utils::FileDataType &fileDataType)
error_code replica_init_info::store_json(const std::string &fname,
const dsn::utils::FileDataType &fileDataType)
{
return write_blob_to_file(fname, json::json_forwarder<replica_init_info>::encode(*this), fileDataType);
return write_blob_to_file(
fname, json::json_forwarder<replica_init_info>::encode(*this), fileDataType);
}

std::string replica_init_info::to_string()
Expand All @@ -148,11 +155,11 @@ std::string replica_init_info::to_string()
return oss.str();
}

error_code replica_app_info::load(const std::string &fname, const dsn::utils::FileDataType &fileDataType)
error_code replica_app_info::load(const std::string &fname,
const dsn::utils::FileDataType &fileDataType)
{
std::string data;
auto s = rocksdb::ReadFileToString(
dsn::utils::PegasusEnv(fileDataType), fname, &data);
auto s = rocksdb::ReadFileToString(dsn::utils::PegasusEnv(fileDataType), fname, &data);
LOG_AND_RETURN_NOT_TRUE(ERROR, s.ok(), ERR_FILE_OPERATION_FAILED, "read file {} failed", fname);
binary_reader reader(blob::create_from_bytes(std::move(data)));
int magic = 0;
Expand All @@ -163,7 +170,8 @@ error_code replica_app_info::load(const std::string &fname, const dsn::utils::Fi
return ERR_OK;
}

error_code replica_app_info::store(const std::string &fname, const dsn::utils::FileDataType &fileDataType)
error_code replica_app_info::store(const std::string &fname,
const dsn::utils::FileDataType &fileDataType)
{
binary_writer writer;
int magic = 0xdeadbeef;
Expand Down Expand Up @@ -458,7 +466,9 @@ error_code replication_app_base::update_init_info(replica *r,
_info.init_offset_in_shared_log = shared_log_offset;
_info.init_offset_in_private_log = private_log_offset;

LOG_AND_RETURN_NOT_OK(ERROR_PREFIX, _info.store(r->dir(), dsn::utils::FileDataType::kSensitive), "store replica_init_info failed");
LOG_AND_RETURN_NOT_OK(ERROR_PREFIX,
_info.store(r->dir(), dsn::utils::FileDataType::kSensitive),
"store replica_init_info failed");

return ERR_OK;
}
Expand Down
3 changes: 2 additions & 1 deletion src/replica/replication_app_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ class replica_init_info

public:
replica_init_info() { memset((void *)this, 0, sizeof(*this)); }
error_code load(const std::string &dir, const dsn::utils::FileDataType &fileDataType) WARN_UNUSED_RESULT;
error_code load(const std::string &dir,
const dsn::utils::FileDataType &fileDataType) WARN_UNUSED_RESULT;
error_code store(const std::string &dir, const dsn::utils::FileDataType &fileDataType);
std::string to_string();

Expand Down

0 comments on commit 4bb8d46

Please sign in to comment.