Bump versions to 4.0.0-SNAPSHOT
[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 java.util.Collection;
22 import java.util.Optional;
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.ActorUtils;
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 @Deprecated(forRemoval = true)
55 final class CDSShardAccessImpl implements CDSShardAccess, LeaderLocationListener, AutoCloseable {
56     private static final Logger LOG = LoggerFactory.getLogger(CDSShardAccessImpl.class);
57
58     private final Collection<LeaderLocationListener> listeners = ConcurrentHashMap.newKeySet();
59     private final DOMDataTreeIdentifier prefix;
60     private final ActorUtils actorUtils;
61     private final Timeout makeLeaderLocalTimeout;
62
63     private ActorRef roleChangeListenerActor;
64
65     private volatile LeaderLocation currentLeader = LeaderLocation.UNKNOWN;
66     private volatile boolean closed = false;
67
68     CDSShardAccessImpl(final DOMDataTreeIdentifier prefix, final ActorUtils actorUtils) {
69         this.prefix = requireNonNull(prefix);
70         this.actorUtils = requireNonNull(actorUtils);
71         this.makeLeaderLocalTimeout =
72                 new Timeout(actorUtils.getDatastoreContext().getShardLeaderElectionTimeout().duration().$times(2));
73
74         // register RoleChangeListenerActor
75         // TODO Maybe we should do this in async
76         final Optional<ActorRef> localShardReply =
77                 actorUtils.findLocalShard(ClusterUtils.getCleanShardName(prefix.getRootIdentifier()));
78         checkState(localShardReply.isPresent(),
79                 "Local shard for {} not present. Cannot register RoleChangeListenerActor", prefix);
80         roleChangeListenerActor =
81                 actorUtils.getActorSystem().actorOf(RoleChangeListenerActor.props(localShardReply.get(), this));
82     }
83
84     private void checkNotClosed() {
85         checkState(!closed, "CDSDataTreeProducer, that this CDSShardAccess is associated with, is no longer valid");
86     }
87
88     @Override
89     public DOMDataTreeIdentifier getShardIdentifier() {
90         checkNotClosed();
91         return prefix;
92     }
93
94     @Override
95     public LeaderLocation getLeaderLocation() {
96         checkNotClosed();
97         // TODO before getting first notification from roleChangeListenerActor
98         // we will always return UNKNOWN
99         return currentLeader;
100     }
101
102     @Override
103     public CompletionStage<Void> makeLeaderLocal() {
104         // TODO when we have running make leader local operation
105         // we should just return the same completion stage
106         checkNotClosed();
107
108         // TODO can we cache local shard actorRef?
109         final Future<ActorRef> localShardReply =
110                 actorUtils.findLocalShardAsync(ClusterUtils.getCleanShardName(prefix.getRootIdentifier()));
111
112         // we have to tell local shard to make leader local
113         final scala.concurrent.Promise<Object> makeLeaderLocalAsk = Futures.promise();
114         localShardReply.onComplete(new OnComplete<ActorRef>() {
115             @Override
116             public void onComplete(final Throwable failure, final ActorRef actorRef) {
117                 if (failure instanceof LocalShardNotFoundException) {
118                     LOG.debug("No local shard found for {} - Cannot request leadership transfer to local shard.",
119                             getShardIdentifier(), failure);
120                     makeLeaderLocalAsk.failure(failure);
121                 } else if (failure != null) {
122                     // TODO should this be WARN?
123                     LOG.debug("Failed to find local shard for {} - Cannot request leadership transfer to local shard.",
124                             getShardIdentifier(), failure);
125                     makeLeaderLocalAsk.failure(failure);
126                 } else {
127                     makeLeaderLocalAsk
128                             .completeWith(actorUtils
129                                     .executeOperationAsync(actorRef, MakeLeaderLocal.INSTANCE, makeLeaderLocalTimeout));
130                 }
131             }
132         }, actorUtils.getClientDispatcher());
133
134         // we have to transform make leader local request result
135         Future<Void> makeLeaderLocalFuture = makeLeaderLocalAsk.future()
136                 .transform(new Mapper<Object, Void>() {
137                     @Override
138                     public Void apply(final Object parameter) {
139                         return null;
140                     }
141                 }, new Mapper<Throwable, Throwable>() {
142                     @Override
143                     public Throwable apply(final Throwable parameter) {
144                         if (parameter instanceof LeadershipTransferFailedException) {
145                             // do nothing with exception and just pass it as it is
146                             return parameter;
147                         }
148                         // wrap exception in LeadershipTransferFailedEx
149                         return new LeadershipTransferFailedException("Leadership transfer failed", parameter);
150                     }
151                 }, actorUtils.getClientDispatcher());
152
153         return FutureConverters.toJava(makeLeaderLocalFuture);
154     }
155
156     @Override
157     public <L extends LeaderLocationListener> LeaderLocationListenerRegistration<L>
158             registerLeaderLocationListener(final L listener) {
159         checkNotClosed();
160         requireNonNull(listener);
161         checkArgument(!listeners.contains(listener), "Listener %s is already registered with ShardAccess %s", listener,
162             this);
163
164         LOG.debug("Registering LeaderLocationListener {}", listener);
165
166         listeners.add(listener);
167
168         return new LeaderLocationListenerRegistration<>() {
169             @Override
170             public L getInstance() {
171                 return listener;
172             }
173
174             @Override
175             public void close() {
176                 listeners.remove(listener);
177             }
178         };
179     }
180
181     @Override
182     @SuppressWarnings("checkstyle:IllegalCatch")
183     public void onLeaderLocationChanged(final LeaderLocation location) {
184         if (closed) {
185             // we are closed already. Do not dispatch any new leader location
186             // change events.
187             return;
188         }
189
190         LOG.debug("Received leader location change notification. New leader location: {}", location);
191         currentLeader = location;
192         listeners.forEach(listener -> {
193             try {
194                 listener.onLeaderLocationChanged(location);
195             } catch (Exception e) {
196                 LOG.warn("Ignoring uncaught exception thrown be LeaderLocationListener {} "
197                         + "during processing leader location change {}", listener, location, e);
198             }
199         });
200     }
201
202     @Override
203     public void close() {
204         // TODO should we also remove all listeners?
205         LOG.debug("Closing {} ShardAccess", prefix);
206         closed = true;
207
208         if (roleChangeListenerActor != null) {
209             // stop RoleChangeListenerActor
210             roleChangeListenerActor.tell(PoisonPill.getInstance(), noSender());
211             roleChangeListenerActor = null;
212         }
213     }
214 }