Search in sources :

Example 1 with RaftActorContext

use of org.opendaylight.controller.cluster.raft.RaftActorContext in project controller by opendaylight.

the class CandidateTest method testWhenACandidateIsCreatedItIncrementsTheCurrentTermAndVotesForItself.

@Test
public void testWhenACandidateIsCreatedItIncrementsTheCurrentTermAndVotesForItself() {
    RaftActorContext raftActorContext = createActorContext();
    long expectedTerm = raftActorContext.getTermInformation().getCurrentTerm();
    candidate = new Candidate(raftActorContext);
    assertEquals("getCurrentTerm", expectedTerm + 1, raftActorContext.getTermInformation().getCurrentTerm());
    assertEquals("getVotedFor", raftActorContext.getId(), raftActorContext.getTermInformation().getVotedFor());
}
Also used : RaftActorContext(org.opendaylight.controller.cluster.raft.RaftActorContext) MockRaftActorContext(org.opendaylight.controller.cluster.raft.MockRaftActorContext) Test(org.junit.Test)

Example 2 with RaftActorContext

use of org.opendaylight.controller.cluster.raft.RaftActorContext in project controller by opendaylight.

the class CandidateTest method testBecomeLeaderOnReceivingMajorityVotesWithNonVotingPeers.

@Test
public void testBecomeLeaderOnReceivingMajorityVotesWithNonVotingPeers() {
    ElectionTerm mockElectionTerm = Mockito.mock(ElectionTerm.class);
    Mockito.doReturn(1L).when(mockElectionTerm).getCurrentTerm();
    RaftActorContext raftActorContext = new RaftActorContextImpl(candidateActor, candidateActor.actorContext(), "candidate", mockElectionTerm, -1, -1, setupPeers(4), new DefaultConfigParamsImpl(), new NonPersistentDataProvider(Runnable::run), applyState -> {
    }, LOG);
    raftActorContext.setReplicatedLog(new MockRaftActorContext.MockReplicatedLogBuilder().build());
    raftActorContext.getPeerInfo("peer1").setVotingState(VotingState.NON_VOTING);
    raftActorContext.getPeerInfo("peer4").setVotingState(VotingState.NON_VOTING);
    candidate = new Candidate(raftActorContext);
    MessageCollectorActor.expectFirstMatching(peerActors[1], RequestVote.class);
    MessageCollectorActor.expectFirstMatching(peerActors[2], RequestVote.class);
    MessageCollectorActor.assertNoneMatching(peerActors[0], RequestVote.class, 300);
    MessageCollectorActor.assertNoneMatching(peerActors[3], RequestVote.class, 100);
    candidate = candidate.handleMessage(peerActors[1], new RequestVoteReply(1, false));
    assertEquals("Behavior", RaftState.Candidate, candidate.state());
    candidate = candidate.handleMessage(peerActors[2], new RequestVoteReply(1, true));
    assertEquals("Behavior", RaftState.Leader, candidate.state());
}
Also used : ElectionTerm(org.opendaylight.controller.cluster.raft.ElectionTerm) RaftActorContextImpl(org.opendaylight.controller.cluster.raft.RaftActorContextImpl) NonPersistentDataProvider(org.opendaylight.controller.cluster.NonPersistentDataProvider) RaftActorContext(org.opendaylight.controller.cluster.raft.RaftActorContext) MockRaftActorContext(org.opendaylight.controller.cluster.raft.MockRaftActorContext) DefaultConfigParamsImpl(org.opendaylight.controller.cluster.raft.DefaultConfigParamsImpl) RequestVoteReply(org.opendaylight.controller.cluster.raft.messages.RequestVoteReply) Test(org.junit.Test)

Example 3 with RaftActorContext

use of org.opendaylight.controller.cluster.raft.RaftActorContext in project controller by opendaylight.

the class CandidateTest method testHandleElectionTimeoutWhenThereAreZeroPeers.

@Test
public void testHandleElectionTimeoutWhenThereAreZeroPeers() {
    RaftActorContext raftActorContext = createActorContext();
    candidate = new Candidate(raftActorContext);
    RaftActorBehavior newBehavior = candidate.handleMessage(candidateActor, ElectionTimeout.INSTANCE);
    assertEquals("Behavior", RaftState.Leader, newBehavior.state());
}
Also used : RaftActorContext(org.opendaylight.controller.cluster.raft.RaftActorContext) MockRaftActorContext(org.opendaylight.controller.cluster.raft.MockRaftActorContext) Test(org.junit.Test)

Example 4 with RaftActorContext

use of org.opendaylight.controller.cluster.raft.RaftActorContext in project controller by opendaylight.

the class ShardTest method testCreateSnapshot.

private void testCreateSnapshot(final boolean persistent, final String shardActorName) throws Exception {
    final AtomicReference<CountDownLatch> latch = new AtomicReference<>(new CountDownLatch(1));
    final AtomicReference<Object> savedSnapshot = new AtomicReference<>();
    class TestPersistentDataProvider extends DelegatingPersistentDataProvider {

        TestPersistentDataProvider(final DataPersistenceProvider delegate) {
            super(delegate);
        }

        @Override
        public void saveSnapshot(final Object obj) {
            savedSnapshot.set(obj);
            super.saveSnapshot(obj);
        }
    }
    dataStoreContextBuilder.persistent(persistent);
    class TestShard extends Shard {

        protected TestShard(final AbstractBuilder<?, ?> builder) {
            super(builder);
            setPersistence(new TestPersistentDataProvider(super.persistence()));
        }

        @Override
        public void handleCommand(final Object message) {
            super.handleCommand(message);
            // XXX:  commit_snapshot equality check references RaftActorSnapshotMessageSupport.COMMIT_SNAPSHOT
            if (message instanceof SaveSnapshotSuccess || "commit_snapshot".equals(message.toString())) {
                latch.get().countDown();
            }
        }

        @Override
        public RaftActorContext getRaftActorContext() {
            return super.getRaftActorContext();
        }
    }
    new ShardTestKit(getSystem()) {

        {
            final Creator<Shard> creator = () -> new TestShard(newShardBuilder());
            final TestActorRef<Shard> shard = actorFactory.createTestActor(Props.create(new DelegatingShardCreator(creator)).withDispatcher(Dispatchers.DefaultDispatcherId()), shardActorName);
            waitUntilLeader(shard);
            writeToStore(shard, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
            final NormalizedNode<?, ?> expectedRoot = readStore(shard, YangInstanceIdentifier.EMPTY);
            // Trigger creation of a snapshot by ensuring
            final RaftActorContext raftActorContext = ((TestShard) shard.underlyingActor()).getRaftActorContext();
            raftActorContext.getSnapshotManager().capture(mock(ReplicatedLogEntry.class), -1);
            awaitAndValidateSnapshot(expectedRoot);
            raftActorContext.getSnapshotManager().capture(mock(ReplicatedLogEntry.class), -1);
            awaitAndValidateSnapshot(expectedRoot);
        }

        private void awaitAndValidateSnapshot(final NormalizedNode<?, ?> expectedRoot) throws InterruptedException, IOException {
            assertEquals("Snapshot saved", true, latch.get().await(5, TimeUnit.SECONDS));
            assertTrue("Invalid saved snapshot " + savedSnapshot.get(), savedSnapshot.get() instanceof Snapshot);
            verifySnapshot((Snapshot) savedSnapshot.get(), expectedRoot);
            latch.set(new CountDownLatch(1));
            savedSnapshot.set(null);
        }

        private void verifySnapshot(final Snapshot snapshot, final NormalizedNode<?, ?> expectedRoot) throws IOException {
            final NormalizedNode<?, ?> actual = ((ShardSnapshotState) snapshot.getState()).getSnapshot().getRootNode().get();
            assertEquals("Root node", expectedRoot, actual);
        }
    };
}
Also used : ShardSnapshotState(org.opendaylight.controller.cluster.datastore.persisted.ShardSnapshotState) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ApplySnapshot(org.opendaylight.controller.cluster.raft.base.messages.ApplySnapshot) MetadataShardDataTreeSnapshot(org.opendaylight.controller.cluster.datastore.persisted.MetadataShardDataTreeSnapshot) Snapshot(org.opendaylight.controller.cluster.raft.persisted.Snapshot) SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) ReplicatedLogEntry(org.opendaylight.controller.cluster.raft.ReplicatedLogEntry) DataPersistenceProvider(org.opendaylight.controller.cluster.DataPersistenceProvider) DelegatingPersistentDataProvider(org.opendaylight.controller.cluster.DelegatingPersistentDataProvider) RaftActorContext(org.opendaylight.controller.cluster.raft.RaftActorContext) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) SaveSnapshotSuccess(akka.persistence.SaveSnapshotSuccess)

Aggregations

RaftActorContext (org.opendaylight.controller.cluster.raft.RaftActorContext)4 Test (org.junit.Test)3 MockRaftActorContext (org.opendaylight.controller.cluster.raft.MockRaftActorContext)3 SaveSnapshotSuccess (akka.persistence.SaveSnapshotSuccess)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 DataPersistenceProvider (org.opendaylight.controller.cluster.DataPersistenceProvider)1 DelegatingPersistentDataProvider (org.opendaylight.controller.cluster.DelegatingPersistentDataProvider)1 NonPersistentDataProvider (org.opendaylight.controller.cluster.NonPersistentDataProvider)1 MetadataShardDataTreeSnapshot (org.opendaylight.controller.cluster.datastore.persisted.MetadataShardDataTreeSnapshot)1 ShardSnapshotState (org.opendaylight.controller.cluster.datastore.persisted.ShardSnapshotState)1 DefaultConfigParamsImpl (org.opendaylight.controller.cluster.raft.DefaultConfigParamsImpl)1 ElectionTerm (org.opendaylight.controller.cluster.raft.ElectionTerm)1 RaftActorContextImpl (org.opendaylight.controller.cluster.raft.RaftActorContextImpl)1 ReplicatedLogEntry (org.opendaylight.controller.cluster.raft.ReplicatedLogEntry)1 ApplySnapshot (org.opendaylight.controller.cluster.raft.base.messages.ApplySnapshot)1 RequestVoteReply (org.opendaylight.controller.cluster.raft.messages.RequestVoteReply)1 SimpleReplicatedLogEntry (org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry)1 Snapshot (org.opendaylight.controller.cluster.raft.persisted.Snapshot)1 NormalizedNode (org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)1