BUG-5280: centralize ShardSnapshot operations
[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 org.opendaylight.controller.cluster.common.actor.AbstractUntypedActorWithMetering;
14 import org.opendaylight.controller.cluster.datastore.persisted.ShardDataTreeSnapshot;
15 import org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshotReply;
16
17 /**
18  * This is an offload actor, which is given an isolated snapshot of the data tree. It performs the potentially
19  * time-consuming operation of serializing the snapshot.
20  *
21  * @author Robert Varga
22  */
23 public final class ShardSnapshotActor extends AbstractUntypedActorWithMetering {
24     // Internal message
25     private static final class SerializeSnapshot {
26         private final ShardDataTreeSnapshot snapshot;
27         private final ActorRef replyTo;
28
29         SerializeSnapshot(final ShardDataTreeSnapshot snapshot, final ActorRef replyTo) {
30             this.snapshot = Preconditions.checkNotNull(snapshot);
31             this.replyTo = Preconditions.checkNotNull(replyTo);
32         }
33
34         ShardDataTreeSnapshot getSnapshot() {
35             return snapshot;
36         }
37
38         ActorRef getReplyTo() {
39             return replyTo;
40         }
41     }
42
43     //actor name override used for metering. This does not change the "real" actor name
44     private static final String ACTOR_NAME_FOR_METERING = "shard-snapshot";
45
46     private ShardSnapshotActor() {
47         super(ACTOR_NAME_FOR_METERING);
48     }
49
50     @Override
51     protected void handleReceive(final Object message) throws Exception {
52         if (message instanceof SerializeSnapshot) {
53             final SerializeSnapshot request = (SerializeSnapshot) message;
54             request.getReplyTo().tell(new CaptureSnapshotReply(request.getSnapshot().serialize()), ActorRef.noSender());
55         } else {
56             unknownMessage(message);
57         }
58     }
59
60     public static void requestSnapshot(final ActorRef snapshotActor, ShardDataTreeSnapshot snapshot,
61             final ActorRef replyTo) {
62         snapshotActor.tell(new SerializeSnapshot(snapshot, replyTo), ActorRef.noSender());
63     }
64
65     public static Props props() {
66         return Props.create(ShardSnapshotActor.class);
67     }
68 }