From d3358dde361ed5bd94466ded15dac0bba45afe9c Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Wed, 23 Mar 2016 16:06:13 +0100 Subject: [PATCH] Split out AbstractBuilder and rename it This is a DTO-like class, not really a Builder with .build() method. Split it out of its enclosing class and rename it to AbstractShardManagerCreator. Change-Id: I891fc685c2d9776384294a708336b32eb34011b5 Signed-off-by: Robert Varga --- .../datastore/DistributedDataStore.java | 10 +- .../AbstractShardManagerCreator.java | 114 ++++++++++++++++++ .../datastore/shardmanager/ShardManager.java | 97 ++------------- .../shardmanager/ShardManagerCreator.java | 12 ++ .../shardmanager/ShardManagerTest.java | 18 +-- 5 files changed, 149 insertions(+), 102 deletions(-) create mode 100644 opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/shardmanager/AbstractShardManagerCreator.java create mode 100644 opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/shardmanager/ShardManagerCreator.java diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/DistributedDataStore.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/DistributedDataStore.java index 3dc4562acb..198c3b2eda 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/DistributedDataStore.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/DistributedDataStore.java @@ -20,7 +20,7 @@ import org.opendaylight.controller.cluster.datastore.identifiers.ShardManagerIde import org.opendaylight.controller.cluster.datastore.jmx.mbeans.DatastoreConfigurationMXBeanImpl; import org.opendaylight.controller.cluster.datastore.jmx.mbeans.DatastoreInfoMXBeanImpl; import org.opendaylight.controller.cluster.datastore.messages.DatastoreSnapshot; -import org.opendaylight.controller.cluster.datastore.shardmanager.ShardManager; +import org.opendaylight.controller.cluster.datastore.shardmanager.ShardManagerCreator; import org.opendaylight.controller.cluster.datastore.utils.ActorContext; import org.opendaylight.controller.cluster.datastore.utils.Dispatchers; import org.opendaylight.controller.cluster.datastore.utils.PrimaryShardInfoFutureCache; @@ -86,11 +86,11 @@ public class DistributedDataStore implements DOMStore, SchemaContextListener, PrimaryShardInfoFutureCache primaryShardInfoCache = new PrimaryShardInfoFutureCache(); - ShardManager.Builder builder = ShardManager.builder().cluster(cluster).configuration(configuration). + ShardManagerCreator creator = new ShardManagerCreator().cluster(cluster).configuration(configuration). datastoreContextFactory(datastoreContextFactory).waitTillReadyCountdownLatch(waitTillReadyCountDownLatch). primaryShardInfoCache(primaryShardInfoCache).restoreFromSnapshot(restoreFromSnapshot); - actorContext = new ActorContext(actorSystem, createShardManager(actorSystem, builder, shardDispatcher, + actorContext = new ActorContext(actorSystem, createShardManager(actorSystem, creator, shardDispatcher, shardManagerId), cluster, configuration, datastoreContextFactory.getBaseDatastoreContext(), primaryShardInfoCache); this.waitTillReadyTimeInMillis = @@ -233,13 +233,13 @@ public class DistributedDataStore implements DOMStore, SchemaContextListener, } } - private static ActorRef createShardManager(ActorSystem actorSystem, ShardManager.Builder builder, + private static ActorRef createShardManager(ActorSystem actorSystem, ShardManagerCreator creator, String shardDispatcher, String shardManagerId) { Exception lastException = null; for(int i=0;i<100;i++) { try { - return actorSystem.actorOf(builder.props().withDispatcher(shardDispatcher).withMailbox( + return actorSystem.actorOf(creator.props().withDispatcher(shardDispatcher).withMailbox( ActorContext.BOUNDED_MAILBOX), shardManagerId); } catch (Exception e){ lastException = e; diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/shardmanager/AbstractShardManagerCreator.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/shardmanager/AbstractShardManagerCreator.java new file mode 100644 index 0000000000..a5804aa7b8 --- /dev/null +++ b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/shardmanager/AbstractShardManagerCreator.java @@ -0,0 +1,114 @@ +/* + * Copyright (c) 2014 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, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.controller.cluster.datastore.shardmanager; + +import akka.actor.Props; +import com.google.common.base.Preconditions; +import java.util.concurrent.CountDownLatch; +import org.opendaylight.controller.cluster.datastore.ClusterWrapper; +import org.opendaylight.controller.cluster.datastore.DatastoreContextFactory; +import org.opendaylight.controller.cluster.datastore.config.Configuration; +import org.opendaylight.controller.cluster.datastore.messages.DatastoreSnapshot; +import org.opendaylight.controller.cluster.datastore.utils.PrimaryShardInfoFutureCache; + +public abstract class AbstractShardManagerCreator> { + private ClusterWrapper cluster; + private Configuration configuration; + private DatastoreContextFactory datastoreContextFactory; + private CountDownLatch waitTillReadyCountdownLatch; + private PrimaryShardInfoFutureCache primaryShardInfoCache; + private DatastoreSnapshot restoreFromSnapshot; + private volatile boolean sealed; + + AbstractShardManagerCreator() { + // Prevent outside instantiation + } + + @SuppressWarnings("unchecked") + private T self() { + return (T) this; + } + + protected final void checkSealed() { + Preconditions.checkState(!sealed, "Builder is already sealed - further modifications are not allowed"); + } + + ClusterWrapper getCluster() { + return cluster; + } + + public T cluster(ClusterWrapper cluster) { + checkSealed(); + this.cluster = cluster; + return self(); + } + + Configuration getConfiguration() { + return configuration; + } + + public T configuration(Configuration configuration) { + checkSealed(); + this.configuration = configuration; + return self(); + } + + DatastoreContextFactory getDdatastoreContextFactory() { + return datastoreContextFactory; + } + + public T datastoreContextFactory(DatastoreContextFactory datastoreContextFactory) { + checkSealed(); + this.datastoreContextFactory = datastoreContextFactory; + return self(); + } + + CountDownLatch getWaitTillReadyCountdownLatch() { + return waitTillReadyCountdownLatch; + } + + public T waitTillReadyCountdownLatch(CountDownLatch waitTillReadyCountdownLatch) { + checkSealed(); + this.waitTillReadyCountdownLatch = waitTillReadyCountdownLatch; + return self(); + } + + PrimaryShardInfoFutureCache getPrimaryShardInfoCache() { + return primaryShardInfoCache; + } + + public T primaryShardInfoCache(PrimaryShardInfoFutureCache primaryShardInfoCache) { + checkSealed(); + this.primaryShardInfoCache = primaryShardInfoCache; + return self(); + } + + DatastoreSnapshot getRestoreFromSnapshot() { + return restoreFromSnapshot; + } + + public T restoreFromSnapshot(DatastoreSnapshot restoreFromSnapshot) { + checkSealed(); + this.restoreFromSnapshot = restoreFromSnapshot; + return self(); + } + + protected void verify() { + sealed = true; + Preconditions.checkNotNull(cluster, "cluster should not be null"); + Preconditions.checkNotNull(configuration, "configuration should not be null"); + Preconditions.checkNotNull(datastoreContextFactory, "datastoreContextFactory should not be null"); + Preconditions.checkNotNull(waitTillReadyCountdownLatch, "waitTillReadyCountdownLatch should not be null"); + Preconditions.checkNotNull(primaryShardInfoCache, "primaryShardInfoCache should not be null"); + } + + public Props props() { + verify(); + return Props.create(ShardManager.class, this); + } +} \ No newline at end of file diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/shardmanager/ShardManager.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/shardmanager/ShardManager.java index 197ae827b0..9bf023d211 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/shardmanager/ShardManager.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/shardmanager/ShardManager.java @@ -117,7 +117,7 @@ import scala.concurrent.duration.FiniteDuration; *
  • Monitor the cluster members and store their addresses *
      */ -public class ShardManager extends AbstractUntypedPersistentActorWithMetering { +class ShardManager extends AbstractUntypedPersistentActorWithMetering { private static final Logger LOG = LoggerFactory.getLogger(ShardManager.class); @@ -156,19 +156,16 @@ public class ShardManager extends AbstractUntypedPersistentActorWithMetering { private final String persistenceId; - /** - */ - protected ShardManager(AbstractBuilder builder) { - - this.cluster = builder.cluster; - this.configuration = builder.configuration; - this.datastoreContextFactory = builder.datastoreContextFactory; - this.type = builder.datastoreContextFactory.getBaseDatastoreContext().getDataStoreName(); + ShardManager(AbstractShardManagerCreator builder) { + this.cluster = builder.getCluster(); + this.configuration = builder.getConfiguration(); + this.datastoreContextFactory = builder.getDdatastoreContextFactory(); + this.type = datastoreContextFactory.getBaseDatastoreContext().getDataStoreName(); this.shardDispatcherPath = new Dispatchers(context().system().dispatchers()).getDispatcherPath(Dispatchers.DispatcherType.Shard); - this.waitTillReadyCountdownLatch = builder.waitTillReadyCountdownLatch; - this.primaryShardInfoCache = builder.primaryShardInfoCache; - this.restoreFromSnapshot = builder.restoreFromSnapshot; + this.waitTillReadyCountdownLatch = builder.getWaitTillReadyCountdownLatch(); + this.primaryShardInfoCache = builder.getPrimaryShardInfoCache(); + this.restoreFromSnapshot = builder.getRestoreFromSnapshot(); String possiblePersistenceId = datastoreContextFactory.getBaseDatastoreContext().getShardManagerPersistenceId(); persistenceId = possiblePersistenceId != null ? possiblePersistenceId : "shard-manager-" + type; @@ -1592,82 +1589,6 @@ public class ShardManager extends AbstractUntypedPersistentActorWithMetering { } } - public static Builder builder() { - return new Builder(); - } - - public static abstract class AbstractBuilder> { - private ClusterWrapper cluster; - private Configuration configuration; - private DatastoreContextFactory datastoreContextFactory; - private CountDownLatch waitTillReadyCountdownLatch; - private PrimaryShardInfoFutureCache primaryShardInfoCache; - private DatastoreSnapshot restoreFromSnapshot; - private volatile boolean sealed; - - @SuppressWarnings("unchecked") - private T self() { - return (T) this; - } - - protected void checkSealed() { - Preconditions.checkState(!sealed, "Builder is already sealed - further modifications are not allowed"); - } - - public T cluster(ClusterWrapper cluster) { - checkSealed(); - this.cluster = cluster; - return self(); - } - - public T configuration(Configuration configuration) { - checkSealed(); - this.configuration = configuration; - return self(); - } - - public T datastoreContextFactory(DatastoreContextFactory datastoreContextFactory) { - checkSealed(); - this.datastoreContextFactory = datastoreContextFactory; - return self(); - } - - public T waitTillReadyCountdownLatch(CountDownLatch waitTillReadyCountdownLatch) { - checkSealed(); - this.waitTillReadyCountdownLatch = waitTillReadyCountdownLatch; - return self(); - } - - public T primaryShardInfoCache(PrimaryShardInfoFutureCache primaryShardInfoCache) { - checkSealed(); - this.primaryShardInfoCache = primaryShardInfoCache; - return self(); - } - - public T restoreFromSnapshot(DatastoreSnapshot restoreFromSnapshot) { - checkSealed(); - this.restoreFromSnapshot = restoreFromSnapshot; - return self(); - } - - protected void verify() { - sealed = true; - Preconditions.checkNotNull(cluster, "cluster should not be null"); - Preconditions.checkNotNull(configuration, "configuration should not be null"); - Preconditions.checkNotNull(datastoreContextFactory, "datastoreContextFactory should not be null"); - Preconditions.checkNotNull(waitTillReadyCountdownLatch, "waitTillReadyCountdownLatch should not be null"); - Preconditions.checkNotNull(primaryShardInfoCache, "primaryShardInfoCache should not be null"); - } - - public Props props() { - verify(); - return Props.create(ShardManager.class, this); - } - } - - public static class Builder extends AbstractBuilder { - } - private void findPrimary(final String shardName, final FindPrimaryResponseHandler handler) { Timeout findPrimaryTimeout = new Timeout(datastoreContextFactory.getBaseDatastoreContext(). getShardInitializationTimeout().duration().$times(2)); diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/shardmanager/ShardManagerCreator.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/shardmanager/ShardManagerCreator.java new file mode 100644 index 0000000000..308f06ad6c --- /dev/null +++ b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/shardmanager/ShardManagerCreator.java @@ -0,0 +1,12 @@ +/* + * Copyright (c) 2016 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, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.controller.cluster.datastore.shardmanager; + +public final class ShardManagerCreator extends AbstractShardManagerCreator { + +} diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/shardmanager/ShardManagerTest.java b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/shardmanager/ShardManagerTest.java index 39158e0c18..8883a5e10b 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/shardmanager/ShardManagerTest.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/shardmanager/ShardManagerTest.java @@ -312,8 +312,8 @@ public class ShardManagerTest extends AbstractActorTest { final PrimaryShardInfoFutureCache primaryShardInfoCache = new PrimaryShardInfoFutureCache(); final CountDownLatch newShardActorLatch = new CountDownLatch(2); class LocalShardManager extends ShardManager { - public LocalShardManager(AbstractBuilder builder) { - super(builder); + public LocalShardManager(AbstractShardManagerCreator creator) { + super(creator); } @Override @@ -334,7 +334,7 @@ public class ShardManagerTest extends AbstractActorTest { private static final long serialVersionUID = 1L; @Override public ShardManager create() throws Exception { - return new LocalShardManager(new GenericBuilder(LocalShardManager.class). + return new LocalShardManager(new GenericCreator(LocalShardManager.class). datastoreContextFactory(mockFactory).primaryShardInfoCache(primaryShardInfoCache). configuration(mockConfig)); } @@ -2085,7 +2085,7 @@ public class ShardManagerTest extends AbstractActorTest { return new Builder(datastoreContextBuilder); } - private static class Builder extends AbstractGenericBuilder { + private static class Builder extends AbstractGenericCreator { private ActorRef shardActor; private final Map shardActors = new HashMap<>(); @@ -2132,11 +2132,11 @@ public class ShardManagerTest extends AbstractActorTest { } } - private static abstract class AbstractGenericBuilder, C extends ShardManager> - extends ShardManager.AbstractBuilder { + private static abstract class AbstractGenericCreator, C extends ShardManager> + extends AbstractShardManagerCreator { private final Class shardManagerClass; - AbstractGenericBuilder(Class shardManagerClass) { + AbstractGenericCreator(Class shardManagerClass) { this.shardManagerClass = shardManagerClass; cluster(new MockClusterWrapper()).configuration(new MockConfiguration()). waitTillReadyCountdownLatch(ready).primaryShardInfoCache(new PrimaryShardInfoFutureCache()); @@ -2149,8 +2149,8 @@ public class ShardManagerTest extends AbstractActorTest { } } - private static class GenericBuilder extends AbstractGenericBuilder, C> { - GenericBuilder(Class shardManagerClass) { + private static class GenericCreator extends AbstractGenericCreator, C> { + GenericCreator(Class shardManagerClass) { super(shardManagerClass); } } -- 2.36.6