Example #1
0
    def on_accept(self,msg):
        log.info('ACCEPTOR: start on_accept')
        if msg.instance_number < self.accepted_lease.instance_number:
            #acceptor dose not vote for values in outdated instance
            log.info('ACCEPTOR: acceptor dose not vote for values in outdated instance')

            m = Message(method=NACK,
                    instance_number=self.accepted_lease.instance_number)

        elif msg.instance_number > self.accepted_lease.instance_number:
            #acceptor has missed instance and votes for value
            log.info('ACCEPTOR: acceptor has missed instance and votes for value')
            self.last_lease.set_none()
            self.accepted_lease.sid = msg.suggested_host
            self.accepted_lease.timeout= msg.suggested_host_timeout
            self.ballot = msg.ballot
            self.accepted_lease.instance_number = msg.instance_number
            #SEND ACK
            m = Message(method=ACK,
                    instance_number=self.accepted_lease.instance_number,
                    suggested_host=self.accepted_lease.sid,
                    suggested_host_timeout=self.accepted_lease.timeout,
                    ballot=self.ballot)

        elif msg.ballot >= self.ballot:
            #acceptor votes for proposal
            log.info('ACCEPTOR: acceptor has voted for proposal, now accept')
            self.accepted_lease.sid = msg.suggested_host
            self.accepted_lease.timeout = msg.suggested_host_timeout
            #TODO
            #maybe need to in on_accept, usually self.ballot was
            #updated in on_prepare.
            #self.ballot = msg.ballot

            #SEND ACK
            m = Message(method=ACK,
                    instance_number=self.accepted_lease.instance_number,
                    suggested_host=self.accepted_lease.sid,
                    suggested_host_timeout=self.accepted_lease.timeout,
                    ballot=msg.ballot)

        else:
            #acceptor has seen newer proposal and connot accept
            #SEND NACK
            log.info('ACCEPTOR: acceptor has seen newer proposal and connot accept')
            m = Message(method=NACK,
                    instance_number=self.accepted_lease.instance_number)

        m.add_sid(conf.myself.sid)
        m.add_echo(msg.timestamp)
        log.debug('ACCEPTOR: response from on_accept %s', m.enpack())
        do_send(msg.sid, m.enpack())
Example #2
0
 def _notify_all(self, m):
     m.add_sid(conf.myself.sid)
     for server in conf.server_list.itervalues():
         do_send(server.sid, m.enpack())
Example #3
0
    def on_prepare(self, msg):
        #maybe RENEW and PREPARE
        log.info('ACCEPTOR: start on_%s', msg.method)
        log.debug('GET msg %s', msg)
        #if msg.instance_number < self.accepted_lease.instance_number and self.last_lease.sid == None:
        if msg.instance_number < self.accepted_lease.instance_number:
            log.info('ACCEPTOR: proposer has missed some instance')
            #proposer has missed some instance
            m = Message(method=OUTDATE,
                    suggested_host=self.accepted_lease.sid,
                    suggested_host_timeout=self.accepted_lease.timeout,
                    ballot=self.ballot,
                    instance_number=self.accepted_lease.instance_number)
            #SEND OUTDATE(None, local_lamda_acc, local_ballot,
            #local_instance_nubmer
        else:
            if  msg.instance_number > self.accepted_lease.instance_number:
                #local acceptor has missed some instacne
                #work instance_number or last lease instance_number?
                if msg.method == RENEW and self.last_lease.sid and \
                        msg.instance_number == self.last_lease.instance_number + 1:

                    log.info('ACCEPTOR: agree to RENEW msg')
                    self.accepted_lease.sid = self.last_lease.sid
                    self.accepted_lease.timeout = self.last_lease.timeout
                    self.ballot = msg.ballot
                    self.accepted_lease.instance_number = msg.instance_number
                    m = Message(method=ACK,
                            ballot=msg.ballot,
                            instance_number=msg.instance_number,
                            suggested_host=self.accepted_lease.sid,
                            suggested_host_timeout=None)
                else:
                    log.info('ACCEPTOR: local acceptor has missed some instacne')
                    self.accepted_lease.set_none()
                    self.last_lease.set_none()
                    self.ballot = msg.ballot
                    self.accepted_lease.instance_number = msg.instance_number
                    m = Message(method=ACK,
                            ballot=msg.ballot,
                            instance_number=msg.instance_number,
                            suggested_host=None,
                            suggested_host_timeout=None)

            elif msg.ballot < self.ballot:
                #acceptor has seen a newer proposal
                log.info('ACCEPTOR: acceptor has seen a newer proposal')
                m = Message(method=NACK,
                        ballot=self.ballot,
                        instance_number=self.accepted_lease.instance_number,
                        suggested_host=None,
                        suggested_host_timeout=None)
            else:
                #accept agree to the proposal
                log.info('ACCEPTOR: accept agree to proposal')
                self.ballot = msg.ballot
                if self.accepted_lease.sid != None and \
                        self.accepted_lease.timeout != None:
                    #!!!acceptor force proposer to use prior value!!!
                    log.info('ACCEPTOR: acceptor force proposer to use prior value')
                    m = Message(method=ACK,
                            ballot=self.ballot,
                            instance_number=msg.instance_number,
                            suggested_host=self.accepted_lease.sid,
                            suggested_host_timeout=self.accepted_lease.timeout)
                else:
                    log.info('ACCEPTOR: no accecpted lease , so accept this')
                    m = Message(method=ACK,
                            ballot=self.ballot,
                            instance_number=msg.instance_number,
                            suggested_host=None,
                            suggested_host_timeout=None)

            #update dict to add sid
        m.add_sid(conf.myself.sid)
        m.add_echo(msg.timestamp)
        do_send(msg.sid, m.enpack())
        log.debug('ACCEPTOR: response from on_prepare %s', m.enpack())