+ @Override
+ public Future<RpcResult<Void>> makeLeaderLocal(final MakeLeaderLocalInput input) {
+ final String shardName = input.getShardName();
+ if (Strings.isNullOrEmpty(shardName)) {
+ return newFailedRpcResultFuture("A valid shard name must be specified");
+ }
+
+ DataStoreType dataStoreType = input.getDataStoreType();
+ if (dataStoreType == null) {
+ return newFailedRpcResultFuture("A valid DataStoreType must be specified");
+ }
+
+ LOG.info("Moving leader to local node for shard {}, datastoreType {}", shardName, dataStoreType);
+
+ ActorContext actorContext = dataStoreType == DataStoreType.Config
+ ? configDataStore.getActorContext()
+ : operDataStore.getActorContext();
+
+ final scala.concurrent.Future<ActorRef> localShardReply =
+ actorContext.findLocalShardAsync(shardName);
+
+ final scala.concurrent.Promise<Object> makeLeaderLocalAsk = akka.dispatch.Futures.promise();
+ localShardReply.onComplete(new OnComplete<ActorRef>() {
+ @Override
+ public void onComplete(final Throwable failure, final ActorRef actorRef) throws Throwable {
+ if (failure != null) {
+ LOG.warn("No local shard found for {} datastoreType {} - Cannot request leadership transfer to"
+ + " local shard.", shardName, failure);
+ makeLeaderLocalAsk.failure(failure);
+ } else {
+ makeLeaderLocalAsk
+ .completeWith(actorContext
+ .executeOperationAsync(actorRef, MakeLeaderLocal.INSTANCE, makeLeaderLocalTimeout));
+ }
+ }
+ }, actorContext.getClientDispatcher());
+
+ final SettableFuture<RpcResult<Void>> future = SettableFuture.create();
+ makeLeaderLocalAsk.future().onComplete(new OnComplete<Object>() {
+ @Override
+ public void onComplete(final Throwable failure, final Object success) throws Throwable {
+ if (failure != null) {
+ LOG.error("Leadership transfer failed for shard {}.", shardName, failure);
+ future.set(RpcResultBuilder.<Void>failed().withError(ErrorType.APPLICATION,
+ "leadership transfer failed", failure).build());
+ return;
+ }
+
+ LOG.debug("Leadership transfer complete {}.", success);
+ future.set(RpcResultBuilder.<Void>success().build());
+ }
+ }, actorContext.getClientDispatcher());
+
+ return future;
+ }
+