def setValue(self, request, context): t = time.time() vmName = request.name print("\nReceived setValue from ", vmName) self.printALL() serverkey = request.serverkey # A.x 1 server, key = serverkey.split(".") # ["A", "x 1"] value = request.value s = " ".join(('SET', serverkey, value)) checkreply = coordinator.checkLock( mp3_pb2.checkMessage(name=vmName, message=s)) print(checkreply.message) # TODO: Implement different logic for abort if (checkreply.message == 'shouldAbort'): #self.doAbort(vmName) checkreply = coordinator.checkLock( mp3_pb2.checkMessage(name=vmName, message='ABORT')) return mp3_pb2.abortReply(message='shouldAbort') if (vmName not in clientDict.keys()): print(vmName, clientDict.keys()) return mp3_pb2.setReply(message='\tMissing Begin statement') else: arr = clientDict[vmName]['commands'] string = 'SET {} {}'.format(serverkey, value) arr.append((t, string)) clientDict[vmName]['commands'] = arr if (key in lockDict.keys()): lockDict[key].append(['SET', vmName]) else: lockDict[key] = [['SET', vmName]] while (lockDict[key][0][1] != vmName): time.sleep(0.000001) clientDict[vmName]['miniDict'][key] = value print("[" + str(t) + "] " + str(vmName) + " setValue " + str(serverkey) + " " + str(value)) self.printALL() return mp3_pb2.setReply(message='OK')
def abort(self, request, context): t = time.time() vmName = request.name if (vmName not in clientDict.keys()): print(vmName, clientDict.keys()) return mp3_pb2.setReply(message='\tMissing Begin statement') print("\nReceived ABORT from ", vmName) checkreply = coordinator.checkLock( mp3_pb2.checkMessage(name=vmName, message='ABORT')) print(checkreply.message) # TODO: Implement different logic for abort self.doAbort(vmName) return mp3_pb2.abortReply(message='ABORTED')
def getValue(self, request, context): # since we update values in masterDict when commit, # the getValue after a set gets 'Not Found' # use a tempDict to handle this? # a while loop goes through the lock queue? # the client still hangs after the other COMMIT # the server doesn't release lock after client's disconnection # the server doesn't release lock after commit t = time.time() vmName = request.name if (vmName not in clientDict.keys()): print(vmName, clientDict.keys()) return mp3_pb2.setReply(message='\tMissing Begin statement') print("\nReceived getValue from ", vmName) #self.printALL() serverkey = request.serverkey server, key = serverkey.split(".") s = " ".join(('GET', serverkey)) checkreply = coordinator.checkLock( mp3_pb2.checkMessage(name=vmName, message=s)) print(checkreply.message) #TODO: Implement different logic for abort if (checkreply.message == 'shouldAbort'): #self.doAbort(vmName) checkreply = coordinator.checkLock( mp3_pb2.checkMessage(name=vmName, message='ABORT')) return mp3_pb2.abortReply(message='shouldAbort') print("[" + str(t) + "] " + str(vmName) + " getValue " + str(serverkey)) # No Locks if (key not in lockDict.keys()): # Key exists in master if (key in masterDict.keys()): # SET LOCK lockDict[key] = [['GET', vmName]] else: # Does key exist locally? if (key not in clientDict[vmName]['miniDict'].keys()): # Should we return NOT FOUND or let it wait # if another client SET the object but not commit yet? return mp3_pb2.getReply(message='NOT FOUND') else: # APPEND LOCK lockDict[key].append(['GET', vmName]) while (self.checkAcquireReadLock(vmName, key) == False): time.sleep(1) print("waiting") string = 'GET {}'.format(serverkey) clientDict[vmName]['commands'].append([t, string]) # Does key exist locally? if (key in clientDict[vmName]['miniDict'].keys()): # Should we return NOT FOUND or let it wait # if another client SET the object but not commit yet val = clientDict[vmName]['miniDict'][key] self.printALL() return mp3_pb2.getReply(message='%s = %s' % (serverkey, val)) if (key in masterDict.keys()): self.printALL() return mp3_pb2.getReply(message='%s = %s' % (serverkey, masterDict[key])) return mp3_pb2.getReply(message='NOT FOUND')
def commit(self, request, context): t = time.time() vmName = request.name if (vmName not in clientDict.keys()): print(vmName, clientDict.keys()) return mp3_pb2.setReply(message='\tMissing Begin statement') print("\nReceived Commit from ", vmName) self.printALL() checkreply = coordinator.checkLock( mp3_pb2.checkMessage(name=vmName, message='COMMIT')) print(checkreply.message) # TODO: Implement different logic for abort if (checkreply.message == 'shouldAbort'): self.doAbort(vmName) checkreply = coordinator.checkLock( mp3_pb2.checkMessage(name=vmName, message='ABORT')) return mp3_pb2.abortReply(message='ABORTED') while (self.checkCommitOK(vmName) == False): time.sleep(0.0001) for t, command in clientDict[vmName]['commands'][1:]: # SET Command if ('SET' in command): type, serverkey, value = command.split(" ") server, key = serverkey.split(".") masterDict[key] = value # ASSUME first entry is ['SET', vmName] if (len(lockDict[key]) > 1): lockDict[key].pop(0) else: del (lockDict[key]) continue # GET Command if ('GET' in command): type, serverkey = command.split(" ") server, key = serverkey.split(".") if (lockDict[key][0] == ['GET', vmName]): print("\nlockDict") for k, v in lockDict.items(): print(k, ", ", v) locks = lockDict[key] if (len(locks) > 1): lockDict[key].pop(0) else: del (lockDict[key]) else: lockDict[key].remove(['GET', vmName]) del (clientDict[vmName]) self.printALL() return mp3_pb2.commitReply(message='COMMIT OK')
coordinatorVM = 'sp19-cs425-g58-10.cs.illinois.edu' channel = grpc.insecure_channel(str(coordinatorVM) + ':50052') server = mp3_pb2_grpc.CoordinatorStub(channel) response = server.SayHi(mp3_pb2.HelloRequest(name='test1')) print("Coordinator client received: " + response.message) try: with sys.stdin as i: while (1): command = input() checkreply = server.checkLock( mp3_pb2.checkMessage(name="test1", message=str(command))) print(checkreply.message) except KeyboardInterrupt: exit(1) # serverVMs = ['[::]:50051', # 'sp19-cs425-g58-01.cs.illinois.edu', # 'sp19-cs425-g58-02.cs.illinois.edu', # 'sp19-cs425-g58-03.cs.illinois.edu', # 'sp19-cs425-g58-04.cs.illinois.edu', # 'sp19-cs425-g58-05.cs.illinois.edu'] # serverLetters = ['A', 'B', 'C', 'D', 'E'] # serverVMs = [