コード例 #1
0
    def send_heartbeat_request(self):
        #print("Sending heartbeat request")
        #logger.debug("Starting HEARTBEATS")

        heartbeat = Raft.raft_packet(sourceID=0x0,
                                     destinationID=self.nodeID,
                                     data=0x0,
                                     logIndex=self.logIndex,
                                     srcIP=args.source,
                                     dstIP=IP_MULTICAST_ADDRESS,
                                     currentTerm=self.term,
                                     messageType=COMMANDS['StartHeartbeat'])

        send_no_reply(heartbeat)
コード例 #2
0
    def start_election(self):
        print('starting election')
        #logger.debug("{} starting election; status: {}, term:{}".format(self.controller_ip, self.status, self.term))
        self.term += 1
        start_election_message = Raft.raft_packet(
            sourceID=0x0,
            destinationID=self.nodeID,
            data=0x0,
            logIndex=self.logIndex,
            srcIP=args.source,
            dstIP=IP_MULTICAST_ADDRESS,
            currentTerm=self.term,
            messageType=COMMANDS['Timeout'])

        send_no_reply(start_election_message)
コード例 #3
0
    def handle_packet(self, packet):
        if packet[Raft].messageType == COMMANDS['RequestVote'] and packet[
                Raft].sourceID == self.nodeID:
            self.status = STATUSES['candidate']
            self.time = timer()
            print('vote request: -> state: {};'.format(self.status))

        if packet[Raft].messageType == COMMANDS['HeartBeatRequest'] and packet[
                Raft].sourceID == self.nodeID:  # received the heartbeat from node -> node has won the election
            if self.status == STATUSES['candidate']:
                print('won election -> status = leader')
                print('time elapsed (in ms): {}'.format(
                    (timer() - self.time) * 1000))
                logger.debug('[nodeID: {}] won election: {}'.format(
                    self.nodeID, timer()))
                print('election absolute time: {}'.format(timer()))

            self.status = STATUSES['leader']
            self.term = packet[Raft].currentTerm
            self.logIndex = packet[Raft].logIndex

        if packet[Raft].messageType == COMMANDS[
                'HeartBeatRequest'] and not packet[
                    Raft].sourceID == self.nodeID:
            self.status = STATUSES['follower']
            self.term = packet[Raft].currentTerm
            self.logIndex = packet[Raft].logIndex

            self.init_timeout()

        if packet[Raft].messageType == COMMANDS[
                'HeartBeatResponse']:  # received a cloned heartbeat response from node -> reset timeout
            #print('resetting timeout; response to destinationID: {}'.format(packet[Raft].destinationID))
            #print('state : {}'.format(self.status))
            self.term = packet[Raft].currentTerm

            if self.status == STATUSES['leader']:
                print('stepping down as leader.')
                self.status = STATUSES['follower']

            self.init_timeout()

        if packet[Raft].messageType == COMMANDS[
                'AppendEntriesReply']:  # received a cloned AppendEntries response from node -> reset timeout
            #print('resetting timeout; AppendEntries from: {}'.format(packet[Raft].destinationID))
            #print('state : {}'.format(self.status))

            self.term = packet[Raft].currentTerm
            self.logIndex = packet[Raft].logIndex

            self.init_timeout()

        # if packet[Raft].messageType == COMMANDS['AppendEntries']:
        #     print('starting Transaction: {}'.format(time.time()))

        if packet[Raft].messageType == COMMANDS[
                'Redirect'] and self.status == STATUSES['leader']:
            # received a redirected New Request from a client
            # new request can be made only by controllers
            self.counter_new_request += 1
            packet[Raft].messageType = COMMANDS['NewRequest']

            logger.debug(
                '[nodeID: {}] redirected New Request received; total: {}; time: {}'
                .format(self.nodeID, self.counter_new_request, timer()))
            #print('New Request received; total: {}; time: {}'.format(self.counter_new_request, time.time()))

            packet[Raft].sourceID = 0x0
            packet[Raft].destinationID = self.nodeID
            packet[IP].srcAddr = args.source
            #packet[Raft].show()
            send_no_reply(packet)

            #if (self.counter_new_request % 1000 == 0) and self.nodeID == 1:  # emulating failure                self.sniffer.start():
            #self.emulate_failure()

        if packet[Raft].messageType == COMMANDS['RejectNewRequest']:
            self.counter_rejected_requests += 1
            logger.debug('[nodeID: {}] New request rejected; total {}'.format(
                self.nodeID, self.counter_rejected_requests))
コード例 #4
0
    def handle_packet(self, packet):
        if packet[Raft].messageType == COMMANDS['RequestVote'] and packet[
                Raft].sourceID == self.nodeID:
            self.status = STATUSES['candidate']
            self.time = timer()
            print('vote request: -> state: {};'.format(self.status))

        if packet[Raft].messageType == COMMANDS['HeartBeatRequest'] and packet[
                Raft].sourceID == self.nodeID:  # received the heartbeat from node -> node has won the election
            if self.status == STATUSES['candidate']:
                print('won election -> status = leader')
                print('time elapsed (in ms): {}'.format(
                    (timer() - self.time) * 1000))

            self.status = STATUSES['leader']
            self.term = packet[Raft].currentTerm
            self.logIndex = packet[Raft].logIndex

        if packet[Raft].messageType == COMMANDS[
                'HeartBeatRequest'] and not packet[
                    Raft].sourceID == self.nodeID:
            # apparently doesn't work maybe because sourceID is overwritten somewhere in the pipeline before packet gets cloned
            #if self.status == STATUSES['leader']:
            print('stepping down not working')

            self.status = STATUSES['follower']
            self.term = packet[Raft].currentTerm
            self.logIndex = packet[Raft].logIndex

            self.init_timeout()

        if packet[Raft].messageType == COMMANDS[
                'HeartBeatResponse']:  # received a cloned heartbeat response from node -> reset timeout
            #print('resetting timeout; response to destinationID: {}'.format(packet[Raft].destinationID))
            #print('state : {}'.format(self.status))
            self.term = packet[Raft].currentTerm

            if self.status == STATUSES['leader']:
                print('stepping down as leader.')
                self.status = STATUSES['follower']

            self.init_timeout()

        if packet[Raft].messageType == COMMANDS[
                'AppendEntriesReply']:  # received a cloned AppendEntries response from node -> reset timeout
            #print('resetting timeout; AppendEntries from: {}'.format(packet[Raft].destinationID))
            #print('state : {}'.format(self.status))

            self.term = packet[Raft].currentTerm
            self.logIndex = packet[Raft].logIndex

            self.init_timeout()
        if packet[Raft].messageType == COMMANDS['AppendEntries']:
            print('starting Transaction: {}'.format(time.time()))

        if packet[Raft].messageType == COMMANDS[
                'NewRequest'] and self.status == STATUSES['leader']:
            # received a redirected New Request from a client
            # new request can be made only by controllers
            self.counter_new_request += 1
            print('New Request received; total: {}; time: {}'.format(
                self.counter_new_request, time.time()))

            packet[Raft].sourceID = 0x0
            packet[Raft].destinationID = self.nodeID
            packet[IP].srcAddr = args.source
            #packet[Raft].show()
            send_no_reply(packet)

            if (self.counter_new_request %
                    50) == 0 and self.nodeID == 4:  # emulating failure
                import os
                print('emulating failure')
                os._exit(0)

        if packet[Raft].messageType == COMMANDS['RejectNewRequest']:
            self.counter_rejected_requests += 1
            print('New request rejected; total {}'.format(
                self.counter_rejected_requests))

            #print('state : {}'.format(self.status))
        if packet[Raft].messageType == COMMANDS['CommitValue']:
            print('Transaction complete. time: {}'.format(time.time()))

        if packet[Raft].messageType == COMMANDS['RetrieveLog']:
            print('Retrieved Value: {} at Index: {}'.format(
                packet[Raft].data, packet[Raft].logIndex))