bdc2ec48d7ba12bbe373f25f8db096c7e8926ef1
[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 static java.util.Objects.requireNonNull;
11
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;
22
23 /**
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.
26  *
27  * @author Robert Varga
28  */
29 public final class ShardSnapshotActor extends AbstractUntypedActorWithMetering {
30     // Internal message
31     private static final class SerializeSnapshot {
32         private final ShardDataTreeSnapshot snapshot;
33         private final Optional<OutputStream> installSnapshotStream;
34         private final ActorRef replyTo;
35
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);
41         }
42
43         ShardDataTreeSnapshot getSnapshot() {
44             return snapshot;
45         }
46
47         Optional<OutputStream> getInstallSnapshotStream() {
48             return installSnapshotStream;
49         }
50
51         ActorRef getReplyTo() {
52             return replyTo;
53         }
54     }
55
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";
58
59     private ShardSnapshotActor() {
60         super(ACTOR_NAME_FOR_METERING);
61     }
62
63     @Override
64     protected void handleReceive(final Object message) {
65         if (message instanceof SerializeSnapshot) {
66             onSerializeSnapshot((SerializeSnapshot) message);
67         } else {
68             unknownMessage(message);
69         }
70     }
71
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);
80             }
81         }
82
83         request.getReplyTo().tell(new CaptureSnapshotReply(new ShardSnapshotState(request.getSnapshot()),
84                 installSnapshotStream), ActorRef.noSender());
85     }
86
87     /**
88      * Sends a request to a ShardSnapshotActor to process a snapshot and send a CaptureSnapshotReply.
89      *
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
93      *        on a follower.
94      * @param replyTo the actor to which to send the CaptureSnapshotReply
95      */
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());
99     }
100
101     public static Props props() {
102         return Props.create(ShardSnapshotActor.class);
103     }
104 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.