Пример #1
0
    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
Пример #2
0
    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
Пример #3
0
    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
Пример #4
0
    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