def TPCPhase2(self, slave, reqmsg): ret = connectTo(slave.host, slave.port, TPCMaster.timeoutSeconds) if (ret[0] < 0): return sockObj = ret[1] while (True): reqmsg.KVMessageSend(sockObj) # print(reqmsg.key,reqmsg.value,reqmsg.msgType) respmsg = KVMessage() respmsg.KVMessageParse(sockObj) if respmsg.msgType == ErrorCodes.KVMessageType["ACK"]: break sockObj.close()
def KVServerHandle(self, sock_obj): messageobj = KVMessage() messageobj.KVMessageParse(sock_obj) # print(reqmsg) #Maybe Error Handling maybe null #respmsg = KVMessage() if self.useTPC is False: respmsg = self.KVServerHandleNoTPC(messageobj) else: respmsg = self.KVServerHandleTPC(messageobj) respmsg.KVMessageSend(sock_obj)
def TPCPhase1(self, slave, reqmsg): ret = connectTo(slave.host, slave.port, TPCMaster.timeoutSeconds) if (ret[0] < 0): return sockObj = ret[1] reqmsg.KVMessageSend(sockObj) respmsg = KVMessage() respmsg.KVMessageParse(sockObj) sockObj.close() self.state = ErrorCodes.TPCStates["TPC_COMMIT"] if (respmsg.message == None or respmsg.message == "" or respmsg.msgType == ErrorCodes.KVMessageType["VOTE_ABORT"]): self.state = ErrorCodes.TPCStates["TPC_ABORT"] self.errMsg = respmsg.message
def KVServerRegisterMaster(self, sockObj): reqmsg = KVMessage() reqmsg.msgType = ErrorCodes.KVMessageType["REGISTER"] reqmsg.key = self.hostname reqmsg.value = str(self.port) reqmsg.KVMessageSend(sockObj) respmsg = KVMessage() respmsg.KVMessageParse(sockObj) if (respmsg.message == None or respmsg.message != ErrorCodes.Successmsg): return -1 else: self.state = ErrorCodes.TPCStates["TPC_READY"] return 0
def TPCMasterHandle(self, sockObj): reqmsg = KVMessage() respmsg = KVMessage() reqmsg.KVMessageParse(sockObj) respmsg.msgType = ErrorCodes.KVMessageType["RESP"] if reqmsg.key != None or reqmsg.key == "": respmsg.key = reqmsg.key #Copy the message? if (reqmsg == None or reqmsg.key == None): respmsg.message = ErrorCodes.getErrorMessage( ErrorCodes.InvalidRequest) elif (reqmsg.msgType == ErrorCodes.KVMessageType["INFO"]): respmsg = self.TPCMasterInfo(reqmsg) elif (reqmsg.msgType == ErrorCodes.KVMessageType["REGISTER"]): respmsg = self.TPCMasterRegister(reqmsg) elif (reqmsg.msgType == ErrorCodes.KVMessageType["GETREQ"]): respmsg = self.TPCMasterHandleGet(reqmsg) else: respmsg = self.TPCMasterHandleTPC(reqmsg) respmsg.KVMessageSend(sockObj)
def TPCMasterHandleGet(self, reqmsg): respmsg = KVMessage() respmsg.msgType = ErrorCodes.KVMessageType["RESP"] if (reqmsg.value == None or reqmsg.key == None): respmsg.message = ErrorCodes.getErrorMessage( ErrorCodes.InvalidRequest) # print("here in error") return respmsg value = "" receivedResponse = KVMessage() lock = self.cache.KVCacheGetLock(reqmsg.key) lock.acquire() cacheGet = self.cache.KVCacheGet(reqmsg.key) if (cacheGet[0] == 1): lock.release() respmsg.key = reqmsg.key respmsg.value = cacheGet[1] respmsg.msgType = ErrorCodes.KVMessageType["GETRESP"] else: lock.release() slave = self.TPCMasterGetPrimary(reqmsg.key) successfulConnection = False for i in range(0, self.redundancy): ret = connectTo(slave.host, slave.port, TPCMaster.timeoutSeconds) fd = ret[1] if (ret[0] < 0): slave = self.tpcMasterGetSuccessor(slave) else: successfulConnection = True break if (successfulConnection == False): respmsg.msgType = ErrorCodes.KVMessageType["RESP"] respmsg.message = ErrorCodes.getErrorMessage(-1) return respmsg respmsg.msgType = ErrorCodes.KVMessageType["GETREQ"] respmsg.key = reqmsg.key respmsg.value = "" respmsg.KVMessageSend(fd) receivedResponse.KVMessageParse(fd) fd.close() if (receivedResponse.msgType == None): respmsg.msgType = ErrorCodes.KVMessageType["RESP"] respmsg.message = ErrorCodes.getErrorMessage(-1) return respmsg if (receivedResponse.msgType != ErrorCodes.KVMessageType["GETRESP"]): respmsg.msgType = ErrorCodes.KVMessageType["RESP"] respmsg.message = receivedResponse.message else: respmsg.key = receivedResponse.key respmsg.value = receivedResponse.value respmsg.msgType = receivedResponse.msgType respmsg.message = ErrorCodes.Successmsg lock.acquire() self.cache.KVCachePut(respmsg.key, respmsg.value) lock.release() return respmsg