当前位置: 首页>>代码示例>>Python>>正文


Python Pool.majority方法代码示例

本文整理汇总了Python中pool.Pool.majority方法的典型用法代码示例。如果您正苦于以下问题:Python Pool.majority方法的具体用法?Python Pool.majority怎么用?Python Pool.majority使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pool.Pool的用法示例。


在下文中一共展示了Pool.majority方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Server

# 需要导入模块: from pool import Pool [as 别名]
# 或者: from pool.Pool import majority [as 别名]

#.........这里部分代码省略.........
        if request['term'] < self.term:
            return

        self.reset_heartbeat()

        log_index = request['log_index']
        log_term = request['log_term']

        if not self._log.match(log_index, log_term):
            return {'rpc': 'append_entries_resp',
                    'peer_id': self.peer_id,
                    'term': self.term,
                    'log_index': self._log.index,
                    'success': False
                    }

        log_entries = request['log_entries']
        self._log.append(log_index, log_entries)

        log_commit = request['log_commit']
        if self._log.commit < log_commit:
            index = min(self._log.index, log_commit)
            self._log.commit = index
            self._log.apply(index)

        if not log_entries:  # no need to answer, the peer might have committed
            return  # new entries but has certainly not replicated new ones

        return {'rpc': 'append_entries_resp',
                'peer_id': self.peer_id,
                'term': self.term,
                'log_index': self._log.index,
                'log_term': self._log.term,
                'success': True,
                }

    def handle_append_entries_resp(self, response):
        if response['success']:
            self._logger.debug('append entries succeeded')

            log_index = response['log_index']
            log_term = response['log_term']
            peer_id = response['peer_id']

            self._pool[peer_id].match = log_index
            self._pool[peer_id].next = log_index + 1

            if (self._log.commit < log_index and
                self._pool.ack(log_index) and log_term == self.term):
                self._log.commit = log_index
                results = self._log.apply(log_index)
                self.return_results(results)

        else:
            peer = self._pool[response['peer_id']]
            peer.next -= 1
            self.append_entries(peer)

            # self._logger.debug('append entries failed')

    def handle_request_vote_req(self, request):
        self._logger.debug('request vote request received')

        if request['term'] < self.term:
            return

        log_index = request['log_index']
        log_term = request['log_term']
        peer_id = request['peer_id']

        if self.voted in (None, peer_id) and self._log.match(log_index, log_term):
            granted = True
            self.reset_heartbeat()
        else:
            granted = False

        return {'rpc': 'request_vote_resp',
                'peer_id': self.peer_id,
                'term': self.term,
                'granted': granted,
                }

    def handle_request_vote_resp(self, response):
        if self.term == response['term'] and response['granted']:
            self.votes.add(response['peer_id'])

            if self._pool.majority(len(self.votes)):
                self.to_leader()

    def handle_client(self, cmd, transport):
        self._log.add(self.term, cmd)
        self._pending_clients[(self.term, self._log.index)] = transport
        self.append_entries()

    def return_results(self, results):
        for result in results:
            term, index, result = result
            transport = self._pending_clients.pop((term, index))
            transport.write(self.encode(result))
            transport.close()
开发者ID:guilload,项目名称:aio-raft,代码行数:104,代码来源:server.py


注:本文中的pool.Pool.majority方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。