Reduce JSR305 proliferation
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / sharding / CDSShardAccessImpl.java
1 /*
2  * Copyright (c) 2017 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.sharding;
9
10 import static akka.actor.ActorRef.noSender;
11 import static com.google.common.base.Preconditions.checkArgument;
12 import static com.google.common.base.Preconditions.checkState;
13 import static java.util.Objects.requireNonNull;
14
15 import akka.actor.ActorRef;
16 import akka.actor.PoisonPill;
17 import akka.dispatch.Futures;
18 import akka.dispatch.Mapper;
19 import akka.dispatch.OnComplete;
20 import akka.util.Timeout;
21 import com.google.common.base.Optional;
22 import java.util.Collection;
23 import java.util.concurrent.CompletionStage;
24 import java.util.concurrent.ConcurrentHashMap;
25 import org.opendaylight.controller.cluster.datastore.exceptions.LocalShardNotFoundException;
26 import org.opendaylight.controller.cluster.datastore.messages.MakeLeaderLocal;
27 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
28 import org.opendaylight.controller.cluster.datastore.utils.ClusterUtils;
29 import org.opendaylight.controller.cluster.dom.api.CDSShardAccess;
30 import org.opendaylight.controller.cluster.dom.api.LeaderLocation;
31 import org.opendaylight.controller.cluster.dom.api.LeaderLocationListener;
32 import org.opendaylight.controller.cluster.dom.api.LeaderLocationListenerRegistration;
33 import org.opendaylight.controller.cluster.raft.LeadershipTransferFailedException;
34 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import scala.compat.java8.FutureConverters;
38 import scala.concurrent.Future;
39
40 /**
41  * Default {@link CDSShardAccess} implementation. Listens on leader location
42  * change events and distributes them to registered listeners. Also updates
43  * current information about leader location accordingly.
44  *
45  * <p>
46  * Sends {@link MakeLeaderLocal} message to local shards and translates its result
47  * on behalf users {@link #makeLeaderLocal()} calls.
48  *
49  * <p>
50  * {@link org.opendaylight.controller.cluster.dom.api.CDSDataTreeProducer} that
51  * creates instances of this class has to call {@link #close()} once it is no
52  * longer valid.
53  */
54 final class CDSShardAccessImpl implements CDSShardAccess, LeaderLocationListener, AutoCloseable {
55     private static final Logger LOG = LoggerFactory.getLogger(CDSShardAccessImpl.class);
56
57     private final Collection<LeaderLocationListener> listeners = ConcurrentHashMap.newKeySet();
58     private final DOMDataTreeIdentifier prefix;
59     private final ActorContext actorContext;
60     private final Timeout makeLeaderLocalTimeout;
61
62     private ActorRef roleChangeListenerActor;
63
64     private volatile LeaderLocation currentLeader = LeaderLocation.UNKNOWN;
65     private volatile boolean closed = false;
66
67     CDSShardAccessImpl(final DOMDataTreeIdentifier prefix, final ActorContext actorContext) {
68         this.prefix = requireNonNull(prefix);
69         this.actorContext = requireNonNull(actorContext);
70         this.makeLeaderLocalTimeout =
71                 new Timeout(actorContext.getDatastoreContext().getShardLeaderElectionTimeout().duration().$times(2));
72
73         // register RoleChangeListenerActor
74         // TODO Maybe we should do this in async
75         final Optional<ActorRef> localShardReply =
76                 actorContext.findLocalShard(ClusterUtils.getCleanShardName(prefix.getRootIdentifier()));
77         checkState(localShardReply.isPresent(),
78                 "Local shard for {} not present. Cannot register RoleChangeListenerActor", prefix);
79         roleChangeListenerActor =
80                 actorContext.getActorSystem().actorOf(RoleChangeListenerActor.props(localShardReply.get(), this));
81     }
82
83     private void checkNotClosed() {
84         checkState(!closed, "CDSDataTreeProducer, that this CDSShardAccess is associated with, is no longer valid");
85     }
86
87     @Override
88     public DOMDataTreeIdentifier getShardIdentifier() {
89         checkNotClosed();
90         return prefix;
91     }
92
93     @Override
94     public LeaderLocation getLeaderLocation() {
95         checkNotClosed();
96         // TODO before getting first notification from roleChangeListenerActor
97         // we will always return UNKNOWN
98         return currentLeader;
99     }
100
101     @Override
102     public CompletionStage<Void> makeLeaderLocal() {
103         // TODO when we have running make leader local operation
104         // we should just return the same completion stage
105         checkNotClosed();
106
107         // TODO can we cache local shard actorRef?
108         final Future<ActorRef> localShardReply =
109                 actorContext.findLocalShardAsync(ClusterUtils.getCleanShardName(prefix.getRootIdentifier()));
110
111         // we have to tell local shard to make leader local
112         final scala.concurrent.Promise<Object> makeLeaderLocalAsk = Futures.promise();
113         localShardReply.onComplete(new OnComplete<ActorRef>() {
114             @Override
115             public void onComplete(final Throwable failure, final ActorRef actorRef) {
116                 if (failure instanceof LocalShardNotFoundException) {
117                     LOG.debug("No local shard found for {} - Cannot request leadership transfer to local shard.",
118                             getShardIdentifier(), failure);
119                     makeLeaderLocalAsk.failure(failure);
120                 } else if (failure != null) {
121                     // TODO should this be WARN?
122                     LOG.debug("Failed to find local shard for {} - Cannot request leadership transfer to local shard.",
123                             getShardIdentifier(), failure);
124                     makeLeaderLocalAsk.failure(failure);
125                 } else {
126                     makeLeaderLocalAsk
127                             .completeWith(actorContext
128                                     .executeOperationAsync(actorRef, MakeLeaderLocal.INSTANCE, makeLeaderLocalTimeout));
129                 }
130             }
131         }, actorContext.getClientDispatcher());
132
133         // we have to transform make leader local request result
134         Future<Void> makeLeaderLocalFuture = makeLeaderLocalAsk.future()
135                 .transform(new Mapper<Object, Void>() {
136                     @Override
137                     public Void apply(final Object parameter) {
138                         return null;
139                     }
140                 }, new Mapper<Throwable, Throwable>() {
141                     @Override
142                     public Throwable apply(final Throwable parameter) {
143                         if (parameter instanceof LeadershipTransferFailedException) {
144                             // do nothing with exception and just pass it as it is
145                             return parameter;
146                         }
147                         // wrap exception in LeadershipTransferFailedEx
148                         return new LeadershipTransferFailedException("Leadership transfer failed", parameter);
149                     }
150                 }, actorContext.getClientDispatcher());
151
152         return FutureConverters.toJava(makeLeaderLocalFuture);
153     }
154
155     @Override
156     public <L extends LeaderLocationListener> LeaderLocationListenerRegistration<L>
157             registerLeaderLocationListener(final L listener) {
158         checkNotClosed();
159         requireNonNull(listener);
160         checkArgument(!listeners.contains(listener), "Listener %s is already registered with ShardAccess %s", listener,
161             this);
162
163         LOG.debug("Registering LeaderLocationListener {}", listener);
164
165         listeners.add(listener);
166
167         return new LeaderLocationListenerRegistration<L>() {
168             @Override
169             public L getInstance() {
170                 return listener;
171             }
172
173             @Override
174             public void close() {
175                 listeners.remove(listener);
176             }
177         };
178     }
179
180     @Override
181     @SuppressWarnings("checkstyle:IllegalCatch")
182     public void onLeaderLocationChanged(final LeaderLocation location) {
183         if (closed) {
184             // we are closed already. Do not dispatch any new leader location
185             // change events.
186             return;
187         }
188
189         LOG.debug("Received leader location change notification. New leader location: {}", location);
190         currentLeader = location;
191         listeners.forEach(listener -> {
192             try {
193                 listener.onLeaderLocationChanged(location);
194             } catch (Exception e) {
195                 LOG.warn("Ignoring uncaught exception thrown be LeaderLocationListener {} "
196                         + "during processing leader location change {}", listener, location, e);
197             }
198         });
199     }
200
201     @Override
202     public void close() {
203         // TODO should we also remove all listeners?
204         LOG.debug("Closing {} ShardAccess", prefix);
205         closed = true;
206
207         if (roleChangeListenerActor != null) {
208             // stop RoleChangeListenerActor
209             roleChangeListenerActor.tell(PoisonPill.getInstance(), noSender());
210             roleChangeListenerActor = null;
211         }
212     }
213 }