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.util.Timeout;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.BiMap;
13 import com.google.common.collect.ImmutableBiMap;
14 import com.google.common.primitives.UnsignedLong;
15 import java.util.concurrent.CompletableFuture;
16 import java.util.concurrent.CompletionStage;
17 import java.util.concurrent.TimeUnit;
18 import org.opendaylight.controller.cluster.access.ABIVersion;
19 import org.opendaylight.controller.cluster.datastore.DataStoreVersions;
20 import org.opendaylight.controller.cluster.datastore.actors.client.BackendInfo;
21 import org.opendaylight.controller.cluster.datastore.actors.client.BackendInfoResolver;
22 import org.opendaylight.controller.cluster.datastore.messages.PrimaryShardInfo;
23 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26 import scala.compat.java8.FutureConverters;
29 * {@link BackendInfoResolver} implementation for static shard configuration based on ShardManager. Each string-named
30 * shard is assigned a single cookie and this mapping is stored in a bidirectional map. Information about corresponding
31 * shard leader is resolved via {@link ActorContext}. The product of resolution is {@link ShardBackendInfo}.
33 * @author Robert Varga
35 final class ModuleShardBackendResolver extends BackendInfoResolver<ShardBackendInfo> {
36 private static final Logger LOG = LoggerFactory.getLogger(ModuleShardBackendResolver.class);
38 * Fall-over-dead timeout. If we do not make progress in this long, just fall over and propagate the failure.
39 * All users are expected to fail, possibly attempting to recover by restarting. It is fair to remain
42 // TODO: maybe make this configurable somehow?
43 private static final Timeout DEAD_TIMEOUT = Timeout.apply(15, TimeUnit.MINUTES);
45 private final ActorContext actorContext;
47 private volatile BiMap<String, Long> shards = ImmutableBiMap.of();
49 // FIXME: we really need just ActorContext.findPrimaryShardAsync()
50 ModuleShardBackendResolver(final ActorContext actorContext) {
51 this.actorContext = Preconditions.checkNotNull(actorContext);
55 protected void invalidateBackendInfo(final CompletionStage<? extends BackendInfo> info) {
56 LOG.trace("Initiated invalidation of backend information {}", info);
57 info.thenAccept(this::invalidate);
60 private void invalidate(final BackendInfo result) {
61 Preconditions.checkArgument(result instanceof ShardBackendInfo);
62 LOG.debug("Invalidating backend information {}", result);
63 actorContext.getPrimaryShardInfoCache().remove(((ShardBackendInfo)result).getShardName());
67 protected CompletionStage<ShardBackendInfo> resolveBackendInfo(final Long cookie) {
68 final String shardName = shards.inverse().get(cookie);
69 if (shardName == null) {
70 LOG.warn("Failing request for non-existent cookie {}", cookie);
71 return CompletableFuture.completedFuture(null);
74 LOG.debug("Resolving cookie {} to shard {}", cookie, shardName);
75 return FutureConverters.toJava(actorContext.findPrimaryShardAsync(shardName))
76 .thenApply(o -> createBackendInfo(o, shardName, cookie));
79 private static ABIVersion toABIVersion(final short version) {
81 case DataStoreVersions.BORON_VERSION:
82 return ABIVersion.BORON;
85 throw new IllegalArgumentException("Unsupported version " + version);
88 private static ShardBackendInfo createBackendInfo(final Object result, final String shardName, final Long cookie) {
89 Preconditions.checkArgument(result instanceof PrimaryShardInfo);
90 final PrimaryShardInfo info = (PrimaryShardInfo) result;
92 LOG.debug("Creating backend information for {}", info);
93 return new ShardBackendInfo(info.getPrimaryShardActor().resolveOne(DEAD_TIMEOUT).value().get().get(),
94 toABIVersion(info.getPrimaryShardVersion()), shardName, UnsignedLong.fromLongBits(cookie),
95 info.getLocalShardDataTree());