use of org.opendaylight.controller.cluster.raft.LeadershipTransferFailedException 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);
}
use of org.opendaylight.controller.cluster.raft.LeadershipTransferFailedException 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);
}
}
use of org.opendaylight.controller.cluster.raft.LeadershipTransferFailedException in project controller by opendaylight.
the class Shard method onMakeLeaderLocal.
private void onMakeLeaderLocal() {
LOG.debug("{}: onMakeLeaderLocal received", persistenceId());
if (isLeader()) {
getSender().tell(new Status.Success(null), getSelf());
return;
}
final ActorSelection leader = getLeader();
if (leader == null) {
// Leader is not present. The cluster is most likely trying to
// elect a leader and we should let that run its normal course
// TODO we can wait for the election to complete and retry the
// request. We can also let the caller retry by sending a flag
// in the response indicating the request is "reTryable".
getSender().tell(new Failure(new LeadershipTransferFailedException("We cannot initiate leadership transfer to local node. " + "Currently there is no leader for " + persistenceId())), getSelf());
return;
}
leader.tell(new RequestLeadership(getId(), getSender()), getSelf());
}
Aggregations