Remove unneeded cast
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / sharding / DistributedShardedDOMDataTree.java
index 506498ce9a77fecbf9d7911296aafb49c1160f11..99c03768ed111bff08a9a8d9fbd8bcbd98e739d6 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
+ * Copyright (c) 2016, 2017 Cisco Systems, Inc. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
@@ -161,7 +161,7 @@ public class DistributedShardedDOMDataTree implements DOMDataTreeService, DOMDat
         createPrefixConfigShard(distributedOperDatastore);
     }
 
-    private void createPrefixConfigShard(final AbstractDataStore dataStore) {
+    private static void createPrefixConfigShard(final AbstractDataStore dataStore) {
         Configuration configuration = dataStore.getActorContext().getConfiguration();
         Collection<MemberName> memberNames = configuration.getUniqueMemberNamesForAllShards();
         CreateShard createShardMessage =
@@ -307,7 +307,7 @@ public class DistributedShardedDOMDataTree implements DOMDataTreeService, DOMDat
             LOG.debug("{} - Received success from remote nodes, creating producer:{}",
                     distributedConfigDatastore.getActorContext().getClusterWrapper().getCurrentMemberName(), subtrees);
             return new ProxyProducer(producer, subtrees, shardedDataTreeActor,
-                    distributedConfigDatastore.getActorContext());
+                    distributedConfigDatastore.getActorContext(), shards);
         } else if (response instanceof Exception) {
             closeProducer(producer);
             throw Throwables.propagate((Exception) response);
@@ -402,16 +402,15 @@ public class DistributedShardedDOMDataTree implements DOMDataTreeService, DOMDat
                 prefix.getDatastoreType().equals(org.opendaylight.mdsal.common.api.LogicalDatastoreType.CONFIGURATION)
                         ? distributedConfigDatastore : distributedOperDatastore;
 
-        try (final DOMDataTreeProducer producer = localCreateProducer(Collections.singletonList(prefix))) {
+        try (DOMDataTreeProducer producer = localCreateProducer(Collections.singletonList(prefix))) {
             final Entry<DataStoreClient, ActorRef> entry =
                     createDatastoreClient(shardName, distributedDataStore.getActorContext());
 
             final DistributedShardFrontend shard =
                     new DistributedShardFrontend(distributedDataStore, entry.getKey(), prefix);
 
-            @SuppressWarnings("unchecked")
             final DOMDataTreeShardRegistration<DOMDataTreeShard> reg =
-                    (DOMDataTreeShardRegistration) shardedDOMDataTree.registerDataTreeShard(prefix, shard, producer);
+                    shardedDOMDataTree.registerDataTreeShard(prefix, shard, producer);
 
             synchronized (shards) {
                 shards.store(prefix, reg);
@@ -451,12 +450,12 @@ public class DistributedShardedDOMDataTree implements DOMDataTreeService, DOMDat
 
         Futures.addCallback(future, new FutureCallback<Void>() {
             @Override
-            public void onSuccess(@Nullable Void result) {
+            public void onSuccess(@Nullable final Void result) {
                 LOG.debug("{} - Succesfuly removed shard for {}", memberName, prefix);
             }
 
             @Override
-            public void onFailure(Throwable throwable) {
+            public void onFailure(final Throwable throwable) {
                 LOG.error("Removal of shard {} from configuration failed.", prefix, throwable);
             }
         });
@@ -464,8 +463,9 @@ public class DistributedShardedDOMDataTree implements DOMDataTreeService, DOMDat
 
     DOMDataTreePrefixTableEntry<DOMDataTreeShardRegistration<DOMDataTreeShard>> lookupShardFrontend(
             final DOMDataTreeIdentifier prefix) {
-        return shards.lookup(prefix);
-
+        synchronized (shards) {
+            return shards.lookup(prefix);
+        }
     }
 
     DOMDataTreeProducer localCreateProducer(final Collection<DOMDataTreeIdentifier> prefix) {
@@ -641,14 +641,21 @@ public class DistributedShardedDOMDataTree implements DOMDataTreeService, DOMDat
         @GuardedBy("shardAccessMap")
         private final Map<DOMDataTreeIdentifier, CDSShardAccessImpl> shardAccessMap = new HashMap<>();
 
+        // We don't have to guard access to shardTable in ProxyProducer.
+        // ShardTable's entries relevant to this ProxyProducer shouldn't
+        // change during producer's lifetime.
+        private final DOMDataTreePrefixTable<DOMDataTreeShardRegistration<DOMDataTreeShard>> shardTable;
+
         ProxyProducer(final DOMDataTreeProducer delegate,
                       final Collection<DOMDataTreeIdentifier> subtrees,
                       final ActorRef shardDataTreeActor,
-                      final ActorContext actorContext) {
+                      final ActorContext actorContext,
+                      final DOMDataTreePrefixTable<DOMDataTreeShardRegistration<DOMDataTreeShard>> shardLayout) {
             this.delegate = Preconditions.checkNotNull(delegate);
             this.subtrees = Preconditions.checkNotNull(subtrees);
             this.shardDataTreeActor = Preconditions.checkNotNull(shardDataTreeActor);
             this.actorContext = Preconditions.checkNotNull(actorContext);
+            this.shardTable = Preconditions.checkNotNull(shardLayout);
         }
 
         @Nonnull
@@ -669,13 +676,14 @@ public class DistributedShardedDOMDataTree implements DOMDataTreeService, DOMDat
         @SuppressWarnings("checkstyle:IllegalCatch")
         public void close() throws DOMDataTreeProducerException {
             delegate.close();
+
             synchronized (shardAccessMap) {
                 shardAccessMap.values().forEach(CDSShardAccessImpl::close);
             }
 
             final Object o = actorContext.executeOperation(shardDataTreeActor, new ProducerRemoved(subtrees));
             if (o instanceof DOMDataTreeProducerException) {
-                throw ((DOMDataTreeProducerException) o);
+                throw (DOMDataTreeProducerException) o;
             } else if (o instanceof Throwable) {
                 throw new DOMDataTreeProducerException("Unable to close producer", (Throwable) o);
             }
@@ -689,19 +697,28 @@ public class DistributedShardedDOMDataTree implements DOMDataTreeService, DOMDat
         @Nonnull
         @Override
         public CDSShardAccess getShardAccess(@Nonnull final DOMDataTreeIdentifier subtree) {
+            Preconditions.checkArgument(
+                    subtrees.stream().anyMatch(dataTreeIdentifier -> dataTreeIdentifier.contains(subtree)),
+                    "Subtree {} is not controlled by this producer {}", subtree, this);
+
+            final DOMDataTreePrefixTableEntry<DOMDataTreeShardRegistration<DOMDataTreeShard>> lookup =
+                    shardTable.lookup(subtree);
+            Preconditions.checkState(lookup != null, "Subtree {} is not contained in any registered shard.");
+
+            final DOMDataTreeIdentifier lookupId = lookup.getValue().getPrefix();
+
             synchronized (shardAccessMap) {
-                Preconditions.checkArgument(subtrees.contains(subtree),
-                        "Subtree {} is not controlled by this producer {}", subtree, this);
-                if (shardAccessMap.get(subtree) != null) {
-                    return shardAccessMap.get(subtree);
+                if (shardAccessMap.get(lookupId) != null) {
+                    return shardAccessMap.get(lookupId);
                 }
 
                 // TODO Maybe we can have static factory method and return the same instance
                 // for same subtrees. But maybe it is not needed since there can be only one
                 // producer attached to some subtree at a time. And also how we can close ShardAccess
                 // then
-                final CDSShardAccessImpl shardAccess = new CDSShardAccessImpl(subtree, actorContext);
-                return shardAccessMap.put(subtree, shardAccess);
+                final CDSShardAccessImpl shardAccess = new CDSShardAccessImpl(lookupId, actorContext);
+                shardAccessMap.put(lookupId, shardAccess);
+                return shardAccess;
             }
         }
     }