Bump odlparent to 5.0.0
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / databroker / actors / dds / AbstractShardBackendResolver.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.databroker.actors.dds;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
13 import akka.actor.ActorRef;
14 import akka.util.Timeout;
15 import com.google.common.primitives.UnsignedLong;
16 import java.util.Set;
17 import java.util.concurrent.CompletableFuture;
18 import java.util.concurrent.CompletionStage;
19 import java.util.concurrent.ConcurrentHashMap;
20 import java.util.concurrent.TimeUnit;
21 import java.util.concurrent.TimeoutException;
22 import java.util.concurrent.atomic.AtomicLong;
23 import java.util.function.Consumer;
24 import org.checkerframework.checker.lock.qual.GuardedBy;
25 import org.eclipse.jdt.annotation.NonNull;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.opendaylight.controller.cluster.access.ABIVersion;
28 import org.opendaylight.controller.cluster.access.client.BackendInfoResolver;
29 import org.opendaylight.controller.cluster.access.commands.ConnectClientRequest;
30 import org.opendaylight.controller.cluster.access.commands.ConnectClientSuccess;
31 import org.opendaylight.controller.cluster.access.commands.NotLeaderException;
32 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
33 import org.opendaylight.controller.cluster.access.concepts.RequestFailure;
34 import org.opendaylight.controller.cluster.common.actor.ExplicitAsk;
35 import org.opendaylight.controller.cluster.datastore.exceptions.NoShardLeaderException;
36 import org.opendaylight.controller.cluster.datastore.exceptions.NotInitializedException;
37 import org.opendaylight.controller.cluster.datastore.exceptions.PrimaryNotFoundException;
38 import org.opendaylight.controller.cluster.datastore.messages.PrimaryShardInfo;
39 import org.opendaylight.controller.cluster.datastore.utils.ActorUtils;
40 import org.opendaylight.yangtools.concepts.Registration;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import scala.Function1;
44 import scala.compat.java8.FutureConverters;
45
46 /**
47  * {@link BackendInfoResolver} implementation for static shard configuration based on ShardManager. Each string-named
48  * shard is assigned a single cookie and this mapping is stored in a bidirectional map. Information about corresponding
49  * shard leader is resolved via {@link ActorUtils}. The product of resolution is {@link ShardBackendInfo}.
50  *
51  * <p>
52  * This class is thread-safe.
53  *
54  * @author Robert Varga
55  */
56 abstract class AbstractShardBackendResolver extends BackendInfoResolver<ShardBackendInfo> {
57     static final class ShardState {
58         private final CompletionStage<ShardBackendInfo> stage;
59         @GuardedBy("this")
60         private ShardBackendInfo result;
61
62         ShardState(final CompletionStage<ShardBackendInfo> stage) {
63             this.stage = requireNonNull(stage);
64             stage.whenComplete(this::onStageResolved);
65         }
66
67         @NonNull CompletionStage<ShardBackendInfo> getStage() {
68             return stage;
69         }
70
71         synchronized @Nullable ShardBackendInfo getResult() {
72             return result;
73         }
74
75         private synchronized void onStageResolved(final ShardBackendInfo info, final Throwable failure) {
76             if (failure == null) {
77                 this.result = requireNonNull(info);
78             } else {
79                 LOG.warn("Failed to resolve shard", failure);
80             }
81         }
82     }
83
84     private static final Logger LOG = LoggerFactory.getLogger(AbstractShardBackendResolver.class);
85
86     /**
87      * Connect request timeout. If the shard does not respond within this interval, we retry the lookup and connection.
88      */
89     // TODO: maybe make this configurable somehow?
90     private static final Timeout CONNECT_TIMEOUT = Timeout.apply(5, TimeUnit.SECONDS);
91
92     private final AtomicLong nextSessionId = new AtomicLong();
93     private final Function1<ActorRef, ?> connectFunction;
94     private final ActorUtils actorUtils;
95     private final Set<Consumer<Long>> staleBackendInfoCallbacks = ConcurrentHashMap.newKeySet();
96
97     // FIXME: we really need just ActorContext.findPrimaryShardAsync()
98     AbstractShardBackendResolver(final ClientIdentifier clientId, final ActorUtils actorUtils) {
99         this.actorUtils = requireNonNull(actorUtils);
100         this.connectFunction = ExplicitAsk.toScala(t -> new ConnectClientRequest(clientId, t, ABIVersion.BORON,
101             ABIVersion.current()));
102     }
103
104     @Override
105     public Registration notifyWhenBackendInfoIsStale(final Consumer<Long> callback) {
106         staleBackendInfoCallbacks.add(callback);
107         return () -> staleBackendInfoCallbacks.remove(callback);
108     }
109
110     protected void notifyStaleBackendInfoCallbacks(Long cookie) {
111         staleBackendInfoCallbacks.forEach(callback -> callback.accept(cookie));
112     }
113
114     protected ActorUtils actorUtils() {
115         return actorUtils;
116     }
117
118     protected final void flushCache(final String shardName) {
119         actorUtils.getPrimaryShardInfoCache().remove(shardName);
120     }
121
122     protected final ShardState resolveBackendInfo(final String shardName, final long cookie) {
123         LOG.debug("Resolving cookie {} to shard {}", cookie, shardName);
124
125         final CompletableFuture<ShardBackendInfo> future = new CompletableFuture<>();
126         FutureConverters.toJava(actorUtils.findPrimaryShardAsync(shardName)).whenComplete((info, failure) -> {
127             if (failure == null) {
128                 connectShard(shardName, cookie, info, future);
129                 return;
130             }
131
132             LOG.debug("Shard {} failed to resolve", shardName, failure);
133             if (failure instanceof NoShardLeaderException) {
134                 future.completeExceptionally(wrap("Shard has no current leader", failure));
135             } else if (failure instanceof NotInitializedException) {
136                 // FIXME: this actually is an exception we can retry on
137                 LOG.info("Shard {} has not initialized yet", shardName);
138                 future.completeExceptionally(failure);
139             } else if (failure instanceof PrimaryNotFoundException) {
140                 LOG.info("Failed to find primary for shard {}", shardName);
141                 future.completeExceptionally(failure);
142             } else {
143                 future.completeExceptionally(failure);
144             }
145         });
146
147         return new ShardState(future);
148     }
149
150     private static TimeoutException wrap(final String message, final Throwable cause) {
151         final TimeoutException ret = new TimeoutException(message);
152         ret.initCause(requireNonNull(cause));
153         return ret;
154     }
155
156     private void connectShard(final String shardName, final long cookie, final PrimaryShardInfo info,
157             final CompletableFuture<ShardBackendInfo> future) {
158         LOG.debug("Shard {} resolved to {}, attempting to connect", shardName, info);
159
160         FutureConverters.toJava(ExplicitAsk.ask(info.getPrimaryShardActor(), connectFunction, CONNECT_TIMEOUT))
161             .whenComplete((response, failure) -> onConnectResponse(shardName, cookie, future, response, failure));
162     }
163
164     private void onConnectResponse(final String shardName, final long cookie,
165             final CompletableFuture<ShardBackendInfo> future, final Object response, final Throwable failure) {
166         if (failure != null) {
167             LOG.debug("Connect attempt to {} failed, will retry", shardName, failure);
168             future.completeExceptionally(wrap("Connection attempt failed", failure));
169             return;
170         }
171         if (response instanceof RequestFailure) {
172             final Throwable cause = ((RequestFailure<?, ?>) response).getCause().unwrap();
173             LOG.debug("Connect attempt to {} failed to process", shardName, cause);
174             final Throwable result = cause instanceof NotLeaderException
175                     ? wrap("Leader moved during establishment", cause) : cause;
176             future.completeExceptionally(result);
177             return;
178         }
179
180         LOG.debug("Resolved backend information to {}", response);
181         checkArgument(response instanceof ConnectClientSuccess, "Unhandled response %s", response);
182         final ConnectClientSuccess success = (ConnectClientSuccess) response;
183         future.complete(new ShardBackendInfo(success.getBackend(), nextSessionId.getAndIncrement(),
184             success.getVersion(), shardName, UnsignedLong.fromLongBits(cookie), success.getDataTree(),
185             success.getMaxMessages()));
186     }
187 }