Working on legislator interfaces
This commit is contained in:
1 parent
b6c825a877
commit
fd4de7091c
15 files changed
+226
-7
No files matched your search
@@ -0,0 +1,65 @@
|
||||
#include "ledger.hh"
|
||||
#include <fstream>
|
||||
#include <filesystem>
|
||||
|
||||
namespace paxos
|
||||
{
|
||||
Ledger::Ledger(std::string path)
|
||||
: base_path_(path)
|
||||
{
|
||||
std::filesystem::create_directory(path);
|
||||
base_path_ += "/";
|
||||
}
|
||||
|
||||
std::vector<Decree> Ledger::get_decrees()
|
||||
{
|
||||
return std::vector<Decree>();
|
||||
}
|
||||
|
||||
int Ledger::last_tried()
|
||||
{
|
||||
return read_file(base_path_ + "last-tried.txt");
|
||||
}
|
||||
|
||||
void Ledger::set_last_tried(int b)
|
||||
{
|
||||
write_file(base_path_ + "last-tried.txt", b);
|
||||
}
|
||||
|
||||
|
||||
int Ledger::prev_vote()
|
||||
{
|
||||
return read_file(base_path_ + "prev-vote.txt");
|
||||
}
|
||||
|
||||
void Ledger::set_prev_vote(int v)
|
||||
{
|
||||
write_file(base_path_ + "prev-vote.txt", v);
|
||||
}
|
||||
|
||||
int Ledger::next_bal()
|
||||
{
|
||||
return read_file(base_path_ + "next-bal.txt");
|
||||
}
|
||||
|
||||
void Ledger::set_next_bal(int b)
|
||||
{
|
||||
write_file(base_path_ + "next-bal.txt", b);
|
||||
}
|
||||
|
||||
int Ledger::read_file(std::string path)
|
||||
{
|
||||
std::ifstream stream(path);
|
||||
if (!stream.good())
|
||||
return -1;
|
||||
int res;
|
||||
stream >> res;
|
||||
return res;
|
||||
}
|
||||
|
||||
void Ledger::write_file(std::string path, int n)
|
||||
{
|
||||
std::ofstream stream(path);
|
||||
stream << n;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "law/decree.hh"
|
||||
|
||||
namespace paxos
|
||||
{
|
||||
class Ledger
|
||||
{
|
||||
public:
|
||||
Ledger(std::string path);
|
||||
|
||||
std::vector<Decree> get_decrees();
|
||||
|
||||
int last_tried();
|
||||
void set_last_tried(int b);
|
||||
|
||||
int prev_vote();
|
||||
void set_prev_vote(int v);
|
||||
|
||||
int next_bal();
|
||||
void set_next_bal(int b);
|
||||
|
||||
private:
|
||||
std::string base_path_;
|
||||
|
||||
int read_file(std::string path);
|
||||
void write_file(std::string path, int n);
|
||||
};
|
||||
}
|
||||
@@ -1,9 +1,63 @@
|
||||
#include "legislator.hh"
|
||||
#include "misc/logger.hh"
|
||||
#include <string>
|
||||
|
||||
namespace paxos
|
||||
{
|
||||
Legislator::Legislator(const LegislatorConfig& config)
|
||||
: config_(config)
|
||||
: config_(config), ledger(config.name)
|
||||
{
|
||||
log("created legislator " + config.name, blue);
|
||||
}
|
||||
|
||||
void Legislator::initiate_ballot()
|
||||
{
|
||||
int new_ballot_number = ledger.last_tried() + 1;
|
||||
ledger.set_last_tried(new_ballot_number);
|
||||
log(config_.name + " is initiating ballot " + std::to_string(new_ballot_number), cyan);
|
||||
send_next_ballot(new_ballot_number);
|
||||
}
|
||||
|
||||
void Legislator::send_next_ballot(int ballot)
|
||||
{
|
||||
ballot = ballot;
|
||||
return;
|
||||
}
|
||||
|
||||
void Legislator::receive_next_ballot(int ballot, std::string sender)
|
||||
{
|
||||
log(config_.name + " has received a NextBallot("
|
||||
+ std::to_string(ballot)
|
||||
+ ") from " + sender, cyan);
|
||||
int next_ballot = ledger.next_bal();
|
||||
if (ballot <= next_ballot)
|
||||
{
|
||||
log("but it was discarded because ballot " + std::to_string(ballot)
|
||||
+ " is inferior or equal to nextBallot "
|
||||
+ std::to_string(next_ballot), red);
|
||||
return;
|
||||
}
|
||||
ledger.set_next_bal(ballot);
|
||||
int previous_vote = ledger.prev_vote();
|
||||
previous_vote = previous_vote;
|
||||
//XXX send a LastVote to sender
|
||||
}
|
||||
|
||||
void Legislator::receive_enough_last_vote
|
||||
(std::unordered_map<std::string, int> quorum_last_votes)
|
||||
{
|
||||
quorum_last_votes = quorum_last_votes;
|
||||
//find d to satisfy B3
|
||||
//send BeginBallot to the quorum
|
||||
}
|
||||
|
||||
void Legislator::receive_begin_ballot(int ballot, int decree)
|
||||
{
|
||||
if (ballot != ledger.next_bal())
|
||||
return;
|
||||
ledger.set_prev_vote(ballot);
|
||||
|
||||
decree = decree;
|
||||
//XXX send Voted
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
#pragma once
|
||||
#include "config/config.hh"
|
||||
#include "ledger.hh"
|
||||
#include <unordered_map>
|
||||
|
||||
namespace paxos
|
||||
{
|
||||
@@ -9,6 +11,21 @@ namespace paxos
|
||||
Legislator(const LegislatorConfig& config);
|
||||
LegislatorConfig config_;
|
||||
|
||||
void initiate_ballot();
|
||||
|
||||
void send_next_ballot(int ballot);
|
||||
|
||||
void receive_next_ballot(int ballot, std::string sender);
|
||||
|
||||
void receive_enough_last_vote
|
||||
(std::unordered_map<std::string, int> quorum_last_votes);
|
||||
|
||||
void receive_begin_ballot(int ballot, int decree);
|
||||
|
||||
Ledger ledger;
|
||||
};
|
||||
using shared_legislator = std::shared_ptr<Legislator>;
|
||||
|
||||
extern shared_legislator self;
|
||||
extern std::vector<shared_legislator> legislators;
|
||||
}
|
||||
Whitespace-only changes.
@@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
#include "legislator.hh"
|
||||
#include "law/decree.hh"
|
||||
|
||||
namespace paxos
|
||||
{
|
||||
struct Vote
|
||||
{
|
||||
Legislator legislator;
|
||||
int ballot_id;
|
||||
Decree decree;
|
||||
};
|
||||
}
|
||||