Skip to content

Commit

Permalink
[ballot] ballot init not need return
Browse files Browse the repository at this point in the history
  • Loading branch information
ehds committed Apr 9, 2024
1 parent 92703cd commit c30ffe2
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 11 deletions.
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
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

0 comments on commit c30ffe2

Please sign in to comment.