Search in sources :

Example 1 with ShardManagerSnapshot

use of org.opendaylight.controller.cluster.datastore.persisted.ShardManagerSnapshot in project controller by opendaylight.

the class ShardManagerTest method testShardPersistenceWithRestoredData.

@Test
public void testShardPersistenceWithRestoredData() throws Exception {
    LOG.info("testShardPersistenceWithRestoredData starting");
    new TestKit(getSystem()) {

        {
            MockConfiguration mockConfig = new MockConfiguration(ImmutableMap.<String, List<String>>builder().put("default", Arrays.asList("member-1", "member-2")).put("astronauts", Arrays.asList("member-2")).put("people", Arrays.asList("member-1", "member-2")).build());
            String[] restoredShards = { "default", "astronauts" };
            ShardManagerSnapshot snapshot = new ShardManagerSnapshot(Arrays.asList(restoredShards), Collections.emptyMap());
            InMemorySnapshotStore.addSnapshot("shard-manager-" + shardMrgIDSuffix, snapshot);
            // create shardManager to come up with restored data
            TestActorRef<TestShardManager> newRestoredShardManager = actorFactory.createTestActor(newShardMgrProps(mockConfig).withDispatcher(Dispatchers.DefaultDispatcherId()));
            newRestoredShardManager.underlyingActor().waitForRecoveryComplete();
            newRestoredShardManager.tell(new FindLocalShard("people", false), getRef());
            LocalShardNotFound notFound = expectMsgClass(duration("5 seconds"), LocalShardNotFound.class);
            assertEquals("for uninitialized shard", "people", notFound.getShardName());
            // Verify a local shard is created for the restored shards,
            // although we expect a NotInitializedException for the shards
            // as the actor initialization
            // message is not sent for them
            newRestoredShardManager.tell(new FindLocalShard("default", false), getRef());
            expectMsgClass(duration("5 seconds"), NotInitializedException.class);
            newRestoredShardManager.tell(new FindLocalShard("astronauts", false), getRef());
            expectMsgClass(duration("5 seconds"), NotInitializedException.class);
        }
    };
    LOG.info("testShardPersistenceWithRestoredData ending");
}
Also used : LocalShardNotFound(org.opendaylight.controller.cluster.datastore.messages.LocalShardNotFound) ShardManagerSnapshot(org.opendaylight.controller.cluster.datastore.persisted.ShardManagerSnapshot) FindLocalShard(org.opendaylight.controller.cluster.datastore.messages.FindLocalShard) MockConfiguration(org.opendaylight.controller.cluster.datastore.utils.MockConfiguration) TestKit(akka.testkit.javadsl.TestKit) AddressFromURIString(akka.actor.AddressFromURIString) Test(org.junit.Test) AbstractShardManagerTest(org.opendaylight.controller.cluster.datastore.AbstractShardManagerTest)

Example 2 with ShardManagerSnapshot

use of org.opendaylight.controller.cluster.datastore.persisted.ShardManagerSnapshot in project controller by opendaylight.

the class ShardManagerTest method testGetSnapshot.

@Test
public void testGetSnapshot() throws Exception {
    LOG.info("testGetSnapshot starting");
    TestKit kit = new TestKit(getSystem());
    MockConfiguration mockConfig = new MockConfiguration(ImmutableMap.<String, List<String>>builder().put("shard1", Arrays.asList("member-1")).put("shard2", Arrays.asList("member-1")).put("astronauts", Collections.<String>emptyList()).build());
    TestActorRef<TestShardManager> shardManager = actorFactory.createTestActor(newShardMgrProps(mockConfig).withDispatcher(Dispatchers.DefaultDispatcherId()));
    shardManager.tell(GetSnapshot.INSTANCE, kit.getRef());
    Failure failure = kit.expectMsgClass(Failure.class);
    assertEquals("Failure cause type", IllegalStateException.class, failure.cause().getClass());
    shardManager.tell(new UpdateSchemaContext(TestModel.createTestContext()), ActorRef.noSender());
    waitForShardInitialized(shardManager, "shard1", kit);
    waitForShardInitialized(shardManager, "shard2", kit);
    shardManager.tell(GetSnapshot.INSTANCE, kit.getRef());
    DatastoreSnapshot datastoreSnapshot = expectMsgClassOrFailure(DatastoreSnapshot.class, kit, "GetSnapshot");
    assertEquals("getType", shardMrgIDSuffix, datastoreSnapshot.getType());
    assertNull("Expected null ShardManagerSnapshot", datastoreSnapshot.getShardManagerSnapshot());
    Function<ShardSnapshot, String> shardNameTransformer = ShardSnapshot::getName;
    assertEquals("Shard names", Sets.newHashSet("shard1", "shard2"), Sets.newHashSet(Lists.transform(datastoreSnapshot.getShardSnapshots(), shardNameTransformer)));
    // Add a new replica
    TestKit mockShardLeaderKit = new TestKit(getSystem());
    TestShardManager shardManagerInstance = shardManager.underlyingActor();
    shardManagerInstance.setMessageInterceptor(newFindPrimaryInterceptor(mockShardLeaderKit.getRef()));
    shardManager.tell(new AddShardReplica("astronauts"), kit.getRef());
    mockShardLeaderKit.expectMsgClass(AddServer.class);
    mockShardLeaderKit.reply(new AddServerReply(ServerChangeStatus.OK, ""));
    kit.expectMsgClass(Status.Success.class);
    waitForShardInitialized(shardManager, "astronauts", kit);
    // Send another GetSnapshot and verify
    shardManager.tell(GetSnapshot.INSTANCE, kit.getRef());
    datastoreSnapshot = expectMsgClassOrFailure(DatastoreSnapshot.class, kit, "GetSnapshot");
    assertEquals("Shard names", Sets.newHashSet("shard1", "shard2", "astronauts"), Sets.newHashSet(Lists.transform(datastoreSnapshot.getShardSnapshots(), shardNameTransformer)));
    ShardManagerSnapshot snapshot = datastoreSnapshot.getShardManagerSnapshot();
    assertNotNull("Expected ShardManagerSnapshot", snapshot);
    assertEquals("Shard names", Sets.newHashSet("shard1", "shard2", "astronauts"), Sets.newHashSet(snapshot.getShardList()));
    LOG.info("testGetSnapshot ending");
}
Also used : FollowerInitialSyncUpStatus(org.opendaylight.controller.cluster.raft.base.messages.FollowerInitialSyncUpStatus) ChangeShardMembersVotingStatus(org.opendaylight.controller.cluster.datastore.messages.ChangeShardMembersVotingStatus) Status(akka.actor.Status) ServerChangeStatus(org.opendaylight.controller.cluster.raft.messages.ServerChangeStatus) ChangeServersVotingStatus(org.opendaylight.controller.cluster.raft.messages.ChangeServersVotingStatus) UpdateSchemaContext(org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext) TestKit(akka.testkit.javadsl.TestKit) AddressFromURIString(akka.actor.AddressFromURIString) ShardSnapshot(org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot.ShardSnapshot) ShardManagerSnapshot(org.opendaylight.controller.cluster.datastore.persisted.ShardManagerSnapshot) MockConfiguration(org.opendaylight.controller.cluster.datastore.utils.MockConfiguration) AddShardReplica(org.opendaylight.controller.cluster.datastore.messages.AddShardReplica) DatastoreSnapshot(org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot) Failure(akka.actor.Status.Failure) AddServerReply(org.opendaylight.controller.cluster.raft.messages.AddServerReply) Test(org.junit.Test) AbstractShardManagerTest(org.opendaylight.controller.cluster.datastore.AbstractShardManagerTest)

Example 3 with ShardManagerSnapshot

use of org.opendaylight.controller.cluster.datastore.persisted.ShardManagerSnapshot in project controller by opendaylight.

the class ShardManagerTest method testRestoreFromSnapshot.

@Test
public void testRestoreFromSnapshot() throws Exception {
    LOG.info("testRestoreFromSnapshot starting");
    datastoreContextBuilder.shardInitializationTimeout(3, TimeUnit.SECONDS);
    TestKit kit = new TestKit(getSystem());
    MockConfiguration mockConfig = new MockConfiguration(ImmutableMap.<String, List<String>>builder().put("shard1", Collections.<String>emptyList()).put("shard2", Collections.<String>emptyList()).put("astronauts", Collections.<String>emptyList()).build());
    ShardManagerSnapshot snapshot = new ShardManagerSnapshot(Arrays.asList("shard1", "shard2", "astronauts"), Collections.emptyMap());
    DatastoreSnapshot restoreFromSnapshot = new DatastoreSnapshot(shardMrgIDSuffix, snapshot, Collections.<ShardSnapshot>emptyList());
    TestActorRef<TestShardManager> shardManager = actorFactory.createTestActor(newTestShardMgrBuilder(mockConfig).restoreFromSnapshot(restoreFromSnapshot).props().withDispatcher(Dispatchers.DefaultDispatcherId()));
    shardManager.underlyingActor().waitForRecoveryComplete();
    shardManager.tell(new UpdateSchemaContext(TestModel.createTestContext()), ActorRef.noSender());
    waitForShardInitialized(shardManager, "shard1", kit);
    waitForShardInitialized(shardManager, "shard2", kit);
    waitForShardInitialized(shardManager, "astronauts", kit);
    shardManager.tell(GetSnapshot.INSTANCE, kit.getRef());
    DatastoreSnapshot datastoreSnapshot = expectMsgClassOrFailure(DatastoreSnapshot.class, kit, "GetSnapshot");
    assertEquals("getType", shardMrgIDSuffix, datastoreSnapshot.getType());
    assertNotNull("Expected ShardManagerSnapshot", datastoreSnapshot.getShardManagerSnapshot());
    assertEquals("Shard names", Sets.newHashSet("shard1", "shard2", "astronauts"), Sets.newHashSet(datastoreSnapshot.getShardManagerSnapshot().getShardList()));
    LOG.info("testRestoreFromSnapshot ending");
}
Also used : UpdateSchemaContext(org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext) ShardManagerSnapshot(org.opendaylight.controller.cluster.datastore.persisted.ShardManagerSnapshot) MockConfiguration(org.opendaylight.controller.cluster.datastore.utils.MockConfiguration) TestKit(akka.testkit.javadsl.TestKit) AddressFromURIString(akka.actor.AddressFromURIString) DatastoreSnapshot(org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot) Test(org.junit.Test) AbstractShardManagerTest(org.opendaylight.controller.cluster.datastore.AbstractShardManagerTest)

Example 4 with ShardManagerSnapshot

use of org.opendaylight.controller.cluster.datastore.persisted.ShardManagerSnapshot in project controller by opendaylight.

the class ShardManagerTest method testAddShardReplica.

@Test
public void testAddShardReplica() throws Exception {
    LOG.info("testAddShardReplica starting");
    MockConfiguration mockConfig = new MockConfiguration(ImmutableMap.<String, List<String>>builder().put("default", Arrays.asList("member-1", "member-2")).put("astronauts", Arrays.asList("member-2")).build());
    final String shardManagerID = ShardManagerIdentifier.builder().type(shardMrgIDSuffix).build().toString();
    datastoreContextBuilder.shardManagerPersistenceId(shardManagerID);
    // Create an ActorSystem ShardManager actor for member-1.
    final ActorSystem system1 = newActorSystem("Member1");
    Cluster.get(system1).join(AddressFromURIString.parse("akka://cluster-test@127.0.0.1:2558"));
    ActorRef mockDefaultShardActor = newMockShardActor(system1, Shard.DEFAULT_NAME, "member-1");
    final TestActorRef<TestShardManager> newReplicaShardManager = TestActorRef.create(system1, newTestShardMgrBuilder(mockConfig).shardActor(mockDefaultShardActor).cluster(new ClusterWrapperImpl(system1)).props().withDispatcher(Dispatchers.DefaultDispatcherId()), shardManagerID);
    // Create an ActorSystem ShardManager actor for member-2.
    final ActorSystem system2 = newActorSystem("Member2");
    Cluster.get(system2).join(AddressFromURIString.parse("akka://cluster-test@127.0.0.1:2558"));
    String memberId2 = "member-2-shard-astronauts-" + shardMrgIDSuffix;
    String name = ShardIdentifier.create("astronauts", MEMBER_2, "config").toString();
    final TestActorRef<MockRespondActor> mockShardLeaderActor = TestActorRef.create(system2, Props.create(MockRespondActor.class, AddServer.class, new AddServerReply(ServerChangeStatus.OK, memberId2)).withDispatcher(Dispatchers.DefaultDispatcherId()), name);
    final TestActorRef<TestShardManager> leaderShardManager = TestActorRef.create(system2, newTestShardMgrBuilder(mockConfig).shardActor(mockShardLeaderActor).cluster(new ClusterWrapperImpl(system2)).props().withDispatcher(Dispatchers.DefaultDispatcherId()), shardManagerID);
    new TestKit(system1) {

        {
            newReplicaShardManager.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
            leaderShardManager.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
            leaderShardManager.tell(new ActorInitialized(), mockShardLeaderActor);
            short leaderVersion = DataStoreVersions.CURRENT_VERSION - 1;
            leaderShardManager.tell(new ShardLeaderStateChanged(memberId2, memberId2, mock(DataTree.class), leaderVersion), mockShardLeaderActor);
            leaderShardManager.tell(new RoleChangeNotification(memberId2, RaftState.Candidate.name(), RaftState.Leader.name()), mockShardLeaderActor);
            newReplicaShardManager.underlyingActor().waitForMemberUp();
            leaderShardManager.underlyingActor().waitForMemberUp();
            // Have a dummy snapshot to be overwritten by the new data
            // persisted.
            String[] restoredShards = { "default", "people" };
            ShardManagerSnapshot snapshot = new ShardManagerSnapshot(Arrays.asList(restoredShards), Collections.emptyMap());
            InMemorySnapshotStore.addSnapshot(shardManagerID, snapshot);
            Uninterruptibles.sleepUninterruptibly(2, TimeUnit.MILLISECONDS);
            InMemorySnapshotStore.addSnapshotSavedLatch(shardManagerID);
            InMemorySnapshotStore.addSnapshotDeletedLatch(shardManagerID);
            // construct a mock response message
            newReplicaShardManager.tell(new AddShardReplica("astronauts"), getRef());
            AddServer addServerMsg = MessageCollectorActor.expectFirstMatching(mockShardLeaderActor, AddServer.class);
            String addServerId = "member-1-shard-astronauts-" + shardMrgIDSuffix;
            assertEquals("AddServer serverId", addServerId, addServerMsg.getNewServerId());
            expectMsgClass(duration("5 seconds"), Status.Success.class);
            InMemorySnapshotStore.waitForSavedSnapshot(shardManagerID, ShardManagerSnapshot.class);
            InMemorySnapshotStore.waitForDeletedSnapshot(shardManagerID);
            List<ShardManagerSnapshot> persistedSnapshots = InMemorySnapshotStore.getSnapshots(shardManagerID, ShardManagerSnapshot.class);
            assertEquals("Number of snapshots persisted", 1, persistedSnapshots.size());
            ShardManagerSnapshot shardManagerSnapshot = persistedSnapshots.get(0);
            assertEquals("Persisted local shards", Sets.newHashSet("default", "astronauts"), Sets.newHashSet(shardManagerSnapshot.getShardList()));
        }
    };
    LOG.info("testAddShardReplica ending");
}
Also used : ActorSystem(akka.actor.ActorSystem) FollowerInitialSyncUpStatus(org.opendaylight.controller.cluster.raft.base.messages.FollowerInitialSyncUpStatus) ChangeShardMembersVotingStatus(org.opendaylight.controller.cluster.datastore.messages.ChangeShardMembersVotingStatus) Status(akka.actor.Status) ServerChangeStatus(org.opendaylight.controller.cluster.raft.messages.ServerChangeStatus) ChangeServersVotingStatus(org.opendaylight.controller.cluster.raft.messages.ChangeServersVotingStatus) ClusterWrapperImpl(org.opendaylight.controller.cluster.datastore.ClusterWrapperImpl) UpdateSchemaContext(org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext) ShardLeaderStateChanged(org.opendaylight.controller.cluster.datastore.messages.ShardLeaderStateChanged) ActorRef(akka.actor.ActorRef) TestActorRef(akka.testkit.TestActorRef) RoleChangeNotification(org.opendaylight.controller.cluster.notifications.RoleChangeNotification) AddressFromURIString(akka.actor.AddressFromURIString) TestKit(akka.testkit.javadsl.TestKit) AddServer(org.opendaylight.controller.cluster.raft.messages.AddServer) ShardManagerSnapshot(org.opendaylight.controller.cluster.datastore.persisted.ShardManagerSnapshot) MockConfiguration(org.opendaylight.controller.cluster.datastore.utils.MockConfiguration) List(java.util.List) ActorInitialized(org.opendaylight.controller.cluster.datastore.messages.ActorInitialized) AddShardReplica(org.opendaylight.controller.cluster.datastore.messages.AddShardReplica) AddServerReply(org.opendaylight.controller.cluster.raft.messages.AddServerReply) Test(org.junit.Test) AbstractShardManagerTest(org.opendaylight.controller.cluster.datastore.AbstractShardManagerTest)

Example 5 with ShardManagerSnapshot

use of org.opendaylight.controller.cluster.datastore.persisted.ShardManagerSnapshot in project controller by opendaylight.

the class ShardManagerGetSnapshotReplyActorTest method testSuccess.

@Test
public void testSuccess() {
    TestKit kit = new TestKit(getSystem());
    List<String> shardList = Arrays.asList("shard1", "shard2", "shard3");
    ShardManagerSnapshot shardManagerSnapshot = new ShardManagerSnapshot(shardList, Collections.emptyMap());
    ActorRef replyActor = getSystem().actorOf(ShardManagerGetSnapshotReplyActor.props(shardList, "config", shardManagerSnapshot, kit.getRef(), "shard-manager", Duration.create(100, TimeUnit.SECONDS)), "testSuccess");
    kit.watch(replyActor);
    ByteState shard1SnapshotState = ByteState.of(new byte[] { 1, 2, 3 });
    replyActor.tell(new GetSnapshotReply(ShardIdentifier.create("shard1", MEMBER_1, "config").toString(), Snapshot.create(shard1SnapshotState, Collections.<ReplicatedLogEntry>emptyList(), 2, 1, 2, 1, 1, "member-1", null)), ActorRef.noSender());
    ByteState shard2SnapshotState = ByteState.of(new byte[] { 4, 5, 6 });
    replyActor.tell(new GetSnapshotReply(ShardIdentifier.create("shard2", MEMBER_1, "config").toString(), Snapshot.create(shard2SnapshotState, Collections.<ReplicatedLogEntry>emptyList(), 2, 1, 2, 1, 1, "member-1", null)), ActorRef.noSender());
    kit.expectNoMsg(FiniteDuration.create(500, TimeUnit.MILLISECONDS));
    ByteState shard3SnapshotState = ByteState.of(new byte[] { 7, 8, 9 });
    replyActor.tell(new GetSnapshotReply(ShardIdentifier.create("shard3", MEMBER_1, "config").toString(), Snapshot.create(shard3SnapshotState, Collections.<ReplicatedLogEntry>emptyList(), 2, 1, 2, 1, 1, "member-1", null)), ActorRef.noSender());
    DatastoreSnapshot datastoreSnapshot = kit.expectMsgClass(DatastoreSnapshot.class);
    assertEquals("getType", "config", datastoreSnapshot.getType());
    assertEquals("getShardManagerSnapshot", shardManagerSnapshot.getShardList(), datastoreSnapshot.getShardManagerSnapshot().getShardList());
    List<ShardSnapshot> shardSnapshots = datastoreSnapshot.getShardSnapshots();
    assertEquals("ShardSnapshot size", 3, shardSnapshots.size());
    assertEquals("ShardSnapshot 1 getName", "shard1", shardSnapshots.get(0).getName());
    assertEquals("ShardSnapshot 1 getSnapshot", shard1SnapshotState, shardSnapshots.get(0).getSnapshot().getState());
    assertEquals("ShardSnapshot 2 getName", "shard2", shardSnapshots.get(1).getName());
    assertEquals("ShardSnapshot 2 getSnapshot", shard2SnapshotState, shardSnapshots.get(1).getSnapshot().getState());
    assertEquals("ShardSnapshot 3 getName", "shard3", shardSnapshots.get(2).getName());
    assertEquals("ShardSnapshot 3 getSnapshot", shard3SnapshotState, shardSnapshots.get(2).getSnapshot().getState());
    kit.expectMsgClass(Terminated.class);
}
Also used : ShardSnapshot(org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot.ShardSnapshot) ShardManagerSnapshot(org.opendaylight.controller.cluster.datastore.persisted.ShardManagerSnapshot) ActorRef(akka.actor.ActorRef) TestKit(akka.testkit.javadsl.TestKit) DatastoreSnapshot(org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot) ByteState(org.opendaylight.controller.cluster.raft.persisted.ByteState) GetSnapshotReply(org.opendaylight.controller.cluster.raft.client.messages.GetSnapshotReply) AbstractActorTest(org.opendaylight.controller.cluster.datastore.AbstractActorTest) Test(org.junit.Test)

Aggregations

ShardManagerSnapshot (org.opendaylight.controller.cluster.datastore.persisted.ShardManagerSnapshot)6 TestKit (akka.testkit.javadsl.TestKit)5 Test (org.junit.Test)5 AddressFromURIString (akka.actor.AddressFromURIString)4 AbstractShardManagerTest (org.opendaylight.controller.cluster.datastore.AbstractShardManagerTest)4 MockConfiguration (org.opendaylight.controller.cluster.datastore.utils.MockConfiguration)4 UpdateSchemaContext (org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext)3 DatastoreSnapshot (org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot)3 ActorRef (akka.actor.ActorRef)2 Status (akka.actor.Status)2 AddShardReplica (org.opendaylight.controller.cluster.datastore.messages.AddShardReplica)2 ChangeShardMembersVotingStatus (org.opendaylight.controller.cluster.datastore.messages.ChangeShardMembersVotingStatus)2 ShardSnapshot (org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot.ShardSnapshot)2 FollowerInitialSyncUpStatus (org.opendaylight.controller.cluster.raft.base.messages.FollowerInitialSyncUpStatus)2 AddServerReply (org.opendaylight.controller.cluster.raft.messages.AddServerReply)2 ChangeServersVotingStatus (org.opendaylight.controller.cluster.raft.messages.ChangeServersVotingStatus)2 ServerChangeStatus (org.opendaylight.controller.cluster.raft.messages.ServerChangeStatus)2 ActorSystem (akka.actor.ActorSystem)1 Failure (akka.actor.Status.Failure)1 TestActorRef (akka.testkit.TestActorRef)1