SearchSECODatabaseAPI
RAFTConsensus.h
1/*
2This program has been developed by students from the bachelor Computer Science at
3Utrecht University within the Software Project course.
4© Copyright Utrecht University (Department of Information and Computing Sciences)
5*/
6
7#pragma once
8#include "Networking.h"
9#include "Statistics.h"
10
11#include <vector>
12#include <boost/shared_ptr.hpp>
13#include <mutex>
14#include <prometheus/counter.h>
15
16#define RESPONSE_OK "ok"
17#define HEARTBEAT_TIME 1000000
18#define LEADER_DROPOUT_WAIT_TIME 1000000
19
20
21class TcpConnection;
22class RequestHandler;
23
28{
29 public:
30 boost::shared_ptr<TcpConnection> connection;
31 std::string ip;
32 std::string port;
33
34 Connection(boost::shared_ptr<TcpConnection> conn, std::string ip, std::string port)
35 : connection(conn), ip(ip), port(port)
36 {
37 }
38};
39
41{
42public:
44
45 RAFTConsensus(Statistics *stats) : stats(stats)
46 {
47 }
48
56 void start(RequestHandler* requestHandler,
57 std::vector<std::pair<std::string, std::string>> ips,
58 bool assumeLeader = false);
59
63 virtual bool isLeader() { return leader; };
64
69 virtual std::string passRequestToLeader(std::string requestType, std::string client, std::string request);
70
80 virtual std::string connectNewNode(boost::shared_ptr<TcpConnection> connection, std::string request);
81
86 virtual std::vector<std::pair<std::string, std::string>> getIps(std::string file = ".env");
87
92 virtual std::vector<std::string> getCurrentIPs();
93
98 virtual std::string getMyIP()
99 {
100 return myIp;
101 }
102
103private:
109 void connectToLeader(std::vector<std::pair<std::string, std::string>> ips);
110
111
116 void listenForHeartbeat();
117
121 void handleHeartbeat(std::string heartbeat);
122
130 void handleInitialData(std::vector<std::string> initialData);
131
138 void tryConnectingWithIp(std::string &ip, std::string &port, std::string &response);
139
140
144 void heartbeatSender();
145
149 std::string getHeartbeat();
150
156 void listenForRequests(boost::shared_ptr<TcpConnection> connection);
157
163 void dropConnection(int i);
164
168 std::string connectionToString(Connection connection);
169
170 bool leader;
171 bool stop = false;
172 bool started = false;
173 std::mutex mtx;
174 Statistics *stats;
175
176 // Non-leader variables.
177 NetworkHandler* networkhandler;
178 std::string leaderIp, leaderPort, myIp, myPort;
179 std::vector<std::pair<std::string, std::string>> nonLeaderNodes;
180
181 // Leader variables.
182 std::vector<Connection>* others;
183 RequestHandler* requestHandler;
184 std::string nodeConnectionChange = "";
185};
Definition: Networking.h:13
Definition: RAFTConsensus.h:41
virtual std::string connectNewNode(boost::shared_ptr< TcpConnection > connection, std::string request)
Will handle a connect request by a new node that wants to join the network. If this node is the leade...
Definition: RAFTConsensus.cpp:214
virtual std::vector< std::pair< std::string, std::string > > getIps(std::string file=".env")
Reads the given file and gets the ips out of it.
Definition: RAFTConsensus.cpp:31
virtual std::string passRequestToLeader(std::string requestType, std::string client, std::string request)
Will pass the given request on to the leader of the network.
Definition: RAFTConsensus.cpp:298
virtual std::string getMyIP()
Gets the ip of this node.
Definition: RAFTConsensus.h:98
virtual bool isLeader()
Returns true if this node is the leader in the network.
Definition: RAFTConsensus.h:63
void start(RequestHandler *requestHandler, std::vector< std::pair< std::string, std::string > > ips, bool assumeLeader=false)
Starts RAFT. Will try to connect to a set list of IP's where we assume the leaders are....
Definition: RAFTConsensus.cpp:81
virtual std::vector< std::string > getCurrentIPs()
Returns the ips currently know to this api.
Definition: RAFTConsensus.cpp:70
Definition: RequestHandler.h:43
Stores the Prometheus statistics variables.
Definition: Statistics.h:35
Definition: ConnectionHandler.h:56
Represents the data of a connection with another node.
Definition: RAFTConsensus.h:28