Search in sources :

Example 1 with LocalShardNotFoundException

use of org.opendaylight.controller.cluster.datastore.exceptions.LocalShardNotFoundException in project controller by opendaylight.

the class CDSShardAccessImpl method makeLeaderLocal.

@Override
@Nonnull
public CompletionStage<Void> makeLeaderLocal() {
    // TODO when we have running make leader local operation
    // we should just return the same completion stage
    checkNotClosed();
    // TODO can we cache local shard actorRef?
    final Future<ActorRef> localShardReply = actorContext.findLocalShardAsync(ClusterUtils.getCleanShardName(prefix.getRootIdentifier()));
    // we have to tell local shard to make leader local
    final scala.concurrent.Promise<Object> makeLeaderLocalAsk = Futures.promise();
    localShardReply.onComplete(new OnComplete<ActorRef>() {

        @Override
        public void onComplete(final Throwable failure, final ActorRef actorRef) throws Throwable {
            if (failure instanceof LocalShardNotFoundException) {
                LOG.debug("No local shard found for {} - Cannot request leadership transfer to local shard.", getShardIdentifier(), failure);
                makeLeaderLocalAsk.failure(failure);
            } else if (failure != null) {
                // TODO should this be WARN?
                LOG.debug("Failed to find local shard for {} - Cannot request leadership transfer to local shard.", getShardIdentifier(), failure);
                makeLeaderLocalAsk.failure(failure);
            } else {
                makeLeaderLocalAsk.completeWith(actorContext.executeOperationAsync(actorRef, MakeLeaderLocal.INSTANCE, makeLeaderLocalTimeout));
            }
        }
    }, actorContext.getClientDispatcher());
    // we have to transform make leader local request result
    Future<Void> makeLeaderLocalFuture = makeLeaderLocalAsk.future().transform(new Mapper<Object, Void>() {

        @Override
        public Void apply(final Object parameter) {
            return null;
        }
    }, new Mapper<Throwable, Throwable>() {

        @Override
        public Throwable apply(final Throwable parameter) {
            if (parameter instanceof LeadershipTransferFailedException) {
                // do nothing with exception and just pass it as it is
                return parameter;
            }
            // wrap exception in LeadershipTransferFailedEx
            return new LeadershipTransferFailedException("Leadership transfer failed", parameter);
        }
    }, actorContext.getClientDispatcher());
    return FutureConverters.toJava(makeLeaderLocalFuture);
}
Also used : LeadershipTransferFailedException(org.opendaylight.controller.cluster.raft.LeadershipTransferFailedException) ActorRef(akka.actor.ActorRef) LocalShardNotFoundException(org.opendaylight.controller.cluster.datastore.exceptions.LocalShardNotFoundException) Nonnull(javax.annotation.Nonnull)

Example 2 with LocalShardNotFoundException

use of org.opendaylight.controller.cluster.datastore.exceptions.LocalShardNotFoundException in project controller by opendaylight.

the class CDSShardAccessImplTest method testMakeLeaderLocal.

@Test
@SuppressWarnings("checkstyle:IllegalCatch")
public void testMakeLeaderLocal() throws Exception {
    final FiniteDuration timeout = new FiniteDuration(5, TimeUnit.SECONDS);
    final ActorRef localShardRef = mock(ActorRef.class);
    final Future<ActorRef> localShardRefFuture = Futures.successful(localShardRef);
    doReturn(localShardRefFuture).when(context).findLocalShardAsync(any());
    // MakeLeaderLocal will reply with success
    doReturn(Futures.successful(null)).when(context).executeOperationAsync((ActorRef) any(), any(), any());
    doReturn(getSystem().dispatcher()).when(context).getClientDispatcher();
    assertEquals(waitOnAsyncTask(shardAccess.makeLeaderLocal(), timeout), null);
    // MakeLeaderLocal will reply with failure
    doReturn(Futures.failed(new LeadershipTransferFailedException("Failure"))).when(context).executeOperationAsync((ActorRef) any(), any(), any());
    try {
        waitOnAsyncTask(shardAccess.makeLeaderLocal(), timeout);
        fail("makeLeaderLocal operation should not be successful");
    } catch (final Exception e) {
        assertTrue(e instanceof LeadershipTransferFailedException);
    }
    // we don't even find local shard
    doReturn(Futures.failed(new LocalShardNotFoundException("Local shard not found"))).when(context).findLocalShardAsync(any());
    try {
        waitOnAsyncTask(shardAccess.makeLeaderLocal(), timeout);
        fail("makeLeaderLocal operation should not be successful");
    } catch (final Exception e) {
        assertTrue(e instanceof LeadershipTransferFailedException);
        assertTrue(e.getCause() instanceof LocalShardNotFoundException);
    }
    // closed shard access should throw IllegalStateEx
    shardAccess.close();
    try {
        shardAccess.makeLeaderLocal();
        fail("Should have thrown IllegalStateEx. ShardAccess is closed");
    } catch (final Exception e) {
        assertTrue(e instanceof IllegalStateException);
    }
}
Also used : LeadershipTransferFailedException(org.opendaylight.controller.cluster.raft.LeadershipTransferFailedException) ActorRef(akka.actor.ActorRef) FiniteDuration(scala.concurrent.duration.FiniteDuration) LocalShardNotFoundException(org.opendaylight.controller.cluster.datastore.exceptions.LocalShardNotFoundException) LocalShardNotFoundException(org.opendaylight.controller.cluster.datastore.exceptions.LocalShardNotFoundException) LeadershipTransferFailedException(org.opendaylight.controller.cluster.raft.LeadershipTransferFailedException) AbstractActorTest(org.opendaylight.controller.cluster.datastore.AbstractActorTest) Test(org.junit.Test)

Aggregations

ActorRef (akka.actor.ActorRef)2 LocalShardNotFoundException (org.opendaylight.controller.cluster.datastore.exceptions.LocalShardNotFoundException)2 LeadershipTransferFailedException (org.opendaylight.controller.cluster.raft.LeadershipTransferFailedException)2 Nonnull (javax.annotation.Nonnull)1 Test (org.junit.Test)1 AbstractActorTest (org.opendaylight.controller.cluster.datastore.AbstractActorTest)1 FiniteDuration (scala.concurrent.duration.FiniteDuration)1