def setUp(self): # Replica fixture simulation = get_mock_simulation() replica = lambda: random.choice(simulation.replicas) # Version fixtures a = Version.new('A')(replica()) b = Version.new('B')(replica()) c = Version.new('C')(replica()) d = Version.new('D')(replica()) # Create mock log self.log = MultiObjectWriteLog() for obj in (a, b, c): self.log.append(obj, 1) # Create unordered log objects. self.log.append(a.nextv(replica()), 2) self.log.append(a.nextv(replica()), 2) self.log.append(a.nextv(replica()), 2) self.log.append(a.nextv(replica()), 3) self.log.append(a.nextv(replica()), 3) self.log.append(b.nextv(replica()), 3) self.log.append(b.nextv(replica()), 4) self.log.append(b.nextv(replica()), 4) self.log.append(c.nextv(replica()), 4) self.log.append(d, 5) self.log.commitIndex = 9
def __init__(self, simulation, **kwargs): ## Initialize the replica super(RaftReplica, self).__init__(simulation, **kwargs) ## Initialize Raft Specific settings self.state = State.FOLLOWER self.currentTerm = 0 self.votedFor = None self.log = MultiObjectWriteLog() self.cache = {} ## Policies self.read_policy = ReadPolicy.get(kwargs.get('read_policy', READ_POLICY)) self.aggregate_writes = kwargs.get('aggregate_writes', AGGREGATE_WRITES) ## Timers for work eto = kwargs.get('election_timeout', ELECTION_TIMEOUT) hbt = kwargs.get('heartbeat_interval', HEARTBEAT_INTERVAL) self.timeout = ElectionTimer.fromReplica(self, eto) self.heartbeat = Timer(self.env, hbt, self.on_heartbeat_timeout) ## Leader state self.nextIndex = None self.matchIndex = None