X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-distributed-datastore%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fdatabroker%2Factors%2Fdds%2FSimpleShardBackendResolver.java;h=012e068a81bcbb23fba18f8163057d744b0f0d6c;hb=f662ce8b1fa94b77ba66f7ece8bcaff91dee809e;hp=056a1ea7e226deb49a6ee12e916769bf7e6dbb0c;hpb=32b322afd58f120a78208c939a01422aa224d0cf;p=controller.git diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/databroker/actors/dds/SimpleShardBackendResolver.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/databroker/actors/dds/SimpleShardBackendResolver.java index 056a1ea7e2..012e068a81 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/databroker/actors/dds/SimpleShardBackendResolver.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/databroker/actors/dds/SimpleShardBackendResolver.java @@ -7,23 +7,24 @@ */ package org.opendaylight.controller.cluster.databroker.actors.dds; -import com.google.common.base.Preconditions; +import static com.google.common.base.Preconditions.checkArgument; +import static java.util.Objects.requireNonNull; + import java.util.concurrent.CompletionStage; -import javax.annotation.concurrent.ThreadSafe; import org.opendaylight.controller.cluster.access.client.BackendInfoResolver; import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier; -import org.opendaylight.controller.cluster.datastore.utils.ActorContext; +import org.opendaylight.controller.cluster.datastore.utils.ActorUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * {@link BackendInfoResolver} implementation for static shard configuration based on ShardManager. Unlike the full * {@link ModuleShardBackendResolver}, this resolver is used in situations where the client corresponds exactly to one - * backend shard, e.g. there is only one fixed cookie assigned and the operation path is not consulted at all. + * backend shard, e.g. there is only one fixed cookie assigned and the operation path is not consulted at all. This + * class is thread-safe. * * @author Robert Varga */ -@ThreadSafe final class SimpleShardBackendResolver extends AbstractShardBackendResolver { private static final Logger LOG = LoggerFactory.getLogger(SimpleShardBackendResolver.class); @@ -32,27 +33,42 @@ final class SimpleShardBackendResolver extends AbstractShardBackendResolver { private volatile ShardState state; // FIXME: we really need just ActorContext.findPrimaryShardAsync() - SimpleShardBackendResolver(final ClientIdentifier clientId, final ActorContext actorContext, + SimpleShardBackendResolver(final ClientIdentifier clientId, final ActorUtils actorUtils, final String shardName) { - super(clientId, actorContext); - this.shardName = Preconditions.checkNotNull(shardName); + super(clientId, actorUtils); + this.shardName = requireNonNull(shardName); } private CompletionStage getBackendInfo(final long cookie) { - Preconditions.checkArgument(cookie == 0); + checkArgument(cookie == 0); - ShardState local = state; - if (local == null) { - synchronized (this) { - local = state; - if (local == null) { - local = resolveBackendInfo(shardName, 0); - state = local; - } - } + final ShardState existing = state; + if (existing != null) { + return existing.getStage(); } - return local.getStage(); + synchronized (this) { + final ShardState recheck = state; + if (recheck != null) { + return recheck.getStage(); + } + + final ShardState newState = resolveBackendInfo(shardName, 0); + state = newState; + + final CompletionStage stage = newState.getStage(); + stage.whenComplete((info, failure) -> { + if (failure != null) { + synchronized (SimpleShardBackendResolver.this) { + if (state == newState) { + state = null; + } + } + } + }); + + return stage; + } } @Override @@ -73,11 +89,16 @@ final class SimpleShardBackendResolver extends AbstractShardBackendResolver { synchronized (this) { LOG.debug("Invalidating backend information {}", staleInfo); flushCache(shardName); - LOG.trace("Invalidated cache %s", staleInfo); + LOG.trace("Invalidated cache {}", staleInfo); state = null; } } return getBackendInfo(cookie); } + + @Override + public String resolveCookieName(Long cookie) { + return shardName; + } }