-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
403 lines (312 loc) · 15.5 KB
/
server.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
from kazoo.client import KazooClient
from kazoo.recipe.watchers import DataWatch
from kazoo.recipe.watchers import ChildrenWatch
import leveldb
import grpc
import chainreplication_pb2
import chainreplication_pb2_grpc
import database_pb2
import database_pb2_grpc
from enum import Enum
from collections import deque
import time
import signal
from sys import exit
import threading
from concurrent import futures
import logging
# TODO:
# 1. Testing
# 2. apportioned queries
logLevel = logging.DEBUG # update log level to display appropriate logs to console
def getLogger(source):
logger = logging.getLogger(source)
c_handler = logging.StreamHandler()
c_format = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s', datefmt='%H:%M:%S')
c_handler.setFormatter(c_format)
logger.addHandler(c_handler)
logger.setLevel(logLevel)
return logger
class ChainReplicator(chainreplication_pb2_grpc.ChainReplicationServicer, database_pb2_grpc.DatabaseServicer):
def __init__(self, dbNodePort:str, crNodePort:str) -> None:
logger = getLogger(self.__init__.__qualname__)
# Current tail notices when a new node is created in ZK
# It starts propagating new reqs to the new node.
# This is queued up until the synchroniztion (i.e bootstrap) is completed
self.bootstrap = True
# Used to serialize appendEntries during crash failures
self.crashrecovery = False
self.mycrnodeport = crNodePort # my CR node port
# set up zookeeper
self.zk = KazooClient(hosts='127.0.0.1:2181')
self.zk.start()
logger.debug("Started Zookeeper Client")
# ensure necessart zk paths
self.zk.ensure_path("/cluster")
self.zk.ensure_path("/tail")
self.zk.ensure_path("/head")
# instantiate levelDB
self.setupDB(crNodePort.split(':')[1])
self.dblock = threading.Lock()
logger.debug("LevelDB set up")
self.crthreadpool = futures.ThreadPoolExecutor(max_workers=1)
self.ackthreadpool = futures.ThreadPoolExecutor(max_workers=1)
# start chain replication grpc server thread
self.crthread = threading.Thread(target=self.__chainReplicationListener, args=(crNodePort, ))
self.crthread.start()
# get peers from zk
peers = self.zk.get_children(path="/cluster")
# let peers know of my existence
self.myzknode = self.zk.create(path="/cluster/guid_", value=crNodePort.encode(), ephemeral=True, sequence=True).split('/')[2]
logger.info("Zookeeper cluster updated, mynode = " + self.myzknode)
if len(peers) == 0:
# first node
logger.info("I am the only server")
self.zk.set("/head", crNodePort.encode())
logger.info("ZK head node updated")
else:
# sync from curr tail
self.syncWithCurrentTail(max(peers))
logger.info("Sync completed with current tail")
self.zk.set("/tail", crNodePort.encode())
logger.info("ZK tail node updated")
self.next = ""
self.prev = ""
# sent queue contains entries sent to peer not acked by tail
self.sentQueue = deque()
# setup ZK watchers and update self.next && self.prev
self.tailwatcher = DataWatch(self.zk, "/tail", self.tailChanged)
self.headwatcher = DataWatch(self.zk, "/head", self.headChanged)
self.clusterwatcher = ChildrenWatch(self.zk, "/cluster", self.clusterChanged)
logger.debug("ZK Watchers enabled")
# start gRPC database server thread
self.dbthread = threading.Thread(target=self.__databaseListener, args=(dbNodePort, ))
self.dbthread.start()
signal.signal(signal.SIGINT, self.signalHandler)
# needed to ensure new data is committed to disk only after synchronization (i.e. bootstrap) is completed
self.bootstrap = False
return
def tailChanged(self, data, stat):
# update tail when changed
logger = getLogger(self.tailChanged.__qualname__)
self.tail = data.decode()
logger.info("Tail updated to "+ data.decode())
def headChanged(self, data, stat):
# update head when changed
logger = getLogger(self.headChanged.__qualname__)
self.head = data.decode()
logger.info("Head updated to "+ data.decode())
def clusterChanged(self, children):
# update peer when changed
logger = getLogger(self.clusterChanged.__qualname__)
peers = sorted(children)
currNodeIdx, currNode = next( (idx, pnode) for (idx, pnode) in enumerate(peers) if pnode == self.myzknode)
# update next peer
if currNodeIdx == len(peers)-1:
nextpeernodeport = ""
else:
mynextnode = peers[currNodeIdx+1]
nextpeernodeport = self.zk.get("/cluster/" + mynextnode)[0].decode()
# update prev peer
if currNodeIdx == 0:
prevpeernodeport = ""
else:
myprevnode = peers[currNodeIdx-1]
prevpeernodeport = self.zk.get("/cluster/" + myprevnode)[0].decode()
logger.info("Prev peer is now - " + prevpeernodeport)
logger.info("Next peer is now - " + nextpeernodeport)
nextcrashed = (self.next != nextpeernodeport)
# update head and tail if changed
if prevpeernodeport == "" and self.prev != prevpeernodeport:
self.zk.set("/head", self.mycrnodeport.encode())
if nextpeernodeport == "" and self.next != nextpeernodeport:
self.zk.set("/tail", self.mycrnodeport.encode())
self.next = nextpeernodeport
self.prev = prevpeernodeport
# Handle failures
if nextcrashed:
logger.info("Next failed, resending items in sent queue...")
self.crashrecovery = True
while len(self.sentQueue) != 0:
self.crthreadpool.submit(self.__appendEntries, self.sentQueue.popleft())
#self.AppendEntries(self.sentQueue.popleft(), None)
self.crashrecovery = False
logger.info("Resend successful")
def __del__(self):
self.zk.stop()
def __chainReplicationListener(self, crNodePort):
# listen for chain replication requests
logger = getLogger(self.__chainReplicationListener.__qualname__)
self.crserver = grpc.server(futures.ThreadPoolExecutor(max_workers=1))
chainreplication_pb2_grpc.add_ChainReplicationServicer_to_server(self, self.crserver)
self.crserver.add_insecure_port(crNodePort)
self.crserver.start()
logger.info("Chain replication server started, listening on " + crNodePort)
self.crserver.wait_for_termination()
def __databaseListener(self, dbNodePort):
# listen for DB requests
logger = getLogger(self.__databaseListener.__qualname__)
self.dbserver = grpc.server(futures.ThreadPoolExecutor(max_workers=1))
database_pb2_grpc.add_DatabaseServicer_to_server(self, self.dbserver)
self.dbserver.add_insecure_port(dbNodePort)
self.dbserver.start()
logger.info("Database server started, listening on " + dbNodePort)
self.dbserver.wait_for_termination()
def syncWithCurrentTail(self, tailzknode):
logger = getLogger(self.syncWithCurrentTail.__qualname__)
# TODO fetch latest tail nodeport upon GRPC failure
# get current tail
tailNodePort = self.zk.get("/cluster/"+tailzknode)[0]
# send sync request to current tail
with grpc.insecure_channel(tailNodePort) as channel:
stub = chainreplication_pb2_grpc.ChainReplicationStub(channel)
response = chainreplication_pb2.SyncResponse(success=False, entries=[])
while response.success == False:
try:
response = stub.Sync(chainreplication_pb2.SyncRequest())
except Exception as e:
logger.error("GRPC exception" + str(e))
time.sleep(0.5)
continue
logger.info("Response from grpc received!")
# commit response entries to levelDB
for e in response.entries:
self.db.Put(bytearray(e.key, encoding="utf8"), bytearray(e.value, encoding="utf8"))
logger.info("LevelDB synced!")
return
def Sync(self, request, context):
while self.bootstrap == True:
time.sleep(0.1)
# handle sync request from new tail
logger = getLogger(self.Sync.__qualname__)
logger.info("Sync request received")
# get all data from levelDB
with self.dblock:
itr = self.db.RangeIter()
data = []
for key, value in itr:
data.append(chainreplication_pb2.KVPair(key=key.decode(), value=value.decode()))
logger.info("Sending fetched data from LevelDB...")
# send it to new tail
return chainreplication_pb2.SyncResponse(success=True, entries=data)
def __appendEntries(self, request:chainreplication_pb2.AppendEntriesRequest):
# helper thread to propagate entries down the chain
logger = getLogger(self.__appendEntries.__qualname__)
# I am not the tail
if self.next != "":
logger.info("Appending entries to "+ self.next)
# append entries to peer
response = chainreplication_pb2.AppendEntriesResponse(success=False)
while response.success == False:
# self.next may change to server crashes.
# Hence, it is essential that self.next is within the while loop
with grpc.insecure_channel(self.next) as channel:
stub = chainreplication_pb2_grpc.ChainReplicationStub(channel)
try:
response = stub.AppendEntries(
chainreplication_pb2.AppendEntriesRequest(
seqnum=request.seqnum, command=request.command,
key=request.key, value=request.value, client=request.client))
except Exception as e:
logger.error("GRPC exception " + str(e))
time.sleep(0.5)
continue
logger.info("Append Entries successful")
self.sentQueue.append(chainreplication_pb2.AppendEntriesRequest(
seqnum=request.seqnum, command=request.command,
key=request.key, value=request.value, client=request.client))
logger.debug("Added entry to sent queue")
# i am the tail, need to respond to client and initiate ACK flow
if self.next == "":
with grpc.insecure_channel(request.client) as channel:
stub = database_pb2_grpc.DatabaseStub(channel)
# what happens if client crashes? maybe try like 10 times and exit?
for _ in range(10):
try:
stub.PutResult(database_pb2.PutResultRequest(seqnum=request.seqnum, success=True))
break
except Exception as e:
logger.error("GRPC exception " + str(e))
time.sleep(0.5)
logger.info("Sent put response to client")
self.ackthreadpool.submit(self.Ack, chainreplication_pb2.AckRequest(seqnum=request.seqnum), None)
logger.info("Submitted ACK job to threadpool")
return
def AppendEntries(self, request:chainreplication_pb2.AppendEntriesRequest, context):
# handle incoming append entries
logger = getLogger(self.AppendEntries.__qualname__)
while self.bootstrap == True or self.crashrecovery == True:
time.sleep(0.1)
logger.info("AppendEntries request received")
# commit to db
with self.dblock:
self.db.Put(bytearray(request.key, encoding="utf8"), bytearray(request.value, encoding="utf8"))
logger.info("Commited entry")
# send to peer in FIFO order
# threadpoolexecutor uses python's SimpleQueue internally
self.crthreadpool.submit(self.__appendEntries, request)
return chainreplication_pb2.AppendEntriesResponse(success=True)
def Ack(self, request, context):
logger = getLogger(self.Ack.__qualname__)
#print("AAAA", self.sentQueue[0].seqnum, request.seqnum)
if self.sentQueue[0].seqnum == request.seqnum:
logger.critical("Received Ack for a request not in queue front :(")
# maybe an old RPC reaching the server??
return chainreplication_pb2.AckResponse()
self.sentQueue.popleft()
logger.debug("Removed completed update request from sent queue")
if self.prev == "":
# I am head
return chainreplication_pb2.AckResponse() # doesnt matter
while True:
# self.prev may change to server crashes.
# Hence, it is essential that self.prev is within the while loop
with grpc.insecure_channel(self.prev) as channel:
stub = chainreplication_pb2_grpc.ChainReplicationStub(channel)
try:
stub.Ack(chainreplication_pb2.AckRequest(seqnum=request.seqnum))
break
except Exception as e:
logger.error("GRPC exception " + str(e))
time.sleep(0.5)
logger.info("Ack successful to prev peer")
return chainreplication_pb2.AckResponse()
def Get(self, request, context):
# handle incoming get requests
logger = getLogger(self.Get.__qualname__)
# respond only if tail
if self.tail == self.mycrnodeport:
logger.info("Get request received")
val = self.db.Get(bytearray(request.key, 'utf-8')).decode()
return database_pb2.GetResponse(error="", value=val)
logger.debug("Get request sent to non-tail server")
return database_pb2.GetResponse(error="contact tail", value="")
def Put(self, request, context):
# handle incoming put requests
logger = getLogger(self.Put.__qualname__)
# process only if head
if self.head == self.mycrnodeport:
logger.info("Put request received")
self.AppendEntries(chainreplication_pb2.AppendEntriesRequest(seqnum=request.seqnum, command="PUT", key=request.key, value=request.value, client=request.client), None)
return database_pb2.PutResponse(error="")
logger.debug("Put request sent to non-head server")
return database_pb2.PutResponse(error="contact head")
def setupDB(self, uid):
# Helper method to set up levelDB
dbpath = './{}_db'.format(uid)
from shutil import rmtree
rmtree(dbpath, ignore_errors=True)
self.db = leveldb.LevelDB(dbpath)
def signalHandler(self, sig, frame):
getLogger(self.signalHandler.__qualname__).info('Server exiting!!')
self.zk.stop()
exit(0)
if __name__ == "__main__":
# parse CLI inputs
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument('-dnp', '--dbnodeport', help='Database NodePort', required=True)
parser.add_argument('-cnp', '--crnodeport', help='Chain Replication NodePort', required=True)
args = parser.parse_args()
server = ChainReplicator(args.dbnodeport, args.crnodeport)
signal.pause()