BUG-5280: implement transaction dispatch
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / databroker / actors / dds / ModuleShardBackendResolver.java
index 6f15c72a46e381123e4c78bd13084e80253e40b6..bef863ddf48a50fc81165f1d04de8d7aba691d3f 100644 (file)
@@ -7,23 +7,30 @@
  */
 package org.opendaylight.controller.cluster.databroker.actors.dds;
 
+import akka.dispatch.ExecutionContexts;
+import akka.dispatch.OnComplete;
 import akka.util.Timeout;
 import com.google.common.base.Preconditions;
 import com.google.common.collect.BiMap;
 import com.google.common.collect.ImmutableBiMap;
+import com.google.common.collect.ImmutableBiMap.Builder;
 import com.google.common.primitives.UnsignedLong;
+import com.google.common.util.concurrent.MoreExecutors;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.CompletionStage;
 import java.util.concurrent.TimeUnit;
+import javax.annotation.concurrent.GuardedBy;
 import org.opendaylight.controller.cluster.access.ABIVersion;
 import org.opendaylight.controller.cluster.datastore.DataStoreVersions;
 import org.opendaylight.controller.cluster.datastore.actors.client.BackendInfo;
 import org.opendaylight.controller.cluster.datastore.actors.client.BackendInfoResolver;
 import org.opendaylight.controller.cluster.datastore.messages.PrimaryShardInfo;
+import org.opendaylight.controller.cluster.datastore.shardstrategy.DefaultShardStrategy;
 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import scala.compat.java8.FutureConverters;
+import scala.concurrent.ExecutionContext;
 
 /**
  * {@link BackendInfoResolver} implementation for static shard configuration based on ShardManager. Each string-named
@@ -33,7 +40,11 @@ import scala.compat.java8.FutureConverters;
  * @author Robert Varga
  */
 final class ModuleShardBackendResolver extends BackendInfoResolver<ShardBackendInfo> {
+    private static final ExecutionContext DIRECT_EXECUTION_CONTEXT =
+            ExecutionContexts.fromExecutor(MoreExecutors.directExecutor());
+    private static final CompletableFuture<ShardBackendInfo> NULL_FUTURE = CompletableFuture.completedFuture(null);
     private static final Logger LOG = LoggerFactory.getLogger(ModuleShardBackendResolver.class);
+
     /**
      * Fall-over-dead timeout. If we do not make progress in this long, just fall over and propagate the failure.
      * All users are expected to fail, possibly attempting to recover by restarting. It is fair to remain
@@ -44,7 +55,10 @@ final class ModuleShardBackendResolver extends BackendInfoResolver<ShardBackendI
 
     private final ActorContext actorContext;
 
-    private volatile BiMap<String, Long> shards = ImmutableBiMap.of();
+    @GuardedBy("this")
+    private long nextShard = 1;
+
+    private volatile BiMap<String, Long> shards = ImmutableBiMap.of(DefaultShardStrategy.DEFAULT_SHARD, 0L);
 
     // FIXME: we really need just ActorContext.findPrimaryShardAsync()
     ModuleShardBackendResolver(final ActorContext actorContext) {
@@ -63,17 +77,49 @@ final class ModuleShardBackendResolver extends BackendInfoResolver<ShardBackendI
         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);
+        if (cookie == null) {
+            synchronized (this) {
+                cookie = shards.get(shardName);
+                if (cookie == null) {
+                    cookie = nextShard++;
+
+                    Builder<String, Long> b = ImmutableBiMap.builder();
+                    b.putAll(shards);
+                    b.put(shardName, cookie);
+                    shards = b.build();
+                }
+            }
+        }
+
+        return cookie;
+    }
+
     @Override
-    protected CompletionStage<ShardBackendInfo> resolveBackendInfo(final Long cookie) {
+    protected CompletableFuture<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 CompletableFuture.completedFuture(null);
+            return NULL_FUTURE;
         }
 
+        final CompletableFuture<ShardBackendInfo> ret = new CompletableFuture<>();
+
+        actorContext.findPrimaryShardAsync(shardName).onComplete(new OnComplete<PrimaryShardInfo>() {
+            @Override
+            public void onComplete(final Throwable t, final PrimaryShardInfo v) {
+                if (t != null) {
+                    ret.completeExceptionally(t);
+                } else {
+                    ret.complete(createBackendInfo(v, shardName, cookie));
+                }
+            }
+        }, DIRECT_EXECUTION_CONTEXT);
+
         LOG.debug("Resolving cookie {} to shard {}", cookie, shardName);
-        return FutureConverters.toJava(actorContext.findPrimaryShardAsync(shardName))
-                .thenApply(o -> createBackendInfo(o, shardName, cookie));
+        return ret;
     }
 
     private static ABIVersion toABIVersion(final short version) {