-
Notifications
You must be signed in to change notification settings - Fork 1
/
cache_client.py
107 lines (79 loc) · 2.74 KB
/
cache_client.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
import sys
import socket
from pprint import pprint
# from LRUCache import LRUCache
from bloom_filter import BloomFilter
from node_ring import NodeRing
from sample_data import USERS
from server_config import NODES
from pickle_hash import serialize_GET, serialize_PUT, serialize_DELETE
from lru_cache import lru_cache
from rendezvous_hash import RendezvousHash
from consistent_hash import ConsistentHash
BUFFER_SIZE = 1024
class UDPClient():
def __init__(self, host, port):
self.host = host
self.port = int(port)
def set_ring(self, client_ring):
return client_ring
def send(self, request):
print('Connecting to server at {}:{}'.format(self.host, self.port))
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.sendto(request, (self.host, self.port))
response, ip = s.recvfrom(BUFFER_SIZE)
return response
except socket.error:
print("Error! {}".format(socket.error))
exit()
bloomfilter = BloomFilter(100, 0.2)
def put(key, value, client_ring):
bloomfilter.add(key)
return client_ring.get_node(key).send(value)
def delete(key, value, client_ring):
if bloomfilter.is_member(key):
return client_ring.get_node(key).send(value)
else:
return None
@lru_cache(5)
def get(key, value, client_ring):
if bloomfilter.is_member(key):
return client_ring.get_node(key).send(value)
else:
return None
def process(udp_clients):
udp_client = udp_clients
hash_codes = set()
client_ring = NodeRing(udp_client)
# these couple lines are for the rendezvous hash
# weights = [5, 5, 5, 5]
# seeds = ["node1", "serve2", "salt1", "clasuter"]
# instance_rendezvous = RendezvousHash(weights, seeds)
# uncomment/comment this to apply consistent hash
instance_consistent_hash = ConsistentHash(NODES)
client_ring.apply_new_hash(instance_consistent_hash)
# PUT all users.
for u in USERS:
data_bytes, key = serialize_PUT(u)
response = put(key, data_bytes, client_ring)
hash_codes.add(response)
print(hash_codes)
print(
f"Number of Users={len(USERS)}\nNumber of Users Cached={len(hash_codes)}")
for hc in hash_codes:
# print(hc)
data_bytes, key = serialize_GET(hc)
response = get(key, data_bytes, client_ring)
# print(response)
for hc in hash_codes:
data_bytes, key = serialize_DELETE(hc)
print("keys from DELETE OPERATAIONS:", data_bytes, key)
response = delete(key, data_bytes, client_ring)
print(response)
if __name__ == "__main__":
clients = [
UDPClient(server['host'], server['port'])
for server in NODES
]
process(clients)