BUG-5280: add AbstractClientConnection
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / databroker / actors / dds / ModuleShardBackendResolver.java
index a1018967e76668b8e1640f86c83d7c2769a5b3d9..9e6485b296e0e87dc20febe7db12ab08991d4db3 100644 (file)
@@ -18,11 +18,15 @@ import com.google.common.primitives.UnsignedLong;
 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.CompletionStage;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicLong;
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
 import javax.annotation.concurrent.GuardedBy;
+import javax.annotation.concurrent.ThreadSafe;
 import org.opendaylight.controller.cluster.access.ABIVersion;
-import org.opendaylight.controller.cluster.access.client.BackendInfo;
 import org.opendaylight.controller.cluster.access.client.BackendInfoResolver;
 import org.opendaylight.controller.cluster.access.commands.ConnectClientRequest;
 import org.opendaylight.controller.cluster.access.commands.ConnectClientSuccess;
@@ -46,7 +50,35 @@ import scala.compat.java8.FutureConverters;
  */
 @SuppressFBWarnings(value = "NP_NONNULL_PARAM_VIOLATION",
                     justification = "Pertains to the NULL_FUTURE field below. Null is allowed and is intended")
+@ThreadSafe
 final class ModuleShardBackendResolver extends BackendInfoResolver<ShardBackendInfo> {
+    private static final class Entry {
+        private final CompletionStage<ShardBackendInfo> stage;
+        @GuardedBy("this")
+        private ShardBackendInfo result;
+
+        Entry(final CompletionStage<ShardBackendInfo> stage) {
+            this.stage = Preconditions.checkNotNull(stage);
+            stage.whenComplete(this::onStageResolved);
+        }
+
+        @Nonnull CompletionStage<ShardBackendInfo> getStage() {
+            return stage;
+        }
+
+        synchronized @Nullable ShardBackendInfo getResult() {
+            return result;
+        }
+
+        private synchronized void onStageResolved(final ShardBackendInfo result, final Throwable failure) {
+            if (failure == null) {
+                this.result = Preconditions.checkNotNull(result);
+            } else {
+                LOG.warn("Failed to resolve shard", failure);
+            }
+        }
+    }
+
     private static final CompletableFuture<ShardBackendInfo> NULL_FUTURE = CompletableFuture.completedFuture(null);
     private static final Logger LOG = LoggerFactory.getLogger(ModuleShardBackendResolver.class);
 
@@ -58,10 +90,10 @@ final class ModuleShardBackendResolver extends BackendInfoResolver<ShardBackendI
     // TODO: maybe make this configurable somehow?
     private static final Timeout DEAD_TIMEOUT = Timeout.apply(15, TimeUnit.MINUTES);
 
-    private final ActorContext actorContext;
-    // FIXME: this counter should be in superclass somewhere
+    private final ConcurrentMap<Long, Entry> backends = new ConcurrentHashMap<>();
     private final AtomicLong nextSessionId = new AtomicLong();
     private final Function1<ActorRef, ?> connectFunction;
+    private final ActorContext actorContext;
 
     @GuardedBy("this")
     private long nextShard = 1;
@@ -75,18 +107,6 @@ final class ModuleShardBackendResolver extends BackendInfoResolver<ShardBackendI
             ABIVersion.current()));
     }
 
-    @Override
-    protected void invalidateBackendInfo(final CompletionStage<? extends BackendInfo> info) {
-        LOG.trace("Initiated invalidation of backend information {}", info);
-        info.thenAccept(this::invalidate);
-    }
-
-    private void invalidate(final BackendInfo result) {
-        Preconditions.checkArgument(result instanceof ShardBackendInfo);
-        LOG.debug("Invalidating backend information {}", result);
-        actorContext.getPrimaryShardInfoCache().remove(((ShardBackendInfo)result).getShardName());
-    }
-
     Long resolveShardForPath(final YangInstanceIdentifier path) {
         final String shardName = actorContext.getShardStrategyFactory().getStrategy(path).findShard(path);
         Long cookie = shards.get(shardName);
@@ -107,17 +127,16 @@ final class ModuleShardBackendResolver extends BackendInfoResolver<ShardBackendI
         return cookie;
     }
 
-    @Override
-    protected CompletableFuture<ShardBackendInfo> resolveBackendInfo(final Long cookie) {
+    private CompletionStage<ShardBackendInfo> resolveBackendInfo(final Long cookie) {
         final String shardName = shards.inverse().get(cookie);
         if (shardName == null) {
             LOG.warn("Failing request for non-existent cookie {}", cookie);
             return NULL_FUTURE;
         }
 
-        final CompletableFuture<ShardBackendInfo> ret = new CompletableFuture<>();
+        LOG.debug("Resolving cookie {} to shard {}", cookie, shardName);
 
-        FutureConverters.toJava(actorContext.findPrimaryShardAsync(shardName)).thenCompose(info -> {
+        return FutureConverters.toJava(actorContext.findPrimaryShardAsync(shardName)).thenCompose(info -> {
             LOG.debug("Looking up primary info for {} from {}", shardName, info);
             return FutureConverters.toJava(ExplicitAsk.ask(info.getPrimaryShardActor(), connectFunction, DEAD_TIMEOUT));
         }).thenApply(response -> {
@@ -135,15 +154,30 @@ final class ModuleShardBackendResolver extends BackendInfoResolver<ShardBackendI
             return new ShardBackendInfo(success.getBackend(),
                 nextSessionId.getAndIncrement(), success.getVersion(), shardName, UnsignedLong.fromLongBits(cookie),
                 success.getDataTree(), success.getMaxMessages());
-        }).whenComplete((info, throwablw) -> {
-            if (throwablw != null) {
-                ret.completeExceptionally(throwablw);
-            } else {
-                ret.complete(info);
-            }
         });
+    }
 
-        LOG.debug("Resolving cookie {} to shard {}", cookie, shardName);
-        return ret;
+    @Override
+    public CompletionStage<? extends ShardBackendInfo> getBackendInfo(final Long cookie) {
+        return backends.computeIfAbsent(cookie, key -> new Entry(resolveBackendInfo(key))).getStage();
+    }
+
+    @Override
+    public CompletionStage<? extends ShardBackendInfo> refreshBackendInfo(final Long cookie,
+            final ShardBackendInfo staleInfo) {
+        final Entry existing = backends.get(cookie);
+        if (existing != null) {
+            if (!staleInfo.equals(existing.getResult())) {
+                return existing.getStage();
+            }
+
+            LOG.debug("Invalidating backend information {}", staleInfo);
+            actorContext.getPrimaryShardInfoCache().remove(staleInfo.getShardName());
+
+            LOG.trace("Invalidated cache %s -> %s", Long.toUnsignedString(cookie), staleInfo);
+            backends.remove(cookie, existing);
+        }
+
+        return getBackendInfo(cookie);
     }
 }