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.dispatch.ExecutionContexts;
11 import akka.dispatch.OnComplete;
12 import akka.util.Timeout;
13 import com.google.common.base.Preconditions;
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 com.google.common.util.concurrent.MoreExecutors;
19 import java.util.concurrent.CompletableFuture;
20 import java.util.concurrent.CompletionStage;
21 import java.util.concurrent.TimeUnit;
22 import javax.annotation.concurrent.GuardedBy;
23 import org.opendaylight.controller.cluster.access.ABIVersion;
24 import org.opendaylight.controller.cluster.access.client.BackendInfo;
25 import org.opendaylight.controller.cluster.access.client.BackendInfoResolver;
26 import org.opendaylight.controller.cluster.datastore.DataStoreVersions;
27 import org.opendaylight.controller.cluster.datastore.messages.PrimaryShardInfo;
28 import org.opendaylight.controller.cluster.datastore.shardstrategy.DefaultShardStrategy;
29 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import scala.concurrent.ExecutionContext;
36 * {@link BackendInfoResolver} implementation for static shard configuration based on ShardManager. Each string-named
37 * shard is assigned a single cookie and this mapping is stored in a bidirectional map. Information about corresponding
38 * shard leader is resolved via {@link ActorContext}. The product of resolution is {@link ShardBackendInfo}.
40 * @author Robert Varga
42 final class ModuleShardBackendResolver extends BackendInfoResolver<ShardBackendInfo> {
43 private static final ExecutionContext DIRECT_EXECUTION_CONTEXT =
44 ExecutionContexts.fromExecutor(MoreExecutors.directExecutor());
45 private static final CompletableFuture<ShardBackendInfo> NULL_FUTURE = CompletableFuture.completedFuture(null);
46 private static final Logger LOG = LoggerFactory.getLogger(ModuleShardBackendResolver.class);
49 * Fall-over-dead timeout. If we do not make progress in this long, just fall over and propagate the failure.
50 * All users are expected to fail, possibly attempting to recover by restarting. It is fair to remain
53 // TODO: maybe make this configurable somehow?
54 private static final Timeout DEAD_TIMEOUT = Timeout.apply(15, TimeUnit.MINUTES);
56 private final ActorContext actorContext;
59 private long nextShard = 1;
61 private volatile BiMap<String, Long> shards = ImmutableBiMap.of(DefaultShardStrategy.DEFAULT_SHARD, 0L);
63 // FIXME: we really need just ActorContext.findPrimaryShardAsync()
64 ModuleShardBackendResolver(final ActorContext actorContext) {
65 this.actorContext = Preconditions.checkNotNull(actorContext);
69 protected void invalidateBackendInfo(final CompletionStage<? extends BackendInfo> info) {
70 LOG.trace("Initiated invalidation of backend information {}", info);
71 info.thenAccept(this::invalidate);
74 private void invalidate(final BackendInfo result) {
75 Preconditions.checkArgument(result instanceof ShardBackendInfo);
76 LOG.debug("Invalidating backend information {}", result);
77 actorContext.getPrimaryShardInfoCache().remove(((ShardBackendInfo)result).getShardName());
80 Long resolveShardForPath(final YangInstanceIdentifier path) {
81 final String shardName = actorContext.getShardStrategyFactory().getStrategy(path).findShard(path);
82 Long cookie = shards.get(shardName);
85 cookie = shards.get(shardName);
89 Builder<String, Long> b = ImmutableBiMap.builder();
91 b.put(shardName, cookie);
101 protected CompletableFuture<ShardBackendInfo> resolveBackendInfo(final Long cookie) {
102 final String shardName = shards.inverse().get(cookie);
103 if (shardName == null) {
104 LOG.warn("Failing request for non-existent cookie {}", cookie);
108 final CompletableFuture<ShardBackendInfo> ret = new CompletableFuture<>();
110 actorContext.findPrimaryShardAsync(shardName).onComplete(new OnComplete<PrimaryShardInfo>() {
112 public void onComplete(final Throwable t, final PrimaryShardInfo v) {
114 ret.completeExceptionally(t);
116 ret.complete(createBackendInfo(v, shardName, cookie));
119 }, DIRECT_EXECUTION_CONTEXT);
121 LOG.debug("Resolving cookie {} to shard {}", cookie, shardName);
125 private static ABIVersion toABIVersion(final short version) {
127 case DataStoreVersions.BORON_VERSION:
128 return ABIVersion.BORON;
131 throw new IllegalArgumentException("Unsupported version " + version);
134 private static ShardBackendInfo createBackendInfo(final Object result, final String shardName, final Long cookie) {
135 Preconditions.checkArgument(result instanceof PrimaryShardInfo);
136 final PrimaryShardInfo info = (PrimaryShardInfo) result;
138 LOG.debug("Creating backend information for {}", info);
139 return new ShardBackendInfo(info.getPrimaryShardActor().resolveOne(DEAD_TIMEOUT).value().get().get(),
140 toABIVersion(info.getPrimaryShardVersion()), shardName, UnsignedLong.fromLongBits(cookie),
141 info.getLocalShardDataTree());