Search in sources :

Example 1 with SwitchBehavior

use of org.opendaylight.controller.cluster.raft.base.messages.SwitchBehavior in project controller by opendaylight.

the class RaftActorTest method testSwitchBehavior.

@Test
public void testSwitchBehavior() {
    String persistenceId = factory.generateActorId("leader-");
    DefaultConfigParamsImpl config = new DefaultConfigParamsImpl();
    config.setCustomRaftPolicyImplementationClass(DisableElectionsRaftPolicy.class.getName());
    config.setHeartBeatInterval(new FiniteDuration(1, TimeUnit.DAYS));
    config.setIsolatedLeaderCheckInterval(new FiniteDuration(1, TimeUnit.DAYS));
    config.setSnapshotBatchCount(5);
    DataPersistenceProvider dataPersistenceProvider = createProvider();
    Map<String, String> peerAddresses = ImmutableMap.<String, String>builder().build();
    TestActorRef<MockRaftActor> mockActorRef = factory.createTestActor(MockRaftActor.props(persistenceId, peerAddresses, config, dataPersistenceProvider), persistenceId);
    MockRaftActor leaderActor = mockActorRef.underlyingActor();
    leaderActor.waitForRecoveryComplete();
    leaderActor.handleCommand(new SwitchBehavior(RaftState.Follower, 100));
    assertEquals(100, leaderActor.getRaftActorContext().getTermInformation().getCurrentTerm());
    assertEquals(RaftState.Follower, leaderActor.getCurrentBehavior().state());
    leaderActor.handleCommand(new SwitchBehavior(RaftState.Leader, 110));
    assertEquals(110, leaderActor.getRaftActorContext().getTermInformation().getCurrentTerm());
    assertEquals(RaftState.Leader, leaderActor.getCurrentBehavior().state());
    leaderActor.handleCommand(new SwitchBehavior(RaftState.Candidate, 125));
    assertEquals(110, leaderActor.getRaftActorContext().getTermInformation().getCurrentTerm());
    assertEquals(RaftState.Leader, leaderActor.getCurrentBehavior().state());
    leaderActor.handleCommand(new SwitchBehavior(RaftState.IsolatedLeader, 125));
    assertEquals(110, leaderActor.getRaftActorContext().getTermInformation().getCurrentTerm());
    assertEquals(RaftState.Leader, leaderActor.getCurrentBehavior().state());
}
Also used : DisableElectionsRaftPolicy(org.opendaylight.controller.cluster.raft.policy.DisableElectionsRaftPolicy) DataPersistenceProvider(org.opendaylight.controller.cluster.DataPersistenceProvider) FiniteDuration(scala.concurrent.duration.FiniteDuration) ByteString(com.google.protobuf.ByteString) SwitchBehavior(org.opendaylight.controller.cluster.raft.base.messages.SwitchBehavior) Test(org.junit.Test)

Example 2 with SwitchBehavior

use of org.opendaylight.controller.cluster.raft.base.messages.SwitchBehavior in project controller by opendaylight.

the class ShardManagerTest method testOnReceiveSwitchShardBehavior.

@Test
public void testOnReceiveSwitchShardBehavior() throws Exception {
    new TestKit(getSystem()) {

        {
            final ActorRef shardManager = actorFactory.createActor(newPropsShardMgrWithMockShardActor());
            shardManager.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
            shardManager.tell(new ActorInitialized(), mockShardActor);
            shardManager.tell(new SwitchShardBehavior(mockShardName, RaftState.Leader, 1000), getRef());
            SwitchBehavior switchBehavior = MessageCollectorActor.expectFirstMatching(mockShardActor, SwitchBehavior.class);
            assertEquals(RaftState.Leader, switchBehavior.getNewState());
            assertEquals(1000, switchBehavior.getNewTerm());
        }
    };
}
Also used : UpdateSchemaContext(org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext) ActorRef(akka.actor.ActorRef) TestActorRef(akka.testkit.TestActorRef) ActorInitialized(org.opendaylight.controller.cluster.datastore.messages.ActorInitialized) TestKit(akka.testkit.javadsl.TestKit) SwitchBehavior(org.opendaylight.controller.cluster.raft.base.messages.SwitchBehavior) Test(org.junit.Test) AbstractShardManagerTest(org.opendaylight.controller.cluster.datastore.AbstractShardManagerTest)

Example 3 with SwitchBehavior

use of org.opendaylight.controller.cluster.raft.base.messages.SwitchBehavior in project controller by opendaylight.

the class ShardManager method onSwitchShardBehavior.

private void onSwitchShardBehavior(final SwitchShardBehavior message) {
    final ShardIdentifier identifier = message.getShardId();
    if (identifier != null) {
        final ShardInformation info = localShards.get(identifier.getShardName());
        if (info == null) {
            getSender().tell(new Status.Failure(new IllegalArgumentException("Shard " + identifier + " is not local")), getSelf());
            return;
        }
        switchShardBehavior(info, new SwitchBehavior(message.getNewState(), message.getTerm()));
    } else {
        for (ShardInformation info : localShards.values()) {
            switchShardBehavior(info, new SwitchBehavior(message.getNewState(), message.getTerm()));
        }
    }
    getSender().tell(new Status.Success(null), getSelf());
}
Also used : FollowerInitialSyncUpStatus(org.opendaylight.controller.cluster.raft.base.messages.FollowerInitialSyncUpStatus) ChangeShardMembersVotingStatus(org.opendaylight.controller.cluster.datastore.messages.ChangeShardMembersVotingStatus) FlipShardMembersVotingStatus(org.opendaylight.controller.cluster.datastore.messages.FlipShardMembersVotingStatus) Status(akka.actor.Status) ServerChangeStatus(org.opendaylight.controller.cluster.raft.messages.ServerChangeStatus) ChangeServersVotingStatus(org.opendaylight.controller.cluster.raft.messages.ChangeServersVotingStatus) ShardIdentifier(org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier) SwitchBehavior(org.opendaylight.controller.cluster.raft.base.messages.SwitchBehavior)

Example 4 with SwitchBehavior

use of org.opendaylight.controller.cluster.raft.base.messages.SwitchBehavior in project controller by opendaylight.

the class RaftActor method handleCommand.

/**
 * Handles a message.
 *
 * @deprecated This method is not final for testing purposes. DO NOT OVERRIDE IT, override
 * {@link #handleNonRaftCommand(Object)} instead.
 */
@Deprecated
@Override
protected // FIXME: make this method final once our unit tests do not need to override it
void handleCommand(final Object message) {
    if (serverConfigurationSupport.handleMessage(message, getSender())) {
        return;
    }
    if (snapshotSupport.handleSnapshotMessage(message, getSender())) {
        return;
    }
    if (message instanceof ApplyState) {
        ApplyState applyState = (ApplyState) message;
        if (!hasFollowers()) {
            // for single node, the capture should happen after the apply state
            // as we delete messages from the persistent journal which have made it to the snapshot
            // capturing the snapshot before applying makes the persistent journal and snapshot out of sync
            // and recovery shows data missing
            context.getReplicatedLog().captureSnapshotIfReady(applyState.getReplicatedLogEntry());
            context.getSnapshotManager().trimLog(context.getLastApplied());
        }
        possiblyHandleBehaviorMessage(message);
    } else if (message instanceof ApplyJournalEntries) {
        ApplyJournalEntries applyEntries = (ApplyJournalEntries) message;
        LOG.debug("{}: Persisting ApplyJournalEntries with index={}", persistenceId(), applyEntries.getToIndex());
        persistence().persistAsync(applyEntries, NoopProcedure.instance());
    } else if (message instanceof FindLeader) {
        getSender().tell(new FindLeaderReply(getLeaderAddress()), getSelf());
    } else if (message instanceof GetOnDemandRaftState) {
        onGetOnDemandRaftStats();
    } else if (message instanceof InitiateCaptureSnapshot) {
        captureSnapshot();
    } else if (message instanceof SwitchBehavior) {
        switchBehavior((SwitchBehavior) message);
    } else if (message instanceof LeaderTransitioning) {
        onLeaderTransitioning((LeaderTransitioning) message);
    } else if (message instanceof Shutdown) {
        onShutDown();
    } else if (message instanceof Runnable) {
        ((Runnable) message).run();
    } else if (message instanceof NoopPayload) {
        persistData(null, null, (NoopPayload) message, false);
    } else if (message instanceof RequestLeadership) {
        onRequestLeadership((RequestLeadership) message);
    } else if (!possiblyHandleBehaviorMessage(message)) {
        handleNonRaftCommand(message);
    }
}
Also used : InitiateCaptureSnapshot(org.opendaylight.controller.cluster.raft.base.messages.InitiateCaptureSnapshot) FindLeaderReply(org.opendaylight.controller.cluster.raft.client.messages.FindLeaderReply) Shutdown(org.opendaylight.controller.cluster.raft.client.messages.Shutdown) RequestLeadership(org.opendaylight.controller.cluster.raft.messages.RequestLeadership) FindLeader(org.opendaylight.controller.cluster.raft.client.messages.FindLeader) NoopPayload(org.opendaylight.controller.cluster.raft.persisted.NoopPayload) ApplyJournalEntries(org.opendaylight.controller.cluster.raft.persisted.ApplyJournalEntries) GetOnDemandRaftState(org.opendaylight.controller.cluster.raft.client.messages.GetOnDemandRaftState) LeaderTransitioning(org.opendaylight.controller.cluster.raft.base.messages.LeaderTransitioning) SwitchBehavior(org.opendaylight.controller.cluster.raft.base.messages.SwitchBehavior) ApplyState(org.opendaylight.controller.cluster.raft.base.messages.ApplyState)

Aggregations

SwitchBehavior (org.opendaylight.controller.cluster.raft.base.messages.SwitchBehavior)4 Test (org.junit.Test)2 ActorRef (akka.actor.ActorRef)1 Status (akka.actor.Status)1 TestActorRef (akka.testkit.TestActorRef)1 TestKit (akka.testkit.javadsl.TestKit)1 ByteString (com.google.protobuf.ByteString)1 DataPersistenceProvider (org.opendaylight.controller.cluster.DataPersistenceProvider)1 AbstractShardManagerTest (org.opendaylight.controller.cluster.datastore.AbstractShardManagerTest)1 ShardIdentifier (org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier)1 ActorInitialized (org.opendaylight.controller.cluster.datastore.messages.ActorInitialized)1 ChangeShardMembersVotingStatus (org.opendaylight.controller.cluster.datastore.messages.ChangeShardMembersVotingStatus)1 FlipShardMembersVotingStatus (org.opendaylight.controller.cluster.datastore.messages.FlipShardMembersVotingStatus)1 UpdateSchemaContext (org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext)1 ApplyState (org.opendaylight.controller.cluster.raft.base.messages.ApplyState)1 FollowerInitialSyncUpStatus (org.opendaylight.controller.cluster.raft.base.messages.FollowerInitialSyncUpStatus)1 InitiateCaptureSnapshot (org.opendaylight.controller.cluster.raft.base.messages.InitiateCaptureSnapshot)1 LeaderTransitioning (org.opendaylight.controller.cluster.raft.base.messages.LeaderTransitioning)1 FindLeader (org.opendaylight.controller.cluster.raft.client.messages.FindLeader)1 FindLeaderReply (org.opendaylight.controller.cluster.raft.client.messages.FindLeaderReply)1