Fix a possible case when president receive a last vote after receiving a voted from someone else for the same ballot

This commit is contained in:
Julien CLEMENT 2020-05-06 21:03:24 +02:00
parent 21dbc3e444
commit b33d2f17ab
2 changed files with 6 additions and 3 deletions

@ -10,13 +10,14 @@
namespace paxos namespace paxos
{ {
Legislator::Legislator(const LegislatorConfig& config) Legislator::Legislator(const LegislatorConfig& config)
: config_(config), ledger(config.name) : config_(config), ledger(config.name), has_started(false)
{ {
log("created legislator " + config.name, blue); log("created legislator " + config.name, blue);
} }
void Legislator::initiate_ballot() void Legislator::initiate_ballot()
{ {
has_started = false;
quorum_previous_votes.clear(); quorum_previous_votes.clear();
int new_ballot_number = ledger.last_tried() + 1; int new_ballot_number = ledger.last_tried() + 1;
ledger.set_last_tried(new_ballot_number); ledger.set_last_tried(new_ballot_number);
@ -92,8 +93,7 @@ namespace paxos
int vote_decree = std::stoi(*message.get_header("decree")); int vote_decree = std::stoi(*message.get_header("decree"));
unsigned int nb_legislators = legislators.size(); unsigned int nb_legislators = legislators.size();
if (ballot != ledger.last_tried() if (ballot != ledger.last_tried() || has_started)
|| quorum_previous_votes.size() > nb_legislators / 2)
{ {
log("but it was discarded because ballot " + std::to_string(ballot) log("but it was discarded because ballot " + std::to_string(ballot)
+ " is outdated, it is either no longer in treatment or already started" + " is outdated, it is either no longer in treatment or already started"
@ -117,6 +117,7 @@ namespace paxos
void Legislator::receive_enough_last_vote() void Legislator::receive_enough_last_vote()
{ {
has_started = true;
int ballot = ledger.last_tried(); int ballot = ledger.last_tried();
std::string ballot_str = std::to_string(ballot); std::string ballot_str = std::to_string(ballot);

@ -40,8 +40,10 @@ namespace paxos
void handle_message(Message message); void handle_message(Message message);
private:
Ledger ledger; Ledger ledger;
std::unordered_map<std::string, Vote> quorum_previous_votes; std::unordered_map<std::string, Vote> quorum_previous_votes;
bool has_started;
}; };
using shared_legislator = std::shared_ptr<Legislator>; using shared_legislator = std::shared_ptr<Legislator>;