From c763a16f9cd72bb4f27997f301f163b83d470a24 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Thu, 7 Apr 2022 17:16:07 +0200 Subject: [PATCH] Use ImmutableBiMap builder fluently We can simply allocate the builder without intermediate local, saving us an import. Also split the locked update part into a separate method to aid inlining. Change-Id: I34b3dffd49c5bd637792c743e9b71e31082201f4 Signed-off-by: Robert Varga --- .../dds/ModuleShardBackendResolver.java | 32 ++++++++----------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/databroker/actors/dds/ModuleShardBackendResolver.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/databroker/actors/dds/ModuleShardBackendResolver.java index 74aca03e86..61bb78ed3f 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/databroker/actors/dds/ModuleShardBackendResolver.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/databroker/actors/dds/ModuleShardBackendResolver.java @@ -13,14 +13,13 @@ import static com.google.common.base.Verify.verifyNotNull; import akka.dispatch.ExecutionContexts; import akka.dispatch.OnComplete; import akka.util.Timeout; -import com.google.common.collect.BiMap; import com.google.common.collect.ImmutableBiMap; -import com.google.common.collect.ImmutableBiMap.Builder; import java.util.concurrent.CompletionStage; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.TimeUnit; import org.checkerframework.checker.lock.qual.GuardedBy; +import org.eclipse.jdt.annotation.NonNull; import org.opendaylight.controller.cluster.access.client.BackendInfoResolver; import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier; import org.opendaylight.controller.cluster.datastore.shardmanager.RegisterForShardAvailabilityChanges; @@ -52,7 +51,7 @@ final class ModuleShardBackendResolver extends AbstractShardBackendResolver { @GuardedBy("this") private long nextShard = 1; - private volatile BiMap shards = ImmutableBiMap.of(DefaultShardStrategy.DEFAULT_SHARD, 0L); + private volatile ImmutableBiMap shards = ImmutableBiMap.of(DefaultShardStrategy.DEFAULT_SHARD, 0L); // FIXME: we really need just ActorContext.findPrimaryShardAsync() ModuleShardBackendResolver(final ClientIdentifier clientId, final ActorUtils actorUtils) { @@ -64,7 +63,7 @@ final class ModuleShardBackendResolver extends AbstractShardBackendResolver { shardAvailabilityChangesRegFuture.onComplete(new OnComplete() { @Override - public void onComplete(Throwable failure, Registration reply) { + public void onComplete(final Throwable failure, final Registration reply) { if (failure != null) { LOG.error("RegisterForShardAvailabilityChanges failed", failure); } @@ -72,7 +71,7 @@ final class ModuleShardBackendResolver extends AbstractShardBackendResolver { }, ExecutionContexts.global()); } - private void onShardAvailabilityChange(String shardName) { + private void onShardAvailabilityChange(final String shardName) { LOG.debug("onShardAvailabilityChange for {}", shardName); Long cookie = shards.get(shardName); @@ -86,21 +85,16 @@ final class ModuleShardBackendResolver extends AbstractShardBackendResolver { Long resolveShardForPath(final YangInstanceIdentifier path) { final String shardName = actorUtils().getShardStrategyFactory().getStrategy(path).findShard(path); + final Long cookie = shards.get(shardName); + return cookie != null ? cookie : populateShard(shardName); + } + + private synchronized @NonNull Long populateShard(final String shardName) { Long cookie = shards.get(shardName); if (cookie == null) { - synchronized (this) { - cookie = shards.get(shardName); - if (cookie == null) { - cookie = nextShard++; - - Builder builder = ImmutableBiMap.builder(); - builder.putAll(shards); - builder.put(shardName, cookie); - shards = builder.build(); - } - } + cookie = nextShard++; + shards = ImmutableBiMap.builder().putAll(shards).put(shardName, cookie).build(); } - return cookie; } @@ -174,14 +168,14 @@ final class ModuleShardBackendResolver extends AbstractShardBackendResolver { public void close() { shardAvailabilityChangesRegFuture.onComplete(new OnComplete() { @Override - public void onComplete(Throwable failure, Registration reply) { + public void onComplete(final Throwable failure, final Registration reply) { reply.close(); } }, ExecutionContexts.global()); } @Override - public String resolveCookieName(Long cookie) { + public String resolveCookieName(final Long cookie) { return verifyNotNull(shards.inverse().get(cookie), "Unexpected null cookie: %s", cookie); } } -- 2.36.6