Skip to content

Commit

Permalink
Add function to support purging existing logs. (#643)
Browse files Browse the repository at this point in the history
This change is necessary for baseline resync and can be called by the upper layer to purge existing logs, which resolves the following issue:
If a follower restarts during baseline resync, it will replay the remaining logs first.
However, shard info has already been cleared at the beginning of resync (from the HO side),
making it impossible to retrieve shard info while replaying logs, which results in errors.

Co-authored-by: yawzhang <[email protected]>
  • Loading branch information
Besroy and yawzhang authored Feb 12, 2025
1 parent 3a34040 commit fb6fd08
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/include/homestore/replication/repl_dev.h
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,9 @@ class ReplDev {
/// @return true if ready, false otherwise
virtual bool is_ready_for_traffic() const = 0;

/// @brief Clean up resources on this repl dev.
virtual void purge() = 0;

virtual void attach_listener(shared< ReplDevListener > listener) { m_listener = std::move(listener); }

virtual void detach_listener() {
Expand Down
7 changes: 7 additions & 0 deletions src/lib/replication/log_store/home_raft_log_store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,13 @@ ulong HomeRaftLogStore::last_durable_index() {
return to_repl_lsn(m_last_durable_lsn);
}

void HomeRaftLogStore::purge_all_logs() {
auto last_lsn = m_log_store->get_contiguous_issued_seq_num(m_last_durable_lsn);
REPL_STORE_LOG(INFO, "Store={} LogDev={}: Purging all logs in the log store, last_lsn={}",
m_logstore_id, m_logdev_id, last_lsn);
m_log_store->truncate(last_lsn, false /* in_memory_truncate_only */);
}

void HomeRaftLogStore::wait_for_log_store_ready() { m_log_store_future.wait(); }

void HomeRaftLogStore::set_last_durable_lsn(repl_lsn_t lsn) { m_last_durable_lsn = to_store_lsn(lsn); }
Expand Down
6 changes: 6 additions & 0 deletions src/lib/replication/log_store/home_raft_log_store.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,12 @@ class HomeRaftLogStore : public nuraft::log_store {
void truncate(uint32_t num_reserved_cnt, repl_lsn_t compact_lsn);
#endif

/**
* Purge all logs in the log store
* It is a dangerous operation and is only used in baseline resync now (purge all logs and restore by snapshot).
*/
void purge_all_logs();

void wait_for_log_store_ready();
void set_last_durable_lsn(repl_lsn_t lsn);

Expand Down
4 changes: 4 additions & 0 deletions src/lib/replication/repl_dev/raft_repl_dev.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ class RaftReplDev : public ReplDev,
if (!ready) { RD_LOGD("Not yet ready for traffic, committed to {} but gate is {}", committed_lsn, gate); }
return ready;
}
void purge() override {
// clean up existing logs in log store
m_data_journal->purge_all_logs();
}

//////////////// Accessor/shortcut methods ///////////////////////
nuraft_mesg::repl_service_ctx* group_msg_service();
Expand Down
1 change: 1 addition & 0 deletions src/lib/replication/repl_dev/solo_repl_dev.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class SoloReplDev : public ReplDev {
return std::vector< peer_info >{peer_info{.id_ = m_group_id, .replication_idx_ = 0, .last_succ_resp_us_ = 0}};
}
bool is_ready_for_traffic() const override { return true; }
void purge() override {}

uuid_t group_id() const override { return m_group_id; }

Expand Down

0 comments on commit fb6fd08

Please sign in to comment.