def KVServerHandleNoTPC(self, reqmsg): error = -1 respmsg = KVMessage() default = ErrorCodes.getErrorMessage(ErrorCodes.InvalidRequest) if (reqmsg.key == ""): if (reqmsg.msgType == ErrorCodes.KVMessageType["GETREQ"] or reqmsg.msgType == ErrorCodes.KVMessageType["PUTREQ"] or reqmsg.msgType == ErrorCodes.KVMessageType["DELREQ"]): respmsg.msgType = ErrorCodes.KVMessageType["RESP"] respmsg.message = default elif (reqmsg.msgType == ErrorCodes.KVMessageType["INFO"]): respmsg.msgType = ErrorCodes.KVMessageType["INFO"] respmsg.message = self.KVServerGetInfoMessage() elif (reqmsg.value == "" and reqmsg.msgType == ErrorCodes.KVMessageType["PUTREQ"]): respmsg.msgType = ErrorCodes.KVMessageType["RESP"] respmsg.message = default elif (reqmsg.msgType == ErrorCodes.KVMessageType["GETREQ"]): ret = self.KVServerGet(reqmsg.key) if (ret[0] >= 0): respmsg.msgType = ErrorCodes.KVMessageType["GETRESP"] respmsg.key = reqmsg.key respmsg.value = ret[1] else: respmsg.msgType = ErrorCodes.KVMessageType["RESP"] respmsg.message = ErrorCodes.getErrorMessage(ret[0]) elif (reqmsg.msgType == ErrorCodes.KVMessageType["PUTREQ"]): ret = self.KVServerPut(reqmsg.key, reqmsg.value) if (ret >= 0): respmsg.msgType = ErrorCodes.KVMessageType["RESP"] respmsg.message = ErrorCodes.Successmsg else: respmsg.msgType = ErrorCodes.KVMessageType["RESP"] respmsg.message = ErrorCodes.getErrorMessage(ret) elif (reqmsg.msgType == ErrorCodes.KVMessageType["DELREQ"]): ret = self.KVServerDelete(reqmsg.key) if (ret >= 0): respmsg.msgType = ErrorCodes.KVMessageType["RESP"] respmsg.message = ErrorCodes.Successmsg else: respmsg.msgType = ErrorCodes.KVMessageType["RESP"] respmsg.message = ErrorCodes.getErrorMessage(ret) else: respmsg.msgType = ErrorCodes.KVMessageType["RESP"] respmsg.message = ErrorCodes.notImplementedMessage return respmsg
def TPCMasterRegister(self, reqmsg): respmsg = KVMessage() respmsg.msgType = ErrorCodes.KVMessageType["RESP"] if (reqmsg.value == None or reqmsg.key == None or reqmsg.value == ""): respmsg.message = ErrorCodes.getErrorMessage( ErrorCodes.InvalidRequest) return respmsg origState = self.state failure = True port = reqmsg.value hostname = reqmsg.key portlen = len(port) hostlen = len(hostname) formatString = str(port) + ":" + hostname hashval = self.hasher(formatString) self.slaveLock.acquire() for slave in self.slaves: if (slave.id == hashval): failure = False self.slaveLock.release() self.state = origState respmsg = None return respmsg if (self.slaveCount == self.slaveCapacity): self.slaveLock.release() self.state = origState respmsg = None return respmsg else: self.slaveCount += 1 self.updateCheckMasterState() slave = TPCSlave() slave.id = hashval slave.host = hostname slave.port = int(port) self.slaves.append(slave) self.slaves.sort(key=retHash) respmsg.message = ErrorCodes.Successmsg self.slaveLock.release() return respmsg
def TPCMasterInfo(self, reqmsg): respmsg = KVMessage() if (reqmsg == None): reqmsg.msgType = ErrorCodes.KVMessageType["RESP"] reqmsg.message = ErrorCodes.getErrorMessage( ErrorCodes.GenericError) return respmsg respmsg.msgType = ErrorCodes.KVMessageType["INFO"] localtime = time.asctime(time.localtime(time.time())) info = localtime + "\n" + "Slaves:\n" self.slaveLock.acquire() for i in self.slaves: info += "{" + str(i.host) + ", " + str(i.port) + "}\n" self.slaveLock.release() respmsg.message = info return respmsg
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 TPCMasterHandleTPC(self, reqmsg): self.TPClock.acquire() respmsg = KVMessage() self.updateCheckMasterState() if (reqmsg == None or reqmsg.key == None): if ((reqmsg.msgType != ErrorCodes.KVMessageType["PUTREQ"] and reqmsg.msgType != ErrorCodes.KVMessageType["DELREQ"]) or (reqmsg.msgType == ErrorCodes.KVMessageType["PUTREQ"] and reqmsg.value == None)): reqmsg.msgType = ErrorCodes.KVMessageType["RESP"] reqmsg.message = ErrorCodes.getErrorMessage(-1) return respmsg primary_slave = self.TPCMasterGetPrimary(reqmsg.key) iter = primary_slave for i in range(self.redundancy): self.TPCPhase1(iter, reqmsg) if (self.state == ErrorCodes.TPCStates["TPC_ABORT"]): break iter = self.tpcMasterGetSuccessor(iter) #need to finish phase1 if (self.state == ErrorCodes.TPCStates["TPC_COMMIT"]): lock = self.cache.KVCacheGetLock(reqmsg.key) # print(reqmsg.msgType, reqmsg.value, reqmsg.key) if (type(lock) != int): lock.acquire() if (reqmsg.msgType == ErrorCodes.KVMessageType["PUTREQ"]): # print(type(reqmsg.msgType), type(ErrorCodes.KVMessageType["PUTREQ"])) self.cache.KVCachePut(reqmsg.key, reqmsg.value) elif (reqmsg.msgType == ErrorCodes.KVMessageType["DELREQ"]): self.cache.KVCacheDelete(reqmsg.key) else: print("Invalid request in TPCMaster handler") lock.release() globalMessage = KVMessage() if (self.state == ErrorCodes.TPCStates["TPC_COMMIT"]): globalMessage.msgType = ErrorCodes.KVMessageType["COMMIT"] else: globalMessage.msgType = ErrorCodes.KVMessageType["ABORT"] iter = primary_slave for i in range(self.redundancy): self.TPCPhase2(iter, globalMessage) iter = self.tpcMasterGetSuccessor(iter) respmsg.msgType = ErrorCodes.KVMessageType["RESP"] if (self.state == ErrorCodes.TPCStates["TPC_COMMIT"]): respmsg.message = ErrorCodes.Successmsg else: respmsg.message = ErrorCodes.getErrorMessage( ErrorCodes.InvalidRequest) # print("req in TPC :",self.state) self.state = ErrorCodes.TPCStates["TPC_INIT"] # print("resp in TPC : ",respmsg.msgType," ",respmsg.message) self.TPClock.release() return respmsg
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
def KVServerHandleTPC(self, reqmsg): respmsg = KVMessage() default = ErrorCodes.getErrorMessage(ErrorCodes.InvalidRequest) if (reqmsg.key == "" and (reqmsg.msgType == ErrorCodes.KVMessageType["GETREQ"] or reqmsg.msgType == ErrorCodes.KVMessageType["PUTREQ"] or reqmsg.msgType == ErrorCodes.KVMessageType["DELREQ"])): if (reqmsg.msgType == ErrorCodes.KVMessageType["INFO"]): respmsg.msgType = ErrorCodes.KVMessageType["INFO"] respmsg.message = self.KVServerGetInfoMessage() else: respmsg.msgType = ErrorCodes.KVMessageType["RESP"] respmsg.message = default elif (reqmsg.value == "" and reqmsg.msgType == ErrorCodes.KVMessageType["PUTREQ"]): respmsg.msgType = ErrorCodes.KVMessageType["RESP"] respmsg.message = default elif (reqmsg.msgType == ErrorCodes.KVMessageType["GETREQ"]): ret = self.KVServerGet(reqmsg.key) if (ret[0] >= 0): respmsg.msgType = ErrorCodes.KVMessageType["GETRESP"] respmsg.key = reqmsg.key respmsg.value = ret[1] else: respmsg.msgType = ErrorCodes.KVMessageType["RESP"] respmsg.message = ErrorCodes.getErrorMessage(ret[0]) elif (reqmsg.msgType == ErrorCodes.KVMessageType["PUTREQ"]): if (self.state == ErrorCodes.TPCStates["TPC_READY"]): respmsg.msgType = ErrorCodes.KVMessageType["RESP"] respmsg.message = default #tpclog() if (self.KVServerPutCheck(reqmsg.key, reqmsg.value) == 1): self.copyAndStoreKVMessage(reqmsg) respmsg.msgType = ErrorCodes.KVMessageType["VOTE_COMMIT"] else: self.state = ErrorCodes.TPCStates["TPC_INIT"] respmsg.msgType = ErrorCodes.KVMessageType["VOTE_ABORT"] respmsg.message = ErrorCodes.getErrorMessage(0) elif (reqmsg.msgType == ErrorCodes.KVMessageType["DELREQ"]): if (self.state == ErrorCodes.TPCStates["TPC_READY"]): respmsg.msgType = ErrorCodes.KVMessageType["RESP"] respmsg.message = default #tpclog() if (self.KVServerDeleteCheck(reqmsg.key) == 1): self.copyAndStoreKVMessage(reqmsg) respmsg.msgType = ErrorCodes.KVMessageType["VOTE_COMMIT"] else: self.state = ErrorCodes.TPCStates["TPC_INIT"] respmsg.msgType = ErrorCodes.KVMessageType["VOTE_ABORT"] respmsg.message = ErrorCodes.getErrorMessage(0) elif (reqmsg.msgType == ErrorCodes.KVMessageType["COMMIT"]): self.state = ErrorCodes.TPCStates["TPC_READY"] #tpclog() # print("in commit") if (self.message.msgType == ErrorCodes.KVMessageType["PUTREQ"]): ret = self.KVServerPut(self.message.key, self.message.value) if (ret < 0): respmsg.msgType = ErrorCodes.KVMessageType["ACK"] # print("ACK") respmsg.message = default else: respmsg.msgType = ErrorCodes.KVMessageType["ACK"] if (self.message.msgType == ErrorCodes.KVMessageType["DELREQ"]): ret = self.KVServerDelete(self.message.key) if (ret < 0): respmsg.msgType = ErrorCodes.KVMessageType["ACK"] respmsg.message = default else: respmsg.msgType = ErrorCodes.KVMessageType["ACK"] elif (reqmsg.msgType == ErrorCodes.KVMessageType["ABORT"]): self.state = ErrorCodes.TPCStates["TPC_READY"] #tpclog() respmsg.msgType = ErrorCodes.KVMessageType["ACK"] else: respmsg.msgType = ErrorCodes.KVMessageType["RESP"] respmsg.message = ErrorCodes.getErrorMessage( ErrorCodes.InvalidRequest) return respmsg