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=e9d837c17ff1e41263e1b839d881cc30dcb554a8;hp=7e2f7547b35ebbcbaa9c912cfc5cb1fde0ac2f3e;hb=15c366198fa48eefd94f4d1a72faa9833e988250;hpb=5273c33b6f2051a7e3b1afcc4eeae4e457b6f26c 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 7e2f7547b3..e9d837c17f 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,46 +8,64 @@ 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.util.concurrent.Uninterruptibles; -import java.util.Collections; -import java.util.Map; +import com.google.common.collect.Sets; +import java.util.Collection; import java.util.concurrent.CountDownLatch; 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.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.ModuleConfig; -import org.opendaylight.controller.cluster.datastore.config.ModuleShardConfigProvider; +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.entityownership.messages.UnregisterListenerLocal; +import org.opendaylight.controller.cluster.datastore.entityownership.selectionstrategy.EntityOwnerSelectionStrategyConfig; +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.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.DataTree; +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; @@ -58,27 +76,31 @@ 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(); - Configuration configuration = new ConfigurationImpl(new ModuleShardConfigProvider() { + Configuration configuration = new ConfigurationImpl(new EmptyModuleShardConfigProvider()) { @Override - public Map retrieveModuleConfigs(Configuration configuration) { - return Collections.emptyMap(); + public Collection getUniqueMemberNamesForAllShards() { + return Sets.newHashSet("member-1"); } - }); + }; + + DatastoreContextFactory mockContextFactory = mock(DatastoreContextFactory.class); + Mockito.doReturn(datastoreContext).when(mockContextFactory).getBaseDatastoreContext(); + Mockito.doReturn(datastoreContext).when(mockContextFactory).getShardDatastoreContext(Mockito.anyString()); - dataStore = new DistributedDataStore(getSystem(), new MockClusterWrapper(), configuration, datastoreContext ); + dataStore = new DistributedDataStore(getSystem(), new MockClusterWrapper(), configuration, mockContextFactory, null); dataStore.onGlobalContextUpdated(SchemaContextHelper.entityOwners()); } @@ -88,10 +110,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); @@ -103,51 +131,36 @@ 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, + verifyEntityCandidate(service.getLocalEntityOwnershipShard(), ENTITY_TYPE, entityId, dataStore.getActorContext().getCurrentMemberName()); // 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, + verifyEntityCandidate(service.getLocalEntityOwnershipShard(), ENTITY_TYPE2, entityId, dataStore.getActorContext().getCurrentMemberName()); service.close(); @@ -155,128 +168,165 @@ public class DistributedEntityOwnershipServiceTest extends AbstractEntityOwnersh @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()); - assertSame("getCandidate", candidate, unregCandidate.getCandidate()); // Re-register - should succeed. + reset(service); + service.registerCandidate(entity); + verifyRegisterCandidateLocal(service, entity); + + service.close(); + } + + @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); + + EntityOwnershipListenerRegistration reg = service.registerListener(entity.getType(), listener); - shardPropsCreator.expectShardMessage(RegisterCandidateLocal.class); + assertNotNull("EntityOwnershipListenerRegistration null", reg); + assertEquals("getEntityType", entity.getType(), reg.getEntityType()); + assertEquals("getInstance", listener, reg.getInstance()); - service.registerCandidate(entity, candidate); + RegisterListenerLocal regListener = verifyMessage(service, RegisterListenerLocal.class); + assertSame("getListener", listener, regListener.getListener()); + assertEquals("getEntityType", entity.getType(), regListener.getEntityType()); - verifyRegisterCandidateLocal(shardPropsCreator, entity, candidate); + 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 { - Uninterruptibles.sleepUninterruptibly(500, TimeUnit.MILLISECONDS); - 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(); + } + + 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()); + } + + 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()); } static class TestEntityOwnershipShard extends EntityOwnershipShard { private final AtomicReference messageReceived; private final AtomicReference receivedMessage; private final AtomicReference> messageClass; + private final AtomicReference dataTree; - protected TestEntityOwnershipShard(ShardIdentifier name, Map peerAddresses, - DatastoreContext datastoreContext, SchemaContext schemaContext, String localMemberName, - AtomicReference> messageClass, AtomicReference messageReceived, - AtomicReference receivedMessage) { - super(name, peerAddresses, datastoreContext, schemaContext, localMemberName); + protected TestEntityOwnershipShard(final EntityOwnershipShard.Builder builder, + final AtomicReference> messageClass, final AtomicReference messageReceived, + final AtomicReference receivedMessage, final AtomicReference dataTree) { + super(builder); this.messageClass = messageClass; this.messageReceived = messageReceived; this.receivedMessage = receivedMessage; + this.dataTree = dataTree; } @Override - public void onReceiveCommand(final Object message) throws Exception { + public void handleCommand(final Object message) { try { - super.onReceiveCommand(message); + if(dataTree.get() != null && message instanceof GetShardDataTree) { + sender().tell(dataTree.get(), self()); + } else { + super.handleCommand(message); + } } finally { Class expMsgClass = messageClass.get(); if(expMsgClass != null && expMsgClass.equals(message.getClass())) {