Skip to content
This repository was archived by the owner on Apr 2, 2019. It is now read-only.

Commit 40531cd

Browse files
committed
Fixed bugs
1 parent 7402371 commit 40531cd

File tree

4 files changed

+263
-141
lines changed

4 files changed

+263
-141
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ DONE
5454
* Designation of backup servers for appropriate range ----- } (DONE)
5555
* Migration from multithreading to multiprocessing - less flaky now (DONE)
5656
* Master uploads data to zookeeper's /cluster node (DONE - actually every server uploads their own server mapping)
57+
* Implement Data integrity (DONE - need to test)
5758

58-
TODO
59-
* Implement Data integrity (this is only half-done)
60-
* Simulate server death - either use interactive shell to stop a service, or use a flag and send a "resource temporarily unavailable for some time limit"
59+
TODO
60+
* Simulate server death - either use interactive shell to stop a service, or use a flag and send a "resource temporarily unavailable for some time limit" (needs some refactoring... how will it be handled? the socket cannot be reused)

client.py

Lines changed: 94 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
import cluster
1+
from nd_service_registry import KazooServiceRegistry
2+
23
import socket
34
import random
45
import json
6+
import time
7+
import pprint
58

69
BUFFER_SIZE = 1024
710

@@ -21,15 +24,11 @@ class Client:
2124
def __init__(self, buf_size=None):
2225
self.buf_size = buf_size if buf_size else self.__buf_size
2326

24-
self.cluster = cluster.Cluster()
25-
self.cluster.master = self.cluster.get_master()
26-
27-
def __del__(self):
28-
self.socket.close()
27+
self.master = tuple(KazooServiceRegistry().get('/cluster/meta')['data']['master'])
2928

3029
def __get_server(self, key, dtype='primary'):
3130
socket = get_socket()
32-
socket.connect(self.cluster.master)
31+
socket.connect(self.master)
3332
request = { 'op' : 'MAP',
3433
'type' : dtype,
3534
'key' : str(key)
@@ -40,9 +39,12 @@ def __get_server(self, key, dtype='primary'):
4039
response = json.loads(socket.recv(self.buf_size).decode('utf-8'))
4140
socket.close()
4241

43-
return tuple(response['data']['address'])
44-
45-
def put(self, key, value, address):
42+
try:
43+
return tuple(response['data']['address'])
44+
except:
45+
raise Exception(str(response['status']))
46+
47+
def put(self, key, value):
4648
socket = get_socket()
4749
address = self.__get_server(key, dtype='primary')
4850

@@ -74,7 +76,7 @@ def get(self, key):
7476
socket.connect(secondary_address)
7577

7678
request = { 'op' : 'GET',
77-
'data' : str(key)
79+
'key' : str(key)
7880
}
7981
socket.sendall(json.dumps(request).encode('utf-8'))
8082
response = json.loads(socket.recv(self.buf_size).decode('utf-8'))
@@ -85,4 +87,85 @@ def get(self, key):
8587
if __name__ == "__main__":
8688
client = Client()
8789

90+
states = {
91+
'AK': 'Alaska',
92+
'AL': 'Alabama',
93+
'AR': 'Arkansas',
94+
'AS': 'American Samoa',
95+
'AZ': 'Arizona',
96+
'CA': 'California',
97+
'CO': 'Colorado',
98+
'CT': 'Connecticut',
99+
'DC': 'District of Columbia',
100+
'DE': 'Delaware',
101+
'FL': 'Florida',
102+
'GA': 'Georgia',
103+
'GU': 'Guam',
104+
'HI': 'Hawaii',
105+
'IA': 'Iowa',
106+
'ID': 'Idaho',
107+
'IL': 'Illinois',
108+
'IN': 'Indiana',
109+
'KS': 'Kansas',
110+
'KY': 'Kentucky',
111+
'LA': 'Louisiana',
112+
'MA': 'Massachusetts',
113+
'MD': 'Maryland',
114+
'ME': 'Maine',
115+
'MI': 'Michigan',
116+
'MN': 'Minnesota',
117+
'MO': 'Missouri',
118+
'MP': 'Northern Mariana Islands',
119+
'MS': 'Mississippi',
120+
'MT': 'Montana',
121+
'NA': 'National',
122+
'NC': 'North Carolina',
123+
'ND': 'North Dakota',
124+
'NE': 'Nebraska',
125+
'NH': 'New Hampshire',
126+
'NJ': 'New Jersey',
127+
'NM': 'New Mexico',
128+
'NV': 'Nevada',
129+
'NY': 'New York',
130+
'OH': 'Ohio',
131+
'OK': 'Oklahoma',
132+
'OR': 'Oregon',
133+
'PA': 'Pennsylvania',
134+
'PR': 'Puerto Rico',
135+
'RI': 'Rhode Island',
136+
'SC': 'South Carolina',
137+
'SD': 'South Dakota',
138+
'TN': 'Tennessee',
139+
'TX': 'Texas',
140+
'UT': 'Utah',
141+
'VA': 'Virginia',
142+
'VI': 'Virgin Islands',
143+
'VT': 'Vermont',
144+
'WA': 'Washington',
145+
'WI': 'Wisconsin',
146+
'WV': 'West Virginia',
147+
'WY': 'Wyoming',
148+
149+
'AB': 'Alberta',
150+
'BC': 'British Columbia',
151+
'MB': 'Manitoba',
152+
'NB': 'New Brunswick',
153+
'NL': 'Newfoundland and Labrador',
154+
'NT': 'Northwest Territories',
155+
'NS': 'Nova Scotia',
156+
'NU': 'Nunavut',
157+
'ON': 'Ontario',
158+
'PE': 'Prince Edward Island',
159+
'QC': 'Quebec',
160+
'SK': 'Saskatchewan',
161+
'YT': 'Yukon'
162+
163+
}
164+
165+
166+
for k, v in states.items():
167+
print(client.put(k, v))
168+
169+
print(client.get('SD'))
170+
88171

cluster.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,13 @@ def get_hash_partition(self, string):
4242

4343

4444
if __name__ == "__main__":
45-
num_servers = 4
45+
num_servers = 3
4646
cluster = Cluster(num_servers)
4747

4848
for i in range(num_servers):
4949
primary = ':'.join(list(map(str, cluster.nd.get('/cluster/mapping/%d/primary' %i)['data']['address'])))
5050
secondary = ':'.join(list(map(str, cluster.nd.get('/cluster/mapping/%d/secondary' %i)['data']['address'])))
5151

5252
pprint.pprint({'key' : i, 'primary' : primary, 'secondary' : secondary } )
53+
54+

0 commit comments

Comments
 (0)