-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnode.cpp
More file actions
76 lines (63 loc) · 2.16 KB
/
node.cpp
File metadata and controls
76 lines (63 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include "node.hpp"
#include "Message.h"
Node::Node(string id, int status, const Cluster& c) : id(id), status(status), cluster(c) {
}
int Node::generate_proposal_id() {
// TODO
return 0;
}
int Node::propose(int client_msg) {
// *************** 1. PROPOSAL ******************** //
// Proposal id
int proposal_id = this->generate_proposal_id();
Message* propose_message = new ProposeMessage(proposal_id);
// get all nodes
// we get back PromisedMessages here
vector<Node*> cluster_nodes = this->c.get_nodes();
vector<Node*> agreed_nodes; // consensus agreements
int total_up_nodes = 1; // include current node
for (auto node_ptr : cluster_nodes) {
if (node_ptr->status == 1 && node_ptr->get_id() != this->id) {
total_up_nodes++;
Message& promise_message = node_ptr->accept(propose_message);
if (promise_message.id == propose_message.id) {
agreed_nodes.push_back(node_ptr);
}
}
}
// check consensus
if (agreed_nodes.size() < (cluster_nodes.size() + 1) / 2) {
return 0;
}
// OK to proceed
// *************** 2. ACCEPT ********************** //
Message* accept_message = new AcceptMessage(proposal_id, client_msg);
int accepted_nodes = 1; // include current node
for (auto node_ptr : agreed_nodes) {
if (node_ptr->status == 1 && node_ptr->get_id() != this->id) {
Message* accepted_message = node_ptr->accept(accept_message);
if (accepted_message->id == proposal_id) {
accepted_nodes++;
}
}
}
// check if accept consensus on this proposal_id was OK
if (accepted_nodes < (cluster_nodes.size() + 1) / 2) {
return 0;
}
// TODO write to learner for this node
}
Message& Node::accept(const Message& message) {
if (message.type == MessageType.Proposal) {
// TODO check Learner for most recent ID
}
else if (message.type == MessageType.Accept) {
// TODO update Learner now with given ID, value
}
}
string Node::get_id() const {
return this->id;
}
int Node::get_status() const {
return this->status;
}