Java Code Examples for org.apache.flink.runtime.rest.handler.HandlerRequest#getPathParameter()

The following examples show how to use org.apache.flink.runtime.rest.handler.HandlerRequest#getPathParameter() . 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: JobExecutionResultHandler.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
protected CompletableFuture<JobExecutionResultResponseBody> handleRequest(
		@Nonnull final HandlerRequest<EmptyRequestBody, JobMessageParameters> request,
		@Nonnull final RestfulGateway gateway) throws RestHandlerException {

	final JobID jobId = request.getPathParameter(JobIDPathParameter.class);

	final CompletableFuture<JobStatus> jobStatusFuture = gateway.requestJobStatus(jobId, timeout);

	return jobStatusFuture.thenCompose(
		jobStatus -> {
			if (jobStatus.isGloballyTerminalState()) {
				return gateway
					.requestJobResult(jobId, timeout)
					.thenApply(JobExecutionResultResponseBody::created);
			} else {
				return CompletableFuture.completedFuture(
					JobExecutionResultResponseBody.inProgress());
			}
		}).exceptionally(throwable -> {
			throw propagateException(throwable);
		});
}
 
Example 2
Source File: AbstractCheckpointHandler.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
protected R handleRequest(HandlerRequest<EmptyRequestBody, M> request, AccessExecutionGraph executionGraph) throws RestHandlerException {
	final long checkpointId = request.getPathParameter(CheckpointIdPathParameter.class);

	final CheckpointStatsSnapshot checkpointStatsSnapshot = executionGraph.getCheckpointStatsSnapshot();

	if (checkpointStatsSnapshot != null) {
		AbstractCheckpointStats checkpointStats = checkpointStatsSnapshot.getHistory().getCheckpointById(checkpointId);

		if (checkpointStats != null) {
			checkpointStatsCache.tryAdd(checkpointStats);
		} else {
			checkpointStats = checkpointStatsCache.tryGet(checkpointId);
		}

		if (checkpointStats != null) {
			return handleCheckpointRequest(request, checkpointStats);
		} else {
			throw new RestHandlerException("Could not find checkpointing statistics for checkpoint " + checkpointId + '.', HttpResponseStatus.NOT_FOUND);
		}
	} else {
		throw new RestHandlerException("Checkpointing was not enabled for job " + executionGraph.getJobID() + '.', HttpResponseStatus.NOT_FOUND);
	}
}
 
Example 3
Source File: JarDeleteHandler.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
protected CompletableFuture<EmptyResponseBody> handleRequest(
		@Nonnull final HandlerRequest<EmptyRequestBody, JarDeleteMessageParameters> request,
		@Nonnull final RestfulGateway gateway) throws RestHandlerException {

	final String jarId = request.getPathParameter(JarIdPathParameter.class);
	return CompletableFuture.supplyAsync(() -> {
		final Path jarToDelete = jarDir.resolve(jarId);
		if (!Files.exists(jarToDelete)) {
			throw new CompletionException(new RestHandlerException(
				String.format("File %s does not exist in %s.", jarId, jarDir),
				HttpResponseStatus.BAD_REQUEST));
		} else {
			try {
				Files.delete(jarToDelete);
				return EmptyResponseBody.getInstance();
			} catch (final IOException e) {
				throw new CompletionException(new RestHandlerException(
					String.format("Failed to delete jar %s.", jarToDelete),
					HttpResponseStatus.INTERNAL_SERVER_ERROR,
					e));
			}
		}
	}, executor);
}
 
Example 4
Source File: JarDeleteHandler.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
protected CompletableFuture<EmptyResponseBody> handleRequest(
		@Nonnull final HandlerRequest<EmptyRequestBody, JarDeleteMessageParameters> request,
		@Nonnull final RestfulGateway gateway) throws RestHandlerException {

	final String jarId = request.getPathParameter(JarIdPathParameter.class);
	return CompletableFuture.supplyAsync(() -> {
		final Path jarToDelete = jarDir.resolve(jarId);
		if (!Files.exists(jarToDelete)) {
			throw new CompletionException(new RestHandlerException(
				String.format("File %s does not exist in %s.", jarId, jarDir),
				HttpResponseStatus.BAD_REQUEST));
		} else {
			try {
				Files.delete(jarToDelete);
				return EmptyResponseBody.getInstance();
			} catch (final IOException e) {
				throw new CompletionException(new RestHandlerException(
					String.format("Failed to delete jar %s.", jarToDelete),
					HttpResponseStatus.INTERNAL_SERVER_ERROR,
					e));
			}
		}
	}, executor);
}
 
Example 5
Source File: SavepointHandlers.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
protected CompletableFuture<String> triggerOperation(
		final HandlerRequest<StopWithSavepointRequestBody, SavepointTriggerMessageParameters> request,
		final RestfulGateway gateway) throws RestHandlerException {

	final JobID jobId = request.getPathParameter(JobIDPathParameter.class);
	final String requestedTargetDirectory = request.getRequestBody().getTargetDirectory();

	if (requestedTargetDirectory == null && defaultSavepointDir == null) {
		throw new RestHandlerException(
				String.format("Config key [%s] is not set. Property [%s] must be provided.",
						CheckpointingOptions.SAVEPOINT_DIRECTORY.key(),
						StopWithSavepointRequestBody.FIELD_NAME_TARGET_DIRECTORY),
				HttpResponseStatus.BAD_REQUEST);
	}

	final boolean advanceToEndOfEventTime = request.getRequestBody().shouldDrain();
	final String targetDirectory = requestedTargetDirectory != null ? requestedTargetDirectory : defaultSavepointDir;
	return gateway.stopWithSavepoint(jobId, targetDirectory, advanceToEndOfEventTime, RpcUtils.INF_TIMEOUT);
}
 
Example 6
Source File: RestClusterClientTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
protected CompletableFuture<AsynchronousOperationResult<AsynchronousOperationInfo>> handleRequest(@Nonnull HandlerRequest<EmptyRequestBody, SavepointDisposalStatusMessageParameters> request, @Nonnull DispatcherGateway gateway) throws RestHandlerException {
	final TriggerId actualTriggerId = request.getPathParameter(TriggerIdPathParameter.class);

	if (actualTriggerId.equals(triggerId)) {
		final OptionalFailure<AsynchronousOperationInfo> nextResponse = responses.poll();

		if (nextResponse != null) {
			if (nextResponse.isFailure()) {
				throw new RestHandlerException("Failure", HttpResponseStatus.BAD_REQUEST, nextResponse.getFailureCause());
			} else {
				return CompletableFuture.completedFuture(AsynchronousOperationResult.completed(nextResponse.getUnchecked()));
			}
		} else {
			throw new AssertionError();
		}
	} else {
		throw new AssertionError();
	}
}
 
Example 7
Source File: SavepointHandlers.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
protected CompletableFuture<String> triggerOperation(
		final HandlerRequest<StopWithSavepointRequestBody, SavepointTriggerMessageParameters> request,
		final RestfulGateway gateway) throws RestHandlerException {

	final JobID jobId = request.getPathParameter(JobIDPathParameter.class);
	final String requestedTargetDirectory = request.getRequestBody().getTargetDirectory();

	if (requestedTargetDirectory == null && defaultSavepointDir == null) {
		throw new RestHandlerException(
				String.format("Config key [%s] is not set. Property [%s] must be provided.",
						CheckpointingOptions.SAVEPOINT_DIRECTORY.key(),
						StopWithSavepointRequestBody.FIELD_NAME_TARGET_DIRECTORY),
				HttpResponseStatus.BAD_REQUEST);
	}

	final boolean advanceToEndOfEventTime = request.getRequestBody().shouldDrain();
	final String targetDirectory = requestedTargetDirectory != null ? requestedTargetDirectory : defaultSavepointDir;
	return gateway.stopWithSavepoint(jobId, targetDirectory, advanceToEndOfEventTime, RpcUtils.INF_TIMEOUT);
}
 
Example 8
Source File: SubtaskExecutionAttemptDetailsHandler.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
protected SubtaskExecutionAttemptDetailsInfo handleRequest(
		HandlerRequest<EmptyRequestBody, SubtaskAttemptMessageParameters> request,
		AccessExecution execution) throws RestHandlerException {

	final JobID jobID = request.getPathParameter(JobIDPathParameter.class);
	final JobVertexID jobVertexID = request.getPathParameter(JobVertexIdPathParameter.class);

	return createDetailsInfo(execution, jobID, jobVertexID, metricFetcher);
}
 
Example 9
Source File: AbstractJobVertexHandler.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
protected R handleRequest(
		HandlerRequest<EmptyRequestBody, M> request,
		AccessExecutionGraph executionGraph) throws RestHandlerException {

	final JobVertexID jobVertexID = request.getPathParameter(JobVertexIdPathParameter.class);
	final AccessExecutionJobVertex jobVertex = executionGraph.getJobVertex(jobVertexID);

	if (jobVertex == null) {
		throw new RestHandlerException("No vertex with ID '" + jobVertexID + "' exists.", HttpResponseStatus.NOT_FOUND);
	}

	return handleRequest(request, jobVertex);
}
 
Example 10
Source File: JobVertexBackPressureHandler.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
protected CompletableFuture<JobVertexBackPressureInfo> handleRequest(
		@Nonnull HandlerRequest<EmptyRequestBody, JobVertexMessageParameters> request,
		@Nonnull RestfulGateway gateway) throws RestHandlerException {
	final JobID jobId = request.getPathParameter(JobIDPathParameter.class);
	final JobVertexID jobVertexId = request.getPathParameter(JobVertexIdPathParameter.class);
	return gateway
		.requestOperatorBackPressureStats(jobId, jobVertexId)
		.thenApply(
			operatorBackPressureStats ->
				operatorBackPressureStats.getOperatorBackPressureStats().map(
					JobVertexBackPressureHandler::createJobVertexBackPressureInfo).orElse(
					JobVertexBackPressureInfo.deprecated()));
}
 
Example 11
Source File: RestClusterClientSavepointTriggerTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
protected CompletableFuture<AsynchronousOperationResult<SavepointInfo>> handleRequest(
		@Nonnull HandlerRequest<EmptyRequestBody, SavepointStatusMessageParameters> request,
		@Nonnull DispatcherGateway gateway) throws RestHandlerException {

	final TriggerId triggerId = request.getPathParameter(TriggerIdPathParameter.class);
	return CompletableFuture.completedFuture(AsynchronousOperationResult.completed(savepointHandlerLogic.apply(triggerId)));
}
 
Example 12
Source File: TaskManagerThreadDumpHandler.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
protected CompletableFuture<ThreadDumpInfo> handleRequest(
		@Nonnull HandlerRequest<EmptyRequestBody, TaskManagerMessageParameters> request,
		@Nonnull ResourceManagerGateway gateway) throws RestHandlerException {
	final ResourceID taskManagerId = request.getPathParameter(TaskManagerIdPathParameter.class);
	return gateway.requestThreadDump(taskManagerId, timeout);
}
 
Example 13
Source File: TaskCheckpointStatisticDetailsHandler.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
protected TaskCheckpointStatisticsWithSubtaskDetails handleCheckpointRequest(
		HandlerRequest<EmptyRequestBody, TaskCheckpointMessageParameters> request,
		AbstractCheckpointStats checkpointStats) throws RestHandlerException {

	final JobVertexID jobVertexId = request.getPathParameter(JobVertexIdPathParameter.class);

	final TaskStateStats taskStatistics = checkpointStats.getTaskStateStats(jobVertexId);

	if (taskStatistics == null) {
		throw new NotFoundException("There is no checkpoint statistics for task " + jobVertexId + '.');
	}

	return createCheckpointDetails(checkpointStats, taskStatistics);
}
 
Example 14
Source File: SubtaskMetricsHandler.java    From flink with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
protected MetricStore.ComponentMetricStore getComponentMetricStore(
		HandlerRequest<EmptyRequestBody, SubtaskMetricsMessageParameters> request,
		MetricStore metricStore) {

	final JobID jobId = request.getPathParameter(JobIDPathParameter.class);
	final JobVertexID vertexId = request.getPathParameter(JobVertexIdPathParameter.class);
	final int subtaskIndex = request.getPathParameter(SubtaskIndexPathParameter.class);

	return metricStore.getSubtaskMetricStore(jobId.toString(), vertexId.toString(), subtaskIndex);
}
 
Example 15
Source File: TaskManagerMetricsHandler.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
protected MetricStore.ComponentMetricStore getComponentMetricStore(
		final HandlerRequest<EmptyRequestBody, TaskManagerMetricsMessageParameters> request,
		final MetricStore metricStore) {
	final ResourceID taskManagerId = request.getPathParameter(TaskManagerIdPathParameter.class);
	return metricStore.getTaskManagerMetricStore(taskManagerId.toString());
}
 
Example 16
Source File: AbstractSubtaskHandler.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
protected R handleRequest(
		HandlerRequest<EmptyRequestBody, M> request,
		AccessExecutionJobVertex jobVertex) throws RestHandlerException {

	final Integer subtaskIndex = request.getPathParameter(SubtaskIndexPathParameter.class);
	final AccessExecutionVertex[] executionVertices = jobVertex.getTaskVertices();

	if (subtaskIndex >= executionVertices.length || subtaskIndex < 0) {
		throw new RestHandlerException("Invalid subtask index for vertex " + jobVertex.getJobVertexId(), HttpResponseStatus.NOT_FOUND);
	}

	return handleRequest(request, executionVertices[subtaskIndex]);
}
 
Example 17
Source File: TaskManagerDetailsHandler.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
protected CompletableFuture<TaskManagerDetailsInfo> handleRequest(
		@Nonnull HandlerRequest<EmptyRequestBody, TaskManagerMessageParameters> request,
		@Nonnull ResourceManagerGateway gateway) throws RestHandlerException {
	final ResourceID taskManagerResourceId = request.getPathParameter(TaskManagerIdPathParameter
		.class);

	CompletableFuture<TaskManagerInfo> taskManagerInfoFuture = gateway.requestTaskManagerInfo(taskManagerResourceId, timeout);

	metricFetcher.update();

	return taskManagerInfoFuture.thenApply(
		(TaskManagerInfo taskManagerInfo) -> {
			final MetricStore.TaskManagerMetricStore tmMetrics =
				metricStore.getTaskManagerMetricStore(taskManagerResourceId.getResourceIdString());

			final TaskManagerMetricsInfo taskManagerMetricsInfo;

			if (tmMetrics != null) {
				log.debug("Create metrics info for TaskManager {}.", taskManagerResourceId);
				taskManagerMetricsInfo = createTaskManagerMetricsInfo(tmMetrics);
			} else {
				log.debug("No metrics for TaskManager {}.", taskManagerResourceId);
				taskManagerMetricsInfo = TaskManagerMetricsInfo.empty();
			}

			return new TaskManagerDetailsInfo(
				taskManagerInfo,
				taskManagerMetricsInfo);
		})
		.exceptionally(
			(Throwable throwable) -> {
				final Throwable strippedThrowable = ExceptionUtils.stripExecutionException(throwable);

				if (strippedThrowable instanceof UnknownTaskExecutorException) {
					throw new CompletionException(
						new RestHandlerException(
							"Could not find TaskExecutor " + taskManagerResourceId + '.',
							HttpResponseStatus.NOT_FOUND,
							strippedThrowable));
				} else {
					throw new CompletionException(strippedThrowable);
				}
			}
		);
}
 
Example 18
Source File: JobTerminationHandler.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<EmptyResponseBody> handleRequest(HandlerRequest<EmptyRequestBody, JobTerminationMessageParameters> request, RestfulGateway gateway) {
	final JobID jobId = request.getPathParameter(JobIDPathParameter.class);
	final List<TerminationModeQueryParameter.TerminationMode> terminationModes = request.getQueryParameter(TerminationModeQueryParameter.class);
	final TerminationModeQueryParameter.TerminationMode terminationMode;

	if (terminationModes.isEmpty()) {
		terminationMode = defaultTerminationMode;
	} else {
		// picking the first termination mode value
		terminationMode = terminationModes.get(0);
	}

	final CompletableFuture<Acknowledge> terminationFuture;

	switch (terminationMode) {
		case CANCEL:
			terminationFuture = gateway.cancelJob(jobId, timeout);
			break;
		case STOP:
			terminationFuture = gateway.stopJob(jobId, timeout);
			break;
		default:
			terminationFuture = FutureUtils.completedExceptionally(new RestHandlerException("Unknown termination mode " + terminationMode + '.', HttpResponseStatus.BAD_REQUEST));
	}

	return terminationFuture.handle(
		(Acknowledge ack, Throwable throwable) -> {
			if (throwable != null) {
				Throwable error = ExceptionUtils.stripCompletionException(throwable);

				if (error instanceof TimeoutException) {
					throw new CompletionException(
						new RestHandlerException(
							"Job termination (" + terminationMode + ") timed out.",
							HttpResponseStatus.REQUEST_TIMEOUT, error));
				} else if (error instanceof FlinkJobNotFoundException) {
					throw new CompletionException(
						new RestHandlerException(
							"Job could not be found.",
							HttpResponseStatus.NOT_FOUND, error));
				} else {
					throw new CompletionException(
						new RestHandlerException(
							"Job termination (" + terminationMode + ") failed: " + error.getMessage(),
							HttpResponseStatus.INTERNAL_SERVER_ERROR, error));
				}
			} else {
				return EmptyResponseBody.getInstance();
			}
		});
}
 
Example 19
Source File: TaskManagerDetailsHandler.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
@Override
protected CompletableFuture<TaskManagerDetailsInfo> handleRequest(
		@Nonnull HandlerRequest<EmptyRequestBody, TaskManagerMessageParameters> request,
		@Nonnull ResourceManagerGateway gateway) throws RestHandlerException {
	final ResourceID taskManagerResourceId = request.getPathParameter(TaskManagerIdPathParameter
		.class);

	CompletableFuture<TaskManagerInfo> taskManagerInfoFuture = gateway.requestTaskManagerInfo(taskManagerResourceId, timeout);

	metricFetcher.update();

	return taskManagerInfoFuture.thenApply(
		(TaskManagerInfo taskManagerInfo) -> {
			final MetricStore.TaskManagerMetricStore tmMetrics =
				metricStore.getTaskManagerMetricStore(taskManagerResourceId.getResourceIdString());

			final TaskManagerMetricsInfo taskManagerMetricsInfo;

			if (tmMetrics != null) {
				log.debug("Create metrics info for TaskManager {}.", taskManagerResourceId);
				taskManagerMetricsInfo = createTaskManagerMetricsInfo(tmMetrics);
			} else {
				log.debug("No metrics for TaskManager {}.", taskManagerResourceId);
				taskManagerMetricsInfo = TaskManagerMetricsInfo.empty();
			}

			return new TaskManagerDetailsInfo(
				taskManagerInfo,
				taskManagerMetricsInfo);
		})
		.exceptionally(
			(Throwable throwable) -> {
				final Throwable strippedThrowable = ExceptionUtils.stripExecutionException(throwable);

				if (strippedThrowable instanceof UnknownTaskExecutorException) {
					throw new CompletionException(
						new RestHandlerException(
							"Could not find TaskExecutor " + taskManagerResourceId + '.',
							HttpResponseStatus.NOT_FOUND,
							strippedThrowable));
				} else {
					throw new CompletionException(strippedThrowable);
				}
			}
		);
}
 
Example 20
Source File: JobCancellationHandler.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<EmptyResponseBody> handleRequest(HandlerRequest<EmptyRequestBody, JobCancellationMessageParameters> request, RestfulGateway gateway) throws RestHandlerException {
	final JobID jobId = request.getPathParameter(JobIDPathParameter.class);
	final List<TerminationModeQueryParameter.TerminationMode> terminationModes = request.getQueryParameter(TerminationModeQueryParameter.class);
	final TerminationModeQueryParameter.TerminationMode terminationMode;

	if (terminationModes.isEmpty()) {
		terminationMode = defaultTerminationMode;
	} else {
		// picking the first termination mode value
		terminationMode = terminationModes.get(0);
	}

	final CompletableFuture<Acknowledge> terminationFuture;

	switch (terminationMode) {
		case CANCEL:
			terminationFuture = gateway.cancelJob(jobId, timeout);
			break;
		case STOP:
			throw new RestHandlerException("The termination mode \"stop\" has been removed. For " +
			"an ungraceful shutdown, please use \"cancel\" instead. For a graceful shutdown, " +
			"please use \"jobs/:jobId/stop\" instead." , HttpResponseStatus.PERMANENT_REDIRECT);
		default:
			terminationFuture = FutureUtils.completedExceptionally(new RestHandlerException("Unknown termination mode " + terminationMode + '.', HttpResponseStatus.BAD_REQUEST));
	}

	return terminationFuture.handle(
		(Acknowledge ack, Throwable throwable) -> {
			if (throwable != null) {
				Throwable error = ExceptionUtils.stripCompletionException(throwable);

				if (error instanceof TimeoutException) {
					throw new CompletionException(
						new RestHandlerException(
							"Job cancellation timed out.",
							HttpResponseStatus.REQUEST_TIMEOUT, error));
				} else if (error instanceof FlinkJobNotFoundException) {
					throw new CompletionException(
						new RestHandlerException(
							"Job could not be found.",
							HttpResponseStatus.NOT_FOUND, error));
				} else {
					throw new CompletionException(
						new RestHandlerException(
							"Job cancellation failed: " + error.getMessage(),
							HttpResponseStatus.INTERNAL_SERVER_ERROR, error));
				}
			} else {
				return EmptyResponseBody.getInstance();
			}
		});
}