本文整理匯總了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()