Search in sources :

Example 1 with RaftState

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

the class AbstractLeaderElectionScenarioTest method verifyBehaviorState.

@SuppressWarnings("checkstyle:IllegalCatch")
void verifyBehaviorState(final String name, final MemberActor actor, final RaftState expState) {
    RaftState actualState;
    try {
        actualState = (RaftState) Await.result(Patterns.ask(actor.self(), GetBehaviorState.INSTANCE, Timeout.apply(5, TimeUnit.SECONDS)), Duration.apply(5, TimeUnit.SECONDS));
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    assertEquals(name + " behavior state", expState, actualState);
}
Also used : RaftState(org.opendaylight.controller.cluster.raft.RaftState) TimeoutException(java.util.concurrent.TimeoutException)

Example 2 with RaftState

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

the class AbstractRaftActorBehaviorTest method testHandleAppendEntriesAddSameEntryToLog.

@Test
public void testHandleAppendEntriesAddSameEntryToLog() {
    MockRaftActorContext context = createActorContext();
    context.getTermInformation().update(2, "test");
    // Prepare the receivers log
    MockRaftActorContext.MockPayload payload = new MockRaftActorContext.MockPayload("zero");
    setLastLogEntry(context, 2, 0, payload);
    List<ReplicatedLogEntry> entries = new ArrayList<>();
    entries.add(new SimpleReplicatedLogEntry(0, 2, payload));
    final AppendEntries appendEntries = new AppendEntries(2, "leader-1", -1, -1, entries, 2, -1, (short) 0);
    behavior = createBehavior(context);
    assertFalse("This test should be overridden when testing Candidate", behavior instanceof Candidate);
    RaftState expected = behavior.state();
    // Check that the behavior does not handle unknwon message
    assertNull(behavior.handleMessage(behaviorActor, "unknown"));
    RaftActorBehavior raftBehavior = behavior.handleMessage(behaviorActor, appendEntries);
    assertEquals("Raft state", expected, raftBehavior.state());
    assertEquals("ReplicatedLog size", 1, context.getReplicatedLog().size());
    handleAppendEntriesAddSameEntryToLogReply(behaviorActor);
}
Also used : SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) ReplicatedLogEntry(org.opendaylight.controller.cluster.raft.ReplicatedLogEntry) SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) MockRaftActorContext(org.opendaylight.controller.cluster.raft.MockRaftActorContext) RaftState(org.opendaylight.controller.cluster.raft.RaftState) ArrayList(java.util.ArrayList) AppendEntries(org.opendaylight.controller.cluster.raft.messages.AppendEntries) AbstractActorTest(org.opendaylight.controller.cluster.raft.AbstractActorTest) Test(org.junit.Test)

Example 3 with RaftState

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

the class AbstractRaftActorBehaviorTest method testHandleAppendEntriesSenderTermLessThanReceiverTerm.

/**
 * This test verifies that when an AppendEntries is received with a term that
 * is less that the currentTerm of the RaftActor then the RaftActor does not
 * change it's state and it responds back with a failure.
 */
@Test
public void testHandleAppendEntriesSenderTermLessThanReceiverTerm() {
    MockRaftActorContext context = createActorContext();
    short payloadVersion = 5;
    context.setPayloadVersion(payloadVersion);
    // First set the receivers term to a high number (1000)
    context.getTermInformation().update(1000, "test");
    AppendEntries appendEntries = new AppendEntries(100, "leader-1", 0, 0, Collections.emptyList(), 101, -1, (short) 4);
    behavior = createBehavior(context);
    RaftState expected = behavior.state();
    RaftActorBehavior raftBehavior = behavior.handleMessage(behaviorActor, appendEntries);
    assertEquals("Raft state", expected, raftBehavior.state());
    // Also expect an AppendEntriesReply to be sent where success is false
    AppendEntriesReply reply = MessageCollectorActor.expectFirstMatching(behaviorActor, AppendEntriesReply.class);
    assertEquals("isSuccess", false, reply.isSuccess());
    assertEquals("getPayloadVersion", payloadVersion, reply.getPayloadVersion());
}
Also used : AppendEntriesReply(org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply) MockRaftActorContext(org.opendaylight.controller.cluster.raft.MockRaftActorContext) RaftState(org.opendaylight.controller.cluster.raft.RaftState) AppendEntries(org.opendaylight.controller.cluster.raft.messages.AppendEntries) AbstractActorTest(org.opendaylight.controller.cluster.raft.AbstractActorTest) Test(org.junit.Test)

Example 4 with RaftState

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

the class ClusterAdminRpcServiceTest method testAddShardReplica.

@Test
public void testAddShardReplica() throws Exception {
    String name = "testAddShardReplica";
    String moduleShardsConfig = "module-shards-cars-member-1.conf";
    MemberNode leaderNode1 = MemberNode.builder(memberNodes).akkaConfig("Member1").testName(name).moduleShardsConfig(moduleShardsConfig).waitForShardLeader("cars").build();
    MemberNode newReplicaNode2 = MemberNode.builder(memberNodes).akkaConfig("Member2").testName(name).moduleShardsConfig(moduleShardsConfig).build();
    leaderNode1.waitForMembersUp("member-2");
    doAddShardReplica(newReplicaNode2, "cars", "member-1");
    MemberNode newReplicaNode3 = MemberNode.builder(memberNodes).akkaConfig("Member3").testName(name).moduleShardsConfig(moduleShardsConfig).build();
    leaderNode1.waitForMembersUp("member-3");
    newReplicaNode2.waitForMembersUp("member-3");
    doAddShardReplica(newReplicaNode3, "cars", "member-1", "member-2");
    verifyRaftPeersPresent(newReplicaNode2.configDataStore(), "cars", "member-1", "member-3");
    verifyRaftPeersPresent(newReplicaNode2.operDataStore(), "cars", "member-1", "member-3");
    // Write data to member-2's config datastore and read/verify via member-3
    final NormalizedNode<?, ?> configCarsNode = writeCarsNodeAndVerify(newReplicaNode2.configDataStore(), newReplicaNode3.configDataStore());
    // Write data to member-3's oper datastore and read/verify via member-2
    writeCarsNodeAndVerify(newReplicaNode3.operDataStore(), newReplicaNode2.operDataStore());
    // Verify all data has been replicated. We expect 4 log entries and thus last applied index of 3 -
    // 2 ServerConfigurationPayload entries,  the transaction payload entry plus a purge payload.
    RaftStateVerifier verifier = raftState -> {
        assertEquals("Commit index", 3, raftState.getCommitIndex());
        assertEquals("Last applied index", 3, raftState.getLastApplied());
    };
    verifyRaftState(leaderNode1.configDataStore(), "cars", verifier);
    verifyRaftState(leaderNode1.operDataStore(), "cars", verifier);
    verifyRaftState(newReplicaNode2.configDataStore(), "cars", verifier);
    verifyRaftState(newReplicaNode2.operDataStore(), "cars", verifier);
    verifyRaftState(newReplicaNode3.configDataStore(), "cars", verifier);
    verifyRaftState(newReplicaNode3.operDataStore(), "cars", verifier);
    // Restart member-3 and verify the cars config shard is re-instated.
    Cluster.get(leaderNode1.kit().getSystem()).down(Cluster.get(newReplicaNode3.kit().getSystem()).selfAddress());
    newReplicaNode3.cleanup();
    newReplicaNode3 = MemberNode.builder(memberNodes).akkaConfig("Member3").testName(name).moduleShardsConfig(moduleShardsConfig).createOperDatastore(false).build();
    verifyRaftState(newReplicaNode3.configDataStore(), "cars", verifier);
    readCarsNodeAndVerify(newReplicaNode3.configDataStore(), configCarsNode);
}
Also used : SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) Arrays(java.util.Arrays) AddShardReplicaInputBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.cluster.admin.rev151013.AddShardReplicaInputBuilder) PoisonPill(akka.actor.PoisonPill) ActorRef(akka.actor.ActorRef) Cluster(akka.cluster.Cluster) Optional(com.google.common.base.Optional) Map(java.util.Map) CreateShard(org.opendaylight.controller.cluster.datastore.messages.CreateShard) DOMStoreThreePhaseCommitCohort(org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort) ServerConfigurationPayload(org.opendaylight.controller.cluster.raft.persisted.ServerConfigurationPayload) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Set(java.util.Set) GetPrefixShardRoleOutput(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.cluster.admin.rev151013.GetPrefixShardRoleOutput) People(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.people.rev140818.People) GetShardRoleOutput(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.cluster.admin.rev151013.GetShardRoleOutput) GetShardRoleInputBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.cluster.admin.rev151013.GetShardRoleInputBuilder) Assert.assertFalse(org.junit.Assert.assertFalse) AbstractDataStore(org.opendaylight.controller.cluster.datastore.AbstractDataStore) TRUE(java.lang.Boolean.TRUE) CoreMatchers.anyOf(org.hamcrest.CoreMatchers.anyOf) Iterables(com.google.common.collect.Iterables) BackupDatastoreInputBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.cluster.admin.rev151013.BackupDatastoreInputBuilder) SerializationUtils(org.apache.commons.lang3.SerializationUtils) InMemorySnapshotStore(org.opendaylight.controller.cluster.raft.utils.InMemorySnapshotStore) ArrayList(java.util.ArrayList) ClusterUtils(org.opendaylight.controller.cluster.datastore.utils.ClusterUtils) DOMStoreWriteTransaction(org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction) Lists(com.google.common.collect.Lists) Success(akka.actor.Status.Success) RaftStateVerifier(org.opendaylight.controller.cluster.datastore.MemberNode.RaftStateVerifier) ShardIdentifier(org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier) ChangeMemberVotingStatesForShardInputBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.cluster.admin.rev151013.ChangeMemberVotingStatesForShardInputBuilder) Before(org.junit.Before) PrefixShardConfiguration(org.opendaylight.controller.cluster.datastore.config.PrefixShardConfiguration) AddPrefixShardReplicaInput(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.cluster.admin.rev151013.AddPrefixShardReplicaInput) GetPrefixShardRoleInputBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.cluster.admin.rev151013.GetPrefixShardRoleInputBuilder) MemberNode.verifyRaftState(org.opendaylight.controller.cluster.datastore.MemberNode.verifyRaftState) Assert.assertTrue(org.junit.Assert.assertTrue) AddPrefixShardReplicaInputBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.cluster.admin.rev151013.AddPrefixShardReplicaInputBuilder) Test(org.junit.Test) File(java.io.File) ShardResultBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.cluster.admin.rev151013.shard.result.output.ShardResultBuilder) Assert.assertNull(org.junit.Assert.assertNull) InstanceIdentifier(org.opendaylight.yangtools.yang.binding.InstanceIdentifier) RemovePrefixShardReplicaInputBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.cluster.admin.rev151013.RemovePrefixShardReplicaInputBuilder) LogicalDatastoreType(org.opendaylight.mdsal.common.api.LogicalDatastoreType) Shard(org.opendaylight.controller.cluster.datastore.Shard) Assert.assertEquals(org.junit.Assert.assertEquals) RpcError(org.opendaylight.yangtools.yang.common.RpcError) MakeLeaderLocalInputBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.cluster.admin.rev151013.MakeLeaderLocalInputBuilder) AddReplicasForAllShardsOutput(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.cluster.admin.rev151013.AddReplicasForAllShardsOutput) ChangeMemberVotingStatesForAllShardsInputBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.cluster.admin.rev151013.ChangeMemberVotingStatesForAllShardsInputBuilder) ServerInfo(org.opendaylight.controller.cluster.raft.persisted.ServerInfo) GetPrefixShardRoleInput(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.cluster.admin.rev151013.GetPrefixShardRoleInput) CarsModel(org.opendaylight.controller.md.cluster.datastore.model.CarsModel) Assert.assertThat(org.junit.Assert.assertThat) MemberNode.verifyNoShardPresent(org.opendaylight.controller.cluster.datastore.MemberNode.verifyNoShardPresent) After(org.junit.After) DatastoreSnapshot(org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot) FlipMemberVotingStatesForAllShardsOutput(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.cluster.admin.rev151013.FlipMemberVotingStatesForAllShardsOutput) Assert.fail(org.junit.Assert.fail) BindingNormalizedNodeSerializer(org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer) URI(java.net.URI) DOMDataTreeIdentifier(org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier) ImmutableMap(com.google.common.collect.ImmutableMap) Sets(com.google.common.collect.Sets) PeopleModel(org.opendaylight.controller.md.cluster.datastore.model.PeopleModel) List(java.util.List) DataStoreType(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.cluster.admin.rev151013.DataStoreType) DatastoreContext(org.opendaylight.controller.cluster.datastore.DatastoreContext) Entry(java.util.Map.Entry) MemberVotingStateBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.cluster.admin.rev151013.member.voting.states.input.MemberVotingStateBuilder) PrefixShardCreated(org.opendaylight.controller.cluster.sharding.messages.PrefixShardCreated) RemoveShardReplicaInputBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.cluster.admin.rev151013.RemoveShardReplicaInputBuilder) RpcResult(org.opendaylight.yangtools.yang.common.RpcResult) RemovePrefixShardReplicaInput(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.cluster.admin.rev151013.RemovePrefixShardReplicaInput) InMemoryJournal(org.opendaylight.controller.cluster.raft.utils.InMemoryJournal) HashMap(java.util.HashMap) HashSet(java.util.HashSet) ImmutableList(com.google.common.collect.ImmutableList) MemberNode.verifyRaftPeersPresent(org.opendaylight.controller.cluster.datastore.MemberNode.verifyRaftPeersPresent) GetShardRoleInput(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.cluster.admin.rev151013.GetShardRoleInput) RemoveAllShardReplicasInputBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.cluster.admin.rev151013.RemoveAllShardReplicasInputBuilder) Cars(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.Cars) MemberNode(org.opendaylight.controller.cluster.datastore.MemberNode) SimpleEntry(java.util.AbstractMap.SimpleEntry) FALSE(java.lang.Boolean.FALSE) ModuleShardConfiguration(org.opendaylight.controller.cluster.datastore.config.ModuleShardConfiguration) ChangeMemberVotingStatesForAllShardsOutput(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.cluster.admin.rev151013.ChangeMemberVotingStatesForAllShardsOutput) Assert.assertNotNull(org.junit.Assert.assertNotNull) RaftState(org.opendaylight.controller.cluster.raft.RaftState) RemoveAllShardReplicasOutput(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.cluster.admin.rev151013.RemoveAllShardReplicasOutput) FileInputStream(java.io.FileInputStream) TimeUnit(java.util.concurrent.TimeUnit) Mockito(org.mockito.Mockito) ShardResult(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.cluster.admin.rev151013.shard.result.output.ShardResult) UpdateElectionTerm(org.opendaylight.controller.cluster.raft.persisted.UpdateElectionTerm) Collections(java.util.Collections) MemberName(org.opendaylight.controller.cluster.access.concepts.MemberName) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) MemberNode(org.opendaylight.controller.cluster.datastore.MemberNode) RaftStateVerifier(org.opendaylight.controller.cluster.datastore.MemberNode.RaftStateVerifier) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Aggregations

RaftState (org.opendaylight.controller.cluster.raft.RaftState)4 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 AbstractActorTest (org.opendaylight.controller.cluster.raft.AbstractActorTest)2 MockRaftActorContext (org.opendaylight.controller.cluster.raft.MockRaftActorContext)2 AppendEntries (org.opendaylight.controller.cluster.raft.messages.AppendEntries)2 SimpleReplicatedLogEntry (org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry)2 ActorRef (akka.actor.ActorRef)1 PoisonPill (akka.actor.PoisonPill)1 Success (akka.actor.Status.Success)1 Cluster (akka.cluster.Cluster)1 Optional (com.google.common.base.Optional)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Iterables (com.google.common.collect.Iterables)1 Lists (com.google.common.collect.Lists)1 Sets (com.google.common.collect.Sets)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FALSE (java.lang.Boolean.FALSE)1