Search in sources :

Example 1 with ShardDataTreeSnapshot

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

the class ShardSnapshotActorTest method testSerializeSnapshot.

private static void testSerializeSnapshot(final String testName, final ShardDataTreeSnapshot snapshot, final boolean withInstallSnapshot) throws Exception {
    new TestKit(getSystem()) {

        {
            final ActorRef snapshotActor = getSystem().actorOf(ShardSnapshotActor.props(), testName);
            watch(snapshotActor);
            final NormalizedNode<?, ?> expectedRoot = snapshot.getRootNode().get();
            ByteArrayOutputStream installSnapshotStream = withInstallSnapshot ? new ByteArrayOutputStream() : null;
            ShardSnapshotActor.requestSnapshot(snapshotActor, snapshot, Optional.ofNullable(installSnapshotStream), getRef());
            final CaptureSnapshotReply reply = expectMsgClass(duration("3 seconds"), CaptureSnapshotReply.class);
            assertNotNull("getSnapshotState is null", reply.getSnapshotState());
            assertEquals("SnapshotState type", ShardSnapshotState.class, reply.getSnapshotState().getClass());
            assertEquals("Snapshot", snapshot, ((ShardSnapshotState) reply.getSnapshotState()).getSnapshot());
            if (installSnapshotStream != null) {
                final ShardDataTreeSnapshot deserialized;
                try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(installSnapshotStream.toByteArray()))) {
                    deserialized = ShardDataTreeSnapshot.deserialize(in);
                }
                assertEquals("Deserialized snapshot type", snapshot.getClass(), deserialized.getClass());
                final Optional<NormalizedNode<?, ?>> maybeNode = deserialized.getRootNode();
                assertEquals("isPresent", true, maybeNode.isPresent());
                assertEquals("Root node", expectedRoot, maybeNode.get());
            }
        }
    };
}
Also used : CaptureSnapshotReply(org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshotReply) ShardDataTreeSnapshot(org.opendaylight.controller.cluster.datastore.persisted.ShardDataTreeSnapshot) MetadataShardDataTreeSnapshot(org.opendaylight.controller.cluster.datastore.persisted.MetadataShardDataTreeSnapshot) ByteArrayInputStream(java.io.ByteArrayInputStream) ActorRef(akka.actor.ActorRef) TestKit(akka.testkit.javadsl.TestKit) ByteArrayOutputStream(java.io.ByteArrayOutputStream) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) ObjectInputStream(java.io.ObjectInputStream)

Example 2 with ShardDataTreeSnapshot

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

the class ShardRecoveryCoordinator method applyRecoverySnapshot.

/**
 * Applies a recovered snapshot to the data store.
 *
 * @param snapshotState the serialized snapshot
 */
@Override
@SuppressWarnings("checkstyle:IllegalCatch")
public void applyRecoverySnapshot(final Snapshot.State snapshotState) {
    if (!(snapshotState instanceof ShardSnapshotState)) {
        log.debug("{}: applyRecoverySnapshot ignoring snapshot: {}", snapshotState);
    }
    log.debug("{}: Applying recovered snapshot", shardName);
    ShardDataTreeSnapshot shardSnapshot = ((ShardSnapshotState) snapshotState).getSnapshot();
    try {
        store.applyRecoverySnapshot(shardSnapshot);
    } catch (Exception e) {
        final File f = writeRoot("snapshot", shardSnapshot.getRootNode().orElse(null));
        throw new IllegalStateException(String.format("%s: Failed to apply recovery snapshot %s. Node data was written to file %s", shardName, shardSnapshot, f), e);
    }
}
Also used : ShardDataTreeSnapshot(org.opendaylight.controller.cluster.datastore.persisted.ShardDataTreeSnapshot) ShardSnapshotState(org.opendaylight.controller.cluster.datastore.persisted.ShardSnapshotState) File(java.io.File)

Example 3 with ShardDataTreeSnapshot

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

the class ShardSnapshotCohort method createSnapshot.

@Override
public void createSnapshot(final ActorRef actorRef, final Optional<OutputStream> installSnapshotStream) {
    // Forward the request to the snapshot actor
    final ShardDataTreeSnapshot snapshot = store.takeStateSnapshot();
    log.debug("{}: requesting serialization of snapshot {}", logId, snapshot);
    ShardSnapshotActor.requestSnapshot(snapshotActor, snapshot, installSnapshotStream, actorRef);
}
Also used : ShardDataTreeSnapshot(org.opendaylight.controller.cluster.datastore.persisted.ShardDataTreeSnapshot)

Example 4 with ShardDataTreeSnapshot

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

the class ShardSnapshotCohort method applySnapshot.

@Override
@SuppressWarnings("checkstyle:IllegalCatch")
public void applySnapshot(final Snapshot.State snapshotState) {
    if (!(snapshotState instanceof ShardSnapshotState)) {
        log.debug("{}: applySnapshot ignoring snapshot: {}", snapshotState);
    }
    final ShardDataTreeSnapshot snapshot = ((ShardSnapshotState) snapshotState).getSnapshot();
    // Since this will be done only on Recovery or when this actor is a Follower
    // we can safely commit everything in here. We not need to worry about event notifications
    // as they would have already been disabled on the follower
    log.info("{}: Applying snapshot", logId);
    try {
        store.applySnapshot(snapshot);
    } catch (Exception e) {
        log.error("{}: Failed to apply snapshot {}", logId, snapshot, e);
        return;
    }
    log.info("{}: Done applying snapshot", logId);
}
Also used : ShardDataTreeSnapshot(org.opendaylight.controller.cluster.datastore.persisted.ShardDataTreeSnapshot) ShardSnapshotState(org.opendaylight.controller.cluster.datastore.persisted.ShardSnapshotState) IOException(java.io.IOException)

Aggregations

ShardDataTreeSnapshot (org.opendaylight.controller.cluster.datastore.persisted.ShardDataTreeSnapshot)4 ShardSnapshotState (org.opendaylight.controller.cluster.datastore.persisted.ShardSnapshotState)2 ActorRef (akka.actor.ActorRef)1 TestKit (akka.testkit.javadsl.TestKit)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 IOException (java.io.IOException)1 ObjectInputStream (java.io.ObjectInputStream)1 MetadataShardDataTreeSnapshot (org.opendaylight.controller.cluster.datastore.persisted.MetadataShardDataTreeSnapshot)1 CaptureSnapshotReply (org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshotReply)1 NormalizedNode (org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)1