Add HigherBallot message to automatize presidency change
This commit is contained in:
parent
c68c0a8a07
commit
09f005406a
@ -66,6 +66,7 @@ namespace paxos
|
|||||||
log("but it was discarded because ballot " + std::to_string(ballot)
|
log("but it was discarded because ballot " + std::to_string(ballot)
|
||||||
+ " is inferior or equal to nextBallot "
|
+ " is inferior or equal to nextBallot "
|
||||||
+ std::to_string(next_ballot), red);
|
+ std::to_string(next_ballot), red);
|
||||||
|
send_higher_ballot(next_ballot, sender);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ledger.set_next_bal(ballot);
|
ledger.set_next_bal(ballot);
|
||||||
@ -74,6 +75,35 @@ namespace paxos
|
|||||||
send_last_vote(ballot, previous_vote, sender);
|
send_last_vote(ballot, previous_vote, sender);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Legislator::send_higher_ballot(int ballot, std::string receiver)
|
||||||
|
{
|
||||||
|
Message message;
|
||||||
|
message.set_method("HigherBallot");
|
||||||
|
message.add_header("ballot", std::to_string(ballot));
|
||||||
|
SendEW::send_message(message, legislators[receiver]);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Legislator::receive_higher_ballot(Message message)
|
||||||
|
{
|
||||||
|
std::string ballot_str = *message.get_header("ballot");
|
||||||
|
log(config_.name + " has received a HigherBallot("
|
||||||
|
+ ballot_str + ")", red);
|
||||||
|
|
||||||
|
receive_higher_ballot(std::stoi(ballot_str));
|
||||||
|
}
|
||||||
|
|
||||||
|
void Legislator::receive_higher_ballot(int ballot)
|
||||||
|
{
|
||||||
|
int last_tried = ledger.last_tried();
|
||||||
|
|
||||||
|
while (last_tried + (int)legislators.size() < ballot)
|
||||||
|
{
|
||||||
|
last_tried += legislators.size();
|
||||||
|
}
|
||||||
|
ledger.set_last_tried(last_tried);
|
||||||
|
initiate_ballot();
|
||||||
|
}
|
||||||
|
|
||||||
void Legislator::send_last_vote(int ballot, Vote previous_vote,
|
void Legislator::send_last_vote(int ballot, Vote previous_vote,
|
||||||
std::string sender)
|
std::string sender)
|
||||||
{
|
{
|
||||||
@ -193,8 +223,13 @@ namespace paxos
|
|||||||
void Legislator::receive_begin_ballot(int ballot, int decree_id,
|
void Legislator::receive_begin_ballot(int ballot, int decree_id,
|
||||||
std::string sender)
|
std::string sender)
|
||||||
{
|
{
|
||||||
if (ballot != ledger.next_bal())
|
int next_ballot = ledger.next_bal();
|
||||||
|
if (ballot != next_ballot)
|
||||||
|
{
|
||||||
|
if (ballot < next_ballot)
|
||||||
|
send_higher_ballot(next_ballot, sender);
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
Vote vote;
|
Vote vote;
|
||||||
vote.ballot_id = ballot;
|
vote.ballot_id = ballot;
|
||||||
Decree decree;
|
Decree decree;
|
||||||
@ -288,5 +323,7 @@ namespace paxos
|
|||||||
receive_voted(message);
|
receive_voted(message);
|
||||||
else if (method == "Success")
|
else if (method == "Success")
|
||||||
receive_success(message);
|
receive_success(message);
|
||||||
|
else if (method == "HigherBallot")
|
||||||
|
receive_higher_ballot(message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -22,6 +22,10 @@ namespace paxos
|
|||||||
void receive_next_ballot(Message message);
|
void receive_next_ballot(Message message);
|
||||||
void receive_next_ballot(int ballot, std::string sender);
|
void receive_next_ballot(int ballot, std::string sender);
|
||||||
|
|
||||||
|
void send_higher_ballot(int ballot, std::string receiver);
|
||||||
|
void receive_higher_ballot(Message message);
|
||||||
|
void receive_higher_ballot(int ballot);
|
||||||
|
|
||||||
void send_last_vote(int ballot, Vote previous_vote, std::string sender);
|
void send_last_vote(int ballot, Vote previous_vote, std::string sender);
|
||||||
void receive_last_vote(Message message);
|
void receive_last_vote(Message message);
|
||||||
void receive_enough_last_vote();
|
void receive_enough_last_vote();
|
||||||
|
Loading…
Reference in New Issue
Block a user