-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNode.py
354 lines (289 loc) · 14 KB
/
Node.py
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
"""
Implementation of raft node.
Notes:
- rpc loop takes between 0.004 and 0.006 seconds to execute.
"""
import json
import logging
import os
import random
import socket
import time
import threading
import argparse
from xmlrpc.client import ServerProxy
from xmlrpc.server import SimpleXMLRPCServer
class Node:
def __init__(self, run, name, hostname, port, peers):
self.logger = logging.getLogger(f'Node({name})')
os.makedirs(f'results/{run}', exist_ok=True)
self.state_machine_file = f'./results/{run}/{name}.csv'
os.makedirs(f'logs/{run}', exist_ok=True)
logging.basicConfig(filename=f'logs/{run}/raft({name}).log', level=logging.INFO)
self.name = name
self.leader_id = None
self.state = "follower"
self.currentTerm = 0
self.votedFor = None
self.log = []
self.commitIndex = 0
self.lastApplied = 0
self.nextIndex = {}
self.matchIndex = {}
self.message_received = False
self.electionTimeout = 0
self.update_election_timeout()
self.heartbeatTimeout = 50
socket.setdefaulttimeout(self.heartbeatTimeout / 1000)
self.ip = hostname
self.nodes = [{'name': peer['name'], 'url': f'http://{peer["hostname"]}:{peer["port"]}'} for peer in peers]
# start main loop in a separate thread
thread = threading.Thread(target=self.main_loop)
thread.daemon = True
thread.start()
# todo: is this the best way to do this?
# start RPC server
self.server = SimpleXMLRPCServer((hostname, port), allow_none=True)
# register two rpc functions
self.server.register_function(self.request_vote_rpc, "request_vote_rpc")
self.server.register_function(self.append_entries_rpc, "append_entries_rpc")
self.server.register_function(self.store_message, "store_message")
# Run the server's main loop
self.server.serve_forever()
def update_election_timeout(self):
self.electionTimeout = random.randint(100, 150)
def main_loop(self):
while True:
if self.state == "follower":
self.follower_loop()
elif self.state == "candidate":
self.candidate_loop()
elif self.state == "leader":
self.leader_loop()
else:
raise ValueError("Invalid state")
def follower_loop(self):
self.logger.info("[%s] Follower loop", self.name)
while self.state == "follower":
self.message_received = False
time.sleep(self.electionTimeout / 1000)
# if no message received, become candidate
if not self.message_received:
self.state = "candidate"
def candidate_loop(self):
self.logger.info("[%s] Candidate loop", self.name)
self.currentTerm += 1
self.votedFor = self.name
self.state = "candidate"
self.update_election_timeout()
votes = [True]
# send RequestVote RPCs to all other servers
jobs = []
for node in self.nodes:
# start a new thread to send request vote RPC
thread = threading.Thread(target=self.send_request_vote_rpc, args=(node, votes))
thread.start()
jobs.append(thread)
# wait for all threads to finish
for job in jobs:
job.join()
# if votes > majority, become leader
if sum(votes) > (len(self.nodes) + 1) / 2:
self.state = "leader"
next_index = self.log[-1]['index'] + 1 if self.log else 0
self.nextIndex = {node['name']: next_index for node in self.nodes}
self.matchIndex = {node['name']: 0 for node in self.nodes}
self.logger.info("[%s] Became leader", self.name)
else:
self.state = "follower"
self.logger.info("[%s] Did not become leader", self.name)
def send_request_vote_rpc(self, node, votes):
with ServerProxy(node['url']) as proxy:
try:
last_log_term = self.log[-1]['term'] if self.log else 0
last_log_index = self.log[-1]['index'] if self.log else 0
response = proxy.request_vote_rpc(self.currentTerm, self.name, last_log_index, last_log_term)
# if response contains term T > currentTerm, convert to follower
if response[0] > self.currentTerm:
self.update_current_term_and_become_follower(response[0])
return votes.append(False)
# if response contains voteGranted, increment votes
if response[1]:
return votes.append(True)
except Exception as e:
self.logger.error("[%s] Error in request_vote_rpc to %s: %s", self.name, node['url'], e)
return votes.append(False)
def leader_loop(self):
self.logger.info("[%s] Leader loop", self.name)
while self.state == "leader":
time.sleep(self.heartbeatTimeout / 1000)
self.send_heartbeats()
def update_current_term_and_become_follower(self, term):
self.votedFor = None
self.currentTerm = term
self.state = "follower"
self.logger.info("[%s] Updated current term to %d", self.name, self.currentTerm)
def send_heartbeats(self):
# send AppendEntries RPCs to all other servers
jobs = []
for node in self.nodes:
# start a new thread to send append entries RPC
thread = threading.Thread(target=self.send_append_entries_rpc, args=(node,))
thread.start()
# wait for all threads to finish
for job in jobs:
job.join(timeout=self.heartbeatTimeout / 1000)
def commit_entry(self):
prev_commit_index = self.commitIndex
# if there exists an N such that N > commitIndex, a majority of matchIndex[i] ≥ N,
# and log[N].term == currentTerm set commitIndex = N
for N in range(self.commitIndex, len(self.log)):
majority = 1
for follower_node in self.nodes:
if self.matchIndex[follower_node['name']] >= N:
majority += 1
if majority > (len(self.nodes) + 1) / 2:
self.commitIndex = N
# if commitIndex changed, apply log entries starting from commitIndex
if self.commitIndex > prev_commit_index:
self.logger.info("[%s] Updated commitIndex to %d", self.name, self.commitIndex)
self.apply_log_entries(prev_commit_index, self.commitIndex)
def send_append_entries_rpc(self, node):
with ServerProxy(node['url']) as proxy:
try:
prev_log_index = self.log[-1]['index'] if self.log else 0
prev_log_term = self.log[-1]['term'] if self.log else 0
term = self.currentTerm
if self.nextIndex[node['name']] < len(self.log):
entries = self.log[self.nextIndex[node['name']]:]
else:
entries = []
response = proxy.append_entries_rpc(term,
self.name,
prev_log_index,
prev_log_term,
entries,
self.commitIndex)
# if response contains term T > currentTerm set currentTerm = T and convert to follower
if response[0] > self.currentTerm:
self.update_current_term_and_become_follower(response[0])
return
# if response is successful, update nextIndex and matchIndex
if response[1]:
if entries:
self.nextIndex[node['name']] = entries[-1]['index'] + 1 if self.log else 0
self.matchIndex[node['name']] = entries[-1]['index'] if self.log else 0
# if fails because of log inconsistency, decrement nextIndex and retry
else:
self.nextIndex[node['name']] -= 1
return
except Exception as e:
self.logger.error("[%s] communication with %s Exception: %s", self.name, node['name'], e)
self.commit_entry()
def append_entries_rpc(self, term, leader_id, prev_log_index, prev_log_term, entries, leader_commit) -> (str, bool):
self.message_received = True
try:
# todo: is this correct see rules for servers
# if AppendEntries RPC request received form new leader, convert to follower
if self.state == "candidate" and leader_id != self.name:
self.state = "follower"
self.leader_id = leader_id
# reply false if term < currentTerm
if term < self.currentTerm:
return self.currentTerm, False
# reply false if log doesn't contain an entry at prevLogIndex whose term matches prevLogTerm
if -1 < prev_log_index < len(self.log) and self.log[prev_log_index]['term'] != prev_log_term:
return self.currentTerm, False
# if an existing entry conflicts with a new one (same index but different terms),
# delete the existing entry and all that follow it
for entry in entries:
if self.log and entry['index'] < len(self.log) and self.log[entry['index']]['term'] != entry['term']:
self.log = self.log[:entry['index']]
break
# append any new entries not already in the log
for entry in entries:
if len(self.log) == 0 or entry['index'] > self.log[-1]['index']:
self.log.append(entry)
# update commitIndex
prev_commit_index = self.commitIndex
if leader_commit > self.commitIndex:
self.commitIndex = min(leader_commit, self.log[-1]['index'])
# if commitIndex changed, apply log entries starting from commitIndex
if self.commitIndex > prev_commit_index:
self.logger.info("[%s] Updated commitIndex to %d", self.name, self.commitIndex)
self.apply_log_entries(prev_commit_index, self.commitIndex)
except Exception as e:
self.logger.error("[%s] Exception in append entries rpc: %s", self.name, e)
return self.currentTerm, True
def request_vote_rpc(self, term, candidate_id, last_log_index, last_log_term) -> (str, bool):
self.message_received = True
# reply false if term < currentTerm
if term < self.currentTerm:
return self.currentTerm, False
# if request contains term T > currentTerm, set currentTerm = T and convert to follower
elif term > self.currentTerm:
self.update_current_term_and_become_follower(term)
# reply false if votedFor is already set
if self.votedFor is not None:
return self.currentTerm, False
# reply false if log is more up-to-date than the candidate's
if self.log and self.log[-1]['index'] > last_log_index and self.log[last_log_index]['term'] > last_log_term:
return self.currentTerm, False
# grant vote if candidate's log is at least as up-to-date as receiver's
# and has not been voted for in the election
self.votedFor = candidate_id
self.state = "follower"
return self.currentTerm, True
def store_message(self, message):
# if not leader, send to leader
if self.state != "leader":
return self.leader_id, False
# if leader, append to log
else:
# add ingestion time to the message
message['ingestion_time'] = time.time()
next_index = self.log[-1]['index'] + 1 if self.log else 0
self.log.append({'index': next_index, 'term': self.currentTerm, 'data': message})
# log message and commitIndex
self.logger.info("[%s] Reserved message %s for index %d", self.name, message, next_index)
return self.name, True
def apply_log_entries(self, prev_commit_index, commitIndex):
# append the index, generation_time, ingestion_time and commit_time to a csv file
file = open(self.state_machine_file, 'a')
for entry in self.log[prev_commit_index:commitIndex]:
# append commit time to the message
entry['data']['commit_time'] = time.time()
data = entry['data']
file.write(
f'{str(entry["index"])},{data["id"]},{data["rate"]},{self.state},{data["generation_time"]},{data["ingestion_time"]},{data["commit_time"]}\n')
file.close()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--name', default=0, type=str, help='id of the server in the cluster, used for logging')
parser.add_argument('--local', action='store_true', help='Run locally')
parser.add_argument('--port', default=8000, type=int, help='Listening port')
parser.add_argument('--cluster', nargs='+', type=str, default=[], help='List of peers in the cluster')
parser.add_argument('--run', type=int, default=0, help='Run id of the test')
args = parser.parse_args()
# make the log directory
os.makedirs('logs', exist_ok=True)
# id: node102
# hostname: node102.cm.cluster
# port: 7077
# peers: [node102, node103, node104, node105, node106]
# --id node102 --hostname node102.cm.cluster --port 7077 --cluster node102 node103 node104 node105 node106
peers = [
{
'name': peer,
'hostname': 'localhost' if args.local else f'{peer}.cm.cluster',
'port': args.port + id
}
for id, peer in enumerate(args.cluster)
]
# own port and hostname
hostname = 'localhost' if args.local else f'{args.name}.cm.cluster'
port = args.port + args.cluster.index(args.name)
# filter out self
peers = [peer for peer in peers if peer['name'] != args.name]
# create a node
node = Node(args.run, args.name, hostname, port, peers)