Skip to content

Commit 1b3a140

Browse files
committed
1.Add a switch to control whether to determine whether the peer exists in conf during the pre_vote and elect_self processes.
2.Add reject_log_index_ to reject the raft log of a specific index on the follower, which is convenient for testing.
1 parent f43c6a3 commit 1b3a140

File tree

5 files changed

+368
-2
lines changed

5 files changed

+368
-2
lines changed

src/braft/node.cpp

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ DECLARE_bool(raft_enable_leader_lease);
7070
DEFINE_bool(raft_enable_witness_to_leader, false,
7171
"enable witness temporarily to become leader when leader down accidently");
7272

73+
DEFINE_bool(raft_enable_peer_not_in_conf_can_elec, false,
74+
"enable peer not in the conf can initiate elections");
75+
7376
#ifndef UNIT_TEST
7477
static bvar::Adder<int64_t> g_num_nodes("raft_node_count");
7578
#else
@@ -1622,10 +1625,12 @@ void NodeImpl::pre_vote(std::unique_lock<raft_mutex_t>* lck, bool triggered) {
16221625
" configuration is possibly out of date";
16231626
return;
16241627
}
1625-
if (!_conf.contains(_server_id)) {
1628+
if (!FLAGS_raft_enable_peer_not_in_conf_can_elec) {
1629+
if (!_conf.contains(_server_id)) {
16261630
LOG(WARNING) << "node " << _group_id << ':' << _server_id
16271631
<< " can't do pre_vote as it is not in " << _conf.conf;
16281632
return;
1633+
}
16291634
}
16301635

16311636
int64_t old_term = _current_term;
@@ -1681,10 +1686,12 @@ void NodeImpl::elect_self(std::unique_lock<raft_mutex_t>* lck,
16811686
bool old_leader_stepped_down) {
16821687
LOG(INFO) << "node " << _group_id << ":" << _server_id
16831688
<< " term " << _current_term << " start vote and grant vote self";
1684-
if (!_conf.contains(_server_id)) {
1689+
if (!FLAGS_raft_enable_peer_not_in_conf_can_elec) {
1690+
if (!_conf.contains(_server_id)) {
16851691
LOG(WARNING) << "node " << _group_id << ':' << _server_id
16861692
<< " can't do elect_self as it is not in " << _conf.conf;
16871693
return;
1694+
}
16881695
}
16891696
// cancel follower election timer
16901697
if (_state == STATE_FOLLOWER) {
@@ -2393,6 +2400,16 @@ void NodeImpl::handle_append_entries_request(brpc::Controller* cntl,
23932400
brpc::ClosureGuard done_guard(done);
23942401
std::unique_lock<raft_mutex_t> lck(_mutex);
23952402

2403+
// for test
2404+
const int64_t reject_log_index = get_reject_log_index();
2405+
if (reject_log_index > 0 &&
2406+
request->prev_log_index() + 1 >= reject_log_index) {
2407+
// _last_leader_timestamp = butil::monotonic_time_ms();
2408+
// don't interfere check_dead_nodes
2409+
cntl->SetFailed(EBUSY, "handle_append_entries_request reject_log_index");
2410+
return;
2411+
}
2412+
23962413
// pre set term, to avoid get term in lock
23972414
response->set_term(_current_term);
23982415

@@ -2894,6 +2911,10 @@ void NodeImpl::get_status(NodeStatus* status) {
28942911
}
28952912
}
28962913

2914+
void NodeImpl::get_log_mgr_status(LogManagerStatus* log_manager_status) {
2915+
_log_manager->get_status(log_manager_status);
2916+
}
2917+
28972918
void NodeImpl::stop_replicator(const std::set<PeerId>& keep,
28982919
const std::set<PeerId>& drop) {
28992920
for (std::set<PeerId>::const_iterator

src/braft/node.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,8 @@ friend class VoteBallotCtx;
214214
// see from the website, which is generated by |describe| actually.
215215
void get_status(NodeStatus* status);
216216

217+
void get_log_mgr_status(LogManagerStatus* log_manager_status);
218+
217219
// Readonly mode func
218220
void enter_readonly_mode();
219221
void leave_readonly_mode();
@@ -241,6 +243,10 @@ friend class VoteBallotCtx;
241243

242244
bool disable_cli() const { return _options.disable_cli; }
243245
bool is_witness() const { return _options.witness; }
246+
247+
// for test
248+
void set_reject_log_index(const int64_t log_index) { reject_log_index_ = log_index; }
249+
int64_t get_reject_log_index() const { return reject_log_index_; }
244250
private:
245251
friend class butil::RefCountedThreadSafe<NodeImpl>;
246252

@@ -533,6 +539,9 @@ friend class butil::RefCountedThreadSafe<NodeImpl>;
533539

534540
LeaderLease _leader_lease;
535541
FollowerLease _follower_lease;
542+
543+
// for test
544+
int64_t reject_log_index_ {0};
536545
};
537546

538547
}

src/braft/raft.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,10 @@ void Node::get_status(NodeStatus* status) {
230230
return _impl->get_status(status);
231231
}
232232

233+
void Node::get_log_mgr_status(LogManagerStatus* log_manager_status) {
234+
return _impl->get_log_mgr_status(log_manager_status);
235+
}
236+
233237
void Node::enter_readonly_mode() {
234238
return _impl->enter_readonly_mode();
235239
}
@@ -242,6 +246,14 @@ bool Node::readonly() {
242246
return _impl->readonly();
243247
}
244248

249+
void Node::set_reject_log_index(const int64_t log_index) {
250+
_impl->set_reject_log_index(log_index);
251+
}
252+
253+
int64_t Node::get_reject_log_index() const {
254+
return _impl->get_reject_log_index();
255+
}
256+
245257
// ------------- Iterator
246258
void Iterator::next() {
247259
if (valid()) {

src/braft/raft.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class LeaderChangeContext;
4545
class FileSystemAdaptor;
4646
class SnapshotThrottle;
4747
class LogStorage;
48+
class LogManagerStatus;
4849

4950
const PeerId ANY_PEER(butil::EndPoint(butil::IP_ANY, 0), 0);
5051

@@ -762,6 +763,8 @@ class Node {
762763
// see from the website.
763764
void get_status(NodeStatus* status);
764765

766+
void get_log_mgr_status(LogManagerStatus* log_manager_status);
767+
765768
// Make this node enter readonly mode.
766769
// Readonly mode should only be used to protect the system in some extreme cases.
767770
// For example, in a storage system, too many write requests flood into the system
@@ -789,6 +792,10 @@ class Node {
789792
// is less than the majority.
790793
bool readonly();
791794

795+
void set_reject_log_index(const int64_t log_index);
796+
797+
int64_t get_reject_log_index() const;
798+
792799
private:
793800
NodeImpl* _impl;
794801
};

0 commit comments

Comments
 (0)