示例#1
0
 def xidProcessed(self, XidProcessedRequest, context):
     print("got xid processed: {}".format(XidProcessedRequest))
     if self.head_server:
         print("Returning RC=0")
         return chain_pb2.ChainResponse(rc=0)
     else:
         print("Returning RC=1")
         return chain_pb2.ChainResponse(rc=1)
    def stateTransfer(self, request, context):
        """
        1. receives request for state transfer
        2. transfers source, state and some state update requests
        """
        src = request.src
        if src == self.previousid:
            stateXid = request.stateXid
            statemap = request.state
            allsent = request.sent

            print("TailStateTransferRequest received")
            for sent in allsent:
                xid = sent.xid
                key = sent.key
                value = sent.value
                host = sent.host
                port = sent.port
                cxid = sent.cxid

                if xid > self.currxid:
                    self.currxid = xid
                    if self.istail:
                        # reply to client
                        if self.client.get(host + ":" + port) is None:
                            self.client[host + ":" +
                                        port] = chain_pb2_grpc.TailClientStub(
                                            channel)
                        cxidrequest = chain_pb2.CxidProcessedRequest(cxid=cxid)
                        # cxidprocessed?
                    else:
                        # forward state update to successor if not tail
                        with grpc.insecure_channel("{}:{}".format(
                                host, port)) as channel:
                            stub = chain_pb2_grpc.TailChainReplicaStub(channel)
                            response = stub.stateTransfer(
                                chain_pb2.TailStateTransferRequest(
                                    src=self.currxid,
                                    xid=xid,
                                    key=key,
                                    value=value,
                                    host=host,
                                    port=port,
                                    cxid=cxid))

                            self.next.stateTransfer(response)
                            self.sentlist.append(response)

                            # respond
                            return chain_pb2.ChainResponse(rc=0)  # success

        else:
            print("Improper TailStateTransferRequest received")
            return chain_pb2.ChainResponse(
                rc=1)  # you're not supposed to talk to me
    def proposeStateUpdate(self, request, context):
        """
        1. request contains source, xid, key, value, host, port and cxid
        2. update the state of this replica
        3. if not tail, process and forward
        """
        # making stub to connect to successor
        src = request.src
        if src == self.previousid:
            xid = request.xid
            key = request.key
            value = request.value
            host = request.host
            port = request.port
            cxid = request.cxid

            print(
                "TailStateUpdateRequest received: xid = {}, key = {}, value = {}"
                .format(xid, key, value))
            self.hashtable[key] = value
            currxid = xid

            if self.istail:
                # reply to client
                if self.client.get(host + ":" + port) is None:
                    self.client[host + ":" +
                                port] = chain_pb2_grpc.TailClientStub(channel)
                cxidrequest = chain_pb2.CxidProcessedRequest(cxid=cxid)
                # cxidprocessed?

            else:
                # forward state to successor if not tail
                with grpc.insecure_channel("{}:{}".format(host,
                                                          port)) as channel:
                    stub = chain_pb2_grpc.TailChainReplicaStub(channel)
                    response = stub.proposeStateUpdate(
                        chain_pb2.TailStateUpdateRequest(src=self.replicaid,
                                                         xid=xid,
                                                         key=key,
                                                         value=value,
                                                         host=host,
                                                         port=port,
                                                         cxid=cxid))
                    self.next.proposeStateUpdate(response)
                    self.sentlist.append(response)

                    # respond
                    return chain_pb2.ChainResponse(rc=0)  # success
        else:
            print("Improper TailStateUpdateRequest received")
            return chain_pb2.ChainResponse(
                rc=1)  # you're not supposed to talk to me
示例#4
0
 def stateTransfer(self, HeadStateTransferRequest, context):
     print(
         "got state transfer Request: {}".format(HeadStateTransferRequest))
     if self.head_server:
         print("Returning RC=1, because I'm the Head Server.")
         return chain_pb2.ChainResponse(rc=1)
     if HeadStateTransferRequest.src != int(self.predecessor_Id, 0):
         print("Returning RC=1, because its not from my Predecessor.")
         return chain_pb2.ChainResponse(rc=1)
     self.xid = HeadStateTransferRequest.stateXid
     self.hash_dict = HeadStateTransferRequest.state
     self.sent_list = HeadStateTransferRequest.sent_list
     print("Returning RC=0")
     return chain_pb2.ChainResponse(rc=0)
 def getLatestXid(self, request, context):
     """
     send back xid if the replica is currently tail
     """
     if self.istail:
         print("LatestXidRequest received")
         return chain_pb2.LatestXidResponse(rc=0, xid=self.currxid)
     else:
         print("Improper LatestXidRequest received")
         return chain_pb2.ChainResponse(
             rc=1)  # you're not supposed to talk to me
示例#6
0
 def proposeStateUpdate(self, HeadStateUpdateRequest, context):
     print("got state update request: {}".format(HeadStateUpdateRequest))
     if self.head_server:
         print("Returning RC=1, because I'm the Head.")
         return chain_pb2.ChainResponse(rc=1)
     if HeadStateUpdateRequest.src != int("0x" + self.predecessor_Id, 0):
         print("Returning RC=1, because its not from my Predecessor.")
         return chain_pb2.ChainResponse(rc=1)
     if HeadStateUpdateRequest.xid <= self.xid:
         print("Returning RC=1, because Xid is lesser than Mine.")
         return chain_pb2.ChainResponse(rc=1)
     self.hash_dict[
         HeadStateUpdateRequest.key] = HeadStateUpdateRequest.value
     self.xid = HeadStateUpdateRequest.xid
     #Call Xid Processed request.
     if self.tail_server == True:
         print("Sending Xid Processed Request...")
         request = chain_pb2.XidProcessedRequest(xid=self.xid)
         print(self.head_stub.xidProcessed(request))
     else:
         print("Sending State Update Request...")
         request = chain_pb2.HeadStateUpdateRequest(
             src=int(self.my_sess_id, 0),
             xid=HeadStateUpdateRequest.xid,
             key=HeadStateUpdateRequest.key,
             value=HeadStateUpdateRequest.value)
         self.sent_list.append(request)
         print(self.successor_Stub.proposeStateUpdate(request))
     #Call Latest Xid request.
     print("Latest Xid Request!!!")
     request = chain_pb2.LatestXidRequest()
     print("Sending Get latest Xid Request...")
     response = self.tail_stub.getLatestXid(request)
     if response.rc == 0:
         for request in self.sent_list:
             if (response.xid <= self.xid):
                 self.sent_list.remove(request)
     print("Returning RC=0")
     return chain_pb2.ChainResponse(rc=0)