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

[ballot] Update ballot init function signature #449

Closed
wants to merge 2 commits into from
Closed
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
5 changes: 2 additions & 3 deletions src/braft/ballot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace braft {
Ballot::Ballot() : _quorum(0), _old_quorum(0) {}
Ballot::~Ballot() {}

int Ballot::init(const Configuration& conf, const Configuration* old_conf) {
void Ballot::init(const Configuration& conf, const Configuration* old_conf) {
_peers.clear();
_old_peers.clear();
_quorum = 0;
Expand All @@ -34,15 +34,14 @@ int Ballot::init(const Configuration& conf, const Configuration* old_conf) {
}
_quorum = _peers.size() / 2 + 1;
if (!old_conf) {
return 0;
return;
}
_old_peers.reserve(old_conf->size());
for (Configuration::const_iterator
iter = old_conf->begin(); iter != old_conf->end(); ++iter) {
_old_peers.push_back(*iter);
}
_old_quorum = _old_peers.size() / 2 + 1;
return 0;
}

Ballot::PosHint Ballot::grant(const PeerId& peer, PosHint hint) {
Expand Down
2 changes: 1 addition & 1 deletion src/braft/ballot.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Ballot {
std::swap(_old_quorum, rhs._old_quorum);
}

int init(const Configuration& conf, const Configuration* old_conf);
void init(const Configuration& conf, const Configuration* old_conf);
PosHint grant(const PeerId& peer, PosHint hint);
void grant(const PeerId& peer);
bool granted() const { return _quorum <= 0 && _old_quorum <= 0; }
Expand Down
5 changes: 1 addition & 4 deletions src/braft/ballot_box.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,7 @@ int BallotBox::reset_pending_index(int64_t new_pending_index) {
int BallotBox::append_pending_task(const Configuration& conf, const Configuration* old_conf,
Closure* closure) {
Ballot bl;
if (bl.init(conf, old_conf) != 0) {
CHECK(false) << "Fail to init ballot";
return -1;
}
bl.init(conf, old_conf);

BAIDU_SCOPED_LOCK(_mutex);
CHECK(_pending_index > 0);
Expand Down
16 changes: 12 additions & 4 deletions src/braft/replicator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -447,15 +447,23 @@ void Replicator::_on_rpc_returned(ReplicatorId id, brpc::Controller* cntl,
<< " is " << response->last_log_index();
// The peer contains less logs than leader
r->_next_index = response->last_log_index() + 1;
} else {
} else {
// The peer contains logs from old term which should be truncated,
// decrease _last_log_at_peer by one to test the right index to keep
if (BAIDU_LIKELY(r->_next_index > 1)) {
BRAFT_VLOG << "Group " << r->_options.group_id
BRAFT_VLOG << "Group " << r->_options.group_id
<< " log_index=" << r->_next_index << " mismatch";
--r->_next_index;
// Decrease the |_next_index| when the request with the minimum log
// index mismatch.
// Because when we enable pipeline replication and disable cache,
// the request with larger log index would be handled before the
// smaller request, we should ignore it.
// See https://github.com/baidu/braft/issues/421
if (request->prev_log_index() == r->_next_index - 1) {
--r->_next_index;
}
} else {
LOG(ERROR) << "Group " << r->_options.group_id
LOG(ERROR) << "Group " << r->_options.group_id
<< " peer=" << r->_options.peer_id
<< " declares that log at index=0 doesn't match,"
" which is not supposed to happen";
Expand Down
6 changes: 3 additions & 3 deletions test/test_ballot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ TEST(BallotTest, sanity) {
conf.add_peer(peer2);
conf.add_peer(peer3);
braft::Ballot bl;
ASSERT_EQ(0, bl.init(conf, NULL));
bl.init(conf, NULL);
ASSERT_EQ(2, bl._quorum);
ASSERT_EQ(0, bl._old_quorum);
bl.grant(peer1);
Expand All @@ -54,7 +54,7 @@ TEST(BallotTest, joint_consensus_same_conf) {
conf.add_peer(peer2);
conf.add_peer(peer3);
braft::Ballot bl;
ASSERT_EQ(0, bl.init(conf, &conf));
bl.init(conf, &conf);
ASSERT_EQ(2, bl._quorum);
ASSERT_EQ(2, bl._old_quorum);
bl.grant(peer1);
Expand Down Expand Up @@ -92,7 +92,7 @@ TEST(BallotTest, joint_consensus_different_conf) {
conf2.add_peer(peer3);
conf2.add_peer(peer4);
braft::Ballot bl;
ASSERT_EQ(0, bl.init(conf, &conf2));
bl.init(conf, &conf2);
bl.grant(peer1);
bl.grant(peer2);
ASSERT_FALSE(bl.granted());
Expand Down
Loading