Skip to content

Commit

Permalink
feat(replica): parameterize-fileddatatype
Browse files Browse the repository at this point in the history
  • Loading branch information
yujingwei committed Nov 29, 2023
1 parent 2ff8c02 commit 665b3cb
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/replica/replica_disk_migrator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ 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);
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
28 changes: 14 additions & 14 deletions src/replica/replication_app_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ 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)
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(dsn::utils::FileDataType::kSensitive),
rocksdb::WriteStringToFile(dsn::utils::PegasusEnv(fileDataType),
rocksdb::Slice(data.data(), data.length()),
tmp_fname,
/* should_sync */ true);
Expand All @@ -89,7 +89,7 @@ error_code write_blob_to_file(const std::string &fname, const blob &data)
}
} // namespace

error_code replica_init_info::load(const std::string &dir)
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,
Expand All @@ -98,17 +98,17 @@ error_code replica_init_info::load(const std::string &dir)
"file({}) not exist",
info_path);
LOG_AND_RETURN_NOT_OK(
ERROR, load_json(info_path), "load replica_init_info from {} failed", info_path);
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)
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);
LOG_AND_RETURN_NOT_OK(ERROR,
store_json(info_path),
store_json(info_path, fileDataType),
"store replica_init_info to {} failed, time_used_ns = {}",
info_path,
dsn_now_ns() - start);
Expand All @@ -119,11 +119,11 @@ error_code replica_init_info::store(const std::string &dir)
return ERR_OK;
}

error_code replica_init_info::load_json(const std::string &fname)
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(dsn::utils::FileDataType::kSensitive), fname, &data);
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 @@ -134,9 +134,9 @@ error_code replica_init_info::load_json(const std::string &fname)
return ERR_OK;
}

error_code replica_init_info::store_json(const std::string &fname)
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));
return write_blob_to_file(fname, json::json_forwarder<replica_init_info>::encode(*this), fileDataType);
}

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

error_code replica_app_info::load(const std::string &fname)
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(dsn::utils::FileDataType::kSensitive), fname, &data);
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 @@ -164,7 +164,7 @@ error_code replica_app_info::load(const std::string &fname)
return ERR_OK;
}

error_code replica_app_info::store(const std::string &fname)
error_code replica_app_info::store(const std::string &fname, const dsn::utils::FileDataType fileDataType)
{
binary_writer writer;
int magic = 0xdeadbeef;
Expand All @@ -184,7 +184,7 @@ error_code replica_app_info::store(const std::string &fname)
marshall(writer, tmp, DSF_THRIFT_JSON);
}

return write_blob_to_file(fname, writer.get_buffer());
return write_blob_to_file(fname, writer.get_buffer(), fileDataType);
}

/*static*/
Expand Down
12 changes: 6 additions & 6 deletions src/replica/replication_app_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ class replica_init_info

public:
replica_init_info() { memset((void *)this, 0, sizeof(*this)); }
error_code load(const std::string &dir) WARN_UNUSED_RESULT;
error_code store(const std::string &dir);
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();

private:
error_code load_json(const std::string &fname);
error_code store_json(const std::string &fname);
error_code load_json(const std::string &fname, const dsn::utils::FileDataType fileDataType);
error_code store_json(const std::string &fname, const dsn::utils::FileDataType fileDataType);
};

class replica_app_info
Expand All @@ -87,8 +87,8 @@ class replica_app_info

public:
replica_app_info(app_info *app) { _app = app; }
error_code load(const std::string &fname);
error_code store(const std::string &fname);
error_code load(const std::string &fname, const dsn::utils::FileDataType fileDataType);
error_code store(const std::string &fname, const dsn::utils::FileDataType fileDataType);
};

/// The store engine interface of Pegasus.
Expand Down
4 changes: 2 additions & 2 deletions src/replica/test/replica_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ class replica_test : public replica_test_base
std::cout << "the path of .app-info file is " << path << std::endl;

// load new max_replica_count from file
auto err = replica_info.load(path);
auto err = replica_info.load(path, dsn::utils::FileDataType::kSensitive);
ASSERT_EQ(ERR_OK, err);
ASSERT_EQ(info, _mock_replica->_app_info);
std::cout << "the loaded new app_info is " << info << std::endl;
Expand All @@ -249,7 +249,7 @@ class replica_test : public replica_test_base
_app_info.max_replica_count = reserved_max_replica_count;

// load original max_replica_count from file
err = replica_info.load(path);
err = replica_info.load(path, dsn::utils::FileDataType::kSensitive);
ASSERT_EQ(err, ERR_OK);
ASSERT_EQ(info, _mock_replica->_app_info);
std::cout << "the loaded original app_info is " << info << std::endl;
Expand Down

0 comments on commit 665b3cb

Please sign in to comment.