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 akka.actor.ActorRef;
11 import akka.actor.Props;
12 import com.google.common.base.Preconditions;
13 import java.io.IOException;
14 import java.io.ObjectOutputStream;
15 import java.io.OutputStream;
16 import java.util.Optional;
17 import org.opendaylight.controller.cluster.common.actor.AbstractUntypedActorWithMetering;
18 import org.opendaylight.controller.cluster.datastore.persisted.ShardDataTreeSnapshot;
19 import org.opendaylight.controller.cluster.datastore.persisted.ShardSnapshotState;
20 import org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshotReply;
23 * This is an offload actor, which is given an isolated snapshot of the data tree. It performs the potentially
24 * time-consuming operation of serializing the snapshot.
26 * @author Robert Varga
28 public final class ShardSnapshotActor extends AbstractUntypedActorWithMetering {
30 private static final class SerializeSnapshot {
31 private final ShardDataTreeSnapshot snapshot;
32 private final Optional<OutputStream> installSnapshotStream;
33 private final ActorRef replyTo;
35 SerializeSnapshot(final ShardDataTreeSnapshot snapshot, final Optional<OutputStream> installSnapshotStream,
36 final ActorRef replyTo) {
37 this.snapshot = Preconditions.checkNotNull(snapshot);
38 this.installSnapshotStream = Preconditions.checkNotNull(installSnapshotStream);
39 this.replyTo = Preconditions.checkNotNull(replyTo);
42 ShardDataTreeSnapshot getSnapshot() {
46 Optional<OutputStream> getInstallSnapshotStream() {
47 return installSnapshotStream;
50 ActorRef getReplyTo() {
55 //actor name override used for metering. This does not change the "real" actor name
56 private static final String ACTOR_NAME_FOR_METERING = "shard-snapshot";
58 private ShardSnapshotActor() {
59 super(ACTOR_NAME_FOR_METERING);
63 protected void handleReceive(final Object message) {
64 if (message instanceof SerializeSnapshot) {
65 onSerializeSnapshot((SerializeSnapshot) message);
67 unknownMessage(message);
71 private void onSerializeSnapshot(final SerializeSnapshot request) {
72 Optional<OutputStream> installSnapshotStream = request.getInstallSnapshotStream();
73 if (installSnapshotStream.isPresent()) {
74 try (ObjectOutputStream out = new ObjectOutputStream(installSnapshotStream.get())) {
75 request.getSnapshot().serialize(out);
76 } catch (IOException e) {
77 // TODO - we should communicate the failure in the CaptureSnapshotReply.
78 LOG.error("Error serializing snapshot", e);
82 request.getReplyTo().tell(new CaptureSnapshotReply(new ShardSnapshotState(request.getSnapshot()),
83 installSnapshotStream), ActorRef.noSender());
87 * Sends a request to a ShardSnapshotActor to process a snapshot and send a CaptureSnapshotReply.
89 * @param snapshotActor the ShardSnapshotActor
90 * @param snapshot the snapshot to process
91 * @param installSnapshotStream Optional OutputStream that is present if the snapshot is to also be installed
93 * @param replyTo the actor to which to send the CaptureSnapshotReply
95 public static void requestSnapshot(final ActorRef snapshotActor, final ShardDataTreeSnapshot snapshot,
96 final Optional<OutputStream> installSnapshotStream, final ActorRef replyTo) {
97 snapshotActor.tell(new SerializeSnapshot(snapshot, installSnapshotStream, replyTo), ActorRef.noSender());
100 public static Props props() {
101 return Props.create(ShardSnapshotActor.class);