Bug 7521: Convert DatastoreSnapshot.ShardSnapshot to store Snapshot
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / actors / ShardSnapshotActor.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.datastore.actors;
9
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;
21
22 /**
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.
25  *
26  * @author Robert Varga
27  */
28 public final class ShardSnapshotActor extends AbstractUntypedActorWithMetering {
29     // Internal message
30     private static final class SerializeSnapshot {
31         private final ShardDataTreeSnapshot snapshot;
32         private final Optional<OutputStream> installSnapshotStream;
33         private final ActorRef replyTo;
34
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);
40         }
41
42         ShardDataTreeSnapshot getSnapshot() {
43             return snapshot;
44         }
45
46         Optional<OutputStream> getInstallSnapshotStream() {
47             return installSnapshotStream;
48         }
49
50         ActorRef getReplyTo() {
51             return replyTo;
52         }
53     }
54
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";
57
58     private ShardSnapshotActor() {
59         super(ACTOR_NAME_FOR_METERING);
60     }
61
62     @Override
63     protected void handleReceive(final Object message) throws Exception {
64         if (message instanceof SerializeSnapshot) {
65             onSerializeSnapshot((SerializeSnapshot) message);
66         } else {
67             unknownMessage(message);
68         }
69     }
70
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);
79             }
80         }
81
82         request.getReplyTo().tell(new CaptureSnapshotReply(new ShardSnapshotState(request.getSnapshot()),
83                 installSnapshotStream), ActorRef.noSender());
84     }
85
86     /**
87      * Sends a request to a ShardSnapshotActor to process a snapshot and send a CaptureSnapshotReply.
88      *
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
92      *        on a follower.
93      * @param replyTo the actor to which to send the CaptureSnapshotReply
94      */
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());
98     }
99
100     public static Props props() {
101         return Props.create(ShardSnapshotActor.class);
102     }
103 }