Skip to content

Commit 744a687

Browse files
committed
add on_install_snapshot callback for statemachine
1 parent b37c610 commit 744a687

File tree

7 files changed

+21
-0
lines changed

7 files changed

+21
-0
lines changed

src/braft/fsm_caller.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,10 @@ void FSMCaller::do_stop_following(const LeaderChangeContext& stop_following_cont
496496
_fsm->on_stop_following(stop_following_context);
497497
}
498498

499+
void FSMCaller::on_install_snapshot_start(const GroupId& group_id, const PeerId& peer_id) {
500+
_fsm->on_install_snapshot_start(group_id, peer_id);
501+
}
502+
499503
void FSMCaller::describe(std::ostream &os, bool use_html) {
500504
const char* newline = (use_html) ? "<br>" : "\n";
501505
TaskType cur_task = _cur_task;

src/braft/fsm_caller.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ class BAIDU_CACHELINE_ALIGNMENT FSMCaller {
116116
int on_leader_start(int64_t term, int64_t lease_epoch);
117117
int on_start_following(const LeaderChangeContext& start_following_context);
118118
int on_stop_following(const LeaderChangeContext& stop_following_context);
119+
void on_install_snapshot_start(const GroupId& group_id, const PeerId& peer_id);
119120
BRAFT_MOCK int on_error(const Error& e);
120121
int64_t last_applied_index() const {
121122
return _last_applied_index.load(butil::memory_order_relaxed);

src/braft/node.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,6 +1365,11 @@ void NodeImpl::on_error(const Error& e) {
13651365
lck.unlock();
13661366
}
13671367

1368+
void NodeImpl::install_snapshot_start(const GroupId& group_id, const PeerId& peer_id) {
1369+
_fsm_caller->on_install_snapshot_start(group_id, peer_id);
1370+
}
1371+
1372+
13681373
void NodeImpl::handle_vote_timeout() {
13691374
std::unique_lock<raft_mutex_t> lck(_mutex);
13701375

src/braft/node.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,9 @@ friend class VoteBallotCtx;
241241

242242
bool disable_cli() const { return _options.disable_cli; }
243243
bool is_witness() const { return _options.witness; }
244+
245+
// Called when leader start to install snapshot to remote peer
246+
void install_snapshot_start(const GroupId& group_id, const PeerId& peer_id);
244247
private:
245248
friend class butil::RefCountedThreadSafe<NodeImpl>;
246249

src/braft/raft.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ void StateMachine::on_configuration_committed(const Configuration& conf, int64_t
313313

314314
void StateMachine::on_stop_following(const LeaderChangeContext&) {}
315315
void StateMachine::on_start_following(const LeaderChangeContext&) {}
316+
void StateMachine::on_install_snapshot_start(const GroupId& group_id, const PeerId& peer_id) {}
316317

317318
BootstrapOptions::BootstrapOptions()
318319
: last_log_index(0)

src/braft/raft.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,11 @@ class StateMachine {
265265
// the very leader whom the follower starts to follow.
266266
// User can reset the node's information as it starts to follow some leader.
267267
virtual void on_start_following(const ::braft::LeaderChangeContext& ctx);
268+
269+
// Invoked when the leader of |group_id| start to send snapshot to |peer_id|
270+
// Default: Do nothing
271+
virtual void on_install_snapshot_start(const GroupId& group_id, const PeerId& peer_id);
272+
268273
};
269274

270275
enum State {

src/braft/replicator.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,8 @@ void Replicator::_install_snapshot() {
789789
add_one_more_task(true)) {
790790
return _block(butil::gettimeofday_us(), EBUSY);
791791
}
792+
793+
node_impl->install_snapshot_start(_options.group_id, _options.server_id);
792794

793795
// pre-set replicator state to INSTALLING_SNAPSHOT, so replicator could be
794796
// blocked if something is wrong, such as throttled for a period of time

0 commit comments

Comments
 (0)