From 4bb8d464aa324cc71c97d4de8afa3beea963b3e6 Mon Sep 17 00:00:00 2001 From: yujingwei Date: Fri, 1 Dec 2023 17:34:18 +0800 Subject: [PATCH] feat(replica): parameterize-fileddatatype --- src/replica/replica_disk_migrator.cpp | 3 +- src/replica/replication_app_base.cpp | 50 ++++++++++++++++----------- src/replica/replication_app_base.h | 3 +- 3 files changed, 34 insertions(+), 22 deletions(-) diff --git a/src/replica/replica_disk_migrator.cpp b/src/replica/replica_disk_migrator.cpp index c7f7423bb8..7767a8562f 100644 --- a/src/replica/replica_disk_migrator.cpp +++ b/src/replica/replica_disk_migrator.cpp @@ -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, diff --git a/src/replica/replication_app_base.cpp b/src/replica/replication_app_base.cpp index 231450d7c5..8e6b053716 100644 --- a/src/replica/replication_app_base.cpp +++ b/src/replica/replication_app_base.cpp @@ -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, @@ -88,7 +89,8 @@ 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, @@ -96,13 +98,16 @@ error_code replica_init_info::load(const std::string &dir, const dsn::utils::Fil 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); @@ -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::decode( @@ -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::encode(*this), fileDataType); + return write_blob_to_file( + fname, json::json_forwarder::encode(*this), fileDataType); } std::string replica_init_info::to_string() @@ -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; @@ -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; @@ -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; } diff --git a/src/replica/replication_app_base.h b/src/replica/replication_app_base.h index 7a30c08840..a87d6754e8 100644 --- a/src/replica/replication_app_base.h +++ b/src/replica/replication_app_base.h @@ -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();