forked from procub3r/RepCl.py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto_masterNode.py
218 lines (164 loc) · 7.34 KB
/
auto_masterNode.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
'''
Points to note :
-> Data from the user is formatted as form data
-> Data from the replica is formatted as json data
-> GET requests are serviced from masterData dictionary, not the text file
POST formats :
user -> master : form (write data)
replica -> master : json (sync data)
master -> replica : json (sync + write data)
GET formats :
user -> master : form (read data)
master -> replica : from (fwd read data)
Order of data sent during sync ->
1) Master -> Replica
2) Replica -> Master
So the sync following an update event will result in only the master getting the updated value,
and replica having the old one (get exchanged) -> TIMESTAMPS REQUIRED HERE
-> Delaying POST and DELETE operations only for now
'''
from flask import Flask, request
import uuid, requests, time, pickle
import json, os
from sup_repcl import RepCl
from sup_veccl import VecCl
# RUNNING ON PORT 5000
'''
Read -> 0
Write -> 1
Sync -> 2
Data from user stored as -> {UUID : [data, operation]}
POST to replica sent as -> {key : <UUID>, data : <data>}
GET get to replica sent as -> {key : <UUID>, type : "get"}
GET put to replica sent as -> {key : <UUID>, type : "put"}
GET delete to replica sent as -> {key : <UUID>, type : "delete"}
'''
INTERVAL = os.getenv('INTERVAL')
EPSILON = os.getenv('EPSILON')
app = Flask(__name__)
# masterData will hold only data sent through POST and PUT requests
masterData = {"known-master-1" : "mas_init_1",
"known-master-2" : "mas_init_2",
"known-master-3" : "mas_init_3",
"known-master-4" : "mas_init_4"}
# event_logs will hold info about any request that comes in (POST, GET, PUT, DELETE)
event_logs = {}
fwd_flag = 0
sync_count = 0
repcl_time = RepCl(0)
veccl_time = VecCl(0)
def enable_sync() :
global sync_count
print("inside sync function")
sync_count += 1
print("Syncing\n")
# Sending master's event logs to replica in JSON format
response = requests.post(replica_url, json = {"data" : event_logs})
#print(f"Master recieved replica logs : {response}")
#replica_url = "http://replica-service:5001"
replica_url = "http://replica-service.default.svc.cluster.local"
@app.route('/', methods=['GET', 'POST'])
def index() :
global file, masterData
# New incoming request -> EITHER FROM USER OR REPLICA NODE
if request.method == 'POST' :
try :
# POST REQUEST FROM REPLICA NODE IN JSON FORMAT (records of all events across both nodes) -> aka sync data
syncData = request.get_json()["data"]
for item in syncData.items() :
# New unseen value
if item not in masterData.items() :
event_logs[item[0]] = item[1]
# Writing "After" data to file
with open("/data/master.txt", "w") as file :
#file.write(f"\n\nAfter sync # {sync_count} : \n")
file.write(json.dumps(event_logs))
print("Done syncing\n")
except :
# POST REQUEST FROM THE USER
pickled_repcl = request.files['repcl_time'].read()
pickled_veccl = request.files['veccl_time'].read()
sender_repcl = pickle.loads(pickled_repcl)
sender_veccl = pickle.loads(pickled_veccl)
repcl_time.recv(sender_repcl)
veccl_time.merge(sender_veccl)
data = request.args.get("data")
# Checking whether to forward data or not
if fwd_flag == 0 :
# Storing the data on the master node
# Delaying storing by 3 seconds
time.sleep(3)
event_id = str(uuid.uuid1())
masterData[event_id] = data
event_logs[event_id] = {"RepTime" : repcl_time.to_dict(),
"VecTime" : veccl_time.to_dict(),
"action" : 'POST',
"status" : 'SUC'}
else :
# Forwarding the post request to replica node
requests.post(replica_url, json = {"data" : data})
elif request.method == 'GET' :
event_id = str(uuid.uuid1())
# Format of getting parameters from GET request is diff from POST request
uuid_key = request.args.get("uuid_key")
type = request.args.get("type")
if type != "sync" :
pickled_repcl = request.files['repcl_time'].read()
pickled_veccl = request.files['veccl_time'].read()
sender_repcl = pickle.loads(pickled_repcl)
sender_veccl = pickle.loads(pickled_veccl)
repcl_time.recv(sender_repcl)
veccl_time.merge(sender_veccl)
if type == 'get' :
# Reading from master node
if fwd_flag == 0 :
if uuid_key in masterData.keys() :
status = 'SUC'
else :
status = 'FAIL'
event_logs[event_id] = {"RepTime" : repcl_time.to_dict(),
"VecTime" : veccl_time.to_dict(),
"action" : 'GET',
"status" : status}
# Forwarding read operation to replica node
else :
requests.get(replica_url, params={'type' : type, 'uuid_key': uuid_key})
elif type == 'put' :
new_val = request.args.get("new_val")
if fwd_flag == 0 :
# Updating in master
if uuid_key in masterData.keys() :
masterData[uuid_key] = new_val
status = 'SUC'
else :
status = 'FAIL'
# timestamping after the put operation
event_logs[event_id] = {"RepTime" : repcl_time.to_dict(),
"VecTime" : veccl_time.to_dict(),
"action" : 'PUT',
"status" : status}
else :
# Forwarding update operation to replica
requests.get(replica_url, params = {'type' : type, 'uuid_key' : uuid_key, 'new_val' : new_val})
elif type == 'delete' :
# Deleting from master node
if fwd_flag == 0 :
if uuid_key in masterData.keys() :
del_val = masterData.pop(uuid_key)
status = 'SUC'
else :
status = 'FAIL'
event_logs[event_id] = {"RepTime" : repcl_time.to_dict(),
"VecTime" : veccl_time.to_dict(),
"action" : 'DEL',
"status" : status}
# Forwarding delete operation to replica node
else :
requests.get(replica_url, params={'type' : type, 'uuid_key': uuid_key})
else :
enable_sync()
return '''
This is the master node
'''
if __name__ == '__main__':
app.run(debug=False, host='0.0.0.0', port = 5000)