-
Notifications
You must be signed in to change notification settings - Fork 2
/
EdgeClient.py
1445 lines (1086 loc) · 53.5 KB
/
EdgeClient.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import sys
sys.path.append('./edgefs/gen-py')
import math
import random
import time
from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from fogclient import FogService
from fogclient.ttypes import *
from EdgeServices import EdgeService
from EdgeServices.ttypes import *
import time
import os
import collections
import json
import multiprocessing
from pprint import pprint
import hashlib
######################################################IGNORE ALL THIS ######################################################
MAX_REPRESENTABLE_DISK_SPACE = 32
DIVISION_FACTOR = 32
DISK_UTIL = 2
BLOCK_SIZE = 10*1024*1024
FOG_SERVICE = 0
EDGE_SERVICE = 1
ENCODED_SPACE = 200
BASE_LOG = "/logs/"
FOG_IP = "127.0.0.1"
FOG_PORT = 9092
EDGE_ID = 0
EDGE_IP = 0
EDGE_PORT = 0
EDGE_RELIABILITY = 0
STREAM_RELIABILITY = 0.99
FILE_SIZE = 1000000
sizeChoice = 1
STREAM_ID = 'test'
STREAM_OWNER_FOG_IP = "127.0.0.1"
STREAM_OWNER_FOG_PORT = 9090
#this is assigned when the open() api succeeds
SESSION_SECRET = 'test'
CLIENT_ID = 'test'
#Before every write & incrementBlockCount operation, we issue a renew lease call
#which should succeed always in case left lease time is more than the time taken
#to complete an operation. For this we maintain a conservative estimate of time
#taken for the completion of operations. Also in case if the left lease time is
#less than the time taken to complete an operation, the renew lease call may fail
#i.e. the lease may not be renewed in which case the stream should be reopened
#estimating the putNext() call to succeed in 10 seconds
ESTIMATE_PUT_NEXT = 20*1000
#estimating the incrementBlockCount() call to succeed in 5 seconds
ESTIMATE_INCR_BLOCK_COUNT = 10*1000
#local time when lock is acquired either by opening stream or renew lease
TIME_LOCK_ACQUIRED = 0
#local time when the lease expires
TIME_LEASE_EXPIRE = 0
#whether the client currently holds the lock
IS_LOCK_HELD = False
#now we have lease mechanism in place wherein only a single client can write to a
#stream and other clients will wait to acquire the lease. Since we want the blockIds
#in a stream to be continuous i.e. no gaps between blockIds, use the open() call and
#get the last blockId written to the stream and write from the next id and not the
#current way of writing where each client has a fixed range where it will be writing
#although each stream has a definite starting point.NOTE::For the different stages of
#the experiment, we will give different start points which is fine.
LAST_BLOCK_ID = 0
######################################################IGNORE ALL THIS ######################################################
logs_list = []
class EdgeClient:
def __init__(self):
self.log = {}
#Write to one of the local edge devices
# def writeToEdge(self,IP, port, data):
# print "IP is ",IP
# print "the length of data is ",len(data)
# # Make socket
# transport = TSocket.TSocket('localhost', EDGE_PORT)
# # Buffering is critical. Raw sockets are very slow
# transport = TTransport.TBufferedTransport(transport)
# # Wrap in a protocol
# protocol = TBinaryProtocol.TBinaryProtocol(transport)
# # Create a client to use the protocol encoder
# client = EdgeService.Client(protocol)
# # Connect!
# transport.open()
# # client.write("Somethiong",None,data)
# client.pong()
# metaObj = Metadata("1","2","3")
# result = client.write("md1",metaObj, data)
# print result
# transport.close()
# self.closeSocket(transport)
# Return the fog client instance to talk to the fog
def openSocketConnection(self,ip,port, choice):
print "ip ",ip," port",port," choice ", "open socket connection"
# Make socket
transport = TSocket.TSocket(ip, port)
# Buffering is critical. Raw sockets are very slow
transport = TTransport.TFramedTransport(transport)
# Wrap in a protocol
protocol = TBinaryProtocol.TBinaryProtocol(transport)
# Create a client to use the protocol encoder
if(choice == FOG_SERVICE):
client = FogService.Client(protocol)
else:
print "In the edge service call "
client = EdgeService.Client(protocol)
# Connect!
transport.open()
return client,transport
#Close connection
def closeSocket(self,transport):
print "closing connection"
transport.close()
#utility function to return the systems's utilizaion and free space
def returnDiskSpace(self):
if( hasattr(os,'statvfs')):
#st = os.statvfs("/") #The root part
st = os.statvfs("/")
free = st.f_bavail * st.f_frsize
total = st.f_blocks * st.f_frsize
used = (st.f_blocks - st.f_bfree) * st.f_frsize
print "Disk ",free," : ",total," : ",used
util = used/float(total)*100
# print "Disk util is " ,util
# return GB_part,(MB_part*1024),int(KB_part*1024),int(util)
disk_space_in_MB = float(free /(1024*1024.0))
return disk_space_in_MB,util
return 0 #default return value
#Number to bit string
def returnBinaryString(self,number):
var = ""
while (number!=0):
bit = number%2
var = var + str(bit)
number = number/2
while(len(var)<5):
var = var + '0'
var = var[::-1]
return var
#Encode free space available in disk, (7-bit) 1st bit for GB/MB order, 2nd bit for encoding , 5 bits for units
def encodeFreeSpace(self, space):
disk_space_in_MB,util = self.returnDiskSpace()
print "space requested ",space
disk_space_in_MB = space/(1024*1024) #remove this before using
# print "Free space in GB ",int(free_space_GB)," Free Space in MB ",int(free_space_MB), " Free Space in KB ",int(free_space_KB)
print "Requested space in MB ",int(disk_space_in_MB)
disk_space = 0
free_space_GB = disk_space_in_MB/1000
if(int(free_space_GB)>66):
disk_space = 127
#this is for 11GB to 64GB
if(free_space_GB >=11.0 and free_space_GB<=66.0):
disk_space = free_space_GB + 61
#this is for 1100MB to 10900MB
if(free_space_GB>=1.0 and free_space_GB < 11.0):
disk_space_in_100_MB = (disk_space_in_MB/100.0)
disk_space = disk_space_in_100_MB - 38
#this is for 0MB to 1000MB
if(free_space_GB>= 0.0 and free_space_GB<1.0):
disk_space_in_10_MB = disk_space_in_MB/10.0
disk_space = disk_space_in_10_MB - 128
print "The encoded disk_space is ",int(disk_space)
return int(disk_space),util
#Test Function to check decode logic
def decodeLogic(self,diskSpace):
if (diskSpace>=-128 and diskSpace<-27):
diskSpace = (diskSpace + 128)*10
elif(diskSpace>=-27 and diskSpace<=72):
diskSpace = 1000 + (diskSpace + 28)*100
else:
diskSpace = 11000 + (diskSpace - 72 )*1000
print diskSpace;
#collect disk stats
def prepareDiskStatPayload(self,edgePayload, prevDiskSpace):
freeDiskSpace,util = self.encodeFreeSpace(ENCODED_SPACE)
if(util>DISK_UTIL):
print "Watermark level",DISK_UTIL,"%", " breached!! disk utilization now at ",util, "%"
if(prevDiskSpace!=freeDiskSpace):
edgePayload.encodedStorage = freeDiskSpace
prevDiskSpace = freeDiskSpace
return edgePayload,prevDiskSpace
# Periodically keep sending disk reports
def heartBeat(self,client):
prevDiskSpace = 0
storagePayloadInterval = 5
counter = 0
while True:
edgePayload = EdgePayload(EDGE_ID) #only the edge ID
if(counter%storagePayloadInterval==0): #include payload from storage
edgePayload, prevDiskSpace = self.prepareDiskStatPayload(edgePayload, prevDiskSpace)
result = client.edgeHeartBeats(edgePayload)
print "Hearbeat sent"
counter = counter + 1
time.sleep(10)
#Register the fog device to start sending heartbeats
def registerEdgeDevice(self,nodeId,nodeIp,port,reliability,storage_sent):
timestamp_record = "register edgeJoin,starttime = "+repr(time.time())+","
print "Register Edge device method was called.."
print "NodeId ",nodeId," Node IP ",nodeIp," port ",port, " reliability ",reliability
client,transport = self.openSocketConnection(FOG_IP,FOG_PORT, FOG_SERVICE)
#register Edge device
storage, util = self.encodeFreeSpace(storage_sent)
edgeDevice = EdgeInfoData(nodeId,nodeIp,port,reliability,storage)
client.edgeJoin(edgeDevice)
self.closeSocket(transport)
timestamp_record = timestamp_record +"endtime = " + repr(time.time()) + '\n'
myLogs = open(BASE_LOG+ 'logs.txt','a')
myLogs.write(timestamp_record)
myLogs.close()
#Send periodic updates
def sendPeriodicUpdates(self):
print "The fog IP ",FOG_IP," fog port ",FOG_PORT
client,transport = self.openSocketConnection(FOG_IP,FOG_PORT, FOG_SERVICE)
#keep sending disk reports
self.heartBeat(client)
# Close!
self.closeSocket(transport)
def writeRequestToFog(self,microbatchID,streamId,filePath, data, fogReplicaMap, yetAnotherMap,sizeChoice):
#/home/swamiji/eclipse-workspace/edgefs_Europar/EdgeServer/Audio_02_06_2019_20_57_02.mp3
#Read data and send it
# path = '/home/swamiji/phd/myFile.txt'
#path = filePath
#file = open(path,'r')
#data = file.read() # add try catch here
print "read the file ",len(data)
#statinfo = os.stat(path)
#dataLength = statinfo.st_size
encodedSpace,util = self.encodeFreeSpace(len(data)) #this is the length of the file
#lets see if there is an actual need to renew lease though we will be calling renew method anyways
print "fog ip, fog port , talking to fog for replica locations ",FOG_IP,FOG_PORT
#write request going to a fog
# client,transport = self.openSocketConnection(FOG_IP,FOG_PORT,FOG_SERVICE) #127.0.0.1 9091
transport = TSocket.TSocket(FOG_IP, FOG_PORT)
# Buffering is critical. Raw sockets are very slow
transport = TTransport.TFramedTransport(transport)
# Wrap in a protocol
protocol = TBinaryProtocol.TBinaryProtocol(transport)
# Create a client to use the protocol encoder
myClient = FogService.Client(protocol)
# Connect!
transport.open()
#
blackListedFogs = []
#EdgeInfoData(nodeId,nodeIp,port,reliability,storage)getWriteLocations
# list<WritableFogData> getWriteLocations(1: byte dataLength, 2: Metadata metadata,
# 3: list<i16> blackListedFogs, 4:EdgeInfoData selfInfo)
global STREAM_ID
global CLIENT_ID
global SESSION_SECRET
metaData = Metadata()
metaData.clientId = CLIENT_ID
metaData.sessionSecret = SESSION_SECRET
metaData.mbId = microbatchID
metaData.streamId = streamId
metaData.timestamp = time.time() * 1000
additional_prop = {}
additional_prop["Name"] = "Sheshadri"
metaData.properties = json.dumps(additional_prop)
#print EDGE_ID,EDGE_IP,EDGE_PORT,EDGE_RELIABILITY,encodedSpace
#edgeInfo = EdgeInfoData(EDGE_ID,EDGE_IP,EDGE_PORT,EDGE_RELIABILITY,encodedSpace)
#print "here also ",edgeInfo
print "encodedSpace ",encodedSpace
timestamp_record_getWrite = str(microbatchID) + "," + str(-100) + ", local, " + "getWriteLocations ,starttime = " + repr(time.time()) + ","
#result = myClient.getWriteLocations(encodedSpace,metaData,blackListedFogs,edgeInfo) #datalength,
result = myClient.getWriteLocations(encodedSpace,metaData,blackListedFogs,True)
timestamp_record_getWrite = timestamp_record_getWrite +"endtime = " + repr(time.time()) +" , " + str(sizeChoice) + '\n'
#we are calculating replicas using getWriteLocations()
yetAnotherMap[microbatchID] = {}
insideDict = {}
for w in result:
edgeInfoData = w.edgeInfo
if(edgeInfoData != None ):
if("local" in fogReplicaMap):
fogReplicaMap["local"] = fogReplicaMap["local"] + 1
else:
fogReplicaMap["local"] = 1
if("local" in insideDict):
insideDict["local"] = insideDict["local"] + 1
else:
insideDict["local"] = 1
else:
if(str(w.node.nodeId) in fogReplicaMap):
fogReplicaMap[str(w.node.nodeId)] = fogReplicaMap[str(w.node.nodeId)] + 1
else:
fogReplicaMap[str(w.node.nodeId)] = 1
if(str(w.node.nodeId) in insideDict):
insideDict[str(w.node.nodeId)] = insideDict[str(w.node.nodeId)] + 1
else:
insideDict[str(w.node.nodeId)] = 1
#util map
yetAnotherMap[microbatchID] = insideDict
#before sending the actual writes, lets add the checksum now as there is no point
#in sending the checksum while identifying replicas
hash_md5 = hashlib.md5()
hash_md5.update(data)
metaData.checksum = hash_md5.hexdigest()
print "the write locations are ",result
timestamp_record = str(microbatchID) + ",-1, local ,write req,starttime = " + repr(time.time()) + ","
#lets renew the lease. The behaviour should adhere with the policy of the lease time
#left in comparison to the time taken to complete the operation
print "Lets first issue request to renew the lease for putNext()"
self.renew_lease(metaData.streamId, metaData.clientId, metaData.sessionSecret, 0, ESTIMATE_PUT_NEXT, metaData.mbId)
#ISSUE ALERT:: Since the metaData object is prepared above, it might happen that the clientId and sessionSecret
#were set to dummy global values since before issuing the first write, we do a renew lease which is last code line
#but we set the clientId and secret many lines above. So once renew_lease() returns the proper sessionSecret will
#be with the client and it can properly perform the first write operation. The same fields above cannot be commented
#since both clientId and sessionSecret are required fields (thrift)
metaData.clientId = CLIENT_ID
metaData.sessionSecret = SESSION_SECRET
index = 1
processes = []
# loop is for different fogs(and edges) returned WRITING STARTS HERE : ISHAN
for writable in result:
writeProcess = multiprocessing.Process(target=self.writeToEdge,args=(writable,microbatchID,streamId,data,EDGE_ID,index,sizeChoice, metaData))
processes.append(writeProcess)
writeProcess.start()
index = index + 1
for p in processes:
p.join()
print "all writes to replicas finished "
self.closeSocket(transport)
timestamp_record = timestamp_record +"endtime = " + repr(time.time())+" , " + str(sizeChoice) + '\n'
myLogs = open(BASE_LOG+ 'logs.txt','a')
myLogs.write(timestamp_record)
myLogs.write(timestamp_record_getWrite)#write timestamp for getWrite Locations
myLogs.close()
#the response type is BlockMetadataUpdateResponse
response = self.increment_block_count(metaData)
if response.code == -1:
#this blockId is already written
#In our designed experiment, different clients are writing to different regions so this
#issue will not occur. However this is kept to indicate that such a scenario can occur
#with concurrent clients
print "BlockId : " + str(microbatchID) + " is already written, failing the write"
return -1
elif response.code == -2:
#lease expired
print "Lease expired, should renew the lease before trying again"
return -1
elif response.code == -3:
#client does not hold the lock
print "Client does not hold the lock, should open the stream before writing"
return -1
else:
return 1
# Write to either fog or edge depending on the result
def writeToEdge(self, writable, microbatchID, streamId, data, EDGE_ID, index, sizeChoice, metaData):
device = ""
if(writable.edgeInfo!=None):
device = "local"
else:
device = str(writable.node.nodeId)
localTime = repr(time.time())
timestamp_record = str(microbatchID) + "," + str(index) + "," + device + "," + "write req,starttime = "+localTime+","
print "got the writable ",writable," the microbatchID is ",microbatchID
nodeInfo = writable.node #NodeInfoData
writePreference = writable.preference
reliability = writable.reliability
edgeInfoData = writable.edgeInfo # this is an optional field
#Write to the edge
if edgeInfoData != None :
print "ip ",edgeInfoData.nodeIp," port",edgeInfoData.port
# Make socket
transport = TSocket.TSocket(edgeInfoData.nodeIp, edgeInfoData.port)
# Buffering is critical. Raw sockets are very slow
transport = TTransport.TFramedTransport(transport)
# Wrap in a protocol
protocol = TBinaryProtocol.TBinaryProtocol(transport)
# Create a client to use the protocol encoder
client = EdgeService.Client(protocol)
# Connect!
transport.open()
#byte write(1:string mbId, 2:Metadata mbMetadata, 3:binary mbData)
#local write -150
timestamp_record_local = str(microbatchID) +","+str(-150)+",local,write req,starttime = "+localTime+","
#the response is not a byte anymore, its a WriteResponse
response = client.write(microbatchID,metaData,data)
timestamp_record_local = timestamp_record_local +"endtime = " + repr(time.time())+" , " + str(sizeChoice) + '\n'
print "response from the edge ",response.status
transport.close()
#update the metadata structures for the Fog
client,transport = self.openSocketConnection(nodeInfo.NodeIP,nodeInfo.port,FOG_SERVICE)
#byte insertMetadata(1: Metadata mbMetadata, 2: EdgeInfoData edgeInfoData);
#this was valid as per previous implementation in which we assumed that a local
#write means that the client will be writing to itself. However as per the new
#implementation, it is not necessary and a local write means a write that is written
#to itself or any other edge managed by the same Fog i.e. a neighbor edge of the client
#edgeInfoData = EdgeInfoData()
#edgeInfoData.nodeId = EDGE_ID
#edgeInfoData.nodeIp = EDGE_IP
#edgeInfoData.port = EDGE_PORT
#edgeInfoData.reliability = EDGE_RELIABILITY
#edgeInfoData.storage = 12 #This value is not useful for computation
#update the metadata with the checksum
#hash_md5 = hashlib.md5()
#hash_md5.update(data)
#metaData.checksum = hash_md5.hexdigest()
#metadata insert to fog -50
my_dict = {}
timeMetadata = str(microbatchID) +","+str(-50)+",local ,metadata req,starttime = "+repr(time.time())+","
response = client.insertMetadata(metaData, edgeInfoData, my_dict)
timeMetadata = timeMetadata + " endTime = "+repr(time.time())+" , " + str(sizeChoice)+'\n'
myLogs = open(BASE_LOG+ 'logs.txt','a')
myLogs.write(timestamp_record_local)
myLogs.write(timeMetadata)
myLogs.close()
print "The response from the fog is ", response
self.closeSocket(transport)
else :
print "Have to talk to a fog with preference "
# byte write(1:Metadata mbMetadata, 2:binary data, 3:WritePreference preference);
client,transport = self.openSocketConnection(nodeInfo.NodeIP,nodeInfo.port,FOG_SERVICE)
#response is now a WriteResponse and not a byte
my_dict = {}
response = client.putNext(metaData, data, writable.preference, my_dict)
print "the response from the fog for write ",response.status
self.closeSocket(transport)
timestamp_record = timestamp_record +"endtime = " + repr(time.time())+" , " + str(sizeChoice) + '\n'
print "the time stamp for write request is ",timestamp_record
myLogs = open(BASE_LOG+ 'logs.txt','a')
myLogs.write(timestamp_record)
myLogs.close()
# call to the edge
def readFromEdgeOrFog(self,microbatchID):
client,transport = self.openSocketConnection(FOG_IP,FOG_PORT,FOG_SERVICE)
# ReadResponse read(1: string microbatchId, 2:bool checkLocal, 3:bool checkNeighbors, 4:bool checkBuddies, 5:EdgeInfoData selfInfo);
edgeInfoData = EdgeInfoData()
edgeInfoData.nodeId = EDGE_ID
edgeInfoData.nodeIp = EDGE_IP
edgeInfoData.port = EDGE_PORT
edgeInfoData.reliability = EDGE_RELIABILITY
edgeInfoData.storage = 12
timestamp_record = str(microbatchID)+",read req,starttime = "+repr(time.time())+","
response = client.read(microbatchID,True,True,True,edgeInfoData,True)#last bit is for recovery
timestamp_record = timestamp_record +"endtime = " + repr(time.time()) + '\n'
myLogs = open(BASE_LOG+ 'logs.txt','a')
myLogs.write(timestamp_record)
myLogs.close()
if(response.status == 0):
print "File not found : cannot read file"
else:
edgeInfoRecv = response.edgeInfo
if(edgeInfoRecv!=None):
print "edgeInfoRecv from fog ",edgeInfoRecv
#have to read data from edge
transport = TSocket.TSocket(edgeInfoRecv.nodeIp,edgeInfoRecv.port)
# Buffering is critical. Raw sockets are very slow
transport = TTransport.TFramedTransport(transport)
# Wrap in a protocol
protocol = TBinaryProtocol.TBinaryProtocol(transport)
# Create a client to use the protocol encoder
client = EdgeService.Client(protocol)
# Connect!
transport.open()
response = client.read(microbatchID,1) #this is for recovery
print "Read status is ",response.status
if response.status==0 :
print "File not found : cannot read file"
else:
print "Local Read ",len(response.data)," number of bytes"
print "Metadata also read ",response.metadata
transport.close()
else:
if response.status == 0:
print "File not found : cannot find file"
else:
print "Remote Read ",len(response.data), " number of bytes"
print "Metadata also read ",response.metadata
self.closeSocket(transport)
#find api, from client to a Fog, meta
def find(self, metadataKey, metadataValue, checkNeighbors, checkBuddies):
# FindResponse find(1: string metadataKey, 2:string metadataValue, 3:bool checkNeighbors, 4:bool checkBuddies);
client,transport = self.openSocketConnection(FOG_IP,FOG_PORT,FOG_SERVICE)
response = client.find(metadataKey, metadataValue, True,True)
print "metadataKey ",metadataKey," metadataValue ", metadataValue ,"checkNeighbors ", checkNeighbors,"checkBuddies",checkBuddies
if(response.status==0):
print "Cannot find micro batches of that metadata"
else :
print "the number of micro batches returned are ",len(response.data)
self.closeSocket(transport)
#registers stream for the edge
def registerStream(self, streamId, startBlockNum):
print "fog ip, fog port ",FOG_IP,FOG_PORT
# streamId = "serc1"
#streamPayload = StreamMetadata()
#streamPayload.startTime = time.time()*1000
#streamPayload.reliability = float(STREAM_RELIABILITY)
#streamPayload.minReplica = 2
#streamPayload.maxReplica = 5
streamMD = StreamMetadata()
streamMD.streamId = streamId
streamMD.startTime = I64TypeStreamMetadata(time.time()*1000, False)
streamMD.reliability = DoubleTypeStreamMetadata(float(0.50), True) # STREAM_RELIABILITY
streamMD.minReplica = ByteTypeStreamMetadata(2, True)
streamMD.maxReplica = ByteTypeStreamMetadata(5, True)
streamMD.version = I32TypeStreamMetadata(0, True)
streamMD.otherProperties = dict()
streamMD.otherProperties["update_prop"] = DynamicTypeStreamMetadata("0", "Integer", True)
print "The reliability is set to ",streamMD.reliability.value
print "The min replica is ",streamMD.minReplica.value , "\n The max replica is ",streamMD.maxReplica.value
client,transport = self.openSocketConnection(FOG_IP,FOG_PORT,FOG_SERVICE)
timestamp_record = "register stream req,starttime = "+repr(time.time())+","
response = client.registerStream(streamId, streamMD, startBlockNum)
timestamp_record = timestamp_record +"endtime = " + repr(time.time()) + '\n'
myLogs = open(BASE_LOG+ 'logs.txt','a')
myLogs.write(timestamp_record)
myLogs.close()
print "the response is ",response
self.closeSocket(transport)
#Test code for testing read
def testRecovery(self):
# print "edgeInfoRecv from fog ",edgeInfoRecv
#have to read data from edge
transport = TSocket.TSocket("127.0.0.1",5005)
# Buffering is critical. Raw sockets are very slow
transport = TTransport.TFramedTransport(transport)
# Wrap in a protocol
protocol = TBinaryProtocol.TBinaryProtocol(transport)
# Create a client to use the protocol encoder
client = EdgeService.Client(protocol)
# Connect!
transport.open()
response = client.read("jsr.java",1) #this is for recovery
print "Read status is ",response.status
if response.status==0 :
print "File not found : cannot read file"
else:
print "Local Read ",len(response.data)," number of bytes"
print "Metadata also read ",response.metadata
transport.close()
#find and read replicas for microbatches
def findAndRead(self, microbatchId):
edgeInfoData = EdgeInfoData()
edgeInfoData.nodeId = EDGE_ID
edgeInfoData.nodeIp = EDGE_IP
edgeInfoData.port = EDGE_PORT
edgeInfoData.reliability = EDGE_RELIABILITY
edgeInfoData.storage = 12
client,transport = self.openSocketConnection(FOG_IP,FOG_PORT,FOG_SERVICE)
timestamp_record = str(microbatchId)+ ",23, local ,find req,starttime = "+repr(time.time())+","
response = client.find(microbatchId,True,True,edgeInfoData)
timestamp_record = timestamp_record +"endtime = " + repr(time.time()) + '\n'
print "the time stamp for find request is ",timestamp_record
myLogs = open(BASE_LOG+ 'logs.txt','a')
myLogs.write(timestamp_record)
myLogs.close()
self.closeSocket(transport)
print "Sent replicas ",response
for findReplica in response :
edgeInfoData = findReplica.edgeInfo
if(edgeInfoData!=None):
print "edgeInfoRecv from fog ",edgeInfoData
#have to read data from edge
transport = TSocket.TSocket(edgeInfoData.nodeIp,edgeInfoData.port)
# Buffering is critical. Raw sockets are very slow
transport = TTransport.TFramedTransport(transport)
# Wrap in a protocol
protocol = TBinaryProtocol.TBinaryProtocol(transport)
# Create a client to use the protocol encoder
client = EdgeService.Client(protocol)
# Connect!
transport.open()
timestamp_record = str(microbatchId)+", 25 , "+ str(findReplica.node.nodeId) + " , Read req,starttime = "+repr(time.time())+","
response = client.read(microbatchId,0) #this is for recovery
timestamp_record = timestamp_record +"endtime = " + repr(time.time()) + '\n'
myLogs = open(BASE_LOG+ "logs.txt",'a')
myLogs.write(timestamp_record)
myLogs.close()
print "Read status is ",response.status
if response.status==0 :
print "File not found : cannot read file"
else:
print "Local Read ",len(response.data)," number of bytes"
print "Metadata also read ",response.metadata
return 1 #successful read
transport.close()
elif(findReplica.node!=None) :
fogNode = findReplica.node
client,transport = self.openSocketConnection(fogNode.NodeIP,fogNode.port,FOG_SERVICE)
timestamp_record = str(microbatchId)+", 27 ,"+str(findReplica.node.nodeId) + ",write req,starttime = "+repr(time.time())+","
response = client.read(microbatchId,0)
timestamp_record = timestamp_record +"endtime = " + repr(time.time()) + '\n'
myLogs = open(BASE_LOG+ "logs.txt",'a')
myLogs.write(timestamp_record)
myLogs.close()
if(response.status == 1):
print "Fog Amount of bytes read ",len(response.data)
return 1 #successful read
else:
print "The queried fog does not have data"
self.closeSocket(transport)
else:
print "The queried fog does not have data"
def writeUsingProcess(self, start, streamId, byteArray, process_index, return_dict):
print "Inside a write process"
#as each process will do 100 writes, we need to create the two maps and send
#them to the main method and then merge all maps to create single picture there
fogReplicaMap = {}
yetAnotherMap = {}
newFilePath = ""
numWrites = 0
recycle = 0
#lets note the time taken by each process as well
#this is not for a microbatchId so using -1 for it, -1000 indicates the time for a process completing 100 writes
# and third is the number of process where this ranges from 0 to 9 during stress testing
timestamp_process = "-1, -1000, " + str(process_index) + ",stress test write process req,starttime = "+repr(time.time())+","
while(numWrites<38):# original value is 100
myEdge.writeRequestToFog(str(start),streamId,newFilePath,byteArray[recycle],fogReplicaMap, yetAnotherMap)
numWrites = numWrites + 1
start = start + 1
recycle = (recycle + 1)%10
timestamp_process = timestamp_process +"endtime = " + repr(time.time()) + '\n'
#writing of logs should happen here
myLogs = open(BASE_LOG+ "logs.txt",'a')
myLogs.write(timestamp_process)
myLogs.close()
return_dict[process_index] = (fogReplicaMap, yetAnotherMap)
#fetching stream metadata has an id of 250
def getStreamMetadata(self, sid):
print "Going to fetch stream metadata"
timestamp_record = "-1, 250,"+ str(-1) + ",stream md fetch,starttime = "+repr(time.time())+","
client,transport = self.openSocketConnection(FOG_IP, FOG_PORT, FOG_SERVICE)
streamMetadataInfo = client.getStreamMetadata(sid, True, True, True)
timestamp_record = timestamp_record +"endtime = " + repr(time.time()) + '\n'
self.closeSocket(transport)
myLogs = open(BASE_LOG+ "logs.txt",'a')
myLogs.write(timestamp_record)
myLogs.close()
return streamMetadataInfo.streamMetadata
#stream metadata update has an id of 300
def updateStreamMD(self):
print "Going to update stream metadata, first get the latest stream metadata"
timestamp_record = "-1, 300,"+ str(-1) + ",stream md update,starttime = "+repr(time.time())+","
global STREAM_ID
metadata = self.getStreamMetadata(STREAM_ID)
#type of ownerFog is NodeInfoPrimary
ownerFog = metadata.owner.value
ownerFog_ip = ownerFog.NodeIP
ownerFog_port = ownerFog.port
print "Owner of ", STREAM_ID, " is ", ownerFog_ip, " : ", ownerFog_port
print "============ VERSION IS ============" , metadata.version.value
current_value = int(metadata.otherProperties["update_prop"].value)
next_value = current_value + 1
metadata.otherProperties["update_prop"] = DynamicTypeStreamMetadata(str(next_value), "Integer", True)
client,transport = self.openSocketConnection(ownerFog_ip, ownerFog_port, FOG_SERVICE)
#result type is StreamMetadataUpdateResponse
result = client.updateStreamMetadata(metadata)
#lets add the status as well as the last field in the log
if result.code > 0:
#success case
timestamp_record = timestamp_record +"endtime = " + repr(time.time()) + ",1" + '\n'
else:
#failure case
timestamp_record = timestamp_record +"endtime = " + repr(time.time()) + ",0" + '\n'
self.closeSocket(transport)
myLogs = open(BASE_LOG+ "logs.txt",'a')
myLogs.write(timestamp_record)
myLogs.close()
#open stream has an id of 350
def openStream(self, stream_id, client_id, expected_lease, blockId):
print "Going to open stream for writing"
#stream can only be opened by calling open() on the stream owner
#for writes, initially we will use getStreamMetadata() to get the owner
#which can be cached so that the after successful completion of a write,
#we can invoke incrementBlockCount on the owner directly and further writes
#can also use the cached information till the lease is valid
timestamp_record = str(blockId) + ", 350,"+ str(-1) + ",open stream,starttime = "+repr(time.time())+","
metadata = self.getStreamMetadata(stream_id)
ownerFog = metadata.owner.value
global STREAM_OWNER_FOG_IP
STREAM_OWNER_FOG_IP = ownerFog.NodeIP
global STREAM_OWNER_FOG_PORT
STREAM_OWNER_FOG_PORT = ownerFog.port
client,transport = self.openSocketConnection(STREAM_OWNER_FOG_IP, STREAM_OWNER_FOG_PORT, FOG_SERVICE)
global TIME_LOCK_ACQUIRED
global TIME_LEASE_EXPIRE
global IS_LOCK_HELD
while True:
#type of response is OpenStreamResponse
# sheshadri setlease
response = client.open(stream_id, client_id, expected_lease, True)
print "The response of open stream is ",response.status
if response.status == 1:
# if the set lease is false , then only IS_LOCK_HELD is needed
TIME_LOCK_ACQUIRED = int(time.time() * 1000)
TIME_LEASE_EXPIRE = TIME_LOCK_ACQUIRED + response.leaseTime
print "The lease expire is ",TIME_LEASE_EXPIRE
IS_LOCK_HELD = True
break
else:
time.sleep(1)
timestamp_record = timestamp_record +"endtime = " + repr(time.time()) + '\n'
self.closeSocket(transport)
myLogs = open(BASE_LOG+ "logs.txt",'a')
myLogs.write(timestamp_record)
myLogs.close()
global SESSION_SECRET
SESSION_SECRET = response.sessionSecret
return response
#renewLease() has an id of 400 and the overall time to complete the lock acquiring has an id of 450
#if the left lease time is more than the time for the operation e.g. putNext or increment block count
#then the renewLease() should return with a status of 1 and if that doesn't happen, it should be treated
#as an exception. Similarly if the left lease time is less than the time for the operation, renewLease()
#might return with a status of 0 and a negative code. In that case, some other client would have acquired
#the lock on the stream and we will then use the openStream to acquire the lock. To capture whether an
#exception occurs during this process, we use a code of 475
def renew_lease(self, stream_id, client_id, session_secret, expected_lease, expected_completion_time, blockId):
print "Lets check if there is a need to renew lease"
#lets divide the renewal result in two parts based on whether the left lease time is more or less than the
#time taken to complete the operation. In case we have more lease time left than the operation, we will
#not make call to the server and proceed with the operation
global TIME_LEASE_EXPIRE
global TIME_LOCK_ACQUIRED
current_time = int((time.time() * 1000))
if TIME_LEASE_EXPIRE - current_time > expected_completion_time:
temp = TIME_LEASE_EXPIRE - current_time
#no need to call renewLease now, lets return and perform the operation
print "There is some problem with renew lease ",temp," expected completed time ",expected_completion_time
return
#the behaviour of renewal of lease is as follows: the renewal can only succeed when the client holding
#the lock is the previous holder as well i.e. no one has acquired the lock in between the lease expiration
#and this renewal attempt. In case it fails, then go back to the open() api to get a fresh lock as some
#other client got the lock in between
global STREAM_OWNER_FOG_IP
global STREAM_OWNER_FOG_PORT
global IS_LOCK_HELD
#this log is to capture the total time for acquiring the lock
timestamp_full = str(blockId) + ", 450,"+ STREAM_OWNER_FOG_IP + ",lock reacquire,starttime = "+repr(time.time())+","
timestamp_record = ''
fallback = True
print "The lock status is ",IS_LOCK_HELD
if IS_LOCK_HELD == True:
print "The lock is held"
#for lease renewal, after endtime, adding status flag as well indicating whether it was successful or not