Search in sources :

Example 1 with EntityOwnershipState

use of org.opendaylight.mdsal.eos.common.api.EntityOwnershipState in project controller by opendaylight.

the class DistributedEntityOwnershipIntegrationTest method testCloseCandidateRegistrationInQuickSuccession.

/**
 * Reproduces bug <a href="https://bugs.opendaylight.org/show_bug.cgi?id=4554">4554</a>.
 */
@Test
public void testCloseCandidateRegistrationInQuickSuccession() throws Exception {
    String name = "testCloseCandidateRegistrationInQuickSuccession";
    MemberNode leaderNode = MemberNode.builder(memberNodes).akkaConfig("Member1").testName(name).moduleShardsConfig(MODULE_SHARDS_CONFIG).schemaContext(SCHEMA_CONTEXT).createOperDatastore(false).datastoreContextBuilder(leaderDatastoreContextBuilder).build();
    MemberNode follower1Node = MemberNode.builder(memberNodes).akkaConfig("Member2").testName(name).moduleShardsConfig(MODULE_SHARDS_CONFIG).schemaContext(SCHEMA_CONTEXT).createOperDatastore(false).datastoreContextBuilder(followerDatastoreContextBuilder).build();
    MemberNode follower2Node = MemberNode.builder(memberNodes).akkaConfig("Member3").testName(name).moduleShardsConfig(MODULE_SHARDS_CONFIG).schemaContext(SCHEMA_CONTEXT).createOperDatastore(false).datastoreContextBuilder(followerDatastoreContextBuilder).build();
    AbstractDataStore leaderDistributedDataStore = leaderNode.configDataStore();
    leaderDistributedDataStore.waitTillReady();
    follower1Node.configDataStore().waitTillReady();
    follower2Node.configDataStore().waitTillReady();
    final DOMEntityOwnershipService leaderEntityOwnershipService = newOwnershipService(leaderDistributedDataStore);
    final DOMEntityOwnershipService follower1EntityOwnershipService = newOwnershipService(follower1Node.configDataStore());
    final DOMEntityOwnershipService follower2EntityOwnershipService = newOwnershipService(follower2Node.configDataStore());
    leaderNode.kit().waitUntilLeader(leaderNode.configDataStore().getActorContext(), ENTITY_OWNERSHIP_SHARD_NAME);
    leaderEntityOwnershipService.registerListener(ENTITY_TYPE1, leaderMockListener);
    follower1EntityOwnershipService.registerListener(ENTITY_TYPE1, follower1MockListener);
    follower2EntityOwnershipService.registerListener(ENTITY_TYPE1, follower2MockListener);
    final DOMEntityOwnershipCandidateRegistration candidate1 = leaderEntityOwnershipService.registerCandidate(ENTITY1);
    verify(leaderMockListener, timeout(5000)).ownershipChanged(ownershipChange(ENTITY1, false, true, true));
    final DOMEntityOwnershipCandidateRegistration candidate2 = follower1EntityOwnershipService.registerCandidate(ENTITY1);
    verify(follower1MockListener, timeout(5000)).ownershipChanged(ownershipChange(ENTITY1, false, false, true));
    final DOMEntityOwnershipCandidateRegistration candidate3 = follower2EntityOwnershipService.registerCandidate(ENTITY1);
    verify(follower2MockListener, timeout(5000)).ownershipChanged(ownershipChange(ENTITY1, false, false, true));
    Mockito.reset(leaderMockListener, follower1MockListener, follower2MockListener);
    ArgumentCaptor<DOMEntityOwnershipChange> leaderChangeCaptor = ArgumentCaptor.forClass(DOMEntityOwnershipChange.class);
    ArgumentCaptor<DOMEntityOwnershipChange> follower1ChangeCaptor = ArgumentCaptor.forClass(DOMEntityOwnershipChange.class);
    ArgumentCaptor<DOMEntityOwnershipChange> follower2ChangeCaptor = ArgumentCaptor.forClass(DOMEntityOwnershipChange.class);
    doNothing().when(leaderMockListener).ownershipChanged(leaderChangeCaptor.capture());
    doNothing().when(follower1MockListener).ownershipChanged(follower1ChangeCaptor.capture());
    doNothing().when(follower2MockListener).ownershipChanged(follower2ChangeCaptor.capture());
    candidate1.close();
    candidate2.close();
    candidate3.close();
    boolean passed = false;
    for (int i = 0; i < 100; i++) {
        Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
        final Optional<EntityOwnershipState> leaderState = leaderEntityOwnershipService.getOwnershipState(ENTITY1);
        final Optional<EntityOwnershipState> follower1State = follower1EntityOwnershipService.getOwnershipState(ENTITY1);
        final Optional<EntityOwnershipState> follower2State = follower2EntityOwnershipService.getOwnershipState(ENTITY1);
        final Optional<DOMEntityOwnershipChange> leaderChange = getValueSafely(leaderChangeCaptor);
        final Optional<DOMEntityOwnershipChange> follower1Change = getValueSafely(follower1ChangeCaptor);
        final Optional<DOMEntityOwnershipChange> follower2Change = getValueSafely(follower2ChangeCaptor);
        if (!leaderState.isPresent() || leaderState.get() == EntityOwnershipState.NO_OWNER && follower1State.isPresent() && follower1State.get() == EntityOwnershipState.NO_OWNER && follower2State.isPresent() && follower2State.get() == EntityOwnershipState.NO_OWNER && leaderChange.isPresent() && !leaderChange.get().getState().hasOwner() && follower1Change.isPresent() && !follower1Change.get().getState().hasOwner() && follower2Change.isPresent() && !follower2Change.get().getState().hasOwner()) {
            passed = true;
            break;
        }
    }
    assertTrue("No ownership change message was sent with hasOwner=false", passed);
}
Also used : DOMEntityOwnershipService(org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipService) MemberNode(org.opendaylight.controller.cluster.datastore.MemberNode) DOMEntityOwnershipCandidateRegistration(org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipCandidateRegistration) DOMEntityOwnershipChange(org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipChange) EntityOwnershipState(org.opendaylight.mdsal.eos.common.api.EntityOwnershipState) AbstractDataStore(org.opendaylight.controller.cluster.datastore.AbstractDataStore) Test(org.junit.Test)

Example 2 with EntityOwnershipState

use of org.opendaylight.mdsal.eos.common.api.EntityOwnershipState in project openflowplugin by opendaylight.

the class DeviceOwnershipStatusService method isEntityOwned.

public boolean isEntityOwned(final String nodeId) {
    EntityOwnershipState state = ownershipStateCache.get(nodeId);
    if (state == null) {
        java.util.Optional<EntityOwnershipState> status = getCurrentOwnershipStatus(nodeId);
        if (status.isPresent()) {
            state = status.get();
            ownershipStateCache.put(nodeId, state);
        } else {
            LOG.warn("Fetching ownership status failed for node {}", nodeId);
        }
    }
    return state != null && state.equals(EntityOwnershipState.IS_OWNER);
}
Also used : EntityOwnershipState(org.opendaylight.mdsal.eos.common.api.EntityOwnershipState)

Example 3 with EntityOwnershipState

use of org.opendaylight.mdsal.eos.common.api.EntityOwnershipState in project openflowplugin by opendaylight.

the class LLDPDiscoveryUtils method getCurrentOwnershipStatus.

private static java.util.Optional<EntityOwnershipState> getCurrentOwnershipStatus(final EntityOwnershipService eos, final String nodeId) {
    Entity entity = createNodeEntity(nodeId);
    Optional<EntityOwnershipState> ownershipStatus = eos.getOwnershipState(entity);
    if (ownershipStatus.isPresent()) {
        LOG.debug("Fetched ownership status for node {} is {}", nodeId, ownershipStatus.get());
        return java.util.Optional.of(ownershipStatus.get());
    }
    return java.util.Optional.empty();
}
Also used : Entity(org.opendaylight.mdsal.eos.binding.api.Entity) EntityOwnershipState(org.opendaylight.mdsal.eos.common.api.EntityOwnershipState)

Example 4 with EntityOwnershipState

use of org.opendaylight.mdsal.eos.common.api.EntityOwnershipState in project genius by opendaylight.

the class EntityOwnershipUtils method isEntityOwner.

/**
 * Checks if the local node is the owner of an entity.
 *
 * @param entity the entity
 * @param sleepBetweenRetries the busy wait interval in millis if the entity is not yet present
 * @param tries the total number of busy wait tries
 * @return true is the owner, false otherwise
 */
public boolean isEntityOwner(Entity entity, long sleepBetweenRetries, int tries) {
    while (true) {
        Optional<EntityOwnershipState> entityState = entityOwnershipService.getOwnershipState(entity);
        if (entityState.isPresent()) {
            EntityOwnershipState entityOwnershipState = entityState.get();
            return entityOwnershipState == EntityOwnershipState.IS_OWNER;
        }
        tries--;
        LOG.debug("EntityOwnershipState for {} is not yet available. {} tries left", entity, tries);
        if (tries > 0) {
            try {
                Thread.sleep(sleepBetweenRetries);
            } catch (InterruptedException e) {
                break;
            }
        } else {
            break;
        }
    }
    return false;
}
Also used : EntityOwnershipState(org.opendaylight.mdsal.eos.common.api.EntityOwnershipState)

Example 5 with EntityOwnershipState

use of org.opendaylight.mdsal.eos.common.api.EntityOwnershipState in project controller by opendaylight.

the class DistributedEntityOwnershipServiceTest method testGetOwnershipState.

@Test
public void testGetOwnershipState() throws Exception {
    DistributedEntityOwnershipService service = spy(DistributedEntityOwnershipService.start(dataStore.getActorContext(), EntityOwnerSelectionStrategyConfig.newBuilder().build()));
    final Shard mockShard = Mockito.mock(Shard.class);
    ShardDataTree shardDataTree = new ShardDataTree(mockShard, SchemaContextHelper.entityOwners(), TreeType.OPERATIONAL);
    when(service.getLocalEntityOwnershipShardDataTree()).thenReturn(shardDataTree.getDataTree());
    DOMEntity entity1 = new DOMEntity(ENTITY_TYPE, "one");
    writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, entity1.getIdentifier(), "member-1"), shardDataTree);
    writeNode(ENTITY_OWNERS_PATH, entityOwnersWithEntityTypeEntry(entityTypeEntryWithEntityEntry(entity1.getType(), entityEntryWithOwner(entity1.getIdentifier(), "member-1"))), shardDataTree);
    verifyGetOwnershipState(service, entity1, EntityOwnershipState.IS_OWNER);
    writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, entity1.getIdentifier(), "member-2"), shardDataTree);
    writeNode(entityPath(entity1.getType(), entity1.getIdentifier()), entityEntryWithOwner(entity1.getIdentifier(), "member-2"), shardDataTree);
    verifyGetOwnershipState(service, entity1, EntityOwnershipState.OWNED_BY_OTHER);
    writeNode(entityPath(entity1.getType(), entity1.getIdentifier()), entityEntryWithOwner(entity1.getIdentifier(), ""), shardDataTree);
    verifyGetOwnershipState(service, entity1, EntityOwnershipState.NO_OWNER);
    DOMEntity entity2 = new DOMEntity(ENTITY_TYPE, "two");
    Optional<EntityOwnershipState> state = service.getOwnershipState(entity2);
    assertEquals("getOwnershipState present", false, state.isPresent());
    writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, entity2.getIdentifier(), "member-1"), shardDataTree);
    writeNode(entityPath(entity2.getType(), entity2.getIdentifier()), ImmutableNodes.mapEntry(ENTITY_QNAME, ENTITY_ID_QNAME, entity2.getIdentifier()), shardDataTree);
    verifyGetOwnershipState(service, entity2, EntityOwnershipState.NO_OWNER);
    deleteNode(candidatePath(entityPath(entity2.getType(), entity2.getIdentifier()), "member-1"), shardDataTree);
    Optional<EntityOwnershipState> state2 = service.getOwnershipState(entity2);
    assertEquals("getOwnershipState present", false, state2.isPresent());
    service.close();
}
Also used : EntityOwnershipState(org.opendaylight.mdsal.eos.common.api.EntityOwnershipState) DOMEntity(org.opendaylight.mdsal.eos.dom.api.DOMEntity) Shard(org.opendaylight.controller.cluster.datastore.Shard) ShardDataTree(org.opendaylight.controller.cluster.datastore.ShardDataTree) Test(org.junit.Test)

Aggregations

EntityOwnershipState (org.opendaylight.mdsal.eos.common.api.EntityOwnershipState)6 Test (org.junit.Test)2 AbstractDataStore (org.opendaylight.controller.cluster.datastore.AbstractDataStore)1 MemberNode (org.opendaylight.controller.cluster.datastore.MemberNode)1 Shard (org.opendaylight.controller.cluster.datastore.Shard)1 ShardDataTree (org.opendaylight.controller.cluster.datastore.ShardDataTree)1 Entity (org.opendaylight.mdsal.eos.binding.api.Entity)1 DOMEntity (org.opendaylight.mdsal.eos.dom.api.DOMEntity)1 DOMEntityOwnershipCandidateRegistration (org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipCandidateRegistration)1 DOMEntityOwnershipChange (org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipChange)1 DOMEntityOwnershipService (org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipService)1