Search in sources :

Example 1 with DatastoreSnapshot

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

the class ShardManagerGetSnapshotReplyActor method onGetSnapshotReply.

private void onGetSnapshotReply(final GetSnapshotReply getSnapshotReply) {
    LOG.debug("{}: Received {}", params.id, getSnapshotReply);
    ShardIdentifier shardId = ShardIdentifier.fromShardIdString(getSnapshotReply.getId());
    shardSnapshots.add(new ShardSnapshot(shardId.getShardName(), getSnapshotReply.getSnapshot()));
    remainingShardNames.remove(shardId.getShardName());
    if (remainingShardNames.isEmpty()) {
        LOG.debug("{}: All shard snapshots received", params.id);
        DatastoreSnapshot datastoreSnapshot = new DatastoreSnapshot(params.datastoreType, params.shardManagerSnapshot, shardSnapshots);
        params.replyToActor.tell(datastoreSnapshot, getSelf());
        getSelf().tell(PoisonPill.getInstance(), getSelf());
    }
}
Also used : ShardSnapshot(org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot.ShardSnapshot) ShardIdentifier(org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier) DatastoreSnapshot(org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot)

Example 2 with DatastoreSnapshot

use of org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot 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 DatastoreSnapshot

use of org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot 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 DatastoreSnapshot

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

the class DatastoreSnapshotRestore method initialize.

// synchronize this method so that, in case of concurrent access to getAndRemove(),
// no one ends up with partially initialized data
@SuppressWarnings("checkstyle:IllegalCatch")
private synchronized void initialize() {
    File restoreDirectoryFile = new File(restoreDirectoryPath);
    String[] files = restoreDirectoryFile.list();
    if (files == null || files.length == 0) {
        LOG.debug("Restore directory {} does not exist or is empty", restoreDirectoryFile);
        return;
    }
    if (files.length > 1) {
        LOG.error("Found {} files in clustered datastore restore directory {} - expected 1. No restore will be attempted", files.length, restoreDirectoryFile);
        return;
    }
    File restoreFile = new File(restoreDirectoryFile, files[0]);
    LOG.info("Clustered datastore will be restored from file {}", restoreFile);
    try (FileInputStream fis = new FileInputStream(restoreFile)) {
        DatastoreSnapshotList snapshots = deserialize(fis);
        LOG.debug("Deserialized {} snapshots", snapshots.size());
        for (DatastoreSnapshot snapshot : snapshots) {
            datastoreSnapshots.put(snapshot.getType(), snapshot);
        }
    } catch (ClassNotFoundException | IOException e) {
        LOG.error("Error reading clustered datastore restore file {}", restoreFile, e);
    } finally {
        if (!restoreFile.delete()) {
            LOG.error("Could not delete clustered datastore restore file {}", restoreFile);
        }
    }
}
Also used : DatastoreSnapshotList(org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshotList) IOException(java.io.IOException) DatastoreSnapshot(org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 5 with DatastoreSnapshot

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

the class DistributedDataStoreFactory method createInstance.

public static AbstractDataStore createInstance(final DOMSchemaService schemaService, final DatastoreContext initialDatastoreContext, final DatastoreSnapshotRestore datastoreSnapshotRestore, final ActorSystemProvider actorSystemProvider, final DatastoreContextIntrospector introspector, final DatastoreContextPropertiesUpdater updater, final Configuration orgConfig) {
    final String datastoreName = initialDatastoreContext.getDataStoreName();
    LOG.info("Create data store instance of type : {}", datastoreName);
    final ActorSystem actorSystem = actorSystemProvider.getActorSystem();
    final DatastoreSnapshot restoreFromSnapshot = datastoreSnapshotRestore.getAndRemove(datastoreName);
    Configuration config;
    if (orgConfig == null) {
        config = new ConfigurationImpl(DEFAULT_MODULE_SHARDS_PATH, DEFAULT_MODULES_PATH);
    } else {
        config = orgConfig;
    }
    final ClusterWrapper clusterWrapper = new ClusterWrapperImpl(actorSystem);
    final DatastoreContextFactory contextFactory = introspector.newContextFactory();
    // This is the potentially-updated datastore context, distinct from the initial one
    final DatastoreContext datastoreContext = contextFactory.getBaseDatastoreContext();
    final AbstractDataStore dataStore;
    if (datastoreContext.isUseTellBasedProtocol()) {
        dataStore = new ClientBackedDataStore(actorSystem, clusterWrapper, config, contextFactory, restoreFromSnapshot);
        LOG.info("Data store {} is using tell-based protocol", datastoreName);
    } else {
        dataStore = new DistributedDataStore(actorSystem, clusterWrapper, config, contextFactory, restoreFromSnapshot);
        LOG.info("Data store {} is using ask-based protocol", datastoreName);
    }
    updater.setListener(dataStore);
    schemaService.registerSchemaContextListener(dataStore);
    dataStore.setCloseable(updater);
    dataStore.waitTillReady();
    return dataStore;
}
Also used : ActorSystem(akka.actor.ActorSystem) ClientBackedDataStore(org.opendaylight.controller.cluster.databroker.ClientBackedDataStore) Configuration(org.opendaylight.controller.cluster.datastore.config.Configuration) DatastoreSnapshot(org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot) ConfigurationImpl(org.opendaylight.controller.cluster.datastore.config.ConfigurationImpl)

Aggregations

DatastoreSnapshot (org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot)9 Test (org.junit.Test)6 AddressFromURIString (akka.actor.AddressFromURIString)3 TestKit (akka.testkit.javadsl.TestKit)3 ShardSnapshot (org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot.ShardSnapshot)3 ShardManagerSnapshot (org.opendaylight.controller.cluster.datastore.persisted.ShardManagerSnapshot)3 ActorRef (akka.actor.ActorRef)2 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 AbstractShardManagerTest (org.opendaylight.controller.cluster.datastore.AbstractShardManagerTest)2 UpdateSchemaContext (org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext)2 DatastoreSnapshotList (org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshotList)2 MockConfiguration (org.opendaylight.controller.cluster.datastore.utils.MockConfiguration)2 ActorSystem (akka.actor.ActorSystem)1 Status (akka.actor.Status)1 Failure (akka.actor.Status.Failure)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)1 ClientBackedDataStore (org.opendaylight.controller.cluster.databroker.ClientBackedDataStore)1