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.io.InputOutputStreamFactory;
22 import org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshotReply;
25 * This is an offload actor, which is given an isolated snapshot of the data tree. It performs the potentially
26 * time-consuming operation of serializing the snapshot.
28 * @author Robert Varga
30 public final class ShardSnapshotActor extends AbstractUntypedActorWithMetering {
32 private static final class SerializeSnapshot {
33 private final ShardDataTreeSnapshot snapshot;
34 private final Optional<OutputStream> installSnapshotStream;
35 private final ActorRef replyTo;
37 SerializeSnapshot(final ShardDataTreeSnapshot snapshot, final Optional<OutputStream> installSnapshotStream,
38 final ActorRef replyTo) {
39 this.snapshot = requireNonNull(snapshot);
40 this.installSnapshotStream = requireNonNull(installSnapshotStream);
41 this.replyTo = requireNonNull(replyTo);
44 ShardDataTreeSnapshot getSnapshot() {
48 Optional<OutputStream> getInstallSnapshotStream() {
49 return installSnapshotStream;
52 ActorRef getReplyTo() {
57 //actor name override used for metering. This does not change the "real" actor name
58 private static final String ACTOR_NAME_FOR_METERING = "shard-snapshot";
60 private final InputOutputStreamFactory streamFactory;
62 private ShardSnapshotActor(final InputOutputStreamFactory streamFactory) {
63 super(ACTOR_NAME_FOR_METERING);
64 this.streamFactory = requireNonNull(streamFactory);
68 protected void handleReceive(final Object message) {
69 if (message instanceof SerializeSnapshot) {
70 onSerializeSnapshot((SerializeSnapshot) message);
72 unknownMessage(message);
76 private void onSerializeSnapshot(final SerializeSnapshot request) {
77 Optional<OutputStream> installSnapshotStream = request.getInstallSnapshotStream();
78 if (installSnapshotStream.isPresent()) {
79 try (ObjectOutputStream out = getOutputStream(installSnapshotStream.get())) {
80 request.getSnapshot().serialize(out);
81 } catch (IOException e) {
82 // TODO - we should communicate the failure in the CaptureSnapshotReply.
83 LOG.error("Error serializing snapshot", e);
87 request.getReplyTo().tell(new CaptureSnapshotReply(new ShardSnapshotState(request.getSnapshot()),
88 installSnapshotStream), ActorRef.noSender());
91 private ObjectOutputStream getOutputStream(final OutputStream outputStream) throws IOException {
92 return new ObjectOutputStream(streamFactory.wrapOutputStream(outputStream));
96 * Sends a request to a ShardSnapshotActor to process a snapshot and send a CaptureSnapshotReply.
98 * @param snapshotActor the ShardSnapshotActor
99 * @param snapshot the snapshot to process
100 * @param installSnapshotStream Optional OutputStream that is present if the snapshot is to also be installed
102 * @param replyTo the actor to which to send the CaptureSnapshotReply
104 public static void requestSnapshot(final ActorRef snapshotActor, final ShardDataTreeSnapshot snapshot,
105 final Optional<OutputStream> installSnapshotStream, final ActorRef replyTo) {
106 snapshotActor.tell(new SerializeSnapshot(snapshot, installSnapshotStream, replyTo), ActorRef.noSender());
109 public static Props props(final InputOutputStreamFactory streamFactory) {
110 return Props.create(ShardSnapshotActor.class, streamFactory);