1
0
Fork 0

need to log all changes of term in agency

This commit is contained in:
Kaveh Vahedipour 2016-03-23 19:05:34 +01:00
parent b1a8f1611b
commit 80e7828a97
4 changed files with 21 additions and 10 deletions

View File

@ -56,8 +56,8 @@ void Constituent::configure(Agent* agent) {
}
Constituent::Constituent() :
Thread("Constituent"), _term(0), _id(0), _gen(std::random_device()()),
_role(FOLLOWER), _leader_id(0), _agent(0) {}
Thread("Constituent"), _term(0), _leader_id(0), _id(0), _gen(std::random_device()()),
_role(FOLLOWER), _agent(0) {}
Constituent::~Constituent() {
shutdown();
@ -77,22 +77,29 @@ term_t Constituent::term() const {
return _term;
}
void Constituent::term(term_t t) {
if (t != _term) {
LOG(INFO) << "Updating term to " << t;
}
_term = t;
}
role_t Constituent::role () const {
return _role;
}
void Constituent::follow (term_t term) {
void Constituent::follow (term_t t) {
if (_role != FOLLOWER) {
LOG(WARN) << "Role change: Converted to follower in term " << _term ;
LOG(INFO) << "Role change: Converted to follower in term " << t;
}
_term = term;
this->term(t);
_votes.assign(_votes.size(),false); // void all votes
_role = FOLLOWER;
}
void Constituent::lead () {
if (_role != LEADER) {
LOG(WARN) << "Role change: Converted to leader in term " << _term ;
LOG(INFO) << "Role change: Converted to leader in term " << _term ;
_agent->lead(); // We need to rebuild spear_head and read_db;
}
_role = LEADER;
@ -101,7 +108,7 @@ void Constituent::lead () {
void Constituent::candidate () {
if (_role != CANDIDATE)
LOG(WARN) << "Role change: Converted to candidate in term " << _term ;
LOG(INFO) << "Role change: Converted to candidate in term " << _term ;
_role = CANDIDATE;
}
@ -171,7 +178,7 @@ bool Constituent::vote (
return false; // TODO: Assertion?
} else {
if (term > _term || (_term==term&&_leader_id==leaderId)) {
_term = term;
this->term(term);
_cast = true; // Note that I voted this time around.
_leader_id = leaderId; // The guy I voted for I assume leader.
if (_role>FOLLOWER)

View File

@ -104,6 +104,9 @@ public:
void beginShutdown () override;
private:
/// @brief set term to new term
void term (term_t);
std::vector<std::string> const& end_points() const;
std::string const& end_point(id_t) const;
@ -141,6 +144,8 @@ private:
duration_t sleepFor(double, double);
double sleepFord(double, double);
// mission critical
term_t _term; /**< @brief term number */
std::atomic<bool> _cast; /**< @brief cast a vote this term */

View File

@ -174,7 +174,7 @@ std::ostream& operator<< (std::ostream& o, std::map<S,T> const& d) {
}
Node& Node::root() {
Node *par = _parent, *tmp;
Node *par = _parent, *tmp = 0;
while (par != 0) {
tmp = par;
par = par->_parent;

View File

@ -145,7 +145,6 @@ inline HttpHandler::status_t RestAgencyHandler::handleRead () {
HttpHandler::status_t RestAgencyHandler::handleTest() {
Builder body;
body.add(VPackValue(VPackValueType::Object));
body.add("id", Value(_agent->id()));
body.add("term", Value(_agent->term()));
body.add("leaderId", Value(_agent->leaderID()));
body.add("configuration", _agent->config().toBuilder()->slice());