Java Code Examples for org.apache.flink.runtime.concurrent.FutureUtils#toJava()

The following examples show how to use org.apache.flink.runtime.concurrent.FutureUtils#toJava() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ClusterClient.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * Lists the currently running and finished jobs on the cluster.
 *
 * @return future collection of running and finished jobs
 * @throws Exception if no connection to the cluster could be established
 */
public CompletableFuture<Collection<JobStatusMessage>> listJobs() throws Exception {
	final ActorGateway jobManager = getJobManagerGateway();

	Future<Object> response = jobManager.ask(new RequestJobDetails(true, false), timeout);
	CompletableFuture<Object> responseFuture = FutureUtils.toJava(response);

	return responseFuture.thenApply((responseMessage) -> {
		if (responseMessage instanceof MultipleJobsDetails) {
			MultipleJobsDetails details = (MultipleJobsDetails) responseMessage;

			final Collection<JobDetails> jobDetails = details.getJobs();
			Collection<JobStatusMessage> flattenedDetails = new ArrayList<>(jobDetails.size());
			jobDetails.forEach(detail -> flattenedDetails.add(new JobStatusMessage(detail.getJobId(), detail.getJobName(), detail.getStatus(), detail.getStartTime())));
			return flattenedDetails;
		} else {
			throw new CompletionException(
				new IllegalStateException("Unknown JobManager response of type " + responseMessage.getClass()));
		}
	});
}
 
Example 2
Source File: ClusterClient.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * Triggers a savepoint for the job identified by the job id. The savepoint will be written to the given savepoint
 * directory, or {@link org.apache.flink.configuration.CheckpointingOptions#SAVEPOINT_DIRECTORY} if it is null.
 *
 * @param jobId job id
 * @param savepointDirectory directory the savepoint should be written to
 * @return path future where the savepoint is located
 * @throws FlinkException if no connection to the cluster could be established
 */
public CompletableFuture<String> triggerSavepoint(JobID jobId, @Nullable String savepointDirectory) throws FlinkException {
	final ActorGateway jobManager = getJobManagerGateway();

	Future<Object> response = jobManager.ask(new JobManagerMessages.TriggerSavepoint(jobId, Option.<String>apply(savepointDirectory)),
		new FiniteDuration(1, TimeUnit.HOURS));
	CompletableFuture<Object> responseFuture = FutureUtils.toJava(response);

	return responseFuture.thenApply((responseMessage) -> {
		if (responseMessage instanceof JobManagerMessages.TriggerSavepointSuccess) {
			JobManagerMessages.TriggerSavepointSuccess success = (JobManagerMessages.TriggerSavepointSuccess) responseMessage;
			return success.savepointPath();
		} else if (responseMessage instanceof JobManagerMessages.TriggerSavepointFailure) {
			JobManagerMessages.TriggerSavepointFailure failure = (JobManagerMessages.TriggerSavepointFailure) responseMessage;
			throw new CompletionException(failure.cause());
		} else {
			throw new CompletionException(
				new IllegalStateException("Unknown JobManager response of type " + responseMessage.getClass()));
		}
	});
}
 
Example 3
Source File: FencedAkkaInvocationHandler.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Override
public <V> CompletableFuture<V> callAsyncWithoutFencing(Callable<V> callable, Time timeout) {
	checkNotNull(callable, "callable");
	checkNotNull(timeout, "timeout");

	if (isLocal) {
		@SuppressWarnings("unchecked")
		CompletableFuture<V> resultFuture = (CompletableFuture<V>) FutureUtils.toJava(
			Patterns.ask(
				getActorRef(),
				new UnfencedMessage<>(new CallAsync(callable)),
				timeout.toMilliseconds()));

		return resultFuture;
	} else {
		throw new RuntimeException("Trying to send a Runnable to a remote actor at " +
			getActorRef().path() + ". This is not supported.");
	}
}
 
Example 4
Source File: ClusterClient.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * Requests the {@link JobStatus} of the job with the given {@link JobID}.
 */
public CompletableFuture<JobStatus> getJobStatus(JobID jobId) {
	final ActorGateway jobManager;
	try {
		jobManager = getJobManagerGateway();
	} catch (FlinkException e) {
		throw new RuntimeException("Could not retrieve JobManage gateway.", e);
	}

	Future<Object> response = jobManager.ask(JobManagerMessages.getRequestJobStatus(jobId), timeout);

	CompletableFuture<Object> javaFuture = FutureUtils.toJava(response);

	return javaFuture.thenApply((responseMessage) -> {
		if (responseMessage instanceof JobManagerMessages.CurrentJobStatus) {
			return ((JobManagerMessages.CurrentJobStatus) responseMessage).status();
		} else if (responseMessage instanceof JobManagerMessages.JobNotFound) {
			throw new CompletionException(
				new IllegalStateException("Could not find job with JobId " + jobId));
		} else {
			throw new CompletionException(
				new IllegalStateException("Unknown JobManager response of type " + responseMessage.getClass()));
		}
	});
}
 
Example 5
Source File: FencedAkkaInvocationHandler.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public <V> CompletableFuture<V> callAsyncWithoutFencing(Callable<V> callable, Time timeout) {
	checkNotNull(callable, "callable");
	checkNotNull(timeout, "timeout");

	if (isLocal) {
		@SuppressWarnings("unchecked")
		CompletableFuture<V> resultFuture = (CompletableFuture<V>) FutureUtils.toJava(
			Patterns.ask(
				getActorRef(),
				new UnfencedMessage<>(new CallAsync(callable)),
				timeout.toMilliseconds()));

		return resultFuture;
	} else {
		throw new RuntimeException("Trying to send a Runnable to a remote actor at " +
			getActorRef().path() + ". This is not supported.");
	}
}
 
Example 6
Source File: AkkaRpcServiceTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@AfterClass
public static void shutdown() throws InterruptedException, ExecutionException, TimeoutException {
	final CompletableFuture<Void> rpcTerminationFuture = akkaRpcService.stopService();
	final CompletableFuture<Terminated> actorSystemTerminationFuture = FutureUtils.toJava(actorSystem.terminate());

	FutureUtils
		.waitForAll(Arrays.asList(rpcTerminationFuture, actorSystemTerminationFuture))
		.get(TIMEOUT.toMilliseconds(), TimeUnit.MILLISECONDS);

	actorSystem = null;
	akkaRpcService = null;
}
 
Example 7
Source File: ClusterClient.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
public CompletableFuture<Acknowledge> disposeSavepoint(String savepointPath) throws FlinkException {
	final ActorGateway jobManager = getJobManagerGateway();

	Object msg = new JobManagerMessages.DisposeSavepoint(savepointPath);
	CompletableFuture<Object> responseFuture = FutureUtils.toJava(
		jobManager.ask(
			msg,
			timeout));

	return responseFuture.thenApply(
		(Object response) -> {
			if (response instanceof JobManagerMessages.DisposeSavepointSuccess$) {
				return Acknowledge.get();
			} else if (response instanceof JobManagerMessages.DisposeSavepointFailure) {
				JobManagerMessages.DisposeSavepointFailure failureResponse = (JobManagerMessages.DisposeSavepointFailure) response;

				if (failureResponse.cause() instanceof ClassNotFoundException) {
					throw new CompletionException(
						new ClassNotFoundException("Savepoint disposal failed, because of a " +
							"missing class. This is most likely caused by a custom state " +
							"instance, which cannot be disposed without the user code class " +
							"loader. Please provide the program jar with which you have created " +
							"the savepoint via -j <JAR> for disposal.",
							failureResponse.cause().getCause()));
				} else {
					throw new CompletionException(failureResponse.cause());
				}
			} else {
				throw new CompletionException(new FlinkRuntimeException("Unknown response type " + response.getClass().getSimpleName() + '.'));
			}
		});
}
 
Example 8
Source File: AsyncCallsTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@AfterClass
public static void shutdown() throws InterruptedException, ExecutionException, TimeoutException {
	final CompletableFuture<Void> rpcTerminationFuture = akkaRpcService.stopService();
	final CompletableFuture<Terminated> actorSystemTerminationFuture = FutureUtils.toJava(actorSystem.terminate());

	FutureUtils
		.waitForAll(Arrays.asList(rpcTerminationFuture, actorSystemTerminationFuture))
		.get(timeout.toMilliseconds(), TimeUnit.MILLISECONDS);
}
 
Example 9
Source File: RpcEndpointTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@AfterClass
public static void teardown() throws Exception {

	final CompletableFuture<Void> rpcTerminationFuture = rpcService.stopService();
	final CompletableFuture<Terminated> actorSystemTerminationFuture = FutureUtils.toJava(actorSystem.terminate());

	FutureUtils
		.waitForAll(Arrays.asList(rpcTerminationFuture, actorSystemTerminationFuture))
		.get(TIMEOUT.toMilliseconds(), TimeUnit.MILLISECONDS);
}
 
Example 10
Source File: AsyncCallsTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@AfterClass
public static void shutdown() throws InterruptedException, ExecutionException, TimeoutException {
	final CompletableFuture<Void> rpcTerminationFuture = akkaRpcService.stopService();
	final CompletableFuture<Terminated> actorSystemTerminationFuture = FutureUtils.toJava(actorSystem.terminate());

	FutureUtils
		.waitForAll(Arrays.asList(rpcTerminationFuture, actorSystemTerminationFuture))
		.get(timeout.toMilliseconds(), TimeUnit.MILLISECONDS);
}
 
Example 11
Source File: ActorTaskManagerGateway.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private CompletableFuture<TransientBlobKey> requestTaskManagerLog(TaskManagerMessages.RequestTaskManagerLog request, Time timeout) {
	Preconditions.checkNotNull(request);
	Preconditions.checkNotNull(timeout);

	scala.concurrent.Future<TransientBlobKey> blobKeyFuture = actorGateway
		.ask(
			request,
			new FiniteDuration(timeout.getSize(), timeout.getUnit()))
		.mapTo(ClassTag$.MODULE$.<TransientBlobKey>apply(TransientBlobKey.class));

	return FutureUtils.toJava(blobKeyFuture);
}
 
Example 12
Source File: ActorTaskManagerGateway.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<Acknowledge> stopTask(ExecutionAttemptID executionAttemptID, Time timeout) {
	Preconditions.checkNotNull(executionAttemptID);
	Preconditions.checkNotNull(timeout);

	scala.concurrent.Future<Acknowledge> stopResult = actorGateway.ask(
		new TaskMessages.StopTask(executionAttemptID),
		new FiniteDuration(timeout.getSize(), timeout.getUnit()))
		.mapTo(ClassTag$.MODULE$.<Acknowledge>apply(Acknowledge.class));

	return FutureUtils.toJava(stopResult);
}
 
Example 13
Source File: AkkaRpcServiceTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@AfterClass
public static void shutdown() throws InterruptedException, ExecutionException, TimeoutException {
	final CompletableFuture<Void> rpcTerminationFuture = akkaRpcService.stopService();
	final CompletableFuture<Terminated> actorSystemTerminationFuture = FutureUtils.toJava(actorSystem.terminate());

	FutureUtils
		.waitForAll(Arrays.asList(rpcTerminationFuture, actorSystemTerminationFuture))
		.get(TIMEOUT.toMilliseconds(), TimeUnit.MILLISECONDS);

	actorSystem = null;
	akkaRpcService = null;
}
 
Example 14
Source File: AkkaQueryServiceGateway.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<MetricDumpSerialization.MetricSerializationResult> queryMetrics(Time timeout) {
	return FutureUtils.toJava(
		Patterns.ask(queryServiceActorRef, MetricQueryService.getCreateDump(), timeout.toMilliseconds())
			.mapTo(ClassTag$.MODULE$.apply(MetricDumpSerialization.MetricSerializationResult.class))
	);
}
 
Example 15
Source File: AsyncCallsTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@AfterClass
public static void shutdown() throws InterruptedException, ExecutionException, TimeoutException {
	final CompletableFuture<Void> rpcTerminationFuture = akkaRpcService.stopService();
	final CompletableFuture<Terminated> actorSystemTerminationFuture = FutureUtils.toJava(actorSystem.terminate());

	FutureUtils
		.waitForAll(Arrays.asList(rpcTerminationFuture, actorSystemTerminationFuture))
		.get(timeout.toMilliseconds(), TimeUnit.MILLISECONDS);
}
 
Example 16
Source File: AkkaRpcService.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
@Override
public <T> CompletableFuture<T> execute(Callable<T> callable) {
	Future<T> scalaFuture = Futures.<T>future(callable, actorSystem.dispatcher());

	return FutureUtils.toJava(scalaFuture);
}
 
Example 17
Source File: AkkaRpcService.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public <T> CompletableFuture<T> execute(Callable<T> callable) {
	Future<T> scalaFuture = Futures.<T>future(callable, actorSystem.dispatcher());

	return FutureUtils.toJava(scalaFuture);
}
 
Example 18
Source File: AkkaInvocationHandler.java    From flink with Apache License 2.0 2 votes vote down vote up
/**
 * Sends the message to the RPC endpoint and returns a future containing
 * its response.
 *
 * @param message to send to the RPC endpoint
 * @param timeout time to wait until the response future is failed with a {@link TimeoutException}
 * @return Response future
 */
protected CompletableFuture<?> ask(Object message, Time timeout) {
	return FutureUtils.toJava(
		Patterns.ask(rpcEndpoint, message, timeout.toMilliseconds()));
}
 
Example 19
Source File: AkkaInvocationHandler.java    From flink with Apache License 2.0 2 votes vote down vote up
/**
 * Sends the message to the RPC endpoint and returns a future containing
 * its response.
 *
 * @param message to send to the RPC endpoint
 * @param timeout time to wait until the response future is failed with a {@link TimeoutException}
 * @return Response future
 */
protected CompletableFuture<?> ask(Object message, Time timeout) {
	return FutureUtils.toJava(
		Patterns.ask(rpcEndpoint, message, timeout.toMilliseconds()));
}
 
Example 20
Source File: AkkaInvocationHandler.java    From Flink-CEPplus with Apache License 2.0 2 votes vote down vote up
/**
 * Sends the message to the RPC endpoint and returns a future containing
 * its response.
 *
 * @param message to send to the RPC endpoint
 * @param timeout time to wait until the response future is failed with a {@link TimeoutException}
 * @return Response future
 */
protected CompletableFuture<?> ask(Object message, Time timeout) {
	return FutureUtils.toJava(
		Patterns.ask(rpcEndpoint, message, timeout.toMilliseconds()));
}