2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.cluster.datastore.actors;
10 import static java.util.Objects.requireNonNull;
12 import akka.actor.ActorRef;
13 import akka.actor.Props;
14 import java.io.IOException;
15 import java.io.ObjectOutputStream;
16 import java.io.OutputStream;
17 import java.util.Optional;
18 import org.opendaylight.controller.cluster.common.actor.AbstractUntypedActorWithMetering;
19 import org.opendaylight.controller.cluster.datastore.persisted.ShardDataTreeSnapshot;
20 import org.opendaylight.controller.cluster.datastore.persisted.ShardSnapshotState;
21 import org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshotReply;
24 * This is an offload actor, which is given an isolated snapshot of the data tree. It performs the potentially
25 * time-consuming operation of serializing the snapshot.
27 * @author Robert Varga
29 public final class ShardSnapshotActor extends AbstractUntypedActorWithMetering {
31 private static final class SerializeSnapshot {
32 private final ShardDataTreeSnapshot snapshot;
33 private final Optional<OutputStream> installSnapshotStream;
34 private final ActorRef replyTo;
36 SerializeSnapshot(final ShardDataTreeSnapshot snapshot, final Optional<OutputStream> installSnapshotStream,
37 final ActorRef replyTo) {
38 this.snapshot = requireNonNull(snapshot);
39 this.installSnapshotStream = requireNonNull(installSnapshotStream);
40 this.replyTo = requireNonNull(replyTo);
43 ShardDataTreeSnapshot getSnapshot() {
47 Optional<OutputStream> getInstallSnapshotStream() {
48 return installSnapshotStream;
51 ActorRef getReplyTo() {
56 //actor name override used for metering. This does not change the "real" actor name
57 private static final String ACTOR_NAME_FOR_METERING = "shard-snapshot";
59 private ShardSnapshotActor() {
60 super(ACTOR_NAME_FOR_METERING);
64 protected void handleReceive(final Object message) {
65 if (message instanceof SerializeSnapshot) {
66 onSerializeSnapshot((SerializeSnapshot) message);
68 unknownMessage(message);
72 private void onSerializeSnapshot(final SerializeSnapshot request) {
73 Optional<OutputStream> installSnapshotStream = request.getInstallSnapshotStream();
74 if (installSnapshotStream.isPresent()) {
75 try (ObjectOutputStream out = new ObjectOutputStream(installSnapshotStream.get())) {
76 request.getSnapshot().serialize(out);
77 } catch (IOException e) {
78 // TODO - we should communicate the failure in the CaptureSnapshotReply.
79 LOG.error("Error serializing snapshot", e);
83 request.getReplyTo().tell(new CaptureSnapshotReply(new ShardSnapshotState(request.getSnapshot()),
84 installSnapshotStream), ActorRef.noSender());
88 * Sends a request to a ShardSnapshotActor to process a snapshot and send a CaptureSnapshotReply.
90 * @param snapshotActor the ShardSnapshotActor
91 * @param snapshot the snapshot to process
92 * @param installSnapshotStream Optional OutputStream that is present if the snapshot is to also be installed
94 * @param replyTo the actor to which to send the CaptureSnapshotReply
96 public static void requestSnapshot(final ActorRef snapshotActor, final ShardDataTreeSnapshot snapshot,
97 final Optional<OutputStream> installSnapshotStream, final ActorRef replyTo) {
98 snapshotActor.tell(new SerializeSnapshot(snapshot, installSnapshotStream, replyTo), ActorRef.noSender());
101 public static Props props() {
102 return Props.create(ShardSnapshotActor.class);