X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-distributed-datastore%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fdatastore%2Fentityownership%2FDistributedEntityOwnershipServiceTest.java;h=c052134772d68de17f6588e4cfc412eaddbc6ccd;hp=3e9a51ede9d4d6c8508e8aab0802b78d414c8557;hb=4e3f49788c05730b29468deebc2aaa4ed0d94eef;hpb=a8f617e6dd21f9a453e9ece1e57f4549899584e7 diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/entityownership/DistributedEntityOwnershipServiceTest.java b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/entityownership/DistributedEntityOwnershipServiceTest.java index 3e9a51ede9..c052134772 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/entityownership/DistributedEntityOwnershipServiceTest.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/entityownership/DistributedEntityOwnershipServiceTest.java @@ -8,47 +8,61 @@ package org.opendaylight.controller.cluster.datastore.entityownership; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.reset; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_ID_QNAME; import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_OWNERS_PATH; +import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_QNAME; +import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.candidatePath; +import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.entityEntryWithOwner; +import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.entityOwnersWithCandidate; +import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.entityOwnersWithEntityTypeEntry; +import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.entityPath; +import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.entityTypeEntryWithEntityEntry; import akka.actor.ActorRef; import akka.actor.PoisonPill; -import akka.actor.Props; import com.google.common.base.Optional; -import com.google.common.base.Stopwatch; -import com.google.common.collect.ImmutableMap; -import com.google.common.util.concurrent.Uninterruptibles; -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.concurrent.CountDownLatch; +import com.google.common.collect.Sets; +import java.util.Collection; import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicReference; import org.junit.After; import org.junit.Before; import org.junit.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.Mockito; +import org.opendaylight.controller.cluster.access.concepts.MemberName; import org.opendaylight.controller.cluster.datastore.DatastoreContext; +import org.opendaylight.controller.cluster.datastore.DatastoreContextFactory; import org.opendaylight.controller.cluster.datastore.DistributedDataStore; +import org.opendaylight.controller.cluster.datastore.ShardDataTree; +import org.opendaylight.controller.cluster.datastore.config.Configuration; +import org.opendaylight.controller.cluster.datastore.config.ConfigurationImpl; +import org.opendaylight.controller.cluster.datastore.config.EmptyModuleShardConfigProvider; import org.opendaylight.controller.cluster.datastore.entityownership.messages.RegisterCandidateLocal; +import org.opendaylight.controller.cluster.datastore.entityownership.messages.RegisterListenerLocal; import org.opendaylight.controller.cluster.datastore.entityownership.messages.UnregisterCandidateLocal; -import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier; -import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategy; -import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategyFactory; +import org.opendaylight.controller.cluster.datastore.entityownership.messages.UnregisterListenerLocal; +import org.opendaylight.controller.cluster.datastore.entityownership.selectionstrategy.EntityOwnerSelectionStrategyConfig; import org.opendaylight.controller.cluster.datastore.utils.MockClusterWrapper; -import org.opendaylight.controller.cluster.datastore.utils.MockConfiguration; import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper; import org.opendaylight.controller.md.sal.common.api.clustering.CandidateAlreadyRegisteredException; import org.opendaylight.controller.md.sal.common.api.clustering.Entity; -import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipCandidate; import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipCandidateRegistration; -import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction; +import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListener; +import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListenerRegistration; +import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipState; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; -import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; -import org.opendaylight.yangtools.yang.model.api.SchemaContext; +import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType; +import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes; import scala.concurrent.Await; import scala.concurrent.Future; import scala.concurrent.duration.Duration; @@ -59,43 +73,33 @@ import scala.concurrent.duration.Duration; * @author Thomas Pantelis */ public class DistributedEntityOwnershipServiceTest extends AbstractEntityOwnershipTest { - static String ENTITY_TYPE = "test"; - static String ENTITY_TYPE2 = "test2"; - static int ID_COUNTER = 1; + static final String ENTITY_TYPE = "test"; + static final String ENTITY_TYPE2 = "test2"; static final QName QNAME = QName.create("test", "2015-08-11", "foo"); + static int ID_COUNTER = 1; - private final String dataStoreType = "config" + ID_COUNTER++; + private final String dataStoreName = "config" + ID_COUNTER++; private DistributedDataStore dataStore; @Before public void setUp() { - DatastoreContext datastoreContext = DatastoreContext.newBuilder().dataStoreType(dataStoreType). + DatastoreContext datastoreContext = DatastoreContext.newBuilder().dataStoreName(dataStoreName). shardInitializationTimeout(10, TimeUnit.SECONDS).build(); - // FIXME - remove this MockConfiguration and use the production ConfigurationImpl class when the - // DistributedEntityOwnershipService is changed to setup the ShardStrategy for the entity-owners module. - MockConfiguration configuration = new MockConfiguration(Collections.>emptyMap()) { + Configuration configuration = new ConfigurationImpl(new EmptyModuleShardConfigProvider()) { @Override - public Optional getModuleNameFromNameSpace(String nameSpace) { - return Optional.of("entity-owners"); - } - - @Override - public Map getModuleNameToShardStrategyMap() { - return ImmutableMap.builder().put("entity-owners", new ShardStrategy() { - @Override - public String findShard(YangInstanceIdentifier path) { - return DistributedEntityOwnershipService.ENTITY_OWNERSHIP_SHARD_NAME; - } - }).build(); + public Collection getUniqueMemberNamesForAllShards() { + return Sets.newHashSet(MemberName.forName("member-1")); } }; - dataStore = new DistributedDataStore(getSystem(), new MockClusterWrapper(), configuration, datastoreContext ); + DatastoreContextFactory mockContextFactory = mock(DatastoreContextFactory.class); + Mockito.doReturn(datastoreContext).when(mockContextFactory).getBaseDatastoreContext(); + Mockito.doReturn(datastoreContext).when(mockContextFactory).getShardDatastoreContext(Mockito.anyString()); - dataStore.onGlobalContextUpdated(SchemaContextHelper.entityOwners()); + dataStore = new DistributedDataStore(getSystem(), new MockClusterWrapper(), configuration, mockContextFactory, null); - ShardStrategyFactory.setConfiguration(configuration); + dataStore.onGlobalContextUpdated(SchemaContextHelper.entityOwners()); } @After @@ -103,10 +107,16 @@ public class DistributedEntityOwnershipServiceTest extends AbstractEntityOwnersh dataStore.getActorContext().getShardManager().tell(PoisonPill.getInstance(), ActorRef.noSender()); } + private static T verifyMessage(final DistributedEntityOwnershipService mock, final Class type) { + final ArgumentCaptor message = ArgumentCaptor.forClass(type); + verify(mock).executeLocalEntityOwnershipShardOperation(message.capture()); + return message.getValue(); + } + @Test public void testEntityOwnershipShardCreated() throws Exception { - DistributedEntityOwnershipService service = new DistributedEntityOwnershipService(dataStore); - service.start(); + DistributedEntityOwnershipService service = DistributedEntityOwnershipService.start(dataStore.getActorContext(), + EntityOwnerSelectionStrategyConfig.newBuilder().build()); Future future = dataStore.getActorContext().findLocalShardAsync( DistributedEntityOwnershipService.ENTITY_OWNERSHIP_SHARD_NAME); @@ -118,185 +128,175 @@ public class DistributedEntityOwnershipServiceTest extends AbstractEntityOwnersh @Test public void testRegisterCandidate() throws Exception { - final TestShardPropsCreator shardPropsCreator = new TestShardPropsCreator(); - DistributedEntityOwnershipService service = new DistributedEntityOwnershipService(dataStore) { - @Override - protected EntityOwnershipShardPropsCreator newShardPropsCreator() { - return shardPropsCreator; - } - }; - - service.start(); - - shardPropsCreator.expectShardMessage(RegisterCandidateLocal.class); + DistributedEntityOwnershipService service = spy(DistributedEntityOwnershipService.start( + dataStore.getActorContext(), EntityOwnerSelectionStrategyConfig.newBuilder().build())); YangInstanceIdentifier entityId = YangInstanceIdentifier.of(QNAME); Entity entity = new Entity(ENTITY_TYPE, entityId); - EntityOwnershipCandidate candidate = mock(EntityOwnershipCandidate.class); - - EntityOwnershipCandidateRegistration reg = service.registerCandidate(entity, candidate); + EntityOwnershipCandidateRegistration reg = service.registerCandidate(entity); + verifyRegisterCandidateLocal(service, entity); verifyEntityOwnershipCandidateRegistration(entity, reg); - verifyRegisterCandidateLocal(shardPropsCreator, entity, candidate); - verifyEntityCandidate(readEntityOwners(service.getLocalEntityOwnershipShard()), ENTITY_TYPE, entityId, - dataStore.getActorContext().getCurrentMemberName()); + verifyEntityCandidate(service.getLocalEntityOwnershipShard(), ENTITY_TYPE, entityId, + dataStore.getActorContext().getCurrentMemberName().getName()); // Register the same entity - should throw exception - EntityOwnershipCandidate candidate2 = mock(EntityOwnershipCandidate.class); try { - service.registerCandidate(entity, candidate2); + service.registerCandidate(entity); fail("Expected CandidateAlreadyRegisteredException"); } catch(CandidateAlreadyRegisteredException e) { // expected - assertSame("getCandidate", candidate, e.getRegisteredCandidate()); assertEquals("getEntity", entity, e.getEntity()); } // Register a different entity - should succeed + reset(service); Entity entity2 = new Entity(ENTITY_TYPE2, entityId); - shardPropsCreator.expectShardMessage(RegisterCandidateLocal.class); - - EntityOwnershipCandidateRegistration reg2 = service.registerCandidate(entity2, candidate); - + EntityOwnershipCandidateRegistration reg2 = service.registerCandidate(entity2); + verifyRegisterCandidateLocal(service, entity2); verifyEntityOwnershipCandidateRegistration(entity2, reg2); - verifyRegisterCandidateLocal(shardPropsCreator, entity2, candidate); - verifyEntityCandidate(readEntityOwners(service.getLocalEntityOwnershipShard()), ENTITY_TYPE2, entityId, - dataStore.getActorContext().getCurrentMemberName()); + verifyEntityCandidate(service.getLocalEntityOwnershipShard(), ENTITY_TYPE2, entityId, + dataStore.getActorContext().getCurrentMemberName().getName()); service.close(); } @Test public void testCloseCandidateRegistration() throws Exception { - final TestShardPropsCreator shardPropsCreator = new TestShardPropsCreator(); - DistributedEntityOwnershipService service = new DistributedEntityOwnershipService(dataStore) { - @Override - protected EntityOwnershipShardPropsCreator newShardPropsCreator() { - return shardPropsCreator; - } - }; - - service.start(); - - shardPropsCreator.expectShardMessage(RegisterCandidateLocal.class); + DistributedEntityOwnershipService service = spy(DistributedEntityOwnershipService.start( + dataStore.getActorContext(), EntityOwnerSelectionStrategyConfig.newBuilder().build())); Entity entity = new Entity(ENTITY_TYPE, YangInstanceIdentifier.of(QNAME)); - EntityOwnershipCandidate candidate = mock(EntityOwnershipCandidate.class); - - EntityOwnershipCandidateRegistration reg = service.registerCandidate(entity, candidate); + EntityOwnershipCandidateRegistration reg = service.registerCandidate(entity); verifyEntityOwnershipCandidateRegistration(entity, reg); - verifyRegisterCandidateLocal(shardPropsCreator, entity, candidate); - - shardPropsCreator.expectShardMessage(UnregisterCandidateLocal.class); + verifyRegisterCandidateLocal(service, entity); + reset(service); reg.close(); - - UnregisterCandidateLocal unregCandidate = shardPropsCreator.waitForShardMessage(); + UnregisterCandidateLocal unregCandidate = verifyMessage(service, UnregisterCandidateLocal.class); assertEquals("getEntity", entity, unregCandidate.getEntity()); // Re-register - should succeed. + reset(service); + service.registerCandidate(entity); + verifyRegisterCandidateLocal(service, entity); + + service.close(); + } - shardPropsCreator.expectShardMessage(RegisterCandidateLocal.class); + @Test + public void testListenerRegistration() { + DistributedEntityOwnershipService service = spy(DistributedEntityOwnershipService.start( + dataStore.getActorContext(), EntityOwnerSelectionStrategyConfig.newBuilder().build())); + + YangInstanceIdentifier entityId = YangInstanceIdentifier.of(QNAME); + Entity entity = new Entity(ENTITY_TYPE, entityId); + EntityOwnershipListener listener = mock(EntityOwnershipListener.class); - service.registerCandidate(entity, candidate); + EntityOwnershipListenerRegistration reg = service.registerListener(entity.getType(), listener); - verifyRegisterCandidateLocal(shardPropsCreator, entity, candidate); + assertNotNull("EntityOwnershipListenerRegistration null", reg); + assertEquals("getEntityType", entity.getType(), reg.getEntityType()); + assertEquals("getInstance", listener, reg.getInstance()); + + RegisterListenerLocal regListener = verifyMessage(service, RegisterListenerLocal.class); + assertSame("getListener", listener, regListener.getListener()); + assertEquals("getEntityType", entity.getType(), regListener.getEntityType()); + + reset(service); + reg.close(); + UnregisterListenerLocal unregListener = verifyMessage(service, UnregisterListenerLocal.class); + assertEquals("getEntityType", entity.getType(), unregListener.getEntityType()); + assertSame("getListener", listener, unregListener.getListener()); service.close(); } @Test - public void testRegisterListener() { - } + public void testGetOwnershipState() throws Exception { + DistributedEntityOwnershipService service = spy(DistributedEntityOwnershipService.start( + dataStore.getActorContext(), EntityOwnerSelectionStrategyConfig.newBuilder().build())); - private NormalizedNode readEntityOwners(ActorRef shard) throws Exception { - Stopwatch sw = Stopwatch.createStarted(); - while(sw.elapsed(TimeUnit.MILLISECONDS) <= 5000) { - DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction(); - Optional> optional = readTx.read(ENTITY_OWNERS_PATH). - checkedGet(5, TimeUnit.SECONDS); - if(optional.isPresent()) { - return optional.get(); - } + ShardDataTree shardDataTree = new ShardDataTree(SchemaContextHelper.entityOwners(), TreeType.OPERATIONAL); - Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS); - } + when(service.getLocalEntityOwnershipShardDataTree()).thenReturn(shardDataTree.getDataTree()); - return null; - } + Entity entity1 = new Entity(ENTITY_TYPE, "one"); + writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, entity1.getId(), "member-1"), shardDataTree); + writeNode(ENTITY_OWNERS_PATH, entityOwnersWithEntityTypeEntry(entityTypeEntryWithEntityEntry(entity1.getType(), + entityEntryWithOwner(entity1.getId(), "member-1"))), shardDataTree); + verifyGetOwnershipState(service, entity1, true, true); - private void verifyRegisterCandidateLocal(final TestShardPropsCreator shardPropsCreator, Entity entity, - EntityOwnershipCandidate candidate) { - RegisterCandidateLocal regCandidate = shardPropsCreator.waitForShardMessage(); - assertSame("getCandidate", candidate, regCandidate.getCandidate()); - assertEquals("getEntity", entity, regCandidate.getEntity()); - } + writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, entity1.getId(), "member-2"), shardDataTree); + writeNode(entityPath(entity1.getType(), entity1.getId()), entityEntryWithOwner(entity1.getId(), "member-2"), + shardDataTree); + verifyGetOwnershipState(service, entity1, false, true); - private void verifyEntityOwnershipCandidateRegistration(Entity entity, EntityOwnershipCandidateRegistration reg) { - assertNotNull("EntityOwnershipCandidateRegistration null", reg); - assertEquals("getEntity", entity, reg.getEntity()); + writeNode(entityPath(entity1.getType(), entity1.getId()), entityEntryWithOwner(entity1.getId(), ""), + shardDataTree); + verifyGetOwnershipState(service, entity1, false, false); + + Entity entity2 = new Entity(ENTITY_TYPE, "two"); + Optional state = service.getOwnershipState(entity2); + assertEquals("getOwnershipState present", false, state.isPresent()); + + writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, entity2.getId(), "member-1"), shardDataTree); + writeNode(entityPath(entity2.getType(), entity2.getId()), ImmutableNodes.mapEntry(ENTITY_QNAME, + ENTITY_ID_QNAME, entity2.getId()), shardDataTree); + verifyGetOwnershipState(service, entity2, false, false); + + deleteNode(candidatePath(entityPath(entity2.getType(), entity2.getId()), "member-1"), shardDataTree); + Optional state2 = service.getOwnershipState(entity2); + assertEquals("getOwnershipState present", false, state2.isPresent()); + service.close(); } - static class TestShardPropsCreator extends EntityOwnershipShardPropsCreator { - TestShardPropsCreator() { - super("member-1"); - } + @Test + public void testIsCandidateRegistered() throws CandidateAlreadyRegisteredException { + DistributedEntityOwnershipService service = DistributedEntityOwnershipService.start(dataStore.getActorContext(), + EntityOwnerSelectionStrategyConfig.newBuilder().build()); - private final AtomicReference messageReceived = new AtomicReference<>(); - private final AtomicReference receivedMessage = new AtomicReference<>(); - private final AtomicReference> messageClass = new AtomicReference<>(); + final Entity test = new Entity("test-type", "test"); - @Override - public Props newProps(ShardIdentifier shardId, Map peerAddresses, - DatastoreContext datastoreContext, SchemaContext schemaContext) { - return Props.create(TestEntityOwnershipShard.class, shardId, peerAddresses, datastoreContext, - schemaContext, "member-1", messageClass, messageReceived, receivedMessage); - } + assertFalse(service.isCandidateRegistered(test)); - @SuppressWarnings("unchecked") - T waitForShardMessage() { - assertTrue("Message " + messageClass.get().getSimpleName() + " was not received", - Uninterruptibles.awaitUninterruptibly(messageReceived.get(), 5, TimeUnit.SECONDS)); - assertEquals("Message type", messageClass.get(), receivedMessage.get().getClass()); - return (T) receivedMessage.get(); - } + service.registerCandidate(test); - void expectShardMessage(Class ofType) { - messageReceived.set(new CountDownLatch(1)); - receivedMessage.set(null); - messageClass.set(ofType); - } + assertTrue(service.isCandidateRegistered(test)); + + service.close(); } - static class TestEntityOwnershipShard extends EntityOwnershipShard { - private final AtomicReference messageReceived; - private final AtomicReference receivedMessage; - private final AtomicReference> messageClass; - - protected TestEntityOwnershipShard(ShardIdentifier name, Map peerAddresses, - DatastoreContext datastoreContext, SchemaContext schemaContext, String localMemberName, - AtomicReference> messageClass, AtomicReference messageReceived, - AtomicReference receivedMessage) { - super(name, peerAddresses, datastoreContext, schemaContext, localMemberName); - this.messageClass = messageClass; - this.messageReceived = messageReceived; - this.receivedMessage = receivedMessage; - } + private static void verifyGetOwnershipState(final DistributedEntityOwnershipService service, final Entity entity, + final boolean isOwner, final boolean hasOwner) { + Optional state = service.getOwnershipState(entity); + assertEquals("getOwnershipState present", true, state.isPresent()); + assertEquals("isOwner", isOwner, state.get().isOwner()); + assertEquals("hasOwner", hasOwner, state.get().hasOwner()); + } - @Override - public void onReceiveCommand(final Object message) throws Exception { - try { - super.onReceiveCommand(message); - } finally { - Class expMsgClass = messageClass.get(); - if(expMsgClass != null && expMsgClass.equals(message.getClass())) { - receivedMessage.set(message); - messageReceived.get().countDown(); - } - } - } + private void verifyEntityCandidate(final ActorRef entityOwnershipShard, final String entityType, + final YangInstanceIdentifier entityId, final String candidateName) { + verifyEntityCandidate(entityType, entityId, candidateName, + path -> { + try { + return dataStore.newReadOnlyTransaction().read(path).get(5, TimeUnit.SECONDS).get(); + } catch (Exception e) { + return null; + } + }); + } + + private static void verifyRegisterCandidateLocal(final DistributedEntityOwnershipService service, final Entity entity) { + RegisterCandidateLocal regCandidate = verifyMessage(service, RegisterCandidateLocal.class); + assertEquals("getEntity", entity, regCandidate.getEntity()); + } + + private static void verifyEntityOwnershipCandidateRegistration(final Entity entity, final EntityOwnershipCandidateRegistration reg) { + assertNotNull("EntityOwnershipCandidateRegistration null", reg); + assertEquals("getInstance", entity, reg.getInstance()); } }