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%2Fsharding%2FDistributedShardedDOMDataTree.java;h=157ee8808d5a401e25dbe236d1875186e81081c2;hb=refs%2Fchanges%2F18%2F81318%2F2;hp=53d0270312d2b5e06e8b8dc87fe480d855087a1c;hpb=c0aefbe270a37756cf5eb4987626fee572584562;p=controller.git diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/sharding/DistributedShardedDOMDataTree.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/sharding/DistributedShardedDOMDataTree.java index 53d0270312..157ee8808d 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/sharding/DistributedShardedDOMDataTree.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/sharding/DistributedShardedDOMDataTree.java @@ -5,10 +5,12 @@ * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ - package org.opendaylight.controller.cluster.sharding; import static akka.actor.ActorRef.noSender; +import static com.google.common.base.Preconditions.checkArgument; +import static com.google.common.base.Preconditions.checkState; +import static java.util.Objects.requireNonNull; import akka.actor.ActorRef; import akka.actor.ActorSystem; @@ -19,10 +21,10 @@ import akka.dispatch.OnComplete; import akka.pattern.Patterns; import akka.util.Timeout; import com.google.common.base.Optional; -import com.google.common.base.Preconditions; import com.google.common.base.Throwables; +import com.google.common.collect.ClassToInstanceMap; import com.google.common.collect.ForwardingObject; -import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableClassToInstanceMap; import com.google.common.util.concurrent.FutureCallback; import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; @@ -30,9 +32,9 @@ import com.google.common.util.concurrent.MoreExecutors; import com.google.common.util.concurrent.SettableFuture; import com.google.common.util.concurrent.Uninterruptibles; import java.util.AbstractMap.SimpleEntry; -import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.Comparator; import java.util.EnumMap; import java.util.HashMap; import java.util.List; @@ -43,8 +45,6 @@ import java.util.concurrent.CompletionStage; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; import javax.annotation.concurrent.GuardedBy; import org.opendaylight.controller.cluster.ActorSystemProvider; import org.opendaylight.controller.cluster.access.concepts.MemberName; @@ -56,7 +56,7 @@ import org.opendaylight.controller.cluster.datastore.config.Configuration; import org.opendaylight.controller.cluster.datastore.config.ModuleShardConfiguration; import org.opendaylight.controller.cluster.datastore.messages.CreateShard; import org.opendaylight.controller.cluster.datastore.shardstrategy.ModuleShardStrategy; -import org.opendaylight.controller.cluster.datastore.utils.ActorContext; +import org.opendaylight.controller.cluster.datastore.utils.ActorUtils; import org.opendaylight.controller.cluster.datastore.utils.ClusterUtils; import org.opendaylight.controller.cluster.dom.api.CDSDataTreeProducer; import org.opendaylight.controller.cluster.dom.api.CDSShardAccess; @@ -135,25 +135,25 @@ public class DistributedShardedDOMDataTree implements DOMDataTreeService, DOMDat public DistributedShardedDOMDataTree(final ActorSystemProvider actorSystemProvider, final AbstractDataStore distributedOperDatastore, final AbstractDataStore distributedConfigDatastore) { - this.actorSystem = Preconditions.checkNotNull(actorSystemProvider).getActorSystem(); - this.distributedOperDatastore = Preconditions.checkNotNull(distributedOperDatastore); - this.distributedConfigDatastore = Preconditions.checkNotNull(distributedConfigDatastore); + this.actorSystem = requireNonNull(actorSystemProvider).getActorSystem(); + this.distributedOperDatastore = requireNonNull(distributedOperDatastore); + this.distributedConfigDatastore = requireNonNull(distributedConfigDatastore); shardedDOMDataTree = new ShardedDOMDataTree(); shardedDataTreeActor = createShardedDataTreeActor(actorSystem, new ShardedDataTreeActorCreator() .setShardingService(this) .setActorSystem(actorSystem) - .setClusterWrapper(distributedConfigDatastore.getActorContext().getClusterWrapper()) + .setClusterWrapper(distributedConfigDatastore.getActorUtils().getClusterWrapper()) .setDistributedConfigDatastore(distributedConfigDatastore) .setDistributedOperDatastore(distributedOperDatastore) .setLookupTaskMaxRetries(LOOKUP_TASK_MAX_RETRIES), ACTOR_ID); - this.memberName = distributedConfigDatastore.getActorContext().getCurrentMemberName(); + this.memberName = distributedConfigDatastore.getActorUtils().getCurrentMemberName(); updateHandler = new PrefixedShardConfigUpdateHandler(shardedDataTreeActor, - distributedConfigDatastore.getActorContext().getCurrentMemberName()); + distributedConfigDatastore.getActorUtils().getCurrentMemberName()); LOG.debug("{} - Starting prefix configuration shards", memberName); createPrefixConfigShard(distributedConfigDatastore); @@ -161,15 +161,15 @@ public class DistributedShardedDOMDataTree implements DOMDataTreeService, DOMDat } private static void createPrefixConfigShard(final AbstractDataStore dataStore) { - Configuration configuration = dataStore.getActorContext().getConfiguration(); + Configuration configuration = dataStore.getActorUtils().getConfiguration(); Collection memberNames = configuration.getUniqueMemberNamesForAllShards(); CreateShard createShardMessage = new CreateShard(new ModuleShardConfiguration(PrefixShards.QNAME.getNamespace(), "prefix-shard-configuration", ClusterUtils.PREFIX_CONFIG_SHARD_ID, ModuleShardStrategy.NAME, memberNames), - Shard.builder(), dataStore.getActorContext().getDatastoreContext()); + Shard.builder(), dataStore.getActorUtils().getDatastoreContext()); - dataStore.getActorContext().getShardManager().tell(createShardMessage, noSender()); + dataStore.getActorUtils().getShardManager().tell(createShardMessage, noSender()); } /** @@ -207,7 +207,7 @@ public class DistributedShardedDOMDataTree implements DOMDataTreeService, DOMDat LOG.debug("{}: Prefix configuration shards ready - creating clients", memberName); configurationShardMap.put(LogicalDatastoreType.CONFIGURATION, createDatastoreClient(ClusterUtils.PREFIX_CONFIG_SHARD_ID, - distributedConfigDatastore.getActorContext())); + distributedConfigDatastore.getActorUtils())); } catch (final DOMDataTreeShardCreationFailedException e) { throw new IllegalStateException( "Unable to create datastoreClient for config DS prefix configuration shard.", e); @@ -216,7 +216,7 @@ public class DistributedShardedDOMDataTree implements DOMDataTreeService, DOMDat try { configurationShardMap.put(LogicalDatastoreType.OPERATIONAL, createDatastoreClient(ClusterUtils.PREFIX_CONFIG_SHARD_ID, - distributedOperDatastore.getActorContext())); + distributedOperDatastore.getActorUtils())); } catch (final DOMDataTreeShardCreationFailedException e) { throw new IllegalStateException( @@ -232,22 +232,13 @@ public class DistributedShardedDOMDataTree implements DOMDataTreeService, DOMDat updateHandler.initListener(distributedConfigDatastore, LogicalDatastoreType.CONFIGURATION); updateHandler.initListener(distributedOperDatastore, LogicalDatastoreType.OPERATIONAL); - distributedConfigDatastore.getActorContext().getShardManager().tell(InitConfigListener.INSTANCE, noSender()); - distributedOperDatastore.getActorContext().getShardManager().tell(InitConfigListener.INSTANCE, noSender()); + distributedConfigDatastore.getActorUtils().getShardManager().tell(InitConfigListener.INSTANCE, noSender()); + distributedOperDatastore.getActorUtils().getShardManager().tell(InitConfigListener.INSTANCE, noSender()); //create shard registration for DEFAULT_SHARD - try { - initDefaultShard(LogicalDatastoreType.CONFIGURATION); - } catch (final InterruptedException | ExecutionException e) { - throw new IllegalStateException("Unable to create default shard frontend for config shard", e); - } - - try { - initDefaultShard(LogicalDatastoreType.OPERATIONAL); - } catch (final InterruptedException | ExecutionException e) { - throw new IllegalStateException("Unable to create default shard frontend for operational shard", e); - } + initDefaultShard(LogicalDatastoreType.CONFIGURATION); + initDefaultShard(LogicalDatastoreType.OPERATIONAL); } private ListenableFuture> handleConfigShardLookup() { @@ -266,7 +257,7 @@ public class DistributedShardedDOMDataTree implements DOMDataTreeService, DOMDat ask.onComplete(new OnComplete() { @Override - public void onComplete(final Throwable throwable, final Object result) throws Throwable { + public void onComplete(final Throwable throwable, final Object result) { if (throwable != null) { future.setException(throwable); } else { @@ -278,7 +269,6 @@ public class DistributedShardedDOMDataTree implements DOMDataTreeService, DOMDat return future; } - @Nonnull @Override public ListenerRegistration registerListener( final T listener, final Collection subtrees, @@ -288,22 +278,21 @@ public class DistributedShardedDOMDataTree implements DOMDataTreeService, DOMDat } @Override - public Map, DOMDataTreeServiceExtension> getSupportedExtensions() { - return ImmutableMap.of(); + public ClassToInstanceMap getExtensions() { + return ImmutableClassToInstanceMap.of(); } - @Nonnull @Override - public DOMDataTreeProducer createProducer(@Nonnull final Collection subtrees) { + public DOMDataTreeProducer createProducer(final Collection subtrees) { LOG.debug("{} - Creating producer for {}", memberName, subtrees); final DOMDataTreeProducer producer = shardedDOMDataTree.createProducer(subtrees); - final Object response = distributedConfigDatastore.getActorContext() + final Object response = distributedConfigDatastore.getActorUtils() .executeOperation(shardedDataTreeActor, new ProducerCreated(subtrees)); if (response == null) { LOG.debug("{} - Received success from remote nodes, creating producer:{}", memberName, subtrees); return new ProxyProducer(producer, subtrees, shardedDataTreeActor, - distributedConfigDatastore.getActorContext(), shards); + distributedConfigDatastore.getActorUtils(), shards); } closeProducer(producer); @@ -337,7 +326,7 @@ public class DistributedShardedDOMDataTree implements DOMDataTreeService, DOMDat final Promise shardRegistrationPromise = akka.dispatch.Futures.promise(); Futures.addCallback(writeFuture, new FutureCallback() { @Override - public void onSuccess(@Nullable final Void result) { + public void onSuccess(final Void result) { final Future ask = Patterns.ask(shardedDataTreeActor, new LookupPrefixShard(prefix), SHARD_FUTURE_TIMEOUT); @@ -371,19 +360,11 @@ public class DistributedShardedDOMDataTree implements DOMDataTreeService, DOMDat void resolveShardAdditions(final Set additions) { LOG.debug("{}: Resolving additions : {}", memberName, additions); - final ArrayList list = new ArrayList<>(additions); // we need to register the shards from top to bottom, so we need to atleast make sure the ordering reflects that - Collections.sort(list, (o1, o2) -> { - if (o1.getRootIdentifier().getPathArguments().size() < o2.getRootIdentifier().getPathArguments().size()) { - return -1; - } else if (o1.getRootIdentifier().getPathArguments().size() - == o2.getRootIdentifier().getPathArguments().size()) { - return 0; - } else { - return 1; - } - }); - list.forEach(this::createShardFrontend); + additions + .stream() + .sorted(Comparator.comparingInt(o -> o.getRootIdentifier().getPathArguments().size())) + .forEachOrdered(this::createShardFrontend); } void resolveShardRemovals(final Set removals) { @@ -402,7 +383,7 @@ public class DistributedShardedDOMDataTree implements DOMDataTreeService, DOMDat try (DOMDataTreeProducer producer = localCreateProducer(Collections.singletonList(prefix))) { final Entry entry = - createDatastoreClient(shardName, distributedDataStore.getActorContext()); + createDatastoreClient(shardName, distributedDataStore.getActorUtils()); final DistributedShardFrontend shard = new DistributedShardFrontend(distributedDataStore, entry.getKey(), prefix); @@ -416,7 +397,7 @@ public class DistributedShardedDOMDataTree implements DOMDataTreeService, DOMDat } catch (final DOMDataTreeShardingConflictException e) { LOG.error("{}: Prefix {} is already occupied by another shard", - distributedConfigDatastore.getActorContext().getClusterWrapper().getCurrentMemberName(), prefix, e); + distributedConfigDatastore.getActorUtils().getClusterWrapper().getCurrentMemberName(), prefix, e); } catch (DOMDataTreeProducerException e) { LOG.error("Unable to close producer", e); } catch (DOMDataTreeShardCreationFailedException e) { @@ -448,7 +429,7 @@ public class DistributedShardedDOMDataTree implements DOMDataTreeService, DOMDat Futures.addCallback(future, new FutureCallback() { @Override - public void onSuccess(@Nullable final Void result) { + public void onSuccess(final Void result) { LOG.debug("{} - Succesfuly removed shard for {}", memberName, prefix); } @@ -470,13 +451,10 @@ public class DistributedShardedDOMDataTree implements DOMDataTreeService, DOMDat return shardedDOMDataTree.createProducer(prefix); } - @Nonnull @Override public ListenerRegistration registerDataTreeShard( - @Nonnull final DOMDataTreeIdentifier prefix, - @Nonnull final T shard, - @Nonnull final DOMDataTreeProducer producer) - throws DOMDataTreeShardingConflictException { + final DOMDataTreeIdentifier prefix, final T shard, final DOMDataTreeProducer producer) + throws DOMDataTreeShardingConflictException { LOG.debug("Registering shard[{}] at prefix: {}", shard, prefix); @@ -488,13 +466,12 @@ public class DistributedShardedDOMDataTree implements DOMDataTreeService, DOMDat } @SuppressWarnings("checkstyle:IllegalCatch") - private Entry createDatastoreClient( - final String shardName, final ActorContext actorContext) + private Entry createDatastoreClient(final String shardName, final ActorUtils actorUtils) throws DOMDataTreeShardCreationFailedException { LOG.debug("{}: Creating distributed datastore client for shard {}", memberName, shardName); final Props distributedDataStoreClientProps = - SimpleDataStoreClientActor.props(memberName, "Shard-" + shardName, actorContext, shardName); + SimpleDataStoreClientActor.props(memberName, "Shard-" + shardName, actorUtils, shardName); final ActorRef clientActor = actorSystem.actorOf(distributedDataStoreClientProps); try { @@ -509,8 +486,7 @@ public class DistributedShardedDOMDataTree implements DOMDataTreeService, DOMDat } @SuppressWarnings("checkstyle:IllegalCatch") - private void initDefaultShard(final LogicalDatastoreType logicalDatastoreType) - throws ExecutionException, InterruptedException { + private void initDefaultShard(final LogicalDatastoreType logicalDatastoreType) { final PrefixedShardConfigWriter writer = writerMap.get(logicalDatastoreType); @@ -524,11 +500,11 @@ public class DistributedShardedDOMDataTree implements DOMDataTreeService, DOMDat // TODO we don't have to do it for config and operational default shard separately. Just one of them // should be enough - final ActorContext actorContext = logicalDatastoreType == LogicalDatastoreType.CONFIGURATION - ? distributedConfigDatastore.getActorContext() : distributedOperDatastore.getActorContext(); + final ActorUtils actorUtils = logicalDatastoreType == LogicalDatastoreType.CONFIGURATION + ? distributedConfigDatastore.getActorUtils() : distributedOperDatastore.getActorUtils(); final Optional defaultLocalShardOptional = - actorContext.findLocalShard(ClusterUtils.getCleanShardName(YangInstanceIdentifier.EMPTY)); + actorUtils.findLocalShard(ClusterUtils.getCleanShardName(YangInstanceIdentifier.EMPTY)); if (defaultLocalShardOptional.isPresent()) { LOG.debug("{}: Default shard for {} is already started, creating just frontend", memberName, @@ -542,7 +518,7 @@ public class DistributedShardedDOMDataTree implements DOMDataTreeService, DOMDat // the default shard as a prefix shard is problematic in this scenario so it is commented out. Since // the default shard is a module-based shard by default, it makes sense to always treat it as such, // ie bootstrap it in the same manner as the special prefix-configuration and EOS shards. -// final Collection names = distributedConfigDatastore.getActorContext().getConfiguration() +// final Collection names = distributedConfigDatastore.getActorUtils().getConfiguration() // .getUniqueMemberNamesForAllShards(); // Await.result(FutureConverters.toScala(createDistributedShard( // new DOMDataTreeIdentifier(logicalDatastoreType, YangInstanceIdentifier.EMPTY), names)), @@ -633,7 +609,7 @@ public class DistributedShardedDOMDataTree implements DOMDataTreeService, DOMDat private final DOMDataTreeProducer delegate; private final Collection subtrees; private final ActorRef shardDataTreeActor; - private final ActorContext actorContext; + private final ActorUtils actorUtils; @GuardedBy("shardAccessMap") private final Map shardAccessMap = new HashMap<>(); @@ -645,25 +621,23 @@ public class DistributedShardedDOMDataTree implements DOMDataTreeService, DOMDat ProxyProducer(final DOMDataTreeProducer delegate, final Collection subtrees, final ActorRef shardDataTreeActor, - final ActorContext actorContext, + final ActorUtils actorUtils, final DOMDataTreePrefixTable> 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); + this.delegate = requireNonNull(delegate); + this.subtrees = requireNonNull(subtrees); + this.shardDataTreeActor = requireNonNull(shardDataTreeActor); + this.actorUtils = requireNonNull(actorUtils); + this.shardTable = requireNonNull(shardLayout); } - @Nonnull @Override public DOMDataTreeCursorAwareTransaction createTransaction(final boolean isolated) { return delegate.createTransaction(isolated); } - @Nonnull @Override @SuppressWarnings("checkstyle:hiddenField") - public DOMDataTreeProducer createProducer(@Nonnull final Collection subtrees) { + public DOMDataTreeProducer createProducer(final Collection subtrees) { // TODO we probably don't need to distribute this on the remote nodes since once we have this producer // open we surely have the rights to all the subtrees. return delegate.createProducer(subtrees); @@ -678,7 +652,7 @@ public class DistributedShardedDOMDataTree implements DOMDataTreeService, DOMDat shardAccessMap.values().forEach(CDSShardAccessImpl::close); } - final Object o = actorContext.executeOperation(shardDataTreeActor, new ProducerRemoved(subtrees)); + final Object o = actorUtils.executeOperation(shardDataTreeActor, new ProducerRemoved(subtrees)); if (o instanceof DOMDataTreeProducerException) { throw (DOMDataTreeProducerException) o; } else if (o instanceof Throwable) { @@ -691,16 +665,14 @@ public class DistributedShardedDOMDataTree implements DOMDataTreeService, DOMDat return delegate; } - @Nonnull @Override - public CDSShardAccess getShardAccess(@Nonnull final DOMDataTreeIdentifier subtree) { - Preconditions.checkArgument( - subtrees.stream().anyMatch(dataTreeIdentifier -> dataTreeIdentifier.contains(subtree)), - "Subtree %s is not controlled by this producer %s", subtree, this); + public CDSShardAccess getShardAccess(final DOMDataTreeIdentifier subtree) { + checkArgument(subtrees.stream().anyMatch(dataTreeIdentifier -> dataTreeIdentifier.contains(subtree)), + "Subtree %s is not controlled by this producer %s", subtree, this); final DOMDataTreePrefixTableEntry> lookup = shardTable.lookup(subtree); - Preconditions.checkState(lookup != null, "Subtree %s is not contained in any registered shard.", subtree); + checkState(lookup != null, "Subtree %s is not contained in any registered shard.", subtree); final DOMDataTreeIdentifier lookupId = lookup.getValue().getPrefix(); @@ -713,7 +685,7 @@ public class DistributedShardedDOMDataTree implements DOMDataTreeService, DOMDat // 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(lookupId, actorContext); + final CDSShardAccessImpl shardAccess = new CDSShardAccessImpl(lookupId, actorUtils); shardAccessMap.put(lookupId, shardAccess); return shardAccess; }