Search in sources :

Example 6 with PrimaryShardInfo

use of org.opendaylight.controller.cluster.datastore.messages.PrimaryShardInfo in project controller by opendaylight.

the class ActorContextTest method testFindPrimaryExceptions.

@SuppressWarnings("checkstyle:IllegalCatch")
private static void testFindPrimaryExceptions(final Object expectedException) throws Exception {
    ActorRef shardManager = getSystem().actorOf(MessageCollectorActor.props());
    DatastoreContext dataStoreContext = DatastoreContext.newBuilder().logicalStoreType(LogicalDatastoreType.CONFIGURATION).shardLeaderElectionTimeout(100, TimeUnit.MILLISECONDS).build();
    ActorContext actorContext = new ActorContext(getSystem(), shardManager, mock(ClusterWrapper.class), mock(Configuration.class), dataStoreContext, new PrimaryShardInfoFutureCache()) {

        @Override
        protected Future<Object> doAsk(final ActorRef actorRef, final Object message, final Timeout timeout) {
            return Futures.successful(expectedException);
        }
    };
    Future<PrimaryShardInfo> foobar = actorContext.findPrimaryShardAsync("foobar");
    try {
        Await.result(foobar, Duration.apply(100, TimeUnit.MILLISECONDS));
        fail("Expected" + expectedException.getClass().toString());
    } catch (Exception e) {
        if (!expectedException.getClass().isInstance(e)) {
            fail("Expected Exception of type " + expectedException.getClass().toString());
        }
    }
    Future<PrimaryShardInfo> cached = actorContext.getPrimaryShardInfoCache().getIfPresent("foobar");
    assertNull(cached);
}
Also used : Configuration(org.opendaylight.controller.cluster.datastore.config.Configuration) DatastoreContext(org.opendaylight.controller.cluster.datastore.DatastoreContext) ActorRef(akka.actor.ActorRef) TestActorRef(akka.testkit.TestActorRef) Timeout(akka.util.Timeout) ClusterWrapper(org.opendaylight.controller.cluster.datastore.ClusterWrapper) PrimaryShardInfo(org.opendaylight.controller.cluster.datastore.messages.PrimaryShardInfo) NotInitializedException(org.opendaylight.controller.cluster.datastore.exceptions.NotInitializedException) PrimaryNotFoundException(org.opendaylight.controller.cluster.datastore.exceptions.PrimaryNotFoundException) NoShardLeaderException(org.opendaylight.controller.cluster.datastore.exceptions.NoShardLeaderException)

Example 7 with PrimaryShardInfo

use of org.opendaylight.controller.cluster.datastore.messages.PrimaryShardInfo in project controller by opendaylight.

the class TransactionChainProxy method findPrimaryShard.

/**
 * This method is overridden to ensure the previous Tx's ready operations complete
 * before we initiate the next Tx in the chain to avoid creation failures if the
 * previous Tx's ready operations haven't completed yet.
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
protected Future<PrimaryShardInfo> findPrimaryShard(final String shardName, final TransactionIdentifier txId) {
    // Read current state atomically
    final State localState = currentState;
    // There are no outstanding futures, shortcut
    Future<?> previous = localState.previousFuture();
    if (previous == null) {
        return combineFutureWithPossiblePriorReadOnlyTxFutures(parent.findPrimaryShard(shardName, txId), txId);
    }
    final String previousTransactionId;
    if (localState instanceof Pending) {
        previousTransactionId = ((Pending) localState).getIdentifier().toString();
        LOG.debug("Tx: {} - waiting for ready futures with pending Tx {}", txId, previousTransactionId);
    } else {
        previousTransactionId = "";
        LOG.debug("Waiting for ready futures on chain {}", getHistoryId());
    }
    previous = combineFutureWithPossiblePriorReadOnlyTxFutures(previous, txId);
    // Add a callback for completion of the combined Futures.
    final Promise<PrimaryShardInfo> returnPromise = Futures.promise();
    final OnComplete onComplete = new OnComplete() {

        @Override
        public void onComplete(final Throwable failure, final Object notUsed) {
            if (failure != null) {
                // A Ready Future failed so fail the returned Promise.
                LOG.error("Tx: {} - ready future failed for previous Tx {}", txId, previousTransactionId);
                returnPromise.failure(failure);
            } else {
                LOG.debug("Tx: {} - previous Tx {} readied - proceeding to FindPrimaryShard", txId, previousTransactionId);
                // Send the FindPrimaryShard message and use the resulting Future to complete the
                // returned Promise.
                returnPromise.completeWith(parent.findPrimaryShard(shardName, txId));
            }
        }
    };
    previous.onComplete(onComplete, getActorContext().getClientDispatcher());
    return returnPromise.future();
}
Also used : PrimaryShardInfo(org.opendaylight.controller.cluster.datastore.messages.PrimaryShardInfo) OnComplete(akka.dispatch.OnComplete)

Example 8 with PrimaryShardInfo

use of org.opendaylight.controller.cluster.datastore.messages.PrimaryShardInfo in project controller by opendaylight.

the class ShardManagerTest method testShardAvailabilityChangeOnMemberWithNameContainedInLeaderIdUnreachable.

@Test
public void testShardAvailabilityChangeOnMemberWithNameContainedInLeaderIdUnreachable() throws Exception {
    LOG.info("testShardAvailabilityChangeOnMemberWithNameContainedInLeaderIdUnreachable starting");
    String shardManagerID = ShardManagerIdentifier.builder().type(shardMrgIDSuffix).build().toString();
    MockConfiguration mockConfig = new MockConfiguration(ImmutableMap.<String, List<String>>builder().put("default", Arrays.asList("member-256", "member-2")).build());
    // Create an ActorSystem, ShardManager and actor for member-256.
    final ActorSystem system256 = newActorSystem("Member256");
    // 2562 is the tcp port of Member256 in src/test/resources/application.conf.
    Cluster.get(system256).join(AddressFromURIString.parse("akka://cluster-test@127.0.0.1:2562"));
    final ActorRef mockShardActor256 = newMockShardActor(system256, Shard.DEFAULT_NAME, "member-256");
    final PrimaryShardInfoFutureCache primaryShardInfoCache = new PrimaryShardInfoFutureCache();
    // ShardManager must be created with shard configuration to let its localShards has shards.
    final TestActorRef<TestShardManager> shardManager256 = TestActorRef.create(system256, newTestShardMgrBuilder(mockConfig).shardActor(mockShardActor256).cluster(new ClusterWrapperImpl(system256)).primaryShardInfoCache(primaryShardInfoCache).props().withDispatcher(Dispatchers.DefaultDispatcherId()), shardManagerID);
    // Create an ActorSystem, ShardManager and actor for member-2 whose name is contained in member-256.
    final ActorSystem system2 = newActorSystem("Member2");
    // Join member-2 into the cluster of member-256.
    Cluster.get(system2).join(AddressFromURIString.parse("akka://cluster-test@127.0.0.1:2562"));
    final ActorRef mockShardActor2 = newMockShardActor(system2, Shard.DEFAULT_NAME, "member-2");
    final TestActorRef<TestShardManager> shardManager2 = TestActorRef.create(system2, newTestShardMgrBuilder(mockConfig).shardActor(mockShardActor2).cluster(new ClusterWrapperImpl(system2)).props().withDispatcher(Dispatchers.DefaultDispatcherId()), shardManagerID);
    new TestKit(system256) {

        {
            shardManager256.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
            shardManager2.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
            shardManager256.tell(new ActorInitialized(), mockShardActor256);
            shardManager2.tell(new ActorInitialized(), mockShardActor2);
            String memberId256 = "member-256-shard-default-" + shardMrgIDSuffix;
            String memberId2 = "member-2-shard-default-" + shardMrgIDSuffix;
            shardManager256.tell(new ShardLeaderStateChanged(memberId256, memberId256, mock(DataTree.class), DataStoreVersions.CURRENT_VERSION), mockShardActor256);
            shardManager256.tell(new RoleChangeNotification(memberId256, RaftState.Candidate.name(), RaftState.Leader.name()), mockShardActor256);
            shardManager2.tell(new ShardLeaderStateChanged(memberId2, memberId256, mock(DataTree.class), DataStoreVersions.CURRENT_VERSION), mockShardActor2);
            shardManager2.tell(new RoleChangeNotification(memberId2, RaftState.Candidate.name(), RaftState.Follower.name()), mockShardActor2);
            shardManager256.underlyingActor().waitForMemberUp();
            shardManager256.tell(new FindPrimary("default", true), getRef());
            LocalPrimaryShardFound found = expectMsgClass(duration("5 seconds"), LocalPrimaryShardFound.class);
            String path = found.getPrimaryPath();
            assertTrue("Unexpected primary path " + path + " which must on member-256", path.contains("member-256-shard-default-config"));
            PrimaryShardInfo primaryShardInfo = new PrimaryShardInfo(system256.actorSelection(mockShardActor256.path()), DataStoreVersions.CURRENT_VERSION);
            primaryShardInfoCache.putSuccessful("default", primaryShardInfo);
            // Simulate member-2 become unreachable.
            shardManager256.tell(MockClusterWrapper.createUnreachableMember("member-2", "akka://cluster-test@127.0.0.1:2558"), getRef());
            shardManager256.underlyingActor().waitForUnreachableMember();
            // Make sure leader shard on member-256 is still leader and still in the cache.
            shardManager256.tell(new FindPrimary("default", true), getRef());
            found = expectMsgClass(duration("5 seconds"), LocalPrimaryShardFound.class);
            path = found.getPrimaryPath();
            assertTrue("Unexpected primary path " + path + " which must still not on member-256", path.contains("member-256-shard-default-config"));
            Future<PrimaryShardInfo> futurePrimaryShard = primaryShardInfoCache.getIfPresent("default");
            futurePrimaryShard.onComplete(new OnComplete<PrimaryShardInfo>() {

                @Override
                public void onComplete(final Throwable failure, final PrimaryShardInfo futurePrimaryShardInfo) {
                    if (failure != null) {
                        assertTrue("Primary shard info is unexpectedly removed from primaryShardInfoCache", false);
                    } else {
                        assertEquals("Expected primaryShardInfoCache entry", primaryShardInfo, futurePrimaryShardInfo);
                    }
                }
            }, system256.dispatchers().defaultGlobalDispatcher());
        }
    };
    LOG.info("testShardAvailabilityChangeOnMemberWithNameContainedInLeaderIdUnreachable ending");
}
Also used : ActorSystem(akka.actor.ActorSystem) ClusterWrapperImpl(org.opendaylight.controller.cluster.datastore.ClusterWrapperImpl) UpdateSchemaContext(org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext) ShardLeaderStateChanged(org.opendaylight.controller.cluster.datastore.messages.ShardLeaderStateChanged) ActorRef(akka.actor.ActorRef) TestActorRef(akka.testkit.TestActorRef) RoleChangeNotification(org.opendaylight.controller.cluster.notifications.RoleChangeNotification) AddressFromURIString(akka.actor.AddressFromURIString) TestKit(akka.testkit.javadsl.TestKit) FindPrimary(org.opendaylight.controller.cluster.datastore.messages.FindPrimary) LocalPrimaryShardFound(org.opendaylight.controller.cluster.datastore.messages.LocalPrimaryShardFound) PrimaryShardInfo(org.opendaylight.controller.cluster.datastore.messages.PrimaryShardInfo) PrimaryShardInfoFutureCache(org.opendaylight.controller.cluster.datastore.utils.PrimaryShardInfoFutureCache) MockConfiguration(org.opendaylight.controller.cluster.datastore.utils.MockConfiguration) ActorInitialized(org.opendaylight.controller.cluster.datastore.messages.ActorInitialized) Test(org.junit.Test) AbstractShardManagerTest(org.opendaylight.controller.cluster.datastore.AbstractShardManagerTest)

Example 9 with PrimaryShardInfo

use of org.opendaylight.controller.cluster.datastore.messages.PrimaryShardInfo in project controller by opendaylight.

the class ActorContextTest method testFindPrimaryShardAsyncLocalPrimaryFound.

@Test
public void testFindPrimaryShardAsyncLocalPrimaryFound() throws Exception {
    ActorRef shardManager = getSystem().actorOf(MessageCollectorActor.props());
    DatastoreContext dataStoreContext = DatastoreContext.newBuilder().logicalStoreType(LogicalDatastoreType.CONFIGURATION).shardLeaderElectionTimeout(100, TimeUnit.MILLISECONDS).build();
    final DataTree mockDataTree = Mockito.mock(DataTree.class);
    final String expPrimaryPath = "akka://test-system/find-primary-shard";
    ActorContext actorContext = new ActorContext(getSystem(), shardManager, mock(ClusterWrapper.class), mock(Configuration.class), dataStoreContext, new PrimaryShardInfoFutureCache()) {

        @Override
        protected Future<Object> doAsk(final ActorRef actorRef, final Object message, final Timeout timeout) {
            return Futures.successful((Object) new LocalPrimaryShardFound(expPrimaryPath, mockDataTree));
        }
    };
    Future<PrimaryShardInfo> foobar = actorContext.findPrimaryShardAsync("foobar");
    PrimaryShardInfo actual = Await.result(foobar, Duration.apply(5000, TimeUnit.MILLISECONDS));
    assertNotNull(actual);
    assertEquals("LocalShardDataTree present", true, actual.getLocalShardDataTree().isPresent());
    assertSame("LocalShardDataTree", mockDataTree, actual.getLocalShardDataTree().get());
    assertTrue("Unexpected PrimaryShardActor path " + actual.getPrimaryShardActor().path(), expPrimaryPath.endsWith(actual.getPrimaryShardActor().pathString()));
    assertEquals("getPrimaryShardVersion", DataStoreVersions.CURRENT_VERSION, actual.getPrimaryShardVersion());
    Future<PrimaryShardInfo> cached = actorContext.getPrimaryShardInfoCache().getIfPresent("foobar");
    PrimaryShardInfo cachedInfo = Await.result(cached, FiniteDuration.apply(1, TimeUnit.MILLISECONDS));
    assertEquals(cachedInfo, actual);
    actorContext.getPrimaryShardInfoCache().remove("foobar");
    cached = actorContext.getPrimaryShardInfoCache().getIfPresent("foobar");
    assertNull(cached);
}
Also used : Configuration(org.opendaylight.controller.cluster.datastore.config.Configuration) ActorRef(akka.actor.ActorRef) TestActorRef(akka.testkit.TestActorRef) Timeout(akka.util.Timeout) ClusterWrapper(org.opendaylight.controller.cluster.datastore.ClusterWrapper) LocalPrimaryShardFound(org.opendaylight.controller.cluster.datastore.messages.LocalPrimaryShardFound) DatastoreContext(org.opendaylight.controller.cluster.datastore.DatastoreContext) DataTree(org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree) PrimaryShardInfo(org.opendaylight.controller.cluster.datastore.messages.PrimaryShardInfo) AbstractActorTest(org.opendaylight.controller.cluster.datastore.AbstractActorTest) Test(org.junit.Test)

Example 10 with PrimaryShardInfo

use of org.opendaylight.controller.cluster.datastore.messages.PrimaryShardInfo in project controller by opendaylight.

the class PrimaryShardInfoFutureCacheTest method testOperations.

@Test
public void testOperations() {
    PrimaryShardInfoFutureCache cache = new PrimaryShardInfoFutureCache();
    assertEquals("getIfPresent", null, cache.getIfPresent("foo"));
    PrimaryShardInfo shardInfo = new PrimaryShardInfo(mock(ActorSelection.class), DataStoreVersions.CURRENT_VERSION);
    cache.putSuccessful("foo", shardInfo);
    Future<PrimaryShardInfo> future = cache.getIfPresent("foo");
    assertNotNull("Null future", future);
    assertEquals("getIfPresent", shardInfo, future.value().get().get());
    cache.remove("foo");
    assertEquals("getIfPresent", null, cache.getIfPresent("foo"));
}
Also used : ActorSelection(akka.actor.ActorSelection) PrimaryShardInfo(org.opendaylight.controller.cluster.datastore.messages.PrimaryShardInfo) Test(org.junit.Test)

Aggregations

PrimaryShardInfo (org.opendaylight.controller.cluster.datastore.messages.PrimaryShardInfo)13 ActorSelection (akka.actor.ActorSelection)6 ActorRef (akka.actor.ActorRef)5 TestActorRef (akka.testkit.TestActorRef)5 Test (org.junit.Test)5 LocalPrimaryShardFound (org.opendaylight.controller.cluster.datastore.messages.LocalPrimaryShardFound)4 ActorContext (org.opendaylight.controller.cluster.datastore.utils.ActorContext)4 Timeout (akka.util.Timeout)3 ClientActorContext (org.opendaylight.controller.cluster.access.client.ClientActorContext)3 ClusterWrapper (org.opendaylight.controller.cluster.datastore.ClusterWrapper)3 DatastoreContext (org.opendaylight.controller.cluster.datastore.DatastoreContext)3 Configuration (org.opendaylight.controller.cluster.datastore.config.Configuration)3 FindPrimary (org.opendaylight.controller.cluster.datastore.messages.FindPrimary)3 RemotePrimaryShardFound (org.opendaylight.controller.cluster.datastore.messages.RemotePrimaryShardFound)3 ActorSystem (akka.actor.ActorSystem)2 AddressFromURIString (akka.actor.AddressFromURIString)2 TestKit (akka.testkit.javadsl.TestKit)2 AbstractActorTest (org.opendaylight.controller.cluster.datastore.AbstractActorTest)2 AbstractShardManagerTest (org.opendaylight.controller.cluster.datastore.AbstractShardManagerTest)2 ClusterWrapperImpl (org.opendaylight.controller.cluster.datastore.ClusterWrapperImpl)2