def getElectionMsgsForInstance(self, instId: int) -> \ Sequence[Union[Nomination, Primary]]: """ Get nomination and primary messages for instance with id `instId`. """ msgs = [] replica = self.replicas[instId] # If a primary for this instance has been selected then send a # primary declaration for the selected primary if replica.isPrimary is not None: msgs.append(Primary(replica.primaryName, instId, self.viewNo, replica.last_ordered_3pc[1])) else: # If a primary for this instance has not been selected then send # nomination and primary declaration that this node made for the # instance with id `instId` if self.didReplicaNominate(instId): nm, seqNo = self.nominations[instId][replica.name] msgs.append(Nomination(nm, instId, self.viewNo, seqNo)) if self.didReplicaDeclarePrimary(instId): nm, seqNo = self.primaryDeclarations[instId][replica.name] msgs.append(Primary(nm, instId, self.viewNo, seqNo)) return msgs
def sendPrimary(self, instId: int, primaryName: str, lastOrderedSeqNo: int): """ Declare a primary and broadcast the message. :param instId: the instanceId to which the primary belongs :param primaryName: the name of the primary replica """ replica = self.replicas[instId] self.primaryDeclarations[instId][replica.name] = (primaryName, lastOrderedSeqNo) self.scheduledPrimaryDecisions[instId] = None logger.info("{} declaring primary as: {} on the basis of {}".format( replica, primaryName, self.nominations[instId])) prim = Primary(primaryName, instId, self.viewNo, lastOrderedSeqNo) self.send(prim) self.select_primary(instId, prim)
def testBlacklistNodeOnMultiplePrimaryDeclarations(looper, txnPoolNodeSet): """ A node that sends multiple primary declarations must be blacklisted by other nodes """ A, B, C, D = txnPoolNodeSet # B sends more than 2 primary declarations for i in range(3): B.send(Primary(D.name, 0, B.viewNo)) # B should be blacklisted by A, C, D def chk(): for node in A, C, D: assert node.isNodeBlacklisted(B.name) timeout = waits.expectedPoolNominationTimeout(len(txnPoolNodeSet)) looper.run(eventually(chk, retryWait=1, timeout=timeout))
def primaryByNode(name: str, byNode: TestNode, instId: int): return Primary(name, instId, byNode.viewNo, byNode.replicas[instId].lastOrderedPPSeqNo[1])