-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.py
396 lines (330 loc) · 17.1 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
import zmq
import sys
import json
import time
import threading
from collections import defaultdict
import heapq
HEARTBEAT = {"ping": "pong"}
class Server:
"""
This class represents a server in the cluster. It is responsible for handling the requests from the clients and other servers.
It will handle the following requests from the clients:
- set(key, value)
- get(key)
It will handle the following message from the other servers:
- broadcast message
- broadcast acknoledgement
"""
def __init__(self, server_number, port_number, contacts):
"""
The server need to initialize the key-value store and the sockets for sending and receiving messages.
The contacts contains port numbers of the other servers in the cluster.
"""
# self.kv_store = defaultdict(int)
self.kv_store = {"a": 0, "b": 0}
self.kv_store_lock = threading.Lock()
self.server_number = int(server_number)
self.recv_port, self.send_port, self.api_port = port_number
self.contacts = contacts
self.context = zmq.Context()
self.send_socket = self.context.socket(zmq.PUB) # to send messages to the other servers
self.send_socket.bind(f"tcp://*:{self.send_port}")
# threading.Thread(target=self._daemon).start()
self.api_socket = self.context.socket(zmq.REP) # to communicate with the clients
self.api_socket.bind(f"tcp://*:{self.api_port}")
print(f"Server {self.server_number} is ready to receive the requests from the clients")
self.recv_socket = self.context.socket(zmq.SUB) # to receive messages from the other servers
for contact in self.contacts:
recv_port = self.contacts[contact][1]
self.recv_socket.connect(f"tcp://localhost:{recv_port}")
print(f"Server {self.server_number} is connected to the server {contact}")
self.recv_socket.setsockopt_string(zmq.SUBSCRIBE, '')
self.poller = zmq.Poller()
self.poller.register(self.recv_socket, zmq.POLLIN)
self.poller.register(self.api_socket, zmq.POLLIN)
# self.poller.register(self.send_socket, zmq.POLLOUT)
class Server_linearizability(Server):
"""
This class represents a server in the cluster with linearizability consistency level.
"""
def __init__(self, server_number, port_number, contacts):
super().__init__(server_number, port_number, contacts)
self.lamport_clock = 0
self.lamport_clock_lock = threading.Lock()
self.queue = [] # to store the requests
heapq.heapify(self.queue) # to sort the requests based on the timestamp
self.queue_lock = threading.Lock()
self.acks = defaultdict(int) # to store number of the acknowledgements
self.acks_lock = threading.Lock()
self.total_servers = len(self.contacts)
self.api_thread = threading.Thread(target=self._client_handler)
self.api_thread.start()
self.recv_thread = threading.Thread(target=self._server_handler)
self.recv_thread.start()
self.queue_thread = threading.Thread(target=self._queue_handler)
self.queue_thread.start()
def _update_clock(self, timestamp=0): # if no timestamp is given, then it is a local event, just increment the clock
with self.lamport_clock_lock:
self.lamport_clock = max(self.lamport_clock, timestamp) + 1
def _broadcast(self, message):
# for contact in self.contacts:
# recv_port = self.contacts[contact][0]
# self.send_socket.connect(f"tcp://localhost:{recv_port}")
# # print(f"Server {self.server_number} is connected to the server {contact}")
# self.send_socket.send_json(message)
# # print(f"Server {self.server_number} sent message to server {contact}: {message}")
# self.send_socket.disconnect(f"tcp://localhost:{recv_port}")
self.send_socket.send_json(message)
def _queue_handler(self):
while 1:
if self.queue:
with self.queue_lock:
timestamp, id, operation, key, value = self.queue[0]
with self.acks_lock:
if self.acks[(timestamp, id, operation, key, value)] == self.total_servers:
timestamp, id, operation, key, value = heapq.heappop(self.queue)
if operation == "set":
with self.kv_store_lock:
self.kv_store[key] = value
if id == self.server_number:
self.api_socket.send_string("success")
print(f"Server {self.server_number} set the value of the key {key} to {value}")
elif operation == "get":
if id == self.server_number:
self.api_socket.send_string(f"{key}:{self.kv_store[key]}")
print(f"Server {self.server_number} got the value of the key {key} as {self.kv_store[key]}")
# time.sleep(1)
def _heartbeat(self): # keep api_socket alive
while 1:
self.api_socket.send_string("ping")
response = self.api_socket.recv_string()
if response == "pong":
continue
elif response == "gotcha":
break
time.sleep(1)
def _client_handler(self):
"""
This method is responsible for handling the requests from the clients.
"""
while 1:
socks = dict(self.poller.poll())
if self.api_socket in socks:
message = self.api_socket.recv_json()
# threading.Thread(target=self._heartbeat).start()
# print(f"Server {self.server_number} received message: {message}")
self._update_clock()
broadcast_message = {"timestamp": self.lamport_clock,
"operation": message["type"],
"key": message["key"],
"value": message["value"],
"ack": 0,
"id": self.server_number}
self._broadcast(broadcast_message)
def _daemon(self):
while 1:
self.send_socket.send_json(HEARTBEAT)
time.sleep(0.5)
def _server_handler(self):
"""
This method is responsible for handling the requests from the other servers.
"""
while 1:
# socks = dict(self.poller.poll())
# if self.recv_socket in socks:
message = self.recv_socket.recv_json()
if "ping" in message:
continue
# print(message)
id = message["id"]
# print(f"Server {self.server_number} received message from Server {id}: {message}")
self._update_clock(message["timestamp"])
if message["ack"] == 1:
with self.acks_lock:
self.acks[(message["msg_timestamp"], message["id"], message["operation"], message["key"], message["value"])] += 1
else: # id is the one who broadcasted the message
with self.queue_lock:
heapq.heappush(self.queue, (message["timestamp"],
message["id"],
message["operation"],
message["key"],
message["value"]))
self._update_clock()
ack_message = {"timestamp": self.lamport_clock,
"id": message["id"],
"operation": message["operation"],
"key": message["key"],
"value": message["value"],
"ack": 1,
"msg_timestamp": message["timestamp"]}
self._broadcast(ack_message)
class Server_sequential(Server):
"""
This class represents a server in the cluster with sequential consistency level.
"""
def __init__(self, server_number, port_number, contacts):
super().__init__(server_number, port_number, contacts)
self.lamport_clock = 0
self.lamport_clock_lock = threading.Lock()
self.queue = [] # to store the requests
heapq.heapify(self.queue) # to sort the requests based on the timestamp
self.queue_lock = threading.Lock()
self.acks = defaultdict(int) # to store number of the acknowledgements
self.acks_lock = threading.Lock()
self.total_servers = len(self.contacts)
self.api_thread = threading.Thread(target=self._client_handler)
self.api_thread.start()
self.recv_thread = threading.Thread(target=self._server_handler)
self.recv_thread.start()
self.queue_thread = threading.Thread(target=self._queue_handler)
self.queue_thread.start()
def _update_clock(self, timestamp=0): # if no timestamp is given, then it is a local event, just increment the clock
with self.lamport_clock_lock:
self.lamport_clock = max(self.lamport_clock, timestamp) + 1
def _broadcast(self, message):
self.send_socket.send_json(message)
def _queue_handler(self):
while 1:
if self.queue:
with self.queue_lock:
timestamp, id, operation, key, value = self.queue[0]
with self.acks_lock:
if self.acks[(timestamp, id, operation, key, value)] == self.total_servers:
timestamp, id, operation, key, value = heapq.heappop(self.queue)
self.kv_store[key] = value
if id == self.server_number:
self.api_socket.send_string("success")
print(f"Server {self.server_number} set the value of the key {key} to {value}")
def _client_handler(self):
"""
This method is responsible for handling the requests from the clients.
"""
while 1:
socks = dict(self.poller.poll())
if self.api_socket in socks:
message = self.api_socket.recv_json()
self._update_clock()
if message["type"] == "get": # local read
self.api_socket.send_string(f"{message['key']}:{self.kv_store[message['key']]}")
print(f"Server {self.server_number} got the value of the key {message['key']} as {self.kv_store[message['key']]}")
else:
broadcast_message = {"timestamp": self.lamport_clock,
"operation": message["type"],
"key": message["key"],
"value": message["value"],
"ack": 0,
"id": self.server_number}
self._broadcast(broadcast_message)
def _server_handler(self):
"""
This method is responsible for handling the requests from the other servers.
"""
while 1:
message = self.recv_socket.recv_json()
id = message["id"]
# print(f"Server {self.server_number} received message from Server {id}: {message}")
self._update_clock(message["timestamp"])
if message["ack"] == 1:
with self.acks_lock:
self.acks[(message["msg_timestamp"], message["id"], message["operation"], message["key"], message["value"])] += 1
else: # id is the one who broadcasted the message
with self.queue_lock:
heapq.heappush(self.queue, (message["timestamp"],
message["id"],
message["operation"],
message["key"],
message["value"]))
self._update_clock()
ack_message = {"timestamp": self.lamport_clock,
"id": message["id"],
"operation": message["operation"],
"key": message["key"],
"value": message["value"],
"ack": 1,
"msg_timestamp": message["timestamp"]}
self._broadcast(ack_message)
class Server_eventual(Server):
"""
This class represents a server in the cluster with eventual consistency level.
"""
def __init__(self, server_number, port_number, contacts):
super().__init__(server_number, port_number, contacts)
self.lamport_clock = 0
self.lamport_clock_lock = threading.Lock()
self.last_modified = defaultdict(tuple)
self.last_modified_lock = threading.Lock()
self.api_thread = threading.Thread(target=self._client_handler)
self.api_thread.start()
self.recv_thread = threading.Thread(target=self._server_handler)
self.recv_thread.start()
def _update_clock(self, timestamp=0): # if no timestamp is given, then it is a local event, just increment the clock
with self.lamport_clock_lock:
self.lamport_clock = max(self.lamport_clock, timestamp) + 1
def _broadcast(self, message):
self.send_socket.send_json(message)
self._update_clock()
def _client_handler(self):
"""
This method is responsible for handling the requests from the clients.
"""
while 1:
socks = dict(self.poller.poll())
if self.api_socket in socks:
message = self.api_socket.recv_json()
# print(f"Server {self.server_number} received message: {message}")
self._update_clock()
if message["type"] == "get": # local read
self.api_socket.send_string(f"{message['key']}:{self.kv_store[message['key']]}")
print(f"Server {self.server_number} got the value of the key {message['key']} as {self.kv_store[message['key']]}")
elif message["type"] == "set":
with self.kv_store_lock:
self.kv_store[message["key"]] = message["value"]
self.api_socket.send_string("success")
print(f"Server {self.server_number} set the value of the key {message['key']} to {message['value']}")
self._update_clock()
broadcast_message = {"timestamp": self.lamport_clock,
"operation": message["type"],
"key": message["key"],
"value": message["value"],
"id": self.server_number}
self._broadcast(broadcast_message)
def _server_handler(self):
"""
This method is responsible for handling the requests from the other servers.
"""
while 1:
message = self.recv_socket.recv_json()
id = message["id"]
print(f"Server {self.server_number} received message from Server {id}: {message}")
self._update_clock(message["timestamp"])
modification = (message["timestamp"], message["id"], message["value"])
with self.last_modified_lock:
if self.last_modified[message["key"]] < modification:
self.last_modified[message["key"]] = (message["timestamp"], message["value"])
with self.kv_store_lock:
self.kv_store[message["key"]] = message["value"]
class Server_causal(Server):
def __init__(self, server_number, port_number, contacts):
super().__init__(server_number, port_number, contacts)
# TODO: implement the eventual consistency level
if __name__ == "__main__":
server_number, port_number, consistency_level = sys.argv[1:]
port_number = eval(port_number)
own_port = port_number[server_number]
if consistency_level == "linearizability":
server = Server_linearizability(server_number,
own_port,
contacts=port_number)
elif consistency_level == "sequential":
server = Server_sequential(server_number,
own_port,
contacts=port_number)
elif consistency_level == "eventual":
server = Server_eventual(server_number,
own_port,
contacts=port_number)
elif consistency_level == "causal":
server = Server_causal(server_number,
own_port,
contacts=port_number)