-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMP2Node.h
executable file
·173 lines (149 loc) · 4.18 KB
/
MP2Node.h
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**********************************
* FILE NAME: MP2Node.h
*
* DESCRIPTION: MP2Node class header file
**********************************/
#ifndef MP2NODE_H_
#define MP2NODE_H_
/**
* Header files
*/
#include "stdincludes.h"
#include "EmulNet.h"
#include "Node.h"
#include "HashTable.h"
#include "Log.h"
#include "Params.h"
#include "Message.h"
#include "Queue.h"
#include <unordered_map>
#include <unordered_set>
#define STABILIZATION_TID -1
#define NUM_REPLICAS 3
#define T_CLOSE 10
#define INIT_SEQ -1
class Neighbor{
private:
Address addr;
bool newNeighbor;
public:
Address getAddress() const{
return addr;
}
void setAddress(Address const& a){
addr = a;
}
void setNew(bool i){
newNeighbor = i;
}
bool isNew() const{
return newNeighbor;
}
Neighbor(Address a, bool n) : addr(a), newNeighbor(n){
}
Neighbor(): addr(), newNeighbor(false){
}
bool operator ==(const Neighbor &other){
return addr == other.addr;
}
};
class Transaction{
private:
int id; // Transaction ID
string key; // Key for this transaction
string value; // Value for this transaction
vector<string> replys; // Replys to transaction
MessageType type; // Transaction type
int stime; // Start time of transaction
Log* log; // Place to log this transaction
int numOK; // Number of OK replys
int numReplys; // Number of replys
public:
Transaction(int i, string k, MessageType ty, int st, Log* l);
Transaction(int i, string k, string v, MessageType ty, int st, Log* l);
Transaction();
int addReply(string reply);
int addReply(bool status);
string close(bool*);
int getID();
int getStartTime();
string getKey();
string getValue();
MessageType getType();
};
/**
* CLASS NAME: MP2Node
*
* DESCRIPTION: This class encapsulates all the key-value store functionality
* including:
* 1) Ring
* 2) Stabilization Protocol
* 3) Server side CRUD APIs
* 4) Client side CRUD APIs
*/
class MP2Node {
private:
// Vector of neighboring Nodes
vector<Neighbor> neighbors;
// Ring
vector<Node> ring;
// Hash Table
HashTable * ht;
// Member representing this member
Member *memberNode;
// Params object
Params *par;
// Object of EmulNet
EmulNet * emulNet;
// Object of Log
Log * log;
// Transactions that are currently open at this node
unordered_map<int, Transaction> tmap;
// Last sequence number from membership protocol, used to check if there has been a change
int last_seq;
public:
MP2Node(Member *memberNode, Params *par, EmulNet *emulNet, Log *log, Address *addressOfMember);
Member * getMemberNode() {
return this->memberNode;
}
// ring functionalities
void updateRing();
vector<Node> getMembershipList();
size_t hashFunction(string key);
void updateNeighbors();
// client side CRUD APIs
void clientCreate(string key, string value);
void clientRead(string key);
void clientUpdate(string key, string value);
void clientDelete(string key);
// receive messages from Emulnet
bool recvLoop();
static int enqueueWrapper(void *env, char *buff, int size);
// Message sending wrappers
void sendMessage(Address *toAddr, Message* msg);
void sendMsgToReplicas(string* key, Message* msg);
void sendMsgToReplicaTypes(string* key, Message* msg);
void sendStabilizationMessage(Neighbor const &n, Entry const &e, Message* msg, ReplicaType r);
void sendREPLY(int transID, Address* toAddr, bool status);
// handle messages from receiving queue
void checkMessages();
// coordinator dispatches messages to corresponding nodes
void dispatchMessages(Message message);
// find the addresses of nodes that are responsible for a key
vector<Node> findNodes(string key);
void getHashBounds(size_t* lb, size_t* ub);
// server
bool createKeyValue(string key, string value, ReplicaType replica);
string readKey(string key);
bool updateKeyValue(string key, string value, ReplicaType replica);
bool deletekey(string key);
// stabilization protocol - handle multiple failures
void stabilizationProtocol();
// logging methods
void printRing(void);
void printHashTable(void);
string type2string(MessageType type);
void logAction(MessageType type, int tid, bool isCoord, string key, string value, bool status);
~MP2Node();
};
#endif /* MP2NODE_H_ */