Improve segmented journal actor metrics
[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.io.InputOutputStreamFactory;
22 import org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshotReply;
23
24 /**
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.
27  *
28  * @author Robert Varga
29  */
30 public final class ShardSnapshotActor extends AbstractUntypedActorWithMetering {
31     // Internal message
32     private static final class SerializeSnapshot {
33         private final ShardDataTreeSnapshot snapshot;
34         private final Optional<OutputStream> installSnapshotStream;
35         private final ActorRef replyTo;
36
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);
42         }
43
44         ShardDataTreeSnapshot getSnapshot() {
45             return snapshot;
46         }
47
48         Optional<OutputStream> getInstallSnapshotStream() {
49             return installSnapshotStream;
50         }
51
52         ActorRef getReplyTo() {
53             return replyTo;
54         }
55     }
56
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";
59
60     private final InputOutputStreamFactory streamFactory;
61
62     private ShardSnapshotActor(final InputOutputStreamFactory streamFactory) {
63         super(ACTOR_NAME_FOR_METERING);
64         this.streamFactory = requireNonNull(streamFactory);
65     }
66
67     @Override
68     protected void handleReceive(final Object message) {
69         if (message instanceof SerializeSnapshot) {
70             onSerializeSnapshot((SerializeSnapshot) message);
71         } else {
72             unknownMessage(message);
73         }
74     }
75
76     private void onSerializeSnapshot(final SerializeSnapshot request) {
77         Optional<OutputStream> installSnapshotStream = request.getInstallSnapshotStream();
78         if (installSnapshotStream.isPresent()) {
79             try (ObjectOutputStream out = getOutputStream(installSnapshotStream.orElseThrow())) {
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);
84             }
85         }
86
87         request.getReplyTo().tell(new CaptureSnapshotReply(new ShardSnapshotState(request.getSnapshot()),
88                 installSnapshotStream), ActorRef.noSender());
89     }
90
91     private ObjectOutputStream getOutputStream(final OutputStream outputStream) throws IOException {
92         return new ObjectOutputStream(streamFactory.wrapOutputStream(outputStream));
93     }
94
95     /**
96      * Sends a request to a ShardSnapshotActor to process a snapshot and send a CaptureSnapshotReply.
97      *
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
101      *        on a follower.
102      * @param replyTo the actor to which to send the CaptureSnapshotReply
103      */
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());
107     }
108
109     public static Props props(final InputOutputStreamFactory streamFactory) {
110         return Props.create(ShardSnapshotActor.class, streamFactory);
111     }
112 }