Skip to content

Add file-based persistent log store and state manager#635

Open
bihari123 wants to merge 1 commit intoeBay:masterfrom
bihari123:feature/file-based-persistent-storage
Open

Add file-based persistent log store and state manager#635
bihari123 wants to merge 1 commit intoeBay:masterfrom
bihari123:feature/file-based-persistent-storage

Conversation

@bihari123
Copy link

  • Add file_based_log_store: persists Raft log entries to disk
  • Add file_based_state_mgr: persists cluster config and server state
  • Add --persistent flag to calculator example
  • Add --persistent-dir= option for custom storage location
  • Update example_common.hxx to support persistent storage

This provides a minimal example of durable storage for NuRaft,
addressing the gap between in-memory examples and production use. This comes after the discussion in the PR: #632

  - Add file_based_log_store: persists Raft log entries to disk
  - Add file_based_state_mgr: persists cluster config and server state
  - Add --persistent flag to calculator example
  - Add --persistent-dir=<dir> option for custom storage location
  - Update example_common.hxx to support persistent storage

  This provides a minimal example of durable storage for NuRaft,
  addressing the gap between in-memory examples and production use.
Copy link
Contributor

@greensky00 greensky00 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for submitting the PR. I've left a few comments as a first round review. Please take a look.

Comment on lines +24 to +38
// Global flag to enable persistent storage.
// If true, file-based storage will be used instead of in-memory.
static bool USE_PERSISTENT_STORAGE = false;

// Directory for persistent storage.
static std::string PERSISTENT_STORAGE_DIR = "./nuraft_data";

// Helper to set persistent storage options.
inline void set_persistent_storage(bool enable, const std::string& dir = "./nuraft_data") {
USE_PERSISTENT_STORAGE = enable;
if (enable) {
PERSISTENT_STORAGE_DIR = dir;
}
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are used by calc_server only, not common features. Need to move it to calc_server.cxx.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+)
After moving them to calc_server, set default dir path to ./calc_server_data_<server_id>.

Comment on lines +173 to +183
// State manager: use persistent or in-memory based on flag.
if (USE_PERSISTENT_STORAGE) {
std::cout << "using persistent storage: " << PERSISTENT_STORAGE_DIR << std::endl;
stuff.smgr_ = cs_new<file_based_state_mgr>( stuff.server_id_,
stuff.endpoint_,
PERSISTENT_STORAGE_DIR );
} else {
std::cout << "using in-memory storage" << std::endl;
stuff.smgr_ = cs_new<inmem_state_mgr>( stuff.server_id_,
stuff.endpoint_ );
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above, I suggest modifying init_raft function to take an additional parameter ptr<state_mgr>, which is inmem_state_mgr by default. Only calc_server would pass file-based state mgr.

logger.cc
in_memory_log_store.cxx)
in_memory_log_store.cxx
file_based_log_store.cxx)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

echo_server will not use it, after it is removed from example_common.hxx.

logger.cc
in_memory_log_store.cxx)
in_memory_log_store.cxx
file_based_log_store.cxx)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.

Comment on lines +143 to +147
/**
* In-memory cache of log entries for faster access.
* Map of <log index, log data>.
*/
std::map<ulong, ptr<log_entry>> logs_;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cache is not part of a "minimal implementation" and is unnecessary for this example. The current implementation in this PR combines the existing in-memory log store with file-backed storage, which is likely to confuse readers. Please remove the cache and directly read/write from/to files.

Comment on lines +51 to +52
std::cout << "[TRACE][file_based_state_mgr_ctor] Creating state manager for server "
<< srv_id << " at " << endpoint << ", state dir: " << state_dir << std::endl;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please get rid of these stdout messages, or pass stuff.raft_logger_ here and use it instead.

@bihari123
Copy link
Author

thanks for the review. I'll do these changes on the weekend.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants