2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.databroker.actors.dds;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
13 import akka.actor.ActorRef;
14 import akka.util.Timeout;
15 import com.google.common.primitives.UnsignedLong;
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;
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}.
52 * This class is thread-safe.
54 * @author Robert Varga
56 abstract class AbstractShardBackendResolver extends BackendInfoResolver<ShardBackendInfo> {
57 static final class ShardState {
58 private final CompletionStage<ShardBackendInfo> stage;
60 private ShardBackendInfo result;
62 ShardState(final CompletionStage<ShardBackendInfo> stage) {
63 this.stage = requireNonNull(stage);
64 stage.whenComplete(this::onStageResolved);
67 @NonNull CompletionStage<ShardBackendInfo> getStage() {
71 synchronized @Nullable ShardBackendInfo getResult() {
75 private synchronized void onStageResolved(final ShardBackendInfo info, final Throwable failure) {
76 if (failure == null) {
77 this.result = requireNonNull(info);
79 LOG.warn("Failed to resolve shard", failure);
84 private static final Logger LOG = LoggerFactory.getLogger(AbstractShardBackendResolver.class);
87 * Connect request timeout. If the shard does not respond within this interval, we retry the lookup and connection.
89 // TODO: maybe make this configurable somehow?
90 private static final Timeout CONNECT_TIMEOUT = Timeout.apply(5, TimeUnit.SECONDS);
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();
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()));
105 public Registration notifyWhenBackendInfoIsStale(final Consumer<Long> callback) {
106 staleBackendInfoCallbacks.add(callback);
107 return () -> staleBackendInfoCallbacks.remove(callback);
110 protected void notifyStaleBackendInfoCallbacks(Long cookie) {
111 staleBackendInfoCallbacks.forEach(callback -> callback.accept(cookie));
114 protected ActorUtils actorUtils() {
118 protected final void flushCache(final String shardName) {
119 actorUtils.getPrimaryShardInfoCache().remove(shardName);
122 protected final ShardState resolveBackendInfo(final String shardName, final long cookie) {
123 LOG.debug("Resolving cookie {} to shard {}", cookie, shardName);
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);
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);
143 future.completeExceptionally(failure);
147 return new ShardState(future);
150 private static TimeoutException wrap(final String message, final Throwable cause) {
151 final TimeoutException ret = new TimeoutException(message);
152 ret.initCause(requireNonNull(cause));
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);
160 FutureConverters.toJava(ExplicitAsk.ask(info.getPrimaryShardActor(), connectFunction, CONNECT_TIMEOUT))
161 .whenComplete((response, failure) -> onConnectResponse(shardName, cookie, future, response, failure));
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));
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);
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()));