Migrate to UntypedAbstractActor
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / shardmanager / ShardManagerGetSnapshotReplyActor.java
1 /*
2  * Copyright (c) 2015 Brocade Communications 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.shardmanager;
9
10 import akka.actor.ActorRef;
11 import akka.actor.PoisonPill;
12 import akka.actor.Props;
13 import akka.actor.ReceiveTimeout;
14 import akka.actor.Status.Failure;
15 import akka.actor.UntypedAbstractActor;
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.HashSet;
19 import java.util.List;
20 import java.util.Set;
21 import java.util.concurrent.TimeoutException;
22 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
23 import org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot;
24 import org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot.ShardSnapshot;
25 import org.opendaylight.controller.cluster.datastore.persisted.ShardManagerSnapshot;
26 import org.opendaylight.controller.cluster.raft.client.messages.GetSnapshotReply;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import scala.concurrent.duration.Duration;
30
31 /**
32  * Temporary actor used by the ShardManager to compile GetSnapshot replies from the Shard actors and return
33  * a DatastoreSnapshot instance reply.
34  *
35  * @author Thomas Pantelis
36  */
37 final class ShardManagerGetSnapshotReplyActor extends UntypedAbstractActor {
38     private static final Logger LOG = LoggerFactory.getLogger(ShardManagerGetSnapshotReplyActor.class);
39
40     private final Set<String> remainingShardNames;
41     private final Params params;
42     private final List<ShardSnapshot> shardSnapshots = new ArrayList<>();
43
44     private ShardManagerGetSnapshotReplyActor(final Params params) {
45         this.params = params;
46         remainingShardNames = new HashSet<>(params.shardNames);
47
48         LOG.debug("{}: Expecting {} shard snapshot replies", params.id, params.shardNames.size());
49
50         getContext().setReceiveTimeout(params.receiveTimeout);
51     }
52
53     @Override
54     public void onReceive(final Object message) {
55         if (message instanceof GetSnapshotReply) {
56             onGetSnapshotReply((GetSnapshotReply)message);
57         } else if (message instanceof Failure) {
58             LOG.debug("{}: Received {}", params.id, message);
59
60             params.replyToActor.tell(message, getSelf());
61             getSelf().tell(PoisonPill.getInstance(), getSelf());
62         } else if (message instanceof ReceiveTimeout) {
63             String msg = String.format(
64                     "Timed out after %s ms while waiting for snapshot replies from %d shard(s). %d shard(s) %s "
65                     + "did not respond.", params.receiveTimeout.toMillis(), params.shardNames.size(),
66                     remainingShardNames.size(), remainingShardNames);
67             LOG.warn("{}: {}", params.id, msg);
68             params.replyToActor.tell(new Failure(new TimeoutException(msg)), getSelf());
69             getSelf().tell(PoisonPill.getInstance(), getSelf());
70         }
71     }
72
73     private void onGetSnapshotReply(final GetSnapshotReply getSnapshotReply) {
74         LOG.debug("{}: Received {}", params.id, getSnapshotReply);
75
76         ShardIdentifier shardId = ShardIdentifier.fromShardIdString(getSnapshotReply.getId());
77         shardSnapshots.add(new ShardSnapshot(shardId.getShardName(), getSnapshotReply.getSnapshot()));
78
79         remainingShardNames.remove(shardId.getShardName());
80         if (remainingShardNames.isEmpty()) {
81             LOG.debug("{}: All shard snapshots received", params.id);
82
83             DatastoreSnapshot datastoreSnapshot = new DatastoreSnapshot(params.datastoreType,
84                     params.shardManagerSnapshot, shardSnapshots);
85             params.replyToActor.tell(datastoreSnapshot, getSelf());
86             getSelf().tell(PoisonPill.getInstance(), getSelf());
87         }
88     }
89
90     public static Props props(final Collection<String> shardNames, final String datastoreType,
91             final ShardManagerSnapshot shardManagerSnapshot, final ActorRef replyToActor, final String id,
92             final Duration receiveTimeout) {
93         return Props.create(ShardManagerGetSnapshotReplyActor.class, new Params(shardNames, datastoreType,
94                 shardManagerSnapshot, replyToActor, id, receiveTimeout));
95     }
96
97     private static final class Params {
98         final Collection<String> shardNames;
99         final String datastoreType;
100         final ShardManagerSnapshot shardManagerSnapshot;
101         final ActorRef replyToActor;
102         final String id;
103         final Duration receiveTimeout;
104
105         Params(final Collection<String> shardNames, final String datastoreType,
106                 final ShardManagerSnapshot shardManagerSnapshot, final ActorRef replyToActor, final String id,
107                 final Duration receiveTimeout) {
108             this.shardNames = shardNames;
109             this.datastoreType = datastoreType;
110             this.shardManagerSnapshot = shardManagerSnapshot;
111             this.replyToActor = replyToActor;
112             this.id = id;
113             this.receiveTimeout = receiveTimeout;
114         }
115     }
116 }

©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.