-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathclient.py
250 lines (202 loc) · 8.69 KB
/
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
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
import uuid
import sys
from turtle import st
from urllib import request
import grpc
import gfs_pb2
import gfs_pb2_grpc
from common import Config, isInt
def list_files(file_path):
master = f"localhost:{Config.master_loc}"
with grpc.insecure_channel(master) as channel:
stub = gfs_pb2_grpc.MasterServerToClientStub(channel)
request = gfs_pb2.String(st=file_path)
master_response: str = stub.ListFiles(request).st
fps = master_response.split("|")
print(fps)
def create_file(file_path):
"""TODO: Will the primary create the file or client?
It makes sense for the client to directly command the chunks to make the files since making a file is idempotent
"""
master = f"localhost:{Config.master_loc}"
with grpc.insecure_channel(master) as channel:
stub = gfs_pb2_grpc.MasterServerToClientStub(channel)
request = gfs_pb2.String(st=file_path)
master_response: str = stub.CreateFile(request).st
print(f"Response from master: {master_response}")
if master_response.startswith("ERROR"):
return -1
data = master_response.split("|")
chunk_handle = data[0]
for loc in data[1:]:
chunkserver_addr = f"localhost:{loc}"
with grpc.insecure_channel(chunkserver_addr) as channel:
stub = gfs_pb2_grpc.ChunkServerToClientStub(channel)
request = gfs_pb2.String(st=chunk_handle)
cs_resp = stub.Create(request).st
print(f"Response from chunkserver {loc} : {cs_resp}")
def append_file(file_path, input_data, clientid):
"""reccursively append
Send all the data to all the files and then request the primary to write/commit
"""
master = f"localhost:{Config.master_loc}"
with grpc.insecure_channel(master) as channel:
stub = gfs_pb2_grpc.MasterServerToClientStub(channel)
req = gfs_pb2.String(st=file_path)
master_resp: str = stub.AppendFile(req).st
print(f"Response from master: {master_resp}")
if master_resp.startswith("ERROR"):
return -1
input_size = len(input_data)
data = master_resp.split("|")
chunk_handle = data[0]
# Send data to the chunks
for loc in data[1:]:
chunk_addr = f"localhost:{loc}"
with grpc.insecure_channel(chunk_addr) as channel:
stub = gfs_pb2_grpc.ChunkServerToClientStub(channel)
request = gfs_pb2.String(st=chunk_handle)
cs_resp: str = stub.GetChunkSpace(request).st
print(
f"Response from chunk {loc} : chunk space :{cs_resp} chunk handle {chunk_handle}"
)
if cs_resp.startswith("ERROR"):
return -1
rem_space = int(cs_resp)
if rem_space >= input_size:
st = clientid + "||" + chunk_handle + "|" + input_data
request = gfs_pb2.String(st=st)
cs_resp = stub.AddData(request).st
else:
inp1, inp2 = input_data[:rem_space], input_data[rem_space:]
st = clientid + "||" + chunk_handle + "|" + inp1
req = gfs_pb2.String(st=st)
cs_resp = stub.AddData(req).st
# TODO cs_resp error handling
if cs_resp.startswith("-1"):
print(f"Response from chunk server {loc}: {cs_resp}")
print("Retrying...")
append_file(file_path, input_data, clientid)
print(f"Response from chunk server {loc} : {cs_resp}")
# send Write message to primary
primary_loc = data[1]
primary_addr = f"localhost:{primary_loc}"
with grpc.insecure_channel(primary_addr) as channel:
stub = gfs_pb2_grpc.PrimaryToClientStub(channel)
st = clientid + "|" + "*".join(data[2:])
req = gfs_pb2.String(st=st)
print("* Sending commit request to primary")
primary_resp: str = stub.Commit(req).st
# TODO ADD print statement
if primary_resp.startswith("-2"):
print(f"Response from chunk server {primary_loc}: {primary_resp}")
# TODO call master to get primary
return -2
elif primary_resp.startswith("-1"):
print("All chunk servers did not write, retrying")
append_file(file_path, input_data, uuid.uuid4().hex[:8])
elif primary_resp.startswith("-3"):
print("ERROR: failure: inconsistent data")
return -3
if rem_space >= input_size:
return 0
# if more chunks are needed to be added
with grpc.insecure_channel(master) as channel:
stub = gfs_pb2_grpc.MasterServerToClientStub(channel)
st = file_path + "|" + chunk_handle
req = gfs_pb2.String(st=st)
master_resp: str = stub.CreateChunk(req).st
print(f"Response from master: {master_resp}")
# Create chunks in chunk server
data = master_resp.split("|")
chunk_handle = data[0]
for loc in data[1:]:
chunk_addr = f"localhost:{loc}"
with grpc.insecure_channel(chunk_addr) as channel:
stub = gfs_pb2_grpc.ChunkServerToClientStub(channel)
request = gfs_pb2.String(st=chunk_handle)
cs_resp: str = stub.Create(request).st
print(f"Response from chunk server {loc} : {cs_resp}")
# creating new clinetid so that new data has an unique id
append_file(file_path, inp2, uuid.uuid4().hex[:8])
return 0
def read_file(file_path, offset, numbytes):
"""reads from all chunk?"""
master = f"localhost:{Config.master_loc}"
with grpc.insecure_channel(master) as channel:
stub = gfs_pb2_grpc.MasterServerToClientStub(channel)
st = file_path + "|" + str(offset) + "|" + str(numbytes)
req = gfs_pb2.String(st=st)
master_resp: str = stub.ReadFile(req).st
print(f"Response from master: {master_resp}")
if master_resp.startswith("ERROR"):
return -1
file_content = ""
data = master_resp.split("|")
for chunk_info in data:
chunk_handle, loc, start_offset, numbytes = chunk_info.split("*")
chunk_addr = f"localhost:{loc}"
with grpc.insecure_channel(chunk_addr) as channel:
stub = gfs_pb2_grpc.ChunkServerToClientStub(channel)
st = chunk_handle + "|" + start_offset + "|" + numbytes
req = gfs_pb2.String(st=st)
cs_resp: str = stub.Read(req).st
print(f"Response from chunk server {loc} {cs_resp}")
if cs_resp.startswith("ERROR"):
return -1
file_content += cs_resp
print(f"file_content: {file_content}")
def write_file(file_path, offset, input_data):
raise NotImplementedError
# """Can only write to existing files"""
# master = f"localhost:{Config.master_loc}"
# numbytes = len(input_data)
# with grpc.insecure_channel(master) as channel:
# stub = gfs_pb2_grpc.MasterServerToClientStub(channel)
# st = file_path + "|" + str(offset) + "|" + str(numbytes)
# req = gfs_pb2.String(st=st)
# master_resp: str = stub.ReadFile(req).st
# print(f"Response from master: {master_resp}")
# if master_resp.startswith("ERROR"):
# return -1
# file_content = ""
# data = master_resp.split("|")
# for chunk_info in data:
# chunk_handle, loc, start_offset, numbytes = chunk_info.split("*")
# chunk_addr = f"localhost:{loc}"
# with grpc.insecure_channel(chunk_addr) as channel:
# stub = gfs_pb2_grpc.ChunkServerToClientStub(channel)
# st = clientid + "||" + chunk_handle + "|" + inp1
# req = gfs_pb2.String(st=st)
# cs_resp = stub.AddData(req).st
# st = chunk_handle + "|" + start_offset + "|" + numbytes
# req = gfs_pb2.String(st=st)
# cs_resp: str = stub.Read(req).st
# print(f"Response from chunk server {loc} {cs_resp}")
# if cs_resp.startswith("ERROR"):
# return -1
# file_content += cs_resp
# print(f"file_content: {file_content}")
def run(command: str, file_path: str, args: list):
clientid = uuid.uuid4().hex[:8]
if command == "create":
create_file(file_path)
elif command == "list":
list_files(file_path)
elif command == "append":
if len(args) == 0:
print("No input given to append")
else:
append_file(file_path, args[0], clientid)
elif command == "read":
if len(args) < 2 or not isInt(args[0]) or not isInt(args[1]):
print("Require byte offset and number of bytes to read")
else:
read_file(file_path, int(args[0]), int(args[1]))
else:
print("Invalid Command")
if __name__ == "__main__":
if len(sys.argv) < 3:
print(f"Usage: python {sys.argv[0]} <command> <file_path> <args>")
exit(-1)
run(sys.argv[1], sys.argv[2], sys.argv[3:])