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 akka.actor.ActorRef;
11 import akka.util.Timeout;
12 import com.google.common.base.Preconditions;
13 import com.google.common.primitives.UnsignedLong;
14 import java.util.concurrent.CompletableFuture;
15 import java.util.concurrent.CompletionStage;
16 import java.util.concurrent.TimeUnit;
17 import java.util.concurrent.atomic.AtomicLong;
18 import javax.annotation.Nonnull;
19 import javax.annotation.Nullable;
20 import javax.annotation.concurrent.GuardedBy;
21 import javax.annotation.concurrent.ThreadSafe;
22 import org.opendaylight.controller.cluster.access.ABIVersion;
23 import org.opendaylight.controller.cluster.access.client.BackendInfoResolver;
24 import org.opendaylight.controller.cluster.access.commands.ConnectClientRequest;
25 import org.opendaylight.controller.cluster.access.commands.ConnectClientSuccess;
26 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
27 import org.opendaylight.controller.cluster.access.concepts.RequestException;
28 import org.opendaylight.controller.cluster.access.concepts.RequestFailure;
29 import org.opendaylight.controller.cluster.common.actor.ExplicitAsk;
30 import org.opendaylight.controller.cluster.datastore.exceptions.NoShardLeaderException;
31 import org.opendaylight.controller.cluster.datastore.exceptions.NotInitializedException;
32 import org.opendaylight.controller.cluster.datastore.exceptions.PrimaryNotFoundException;
33 import org.opendaylight.controller.cluster.datastore.messages.PrimaryShardInfo;
34 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import scala.Function1;
38 import scala.compat.java8.FutureConverters;
41 * {@link BackendInfoResolver} implementation for static shard configuration based on ShardManager. Each string-named
42 * shard is assigned a single cookie and this mapping is stored in a bidirectional map. Information about corresponding
43 * shard leader is resolved via {@link ActorContext}. The product of resolution is {@link ShardBackendInfo}.
45 * @author Robert Varga
48 abstract class AbstractShardBackendResolver extends BackendInfoResolver<ShardBackendInfo> {
49 static final class ShardState {
50 private final CompletionStage<ShardBackendInfo> stage;
52 private ShardBackendInfo result;
54 ShardState(final CompletionStage<ShardBackendInfo> stage) {
55 this.stage = Preconditions.checkNotNull(stage);
56 stage.whenComplete(this::onStageResolved);
59 @Nonnull CompletionStage<ShardBackendInfo> getStage() {
63 @Nullable synchronized ShardBackendInfo getResult() {
67 private synchronized void onStageResolved(final ShardBackendInfo result, final Throwable failure) {
68 if (failure == null) {
69 this.result = Preconditions.checkNotNull(result);
71 LOG.warn("Failed to resolve shard", failure);
76 private static final Logger LOG = LoggerFactory.getLogger(AbstractShardBackendResolver.class);
79 * Connect request timeout. If the shard does not respond within this interval, we retry the lookup and connection.
81 // TODO: maybe make this configurable somehow?
82 private static final Timeout CONNECT_TIMEOUT = Timeout.apply(5, TimeUnit.SECONDS);
84 private final AtomicLong nextSessionId = new AtomicLong();
85 private final Function1<ActorRef, ?> connectFunction;
86 private final ActorContext actorContext;
88 // FIXME: we really need just ActorContext.findPrimaryShardAsync()
89 AbstractShardBackendResolver(final ClientIdentifier clientId, final ActorContext actorContext) {
90 this.actorContext = Preconditions.checkNotNull(actorContext);
91 this.connectFunction = ExplicitAsk.toScala(t -> new ConnectClientRequest(clientId, t, ABIVersion.BORON,
92 ABIVersion.current()));
95 protected final void flushCache(final String shardName) {
96 actorContext.getPrimaryShardInfoCache().remove(shardName);
99 protected final ShardState resolveBackendInfo(final String shardName, final long cookie) {
100 LOG.debug("Resolving cookie {} to shard {}", cookie, shardName);
102 final CompletableFuture<ShardBackendInfo> future = new CompletableFuture<>();
103 FutureConverters.toJava(actorContext.findPrimaryShardAsync(shardName)).whenComplete((info, failure) -> {
104 if (failure == null) {
105 connectShard(shardName, cookie, info, future);
109 LOG.debug("Shard {} failed to resolve", shardName, failure);
110 if (failure instanceof NoShardLeaderException) {
111 // FIXME: this actually is an exception we can retry on
112 future.completeExceptionally(failure);
113 } else if (failure instanceof NotInitializedException) {
114 // FIXME: this actually is an exception we can retry on
115 LOG.info("Shard {} has not initialized yet", shardName);
116 future.completeExceptionally(failure);
117 } else if (failure instanceof PrimaryNotFoundException) {
118 LOG.info("Failed to find primary for shard {}", shardName);
119 future.completeExceptionally(failure);
121 future.completeExceptionally(failure);
125 return new ShardState(future);
128 private void connectShard(final String shardName, final long cookie, final PrimaryShardInfo info,
129 final CompletableFuture<ShardBackendInfo> future) {
130 LOG.debug("Shard {} resolved to {}, attempting to connect", shardName, info);
132 FutureConverters.toJava(ExplicitAsk.ask(info.getPrimaryShardActor(), connectFunction, CONNECT_TIMEOUT))
133 .whenComplete((response, failure) -> {
134 if (failure != null) {
135 LOG.debug("Connect attempt to {} failed", shardName, failure);
136 future.completeExceptionally(failure);
139 if (response instanceof RequestFailure) {
140 final RequestException cause = ((RequestFailure<?, ?>) response).getCause();
141 LOG.debug("Connect attempt to {} failed to process", shardName, cause);
142 future.completeExceptionally(cause);
146 LOG.debug("Resolved backend information to {}", response);
147 Preconditions.checkArgument(response instanceof ConnectClientSuccess, "Unhandled response {}",
149 final ConnectClientSuccess success = (ConnectClientSuccess) response;
150 future.complete(new ShardBackendInfo(success.getBackend(), nextSessionId.getAndIncrement(),
151 success.getVersion(), shardName, UnsignedLong.fromLongBits(cookie), success.getDataTree(),
152 success.getMaxMessages()));