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.base.Throwables;
14 import com.google.common.collect.BiMap;
15 import com.google.common.collect.ImmutableBiMap;
16 import com.google.common.collect.ImmutableBiMap.Builder;
17 import com.google.common.primitives.UnsignedLong;
18 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
19 import java.util.concurrent.CompletableFuture;
20 import java.util.concurrent.CompletionStage;
21 import java.util.concurrent.ConcurrentHashMap;
22 import java.util.concurrent.ConcurrentMap;
23 import java.util.concurrent.TimeUnit;
24 import java.util.concurrent.atomic.AtomicLong;
25 import javax.annotation.Nonnull;
26 import javax.annotation.Nullable;
27 import javax.annotation.concurrent.GuardedBy;
28 import javax.annotation.concurrent.ThreadSafe;
29 import org.opendaylight.controller.cluster.access.ABIVersion;
30 import org.opendaylight.controller.cluster.access.client.BackendInfoResolver;
31 import org.opendaylight.controller.cluster.access.commands.ConnectClientRequest;
32 import org.opendaylight.controller.cluster.access.commands.ConnectClientSuccess;
33 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
34 import org.opendaylight.controller.cluster.access.concepts.RequestFailure;
35 import org.opendaylight.controller.cluster.common.actor.ExplicitAsk;
36 import org.opendaylight.controller.cluster.datastore.shardstrategy.DefaultShardStrategy;
37 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
38 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import scala.Function1;
42 import scala.compat.java8.FutureConverters;
45 * {@link BackendInfoResolver} implementation for static shard configuration based on ShardManager. Each string-named
46 * shard is assigned a single cookie and this mapping is stored in a bidirectional map. Information about corresponding
47 * shard leader is resolved via {@link ActorContext}. The product of resolution is {@link ShardBackendInfo}.
49 * @author Robert Varga
51 @SuppressFBWarnings(value = "NP_NONNULL_PARAM_VIOLATION",
52 justification = "Pertains to the NULL_FUTURE field below. Null is allowed and is intended")
54 final class ModuleShardBackendResolver extends BackendInfoResolver<ShardBackendInfo> {
55 private static final class Entry {
56 private final CompletionStage<ShardBackendInfo> stage;
58 private ShardBackendInfo result;
60 Entry(final CompletionStage<ShardBackendInfo> stage) {
61 this.stage = Preconditions.checkNotNull(stage);
62 stage.whenComplete(this::onStageResolved);
65 @Nonnull CompletionStage<ShardBackendInfo> getStage() {
69 synchronized @Nullable ShardBackendInfo getResult() {
73 private synchronized void onStageResolved(final ShardBackendInfo result, final Throwable failure) {
74 if (failure == null) {
75 this.result = Preconditions.checkNotNull(result);
77 LOG.warn("Failed to resolve shard", failure);
82 private static final CompletableFuture<ShardBackendInfo> NULL_FUTURE = CompletableFuture.completedFuture(null);
83 private static final Logger LOG = LoggerFactory.getLogger(ModuleShardBackendResolver.class);
86 * Fall-over-dead timeout. If we do not make progress in this long, just fall over and propagate the failure.
87 * All users are expected to fail, possibly attempting to recover by restarting. It is fair to remain
90 // TODO: maybe make this configurable somehow?
91 private static final Timeout DEAD_TIMEOUT = Timeout.apply(15, TimeUnit.MINUTES);
93 private final ConcurrentMap<Long, Entry> backends = new ConcurrentHashMap<>();
94 private final AtomicLong nextSessionId = new AtomicLong();
95 private final Function1<ActorRef, ?> connectFunction;
96 private final ActorContext actorContext;
99 private long nextShard = 1;
101 private volatile BiMap<String, Long> shards = ImmutableBiMap.of(DefaultShardStrategy.DEFAULT_SHARD, 0L);
103 // FIXME: we really need just ActorContext.findPrimaryShardAsync()
104 ModuleShardBackendResolver(final ClientIdentifier clientId, final ActorContext actorContext) {
105 this.actorContext = Preconditions.checkNotNull(actorContext);
106 this.connectFunction = ExplicitAsk.toScala(t -> new ConnectClientRequest(clientId, t, ABIVersion.BORON,
107 ABIVersion.current()));
110 Long resolveShardForPath(final YangInstanceIdentifier path) {
111 final String shardName = actorContext.getShardStrategyFactory().getStrategy(path).findShard(path);
112 Long cookie = shards.get(shardName);
113 if (cookie == null) {
114 synchronized (this) {
115 cookie = shards.get(shardName);
116 if (cookie == null) {
117 cookie = nextShard++;
119 Builder<String, Long> builder = ImmutableBiMap.builder();
120 builder.putAll(shards);
121 builder.put(shardName, cookie);
122 shards = builder.build();
130 private CompletionStage<ShardBackendInfo> resolveBackendInfo(final Long cookie) {
131 final String shardName = shards.inverse().get(cookie);
132 if (shardName == null) {
133 LOG.warn("Failing request for non-existent cookie {}", cookie);
137 LOG.debug("Resolving cookie {} to shard {}", cookie, shardName);
139 return FutureConverters.toJava(actorContext.findPrimaryShardAsync(shardName)).thenCompose(info -> {
140 LOG.debug("Looking up primary info for {} from {}", shardName, info);
141 return FutureConverters.toJava(ExplicitAsk.ask(info.getPrimaryShardActor(), connectFunction, DEAD_TIMEOUT));
142 }).thenApply(response -> {
143 if (response instanceof RequestFailure) {
144 final RequestFailure<?, ?> failure = (RequestFailure<?, ?>) response;
145 LOG.debug("Connect request failed {}", failure, failure.getCause());
146 throw Throwables.propagate(failure.getCause());
149 LOG.debug("Resolved backend information to {}", response);
151 Preconditions.checkArgument(response instanceof ConnectClientSuccess, "Unhandled response {}", response);
152 final ConnectClientSuccess success = (ConnectClientSuccess) response;
154 return new ShardBackendInfo(success.getBackend(),
155 nextSessionId.getAndIncrement(), success.getVersion(), shardName, UnsignedLong.fromLongBits(cookie),
156 success.getDataTree(), success.getMaxMessages());
161 public CompletionStage<? extends ShardBackendInfo> getBackendInfo(final Long cookie) {
162 return backends.computeIfAbsent(cookie, key -> new Entry(resolveBackendInfo(key))).getStage();
166 public CompletionStage<? extends ShardBackendInfo> refreshBackendInfo(final Long cookie,
167 final ShardBackendInfo staleInfo) {
168 final Entry existing = backends.get(cookie);
169 if (existing != null) {
170 if (!staleInfo.equals(existing.getResult())) {
171 return existing.getStage();
174 LOG.debug("Invalidating backend information {}", staleInfo);
175 actorContext.getPrimaryShardInfoCache().remove(staleInfo.getShardName());
177 LOG.trace("Invalidated cache %s -> %s", Long.toUnsignedString(cookie), staleInfo);
178 backends.remove(cookie, existing);
181 return getBackendInfo(cookie);