Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support diasble election in unit-test #453

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions test/test_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -454,14 +454,19 @@ TEST_P(NodeTest, LeaderFailWithWitness) {
}
cond.wait();

std::vector<braft::Node*> follower_nodes;
cluster.followers(&follower_nodes);
ASSERT_EQ(2, follower_nodes.size());
for (auto node : follower_nodes){
cluster.disable_election(node);
}

// stop leader
butil::EndPoint old_leader = leader->node_id().peer_id.addr;
LOG(WARNING) << "stop leader " << leader->node_id();
cluster.stop(leader->node_id().peer_id.addr);

// apply something when follower
std::vector<braft::Node*> nodes;
cluster.followers(&nodes);
cond.reset(10);
for (int i = 0; i < 10; i++) {
butil::IOBuf data;
Expand All @@ -472,10 +477,14 @@ TEST_P(NodeTest, LeaderFailWithWitness) {
task.data = &data;
task.done = NEW_APPLYCLOSURE(&cond, -1);
// node 0 is witness;
nodes[1]->apply(task);
follower_nodes[1]->apply(task);
}
cond.wait();

for (auto node : follower_nodes){
cluster.enable_election(node);
}

// elect new leader
cluster.wait_leader();
leader = cluster.leader();
Expand Down
12 changes: 12 additions & 0 deletions test/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ class Cluster {
int64_t throttle_throughput_bytes = 10 * 1024 * 1024;
int64_t check_cycle = 10;
_throttle = new braft::ThroughputSnapshotThrottle(throttle_throughput_bytes, check_cycle);
_infinite_election_timeout_ms = 1 << 31;
}
~Cluster() {
stop_all();
Expand Down Expand Up @@ -496,6 +497,16 @@ class Cluster {
goto CHECK;
}

void disable_election(braft::Node* node){
std::lock_guard<raft_mutex_t> guard(_mutex);
node->reset_election_timeout_ms(_infinite_election_timeout_ms, _max_clock_drift_ms);
}

void enable_election(braft::Node* node) {
std::lock_guard<raft_mutex_t> guard(_mutex);
node->reset_election_timeout_ms(_election_timeout_ms, _max_clock_drift_ms);
}

private:
void all_nodes(std::vector<butil::EndPoint>* addrs) {
addrs->clear();
Expand Down Expand Up @@ -540,6 +551,7 @@ class Cluster {
std::map<butil::EndPoint, brpc::Server*> _server_map;
int32_t _election_timeout_ms;
int32_t _max_clock_drift_ms;
int32_t _infinite_election_timeout_ms;
raft_mutex_t _mutex;
braft::SnapshotThrottle* _throttle;
};
Expand Down