+++ /dev/null
-/*
- * Copyright (c) 2015 Brocade Communications 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;
-
-import akka.actor.Props;
-import java.util.Map;
-import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-
-/**
- * Implementation of ShardPropsCreator that creates a Props instance for the Shard class.
- *
- * @author Thomas Pantelis
- */
-public class DefaultShardPropsCreator implements ShardPropsCreator {
-
- @Override
- public Props newProps(ShardIdentifier shardId, Map<String, String> peerAddresses,
- DatastoreContext datastoreContext, SchemaContext schemaContext) {
- return Shard.props(shardId, peerAddresses, datastoreContext, schemaContext);
- }
-}
import akka.actor.ActorSelection;
import akka.actor.Cancellable;
import akka.actor.Props;
-import akka.japi.Creator;
import akka.persistence.RecoveryFailure;
import akka.serialization.Serialization;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import java.io.IOException;
-import java.util.HashMap;
+import java.util.Collections;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nonnull;
private final DataTreeChangeListenerSupport treeChangeSupport = new DataTreeChangeListenerSupport(this);
private final DataChangeListenerSupport changeSupport = new DataChangeListenerSupport(this);
- protected Shard(final ShardIdentifier name, final Map<String, String> peerAddresses,
- final DatastoreContext datastoreContext, final SchemaContext schemaContext) {
- super(name.toString(), new HashMap<>(peerAddresses), Optional.of(datastoreContext.getShardRaftConfig()),
- DataStoreVersions.CURRENT_VERSION);
+ protected Shard(AbstractBuilder<?, ?> builder) {
+ super(builder.getId().toString(), builder.getPeerAddresses(),
+ Optional.of(builder.getDatastoreContext().getShardRaftConfig()), DataStoreVersions.CURRENT_VERSION);
- this.name = name.toString();
- this.datastoreContext = datastoreContext;
+ this.name = builder.getId().toString();
+ this.datastoreContext = builder.getDatastoreContext();
setPersistence(datastoreContext.isPersistent());
LOG.info("Shard created : {}, persistent : {}", name, datastoreContext.isPersistent());
- store = new ShardDataTree(schemaContext);
+ store = new ShardDataTree(builder.getSchemaContext());
shardMBean = ShardMBeanFactory.getShardStatsMBean(name.toString(),
datastoreContext.getDataStoreMXBeanType());
datastoreContext.getShardTransactionCommitTimeoutInSeconds(), TimeUnit.SECONDS) / 2;
}
- public static Props props(final ShardIdentifier name, final Map<String, String> peerAddresses,
- final DatastoreContext datastoreContext, final SchemaContext schemaContext) {
- return Props.create(new ShardCreator(name, peerAddresses, datastoreContext, schemaContext));
- }
-
private Optional<ActorRef> createRoleChangeNotifier(String shardId) {
ActorRef shardRoleChangeNotifier = this.getContext().actorOf(
RoleChangeNotifier.getProps(shardId), shardId + "-notifier");
return datastoreContext;
}
- protected abstract static class AbstractShardCreator implements Creator<Shard> {
- private static final long serialVersionUID = 1L;
+ @VisibleForTesting
+ public ShardDataTree getDataStore() {
+ return store;
+ }
+
+ @VisibleForTesting
+ ShardStats getShardMBean() {
+ return shardMBean;
+ }
+
+ public static Builder builder() {
+ return new Builder();
+ }
- protected final ShardIdentifier name;
- protected final Map<String, String> peerAddresses;
- protected final DatastoreContext datastoreContext;
- protected final SchemaContext schemaContext;
+ public static abstract class AbstractBuilder<T extends AbstractBuilder<T, S>, S extends Shard> {
+ private final Class<S> shardClass;
+ private ShardIdentifier id;
+ private Map<String, String> peerAddresses = Collections.emptyMap();
+ private DatastoreContext datastoreContext;
+ private SchemaContext schemaContext;
+ private volatile boolean sealed;
- protected AbstractShardCreator(final ShardIdentifier name, final Map<String, String> peerAddresses,
- final DatastoreContext datastoreContext, final SchemaContext schemaContext) {
- this.name = Preconditions.checkNotNull(name, "name should not be null");
- this.peerAddresses = Preconditions.checkNotNull(peerAddresses, "peerAddresses should not be null");
- this.datastoreContext = Preconditions.checkNotNull(datastoreContext, "dataStoreContext should not be null");
- this.schemaContext = Preconditions.checkNotNull(schemaContext, "schemaContext should not be null");
+ protected AbstractBuilder(Class<S> shardClass) {
+ this.shardClass = shardClass;
}
- }
- private static class ShardCreator extends AbstractShardCreator {
- private static final long serialVersionUID = 1L;
+ protected void checkSealed() {
+ Preconditions.checkState(!sealed, "Builder isalready sealed - further modifications are not allowed");
+ }
- ShardCreator(final ShardIdentifier name, final Map<String, String> peerAddresses,
- final DatastoreContext datastoreContext, final SchemaContext schemaContext) {
- super(name, peerAddresses, datastoreContext, schemaContext);
+ @SuppressWarnings("unchecked")
+ private T self() {
+ return (T) this;
}
- @Override
- public Shard create() throws Exception {
- return new Shard(name, peerAddresses, datastoreContext, schemaContext);
+ public T id(ShardIdentifier id) {
+ checkSealed();
+ this.id = id;
+ return self();
}
- }
- @VisibleForTesting
- public ShardDataTree getDataStore() {
- return store;
+ public T peerAddresses(Map<String, String> peerAddresses) {
+ checkSealed();
+ this.peerAddresses = peerAddresses;
+ return self();
+ }
+
+ public T datastoreContext(DatastoreContext datastoreContext) {
+ checkSealed();
+ this.datastoreContext = datastoreContext;
+ return self();
+ }
+
+ public T schemaContext(SchemaContext schemaContext) {
+ checkSealed();
+ this.schemaContext = schemaContext;
+ return self();
+ }
+
+ public ShardIdentifier getId() {
+ return id;
+ }
+
+ public Map<String, String> getPeerAddresses() {
+ return peerAddresses;
+ }
+
+ public DatastoreContext getDatastoreContext() {
+ return datastoreContext;
+ }
+
+ public SchemaContext getSchemaContext() {
+ return schemaContext;
+ }
+
+ protected void verify() {
+ Preconditions.checkNotNull(id, "id should not be null");
+ Preconditions.checkNotNull(peerAddresses, "peerAddresses should not be null");
+ Preconditions.checkNotNull(datastoreContext, "dataStoreContext should not be null");
+ Preconditions.checkNotNull(schemaContext, "schemaContext should not be null");
+ }
+
+ public Props props() {
+ sealed = true;
+ verify();
+ return Props.create(shardClass, this);
+ }
}
- @VisibleForTesting
- ShardStats getShardMBean() {
- return shardMBean;
+ public static class Builder extends AbstractBuilder<Builder, Shard> {
+ private Builder() {
+ super(Shard.class);
+ }
}
}
}
ShardInformation info = new ShardInformation(moduleShardConfig.getShardName(), shardId, peerAddresses,
- shardDatastoreContext, createShard.getShardPropsCreator(), peerAddressResolver);
+ shardDatastoreContext, createShard.getShardBuilder(), peerAddressResolver);
localShards.put(info.getShardName(), info);
mBean.addLocalShard(shardId.toString());
String memberName = this.cluster.getCurrentMemberName();
Collection<String> memberShardNames = this.configuration.getMemberShardNames(memberName);
- ShardPropsCreator shardPropsCreator = new DefaultShardPropsCreator();
List<String> localShardActorNames = new ArrayList<>();
for(String shardName : memberShardNames){
ShardIdentifier shardId = getShardIdentifier(memberName, shardName);
Map<String, String> peerAddresses = getPeerAddresses(shardName);
localShardActorNames.add(shardId.toString());
localShards.put(shardName, new ShardInformation(shardName, shardId, peerAddresses,
- newShardDatastoreContext(shardName), shardPropsCreator, peerAddressResolver));
+ newShardDatastoreContext(shardName), Shard.builder(), peerAddressResolver));
}
mBean = ShardManagerInfo.createShardManagerMBean(memberName, "shard-manager-" + this.type,
final ShardInformation shardInfo = new ShardInformation(shardName, shardId,
getPeerAddresses(shardName), datastoreContext,
- new DefaultShardPropsCreator(), peerAddressResolver);
+ Shard.builder(), peerAddressResolver);
localShards.put(shardName, shardInfo);
shardInfo.setActor(newShardActor(schemaContext, shardInfo));
private short leaderVersion;
private DatastoreContext datastoreContext;
- private final ShardPropsCreator shardPropsCreator;
+ private final Shard.AbstractBuilder<?, ?> builder;
private final ShardPeerAddressResolver addressResolver;
private ShardInformation(String shardName, ShardIdentifier shardId,
Map<String, String> initialPeerAddresses, DatastoreContext datastoreContext,
- ShardPropsCreator shardPropsCreator, ShardPeerAddressResolver addressResolver) {
+ Shard.AbstractBuilder<?, ?> builder, ShardPeerAddressResolver addressResolver) {
this.shardName = shardName;
this.shardId = shardId;
this.initialPeerAddresses = initialPeerAddresses;
this.datastoreContext = datastoreContext;
- this.shardPropsCreator = shardPropsCreator;
+ this.builder = builder;
this.addressResolver = addressResolver;
}
Props newProps(SchemaContext schemaContext) {
- return shardPropsCreator.newProps(shardId, initialPeerAddresses, datastoreContext, schemaContext);
+ return builder.id(shardId).peerAddresses(initialPeerAddresses).datastoreContext(datastoreContext).
+ schemaContext(schemaContext).props();
}
String getShardName() {
+++ /dev/null
-/*
- * Copyright (c) 2015 Brocade Communications 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;
-
-import akka.actor.Props;
-import java.util.Map;
-import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-
-/**
- * An interface for creating a Shard actor Props instance.
- *
- * @author Thomas Pantelis
- */
-public interface ShardPropsCreator {
-
- Props newProps(ShardIdentifier shardId, Map<String, String> peerAddresses, DatastoreContext datastoreContext,
- SchemaContext schemaContext);
-}
Collection<String> entityOwnersMemberNames = configuration.getUniqueMemberNamesForAllShards();
CreateShard createShard = new CreateShard(new ModuleShardConfiguration(EntityOwners.QNAME.getNamespace(),
"entity-owners", ENTITY_OWNERSHIP_SHARD_NAME, ModuleShardStrategy.NAME, entityOwnersMemberNames),
- newShardPropsCreator(), null);
+ newShardBuilder(), null);
Future<Object> createFuture = datastore.getActorContext().executeOperationAsync(shardManagerActor,
createShard, MESSAGE_TIMEOUT);
public void close() {
}
- protected EntityOwnershipShardPropsCreator newShardPropsCreator() {
- return new EntityOwnershipShardPropsCreator(datastore.getActorContext().getCurrentMemberName());
+ protected EntityOwnershipShard.Builder newShardBuilder() {
+ return EntityOwnershipShard.newBuilder().localMemberName(datastore.getActorContext().getCurrentMemberName());
}
@VisibleForTesting
import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.entityOwnersWithCandidate;
import akka.actor.ActorRef;
import akka.actor.ActorSelection;
-import akka.actor.Props;
import akka.pattern.Patterns;
import com.google.common.base.Optional;
+import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import java.util.ArrayList;
import java.util.Collection;
import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
import scala.concurrent.Future;
/**
return DatastoreContext.newBuilderFrom(datastoreContext).persistent(false).build();
}
- protected EntityOwnershipShard(ShardIdentifier name, Map<String, String> peerAddresses,
- DatastoreContext datastoreContext, SchemaContext schemaContext, String localMemberName) {
- super(name, peerAddresses, noPersistenceDatastoreContext(datastoreContext), schemaContext);
- this.localMemberName = localMemberName;
- this.commitCoordinator = new EntityOwnershipShardCommitCoordinator(localMemberName, LOG);
+ protected EntityOwnershipShard(Builder builder) {
+ super(builder);
+ this.localMemberName = builder.localMemberName;
+ this.commitCoordinator = new EntityOwnershipShardCommitCoordinator(builder.localMemberName, LOG);
this.listenerSupport = new EntityOwnershipListenerSupport(getContext(), persistenceId());
- for(String peerId: peerAddresses.keySet()) {
+ for(String peerId: getRaftActorContext().getPeerIds()) {
ShardIdentifier shardId = ShardIdentifier.builder().fromShardIdString(peerId).build();
peerIdToMemberNames.put(peerId, shardId.getMemberName());
}
return null;
}
- public static Props props(final ShardIdentifier name, final Map<String, String> peerAddresses,
- final DatastoreContext datastoreContext, final SchemaContext schemaContext, final String localMemberName) {
- return Props.create(new Creator(name, peerAddresses, datastoreContext, schemaContext, localMemberName));
+ private static interface EntityWalker {
+ void onEntity(MapEntryNode entityTypeNode, MapEntryNode entityNode);
}
- private static class Creator extends AbstractShardCreator {
- private static final long serialVersionUID = 1L;
+ public static Builder newBuilder() {
+ return new Builder();
+ }
- private final String localMemberName;
+ static class Builder extends Shard.AbstractBuilder<Builder, EntityOwnershipShard> {
+ private String localMemberName;
+
+ protected Builder() {
+ super(EntityOwnershipShard.class);
+ }
- Creator(final ShardIdentifier name, final Map<String, String> peerAddresses,
- final DatastoreContext datastoreContext, final SchemaContext schemaContext,
- final String localMemberName) {
- super(name, peerAddresses, datastoreContext, schemaContext);
+ Builder localMemberName(String localMemberName) {
+ checkSealed();
this.localMemberName = localMemberName;
+ return this;
}
@Override
- public Shard create() throws Exception {
- return new EntityOwnershipShard(name, peerAddresses, datastoreContext, schemaContext, localMemberName);
+ protected void verify() {
+ super.verify();
+ Preconditions.checkNotNull(localMemberName, "localMemberName should not be null");
}
}
-
- private static interface EntityWalker {
- void onEntity(MapEntryNode entityTypeNode, MapEntryNode entityNode);
- }
}
+++ /dev/null
-/*
- * Copyright (c) 2015 Brocade Communications 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.entityownership;
-
-import akka.actor.Props;
-import java.util.Map;
-import org.opendaylight.controller.cluster.datastore.DatastoreContext;
-import org.opendaylight.controller.cluster.datastore.ShardPropsCreator;
-import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-
-/**
- * Implementation of ShardPropsCreator that creates a Props instance for the EntityOwnershipShard class.
- *
- * @author Thomas Pantelis
- */
-class EntityOwnershipShardPropsCreator implements ShardPropsCreator {
- private final String localMemberName;
-
- EntityOwnershipShardPropsCreator(String localMemberName) {
- this.localMemberName = localMemberName;
- }
-
- @Override
- public Props newProps(ShardIdentifier shardId, Map<String, String> peerAddresses,
- DatastoreContext datastoreContext, SchemaContext schemaContext) {
- return EntityOwnershipShard.props(shardId, peerAddresses, datastoreContext, schemaContext, localMemberName);
- }
-}
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.opendaylight.controller.cluster.datastore.DatastoreContext;
-import org.opendaylight.controller.cluster.datastore.ShardPropsCreator;
+import org.opendaylight.controller.cluster.datastore.Shard;
import org.opendaylight.controller.cluster.datastore.config.ModuleShardConfiguration;
/**
*/
public class CreateShard {
private final ModuleShardConfiguration moduleShardConfig;
- private final ShardPropsCreator shardPropsCreator;
+ private final Shard.AbstractBuilder<?, ?> shardBuilder;
private final DatastoreContext datastoreContext;
/**
* @param datastoreContext the DatastoreContext for the new shard. If null, the default is used.
*/
public CreateShard(@Nonnull ModuleShardConfiguration moduleShardConfig,
- @Nonnull ShardPropsCreator shardPropsCreator, @Nullable DatastoreContext datastoreContext) {
+ @Nonnull Shard.AbstractBuilder<?, ?> shardBuilder, @Nullable DatastoreContext datastoreContext) {
this.moduleShardConfig = Preconditions.checkNotNull(moduleShardConfig);
- this.shardPropsCreator = Preconditions.checkNotNull(shardPropsCreator);
+ this.shardBuilder = Preconditions.checkNotNull(shardBuilder);
this.datastoreContext = datastoreContext;
}
- @Nonnull public ModuleShardConfiguration getModuleShardConfig() {
+ @Nonnull
+ public ModuleShardConfiguration getModuleShardConfig() {
return moduleShardConfig;
}
- @Nonnull public ShardPropsCreator getShardPropsCreator() {
- return shardPropsCreator;
+ @Nonnull
+ public Shard.AbstractBuilder<?, ?> getShardBuilder() {
+ return shardBuilder;
}
- @Nullable public DatastoreContext getDatastoreContext() {
+ @Nullable
+ public DatastoreContext getDatastoreContext() {
return datastoreContext;
}
@Override
public String toString() {
- return "CreateShard [moduleShardConfig=" + moduleShardConfig + ", shardPropsCreator=" + shardPropsCreator + "]";
+ return "CreateShard [moduleShardConfig=" + moduleShardConfig + ", shardPropsCreator=" + shardBuilder + "]";
}
}
import com.google.common.base.Optional;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.Uninterruptibles;
-import java.util.Collections;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
}
protected Props newShardProps() {
- return Shard.props(shardID, Collections.<String,String>emptyMap(),
- newDatastoreContext(), SCHEMA_CONTEXT);
+ return newShardBuilder().props();
+ }
+
+ protected Shard.Builder newShardBuilder() {
+ return Shard.builder().id(shardID).datastoreContext(newDatastoreContext()).schemaContext(SCHEMA_CONTEXT);
}
protected void testRecovery(final Set<Integer> listEntryKeys) throws Exception {
Creator<Shard> creator = new Creator<Shard>() {
@Override
public Shard create() throws Exception {
- return new Shard(shardID, Collections.<String,String>emptyMap(),
- newDatastoreContext(), SCHEMA_CONTEXT) {
+ return new Shard(newShardBuilder()) {
@Override
protected void onRecoveryComplete() {
try {
DatastoreContext datastoreContext = DatastoreContext.newBuilder().shardElectionTimeoutFactor(100).
persistent(false).build();
- TestShardPropsCreator shardPropsCreator = new TestShardPropsCreator();
+ Shard.Builder shardBuilder = Shard.builder();
ModuleShardConfiguration config = new ModuleShardConfiguration(URI.create("foo-ns"), "foo-module",
"foo", null, Arrays.asList("member-1", "member-5", "member-6"));
- shardManager.tell(new CreateShard(config, shardPropsCreator, datastoreContext), getRef());
+ shardManager.tell(new CreateShard(config, shardBuilder, datastoreContext), getRef());
expectMsgClass(duration("5 seconds"), CreateShardReply.class);
expectMsgClass(duration("5 seconds"), LocalShardFound.class);
- assertEquals("isRecoveryApplicable", false, shardPropsCreator.datastoreContext.isPersistent());
- assertTrue("Epxected ShardPeerAddressResolver", shardPropsCreator.datastoreContext.getShardRaftConfig().
+ assertEquals("isRecoveryApplicable", false, shardBuilder.getDatastoreContext().isPersistent());
+ assertTrue("Epxected ShardPeerAddressResolver", shardBuilder.getDatastoreContext().getShardRaftConfig().
getPeerAddressResolver() instanceof ShardPeerAddressResolver);
assertEquals("peerMembers", Sets.newHashSet(new ShardIdentifier("foo", "member-5", shardMrgIDSuffix).toString(),
new ShardIdentifier("foo", "member-6", shardMrgIDSuffix).toString()),
- shardPropsCreator.peerAddresses.keySet());
+ shardBuilder.getPeerAddresses().keySet());
assertEquals("ShardIdentifier", new ShardIdentifier("foo", "member-1", shardMrgIDSuffix),
- shardPropsCreator.shardId);
- assertSame("schemaContext", schemaContext, shardPropsCreator.schemaContext);
+ shardBuilder.getId());
+ assertSame("schemaContext", schemaContext, shardBuilder.getSchemaContext());
// Send CreateShard with same name - should fail.
- shardManager.tell(new CreateShard(config, shardPropsCreator, null), getRef());
+ shardManager.tell(new CreateShard(config, shardBuilder, null), getRef());
expectMsgClass(duration("5 seconds"), akka.actor.Status.Failure.class);
}};
ActorRef shardManager = getSystem().actorOf(newShardMgrProps(
new ConfigurationImpl(new EmptyModuleShardConfigProvider())));
- TestShardPropsCreator shardPropsCreator = new TestShardPropsCreator();
+ Shard.Builder shardBuilder = Shard.builder();
ModuleShardConfiguration config = new ModuleShardConfiguration(URI.create("foo-ns"), "foo-module",
"foo", null, Arrays.asList("member-1"));
- shardManager.tell(new CreateShard(config, shardPropsCreator, null), getRef());
+ shardManager.tell(new CreateShard(config, shardBuilder, null), getRef());
expectMsgClass(duration("5 seconds"), CreateShardReply.class);
expectMsgClass(duration("5 seconds"), LocalShardFound.class);
- assertSame("schemaContext", schemaContext, shardPropsCreator.schemaContext);
- assertNotNull("schemaContext is null", shardPropsCreator.datastoreContext);
+ assertSame("schemaContext", schemaContext, shardBuilder.getSchemaContext());
+ assertNotNull("schemaContext is null", shardBuilder.getDatastoreContext());
}};
}
}
- private static class TestShardPropsCreator implements ShardPropsCreator {
- ShardIdentifier shardId;
- Map<String, String> peerAddresses;
- SchemaContext schemaContext;
- DatastoreContext datastoreContext;
-
- @Override
- public Props newProps(ShardIdentifier shardId, Map<String, String> peerAddresses,
- DatastoreContext datastoreContext, SchemaContext schemaContext) {
- this.shardId = shardId;
- this.peerAddresses = peerAddresses;
- this.schemaContext = schemaContext;
- this.datastoreContext = datastoreContext;
- return Shard.props(shardId, peerAddresses, datastoreContext, schemaContext);
- }
-
- }
-
private static class TestShardManager extends ShardManager {
private final CountDownLatch recoveryComplete = new CountDownLatch(1);
import java.io.IOException;
import java.util.Collections;
import java.util.HashSet;
-import java.util.Map;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
import scala.concurrent.Await;
import scala.concurrent.Future;
import scala.concurrent.duration.FiniteDuration;
// this will cause all other messages to not be queued properly after that.
// The basic issue is that you cannot use TestActorRef with a persistent actor (at least when
// it does do a persist)
- return new Shard(shardID, Collections.<String,String>emptyMap(),
- dataStoreContextBuilder.persistent(false).build(), SCHEMA_CONTEXT) {
+ return new Shard(newShardBuilder()) {
@Override
public void onReceiveCommand(final Object message) throws Exception {
if(message instanceof ElectionTimeout && firstElectionTimeout) {
@Override
public Shard create() throws Exception {
- return new Shard(shardID, Collections.<String,String>emptyMap(),
- dataStoreContextBuilder.persistent(false).build(), SCHEMA_CONTEXT) {
+ return new Shard(Shard.builder().id(shardID).datastoreContext(
+ dataStoreContextBuilder.persistent(false).build()).schemaContext(SCHEMA_CONTEXT)) {
@Override
public void onReceiveCommand(final Object message) throws Exception {
if(message instanceof ElectionTimeout && firstElectionTimeout) {
final CountDownLatch recoveryComplete = new CountDownLatch(1);
class TestShard extends Shard {
TestShard() {
- super(shardID, Collections.<String, String>singletonMap(shardID.toString(), null),
- newDatastoreContext(), SCHEMA_CONTEXT);
+ super(Shard.builder().id(shardID).datastoreContext(newDatastoreContext()).
+ peerAddresses(Collections.<String, String>singletonMap(shardID.toString(), null)).
+ schemaContext(SCHEMA_CONTEXT));
}
String getPeerAddress(String id) {
@Override
public Shard create() throws Exception {
- return new Shard(shardID, Collections.<String,String>emptyMap(),
- newDatastoreContext(), SCHEMA_CONTEXT) {
+ return new Shard(newShardBuilder()) {
@Override
protected boolean isLeader() {
return overrideLeaderCalls.get() ? false : super.isLeader();
final Creator<Shard> creator = new Creator<Shard>() {
@Override
public Shard create() throws Exception {
- return new Shard(shardID, Collections.<String,String>emptyMap(),
- dataStoreContextBuilder.build(), SCHEMA_CONTEXT) {
+ return new Shard(newShardBuilder()) {
@Override
public void onReceiveCommand(final Object message) throws Exception {
super.onReceiveCommand(message);
new ShardTestKit(getSystem()) {{
class TestShard extends Shard {
- protected TestShard(final ShardIdentifier name, final Map<String, String> peerAddresses,
- final DatastoreContext datastoreContext, final SchemaContext schemaContext) {
- super(name, peerAddresses, datastoreContext, schemaContext);
+ protected TestShard(AbstractBuilder<?, ?> builder) {
+ super(builder);
setPersistence(new TestPersistentDataProvider(super.persistence()));
}
final Creator<Shard> creator = new Creator<Shard>() {
@Override
public Shard create() throws Exception {
- return new TestShard(shardID, Collections.<String,String>emptyMap(),
- newDatastoreContext(), SCHEMA_CONTEXT);
+ return new TestShard(newShardBuilder());
}
};
final DatastoreContext persistentContext = DatastoreContext.newBuilder().
shardJournalRecoveryLogBatchSize(3).shardSnapshotBatchCount(5000).persistent(true).build();
- final Props persistentProps = Shard.props(shardID, Collections.<String, String>emptyMap(),
- persistentContext, SCHEMA_CONTEXT);
+ final Props persistentProps = Shard.builder().id(shardID).datastoreContext(persistentContext).
+ schemaContext(SCHEMA_CONTEXT).props();
final DatastoreContext nonPersistentContext = DatastoreContext.newBuilder().
shardJournalRecoveryLogBatchSize(3).shardSnapshotBatchCount(5000).persistent(false).build();
- final Props nonPersistentProps = Shard.props(shardID, Collections.<String, String>emptyMap(),
- nonPersistentContext, SCHEMA_CONTEXT);
+ final Props nonPersistentProps = Shard.builder().id(shardID).datastoreContext(nonPersistentContext).
+ schemaContext(SCHEMA_CONTEXT).props();
new ShardTestKit(getSystem()) {{
final TestActorRef<Shard> shard1 = TestActorRef.create(getSystem(),
@Test
public void testClusteredDataChangeListernerDelayedRegistration() throws Exception {
new ShardTestKit(getSystem()) {{
+ dataStoreContextBuilder.persistent(false);
final CountDownLatch onFirstElectionTimeout = new CountDownLatch(1);
final CountDownLatch onChangeListenerRegistered = new CountDownLatch(1);
final Creator<Shard> creator = new Creator<Shard>() {
+ private static final long serialVersionUID = 1L;
boolean firstElectionTimeout = true;
@Override
public Shard create() throws Exception {
- return new Shard(shardID, Collections.<String,String>emptyMap(),
- dataStoreContextBuilder.persistent(false).build(), SCHEMA_CONTEXT) {
+ return new Shard(newShardBuilder()) {
@Override
public void onReceiveCommand(final Object message) throws Exception {
if(message instanceof ElectionTimeout && firstElectionTimeout) {
@Test
public void testClusteredDataChangeListernerRegistration() throws Exception {
+ dataStoreContextBuilder.persistent(false).build();
new ShardTestKit(getSystem()) {{
final ShardIdentifier member1ShardID = ShardIdentifier.builder().memberName("member-1")
.shardName("inventory").type("config").build();
final ShardIdentifier member2ShardID = ShardIdentifier.builder().memberName("member-2")
.shardName("inventory").type("config").build();
final Creator<Shard> followerShardCreator = new Creator<Shard>() {
+ private static final long serialVersionUID = 1L;
@Override
public Shard create() throws Exception {
- return new Shard(member1ShardID, Collections.singletonMap(member2ShardID.toString(),
- "akka://test/user/" + member2ShardID.toString()),
- dataStoreContextBuilder.persistent(false).build(), SCHEMA_CONTEXT) {
+ return new Shard(Shard.builder().id(member1ShardID).datastoreContext(newDatastoreContext()).
+ peerAddresses(Collections.singletonMap(member2ShardID.toString(),
+ "akka://test/user/" + member2ShardID.toString())).schemaContext(SCHEMA_CONTEXT)) {
@Override
public void onReceiveCommand(final Object message) throws Exception {
};
final Creator<Shard> leaderShardCreator = new Creator<Shard>() {
+ private static final long serialVersionUID = 1L;
@Override
public Shard create() throws Exception {
- return new Shard(member2ShardID, Collections.singletonMap(member1ShardID.toString(),
- "akka://test/user/" + member1ShardID.toString()),
- dataStoreContextBuilder.persistent(false).build(), SCHEMA_CONTEXT) { };
+ return new Shard(Shard.builder().id(member2ShardID).datastoreContext(newDatastoreContext()).
+ peerAddresses(Collections.singletonMap(member1ShardID.toString(),
+ "akka://test/user/" + member1ShardID.toString())).schemaContext(SCHEMA_CONTEXT)) {};
}
};
import akka.actor.Props;
import akka.pattern.AskTimeoutException;
import akka.testkit.TestActorRef;
-import java.util.Collections;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
private final ShardStats shardStats = new ShardStats(SHARD_IDENTIFIER.toString(), "DataStore");
private ActorRef createShard(){
- return getSystem().actorOf(Shard.props(SHARD_IDENTIFIER, Collections.<String, String>emptyMap(), datastoreContext,
- TestModel.createTestContext()));
+ return getSystem().actorOf(Shard.builder().id(SHARD_IDENTIFIER).datastoreContext(datastoreContext).
+ schemaContext(TestModel.createTestContext()).props());
}
@Test(expected = ReadFailedException.class)
import akka.actor.Terminated;
import akka.testkit.JavaTestKit;
import akka.testkit.TestActorRef;
-import java.util.Collections;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.mockito.InOrder;
private int txCounter = 0;
private ActorRef createShard() {
- return getSystem().actorOf(Shard.props(SHARD_IDENTIFIER,
- Collections.<String, String>emptyMap(), datastoreContext, TestModel.createTestContext()));
+ return getSystem().actorOf(Shard.builder().id(SHARD_IDENTIFIER).datastoreContext(datastoreContext).
+ schemaContext(TestModel.createTestContext()).props());
}
private ActorRef newTransactionActor(TransactionType type, AbstractShardDataTreeTransaction<?> transaction, String name) {
import akka.actor.Props;
import akka.dispatch.Dispatchers;
import akka.testkit.TestActorRef;
-import java.util.Collections;
import org.junit.Assert;
import org.junit.Test;
import org.opendaylight.controller.cluster.datastore.AbstractActorTest;
public void testTransactionCommit() throws Exception {
new ShardTestKit(getSystem()) {{
SchemaContext schemaContext = TestModel.createTestContext();
- Props shardProps = Shard.props(ShardIdentifier.builder().memberName("member-1").
- shardName("inventory").type("config").build(),
- Collections.<String,String>emptyMap(),
- DatastoreContext.newBuilder().shardHeartbeatIntervalInMillis(100).build(),
- schemaContext).withDispatcher(Dispatchers.DefaultDispatcherId());
+ Props shardProps = Shard.builder().id(ShardIdentifier.builder().memberName("member-1").
+ shardName("inventory").type("config").build()).datastoreContext(DatastoreContext.newBuilder().
+ shardHeartbeatIntervalInMillis(100).build()).schemaContext(schemaContext).props().
+ withDispatcher(Dispatchers.DefaultDispatcherId());
final TestActorRef<Shard> shard = TestActorRef.create(getSystem(), shardProps,
"testTransactionCommit");
public void testTransactionAbort() throws Exception {
new ShardTestKit(getSystem()) {{
SchemaContext schemaContext = TestModel.createTestContext();
- Props shardProps = Shard.props(ShardIdentifier.builder().memberName("member-1").
- shardName("inventory").type("config").build(),
- Collections.<String,String>emptyMap(),
- DatastoreContext.newBuilder().shardHeartbeatIntervalInMillis(100).build(),
- schemaContext).withDispatcher(Dispatchers.DefaultDispatcherId());
+ Props shardProps = Shard.builder().id(ShardIdentifier.builder().memberName("member-1").
+ shardName("inventory").type("config").build()).datastoreContext(DatastoreContext.newBuilder().
+ shardHeartbeatIntervalInMillis(100).build()).schemaContext(schemaContext).props().
+ withDispatcher(Dispatchers.DefaultDispatcherId());
final TestActorRef<Shard> shard = TestActorRef.create(getSystem(), shardProps,
"testTransactionAbort");
import org.opendaylight.controller.cluster.datastore.entityownership.messages.RegisterListenerLocal;
import org.opendaylight.controller.cluster.datastore.entityownership.messages.UnregisterCandidateLocal;
import org.opendaylight.controller.cluster.datastore.entityownership.messages.UnregisterListenerLocal;
-import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
import org.opendaylight.controller.cluster.datastore.messages.GetShardDataTree;
import org.opendaylight.controller.cluster.datastore.utils.MockClusterWrapper;
import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
import scala.concurrent.Await;
import scala.concurrent.Future;
import scala.concurrent.duration.Duration;
@Test
public void testRegisterCandidate() throws Exception {
- final TestShardPropsCreator shardPropsCreator = new TestShardPropsCreator();
+ final TestShardBuilder shardBuilder = new TestShardBuilder();
DistributedEntityOwnershipService service = new DistributedEntityOwnershipService(dataStore) {
@Override
- protected EntityOwnershipShardPropsCreator newShardPropsCreator() {
- return shardPropsCreator;
+ protected EntityOwnershipShard.Builder newShardBuilder() {
+ return shardBuilder;
}
};
service.start();
- shardPropsCreator.expectShardMessage(RegisterCandidateLocal.class);
+ shardBuilder.expectShardMessage(RegisterCandidateLocal.class);
YangInstanceIdentifier entityId = YangInstanceIdentifier.of(QNAME);
Entity entity = new Entity(ENTITY_TYPE, entityId);
EntityOwnershipCandidateRegistration reg = service.registerCandidate(entity);
verifyEntityOwnershipCandidateRegistration(entity, reg);
- verifyRegisterCandidateLocal(shardPropsCreator, entity);
+ verifyRegisterCandidateLocal(shardBuilder, entity);
verifyEntityCandidate(service.getLocalEntityOwnershipShard(), ENTITY_TYPE, entityId,
dataStore.getActorContext().getCurrentMemberName());
// Register a different entity - should succeed
Entity entity2 = new Entity(ENTITY_TYPE2, entityId);
- shardPropsCreator.expectShardMessage(RegisterCandidateLocal.class);
+ shardBuilder.expectShardMessage(RegisterCandidateLocal.class);
EntityOwnershipCandidateRegistration reg2 = service.registerCandidate(entity2);
verifyEntityOwnershipCandidateRegistration(entity2, reg2);
- verifyRegisterCandidateLocal(shardPropsCreator, entity2);
+ verifyRegisterCandidateLocal(shardBuilder, entity2);
verifyEntityCandidate(service.getLocalEntityOwnershipShard(), ENTITY_TYPE2, entityId,
dataStore.getActorContext().getCurrentMemberName());
@Test
public void testCloseCandidateRegistration() throws Exception {
- final TestShardPropsCreator shardPropsCreator = new TestShardPropsCreator();
+ final TestShardBuilder shardBuilder = new TestShardBuilder();
DistributedEntityOwnershipService service = new DistributedEntityOwnershipService(dataStore) {
@Override
- protected EntityOwnershipShardPropsCreator newShardPropsCreator() {
- return shardPropsCreator;
+ protected EntityOwnershipShard.Builder newShardBuilder() {
+ return shardBuilder;
}
};
service.start();
- shardPropsCreator.expectShardMessage(RegisterCandidateLocal.class);
+ shardBuilder.expectShardMessage(RegisterCandidateLocal.class);
Entity entity = new Entity(ENTITY_TYPE, YangInstanceIdentifier.of(QNAME));
EntityOwnershipCandidateRegistration reg = service.registerCandidate(entity);
verifyEntityOwnershipCandidateRegistration(entity, reg);
- verifyRegisterCandidateLocal(shardPropsCreator, entity);
+ verifyRegisterCandidateLocal(shardBuilder, entity);
- shardPropsCreator.expectShardMessage(UnregisterCandidateLocal.class);
+ shardBuilder.expectShardMessage(UnregisterCandidateLocal.class);
reg.close();
- UnregisterCandidateLocal unregCandidate = shardPropsCreator.waitForShardMessage();
+ UnregisterCandidateLocal unregCandidate = shardBuilder.waitForShardMessage();
assertEquals("getEntity", entity, unregCandidate.getEntity());
// Re-register - should succeed.
- shardPropsCreator.expectShardMessage(RegisterCandidateLocal.class);
+ shardBuilder.expectShardMessage(RegisterCandidateLocal.class);
service.registerCandidate(entity);
- verifyRegisterCandidateLocal(shardPropsCreator, entity);
+ verifyRegisterCandidateLocal(shardBuilder, entity);
service.close();
}
@Test
public void testListenerRegistration() {
- final TestShardPropsCreator shardPropsCreator = new TestShardPropsCreator();
+ final TestShardBuilder shardBuilder = new TestShardBuilder();
DistributedEntityOwnershipService service = new DistributedEntityOwnershipService(dataStore) {
@Override
- protected EntityOwnershipShardPropsCreator newShardPropsCreator() {
- return shardPropsCreator;
+ protected EntityOwnershipShard.Builder newShardBuilder() {
+ return shardBuilder;
}
};
service.start();
- shardPropsCreator.expectShardMessage(RegisterListenerLocal.class);
+ shardBuilder.expectShardMessage(RegisterListenerLocal.class);
YangInstanceIdentifier entityId = YangInstanceIdentifier.of(QNAME);
Entity entity = new Entity(ENTITY_TYPE, entityId);
assertEquals("getEntityType", entity.getType(), reg.getEntityType());
assertEquals("getInstance", listener, reg.getInstance());
- RegisterListenerLocal regListener = shardPropsCreator.waitForShardMessage();
+ RegisterListenerLocal regListener = shardBuilder.waitForShardMessage();
assertSame("getListener", listener, regListener.getListener());
assertEquals("getEntityType", entity.getType(), regListener.getEntityType());
- shardPropsCreator.expectShardMessage(UnregisterListenerLocal.class);
+ shardBuilder.expectShardMessage(UnregisterListenerLocal.class);
reg.close();
- UnregisterListenerLocal unregListener = shardPropsCreator.waitForShardMessage();
+ UnregisterListenerLocal unregListener = shardBuilder.waitForShardMessage();
assertEquals("getEntityType", entity.getType(), unregListener.getEntityType());
assertSame("getListener", listener, unregListener.getListener());
@Test
public void testGetOwnershipState() throws Exception {
- final TestShardPropsCreator shardPropsCreator = new TestShardPropsCreator();
+ final TestShardBuilder shardBuilder = new TestShardBuilder();
DistributedEntityOwnershipService service = new DistributedEntityOwnershipService(dataStore) {
@Override
- protected EntityOwnershipShardPropsCreator newShardPropsCreator() {
- return shardPropsCreator;
+ protected EntityOwnershipShard.Builder newShardBuilder() {
+ return shardBuilder;
}
};
service.start();
ShardDataTree shardDataTree = new ShardDataTree(SchemaContextHelper.entityOwners());
- shardPropsCreator.setDataTree(shardDataTree.getDataTree());
+ shardBuilder.setDataTree(shardDataTree.getDataTree());
Entity entity1 = new Entity(ENTITY_TYPE, "one");
writeNode(ENTITY_OWNERS_PATH, entityOwnersWithEntityTypeEntry(entityTypeEntryWithEntityEntry(entity1.getType(),
});
}
- private static void verifyRegisterCandidateLocal(final TestShardPropsCreator shardPropsCreator, Entity entity) {
- RegisterCandidateLocal regCandidate = shardPropsCreator.waitForShardMessage();
+ private static void verifyRegisterCandidateLocal(final TestShardBuilder shardBuilder, Entity entity) {
+ RegisterCandidateLocal regCandidate = shardBuilder.waitForShardMessage();
assertEquals("getEntity", entity, regCandidate.getEntity());
}
assertEquals("getInstance", entity, reg.getInstance());
}
- static class TestShardPropsCreator extends EntityOwnershipShardPropsCreator {
- TestShardPropsCreator() {
- super("member-1");
+ static class TestShardBuilder extends EntityOwnershipShard.Builder {
+ TestShardBuilder() {
+ localMemberName("member-1");
}
private final AtomicReference<CountDownLatch> messageReceived = new AtomicReference<>();
private final AtomicReference<DataTree> dataTree = new AtomicReference<>();
@Override
- public Props newProps(ShardIdentifier shardId, Map<String, String> peerAddresses,
- DatastoreContext datastoreContext, SchemaContext schemaContext) {
- return Props.create(TestEntityOwnershipShard.class, shardId, peerAddresses, datastoreContext,
- schemaContext, "member-1", messageClass, messageReceived, receivedMessage, dataTree);
+ public Props props() {
+ return Props.create(TestEntityOwnershipShard.class,this, messageClass, messageReceived,
+ receivedMessage, dataTree);
}
@SuppressWarnings("unchecked")
private final AtomicReference<Class<?>> messageClass;
private final AtomicReference<DataTree> dataTree;
- protected TestEntityOwnershipShard(ShardIdentifier name, Map<String, String> peerAddresses,
- DatastoreContext datastoreContext, SchemaContext schemaContext, String localMemberName,
+ protected TestEntityOwnershipShard(EntityOwnershipShard.Builder builder,
AtomicReference<Class<?>> messageClass, AtomicReference<CountDownLatch> messageReceived,
AtomicReference<Object> receivedMessage, AtomicReference<DataTree> dataTree) {
- super(name, peerAddresses, datastoreContext, schemaContext, localMemberName);
+ super(builder);
this.messageClass = messageClass;
this.messageReceived = messageReceived;
this.receivedMessage = receivedMessage;
}
private Props newShardProps(ShardIdentifier shardId, Map<String,String> peers, String memberName) {
- return EntityOwnershipShard.props(shardId, peers, dataStoreContextBuilder.build(),
- SCHEMA_CONTEXT, memberName);
+ return EntityOwnershipShard.newBuilder().id(shardId).peerAddresses(peers).
+ datastoreContext(dataStoreContextBuilder.build()).schemaContext(SCHEMA_CONTEXT).
+ localMemberName(memberName).props().withDispatcher(Dispatchers.DefaultDispatcherId());
}
private static ShardIdentifier newShardId(String memberName) {
TestEntityOwnershipShard(ShardIdentifier name, Map<String, String> peerAddresses,
DatastoreContext datastoreContext) {
- super(name, peerAddresses, datastoreContext, SCHEMA_CONTEXT, LOCAL_MEMBER_NAME);
+ super(newBuilder().id(name).peerAddresses(peerAddresses).datastoreContext(datastoreContext).
+ schemaContext(SCHEMA_CONTEXT).localMemberName(LOCAL_MEMBER_NAME));
}
@Override