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.OutputStream;
15 import java.util.Optional;
16 import org.opendaylight.controller.cluster.common.actor.AbstractUntypedActorWithMetering;
17 import org.opendaylight.controller.cluster.datastore.persisted.ShardDataTreeSnapshot;
18 import org.opendaylight.controller.cluster.datastore.persisted.ShardSnapshotState;
19 import org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshotReply;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
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 {
30 private static final Logger LOG = LoggerFactory.getLogger(ShardSnapshotActor.class);
33 private static final class SerializeSnapshot {
34 private final ShardDataTreeSnapshot snapshot;
35 private final Optional<OutputStream> installSnapshotStream;
36 private final ActorRef replyTo;
38 SerializeSnapshot(final ShardDataTreeSnapshot snapshot, final Optional<OutputStream> installSnapshotStream,
39 final ActorRef replyTo) {
40 this.snapshot = Preconditions.checkNotNull(snapshot);
41 this.installSnapshotStream = Preconditions.checkNotNull(installSnapshotStream);
42 this.replyTo = Preconditions.checkNotNull(replyTo);
45 ShardDataTreeSnapshot getSnapshot() {
49 Optional<OutputStream> getInstallSnapshotStream() {
50 return installSnapshotStream;
53 ActorRef getReplyTo() {
58 //actor name override used for metering. This does not change the "real" actor name
59 private static final String ACTOR_NAME_FOR_METERING = "shard-snapshot";
61 private ShardSnapshotActor() {
62 super(ACTOR_NAME_FOR_METERING);
66 protected void handleReceive(final Object message) throws Exception {
67 if (message instanceof SerializeSnapshot) {
68 onSerializeSnapshot((SerializeSnapshot) message);
70 unknownMessage(message);
74 private void onSerializeSnapshot(SerializeSnapshot request) {
75 Optional<OutputStream> installSnapshotStream = request.getInstallSnapshotStream();
76 if (installSnapshotStream.isPresent()) {
78 request.getSnapshot().serialize(installSnapshotStream.get());
79 } catch (IOException e) {
80 // TODO - we should communicate the failure in the CaptureSnapshotReply.
81 LOG.error("Error serializing snapshot", e);
85 request.getReplyTo().tell(new CaptureSnapshotReply(new ShardSnapshotState(request.getSnapshot()),
86 installSnapshotStream), ActorRef.noSender());
90 * Sends a request to a ShardSnapshotActor to process a snapshot and send a CaptureSnapshotReply.
92 * @param snapshotActor the ShardSnapshotActor
93 * @param snapshot the snapshot to process
94 * @param installSnapshotStream Optional OutputStream that is present if the snapshot is to also be installed
96 * @param replyTo the actor to which to send the CaptureSnapshotReply
98 public static void requestSnapshot(final ActorRef snapshotActor, final ShardDataTreeSnapshot snapshot,
99 final Optional<OutputStream> installSnapshotStream, final ActorRef replyTo) {
100 snapshotActor.tell(new SerializeSnapshot(snapshot, installSnapshotStream, replyTo), ActorRef.noSender());
103 public static Props props() {
104 return Props.create(ShardSnapshotActor.class);