org.apache.flink.runtime.client.JobCancellationException Java Examples

The following examples show how to use org.apache.flink.runtime.client.JobCancellationException. 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: JobResultTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Test
public void testCancelledJobThrowsJobCancellationException() throws Exception {
	final FlinkException cause = new FlinkException("Test exception");
	final JobResult jobResult = JobResult.createFrom(
		new ArchivedExecutionGraphBuilder()
			.setJobID(new JobID())
			.setState(JobStatus.CANCELED)
			.setFailureCause(new ErrorInfo(cause, 42L))
			.build());

	try {
		jobResult.toJobExecutionResult(getClass().getClassLoader());
		fail("Job should fail with an JobCancellationException.");
	} catch (JobCancellationException expected) {
		assertThat(expected.getCause(), is(equalTo(cause)));
	}
}
 
Example #2
Source File: JobResultTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testCancelledJobThrowsJobCancellationException() throws Exception {
	final FlinkException cause = new FlinkException("Test exception");
	final JobResult jobResult = JobResult.createFrom(
		new ArchivedExecutionGraphBuilder()
			.setJobID(new JobID())
			.setState(JobStatus.CANCELED)
			.setFailureCause(new ErrorInfo(cause, 42L))
			.build());

	try {
		jobResult.toJobExecutionResult(getClass().getClassLoader());
		fail("Job should fail with an JobCancellationException.");
	} catch (JobCancellationException expected) {
		assertThat(expected.getCause(), is(equalTo(cause)));
	}
}
 
Example #3
Source File: JobResultTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testCancelledJobThrowsJobCancellationException() throws Exception {
	final FlinkException cause = new FlinkException("Test exception");
	final JobResult jobResult = JobResult.createFrom(
		new ArchivedExecutionGraphBuilder()
			.setJobID(new JobID())
			.setState(JobStatus.CANCELED)
			.setFailureCause(new ErrorInfo(cause, 42L))
			.build());

	try {
		jobResult.toJobExecutionResult(getClass().getClassLoader());
		fail("Job should fail with an JobCancellationException.");
	} catch (JobCancellationException expected) {
		// the failure cause in the execution graph should not be the cause of the canceled job result
		assertThat(expected.getCause(), is(nullValue()));
	}
}
 
Example #4
Source File: ApplicationDispatcherBootstrap.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Runs the user program entrypoint using {@link #runApplicationAsync(DispatcherGateway,
 * ScheduledExecutor, boolean)} and shuts down the given dispatcher when the application
 * completes (either successfully or in case of failure).
 */
@VisibleForTesting
CompletableFuture<Acknowledge> runApplicationAndShutdownClusterAsync(
		final DispatcherGateway dispatcher,
		final ScheduledExecutor scheduledExecutor) {

	applicationCompletionFuture = fixJobIdAndRunApplicationAsync(dispatcher, scheduledExecutor);

	return applicationCompletionFuture
			.handle((r, t) -> {
				final ApplicationStatus applicationStatus;
				if (t != null) {

					final Optional<JobCancellationException> cancellationException =
							ExceptionUtils.findThrowable(t, JobCancellationException.class);

					if (cancellationException.isPresent()) {
						// this means the Flink Job was cancelled
						applicationStatus = ApplicationStatus.CANCELED;
					} else if (t instanceof CancellationException) {
						// this means that the future was cancelled
						applicationStatus = ApplicationStatus.UNKNOWN;
					} else {
						applicationStatus = ApplicationStatus.FAILED;
					}

					LOG.warn("Application {}: ", applicationStatus, t);
				} else {
					applicationStatus = ApplicationStatus.SUCCEEDED;
					LOG.info("Application completed SUCCESSFULLY");
				}
				return dispatcher.shutDownCluster(applicationStatus);
			})
			.thenCompose(Function.identity());
}
 
Example #5
Source File: ApplicationDispatcherBootstrap.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * If the given {@link JobResult} indicates success, this passes through the {@link JobResult}.
 * Otherwise, this returns a future that is finished exceptionally (potentially with an
 * exception from the {@link JobResult}.
 */
private CompletableFuture<JobResult> unwrapJobResultException(
		CompletableFuture<JobResult> jobResult) {
	return jobResult.thenApply(result -> {
		if (result.isSuccess()) {
			return result;
		}

		Optional<SerializedThrowable> serializedThrowable = result.getSerializedThrowable();

		if (result.getApplicationStatus() == ApplicationStatus.CANCELED) {
			throw new CompletionException(
					new JobCancellationException(
							result.getJobId(),
							"Job was cancelled.",
							serializedThrowable.orElse(null)));
		}

		if (serializedThrowable.isPresent()) {
			Throwable throwable =
					serializedThrowable
							.get()
							.deserializeError(application.getUserCodeClassLoader());
			throw new CompletionException(throwable);
		}
		throw new RuntimeException("Job execution failed for unknown reason.");
	});
}
 
Example #6
Source File: ApplicationDispatcherBootstrapTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private static JobResult createCancelledJobResult(final JobID jobId) {
	return new JobResult.Builder()
			.jobId(jobId)
			.netRuntime(2L)
			.serializedThrowable(
					new SerializedThrowable(
							new JobCancellationException(jobId, "Hello", null)))
			.applicationStatus(ApplicationStatus.CANCELED)
			.build();
}
 
Example #7
Source File: ClassLoaderITCase.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
/**
 * Tests disposal of a savepoint, which contains custom user code KvState.
 */
@Test
public void testDisposeSavepointWithCustomKvState() throws Exception {
	ClusterClient<?> clusterClient = new MiniClusterClient(new Configuration(), miniClusterResource.getMiniCluster());

	Deadline deadline = new FiniteDuration(100, TimeUnit.SECONDS).fromNow();

	File checkpointDir = FOLDER.newFolder();
	File outputDir = FOLDER.newFolder();

	final PackagedProgram program = new PackagedProgram(
			new File(CUSTOM_KV_STATE_JAR_PATH),
			new String[] {
					String.valueOf(parallelism),
					checkpointDir.toURI().toString(),
					"5000",
					outputDir.toURI().toString()
			});

	TestStreamEnvironment.setAsContext(
		miniClusterResource.getMiniCluster(),
		parallelism,
		Collections.singleton(new Path(CUSTOM_KV_STATE_JAR_PATH)),
		Collections.<URL>emptyList()
	);

	// Execute detached
	Thread invokeThread = new Thread(new Runnable() {
		@Override
		public void run() {
			try {
				program.invokeInteractiveModeForExecution();
			} catch (ProgramInvocationException ignored) {
				if (ignored.getCause() == null ||
					!(ignored.getCause() instanceof JobCancellationException)) {
					ignored.printStackTrace();
				}
			}
		}
	});

	LOG.info("Starting program invoke thread");
	invokeThread.start();

	// The job ID
	JobID jobId = null;

	LOG.info("Waiting for job status running.");

	// Wait for running job
	while (jobId == null && deadline.hasTimeLeft()) {

		Collection<JobStatusMessage> jobs = clusterClient.listJobs().get(deadline.timeLeft().toMillis(), TimeUnit.MILLISECONDS);
		for (JobStatusMessage job : jobs) {
			if (job.getJobState() == JobStatus.RUNNING) {
				jobId = job.getJobId();
				LOG.info("Job running. ID: " + jobId);
				break;
			}
		}

		// Retry if job is not available yet
		if (jobId == null) {
			Thread.sleep(100L);
		}
	}

	// Trigger savepoint
	String savepointPath = null;
	for (int i = 0; i < 20; i++) {
		LOG.info("Triggering savepoint (" + (i + 1) + "/20).");
		try {
			savepointPath = clusterClient.triggerSavepoint(jobId, null)
				.get(deadline.timeLeft().toMillis(), TimeUnit.MILLISECONDS);
		} catch (Exception cause) {
			LOG.info("Failed to trigger savepoint. Retrying...", cause);
			// This can fail if the operators are not opened yet
			Thread.sleep(500);
		}
	}

	assertNotNull("Failed to trigger savepoint", savepointPath);

	clusterClient.disposeSavepoint(savepointPath).get();

	clusterClient.cancel(jobId);

	// make sure, the execution is finished to not influence other test methods
	invokeThread.join(deadline.timeLeft().toMillis());
	assertFalse("Program invoke thread still running", invokeThread.isAlive());
}
 
Example #8
Source File: ClassLoaderITCase.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Tests disposal of a savepoint, which contains custom user code KvState.
 */
@Test
public void testDisposeSavepointWithCustomKvState() throws Exception {
	ClusterClient<?> clusterClient = new MiniClusterClient(new Configuration(), miniClusterResource.getMiniCluster());

	Deadline deadline = new FiniteDuration(100, TimeUnit.SECONDS).fromNow();

	File checkpointDir = FOLDER.newFolder();
	File outputDir = FOLDER.newFolder();

	final PackagedProgram program = new PackagedProgram(
			new File(CUSTOM_KV_STATE_JAR_PATH),
			new String[] {
					String.valueOf(parallelism),
					checkpointDir.toURI().toString(),
					"5000",
					outputDir.toURI().toString()
			});

	TestStreamEnvironment.setAsContext(
		miniClusterResource.getMiniCluster(),
		parallelism,
		Collections.singleton(new Path(CUSTOM_KV_STATE_JAR_PATH)),
		Collections.<URL>emptyList()
	);

	// Execute detached
	Thread invokeThread = new Thread(new Runnable() {
		@Override
		public void run() {
			try {
				program.invokeInteractiveModeForExecution();
			} catch (ProgramInvocationException ignored) {
				if (ignored.getCause() == null ||
					!(ignored.getCause() instanceof JobCancellationException)) {
					ignored.printStackTrace();
				}
			}
		}
	});

	LOG.info("Starting program invoke thread");
	invokeThread.start();

	// The job ID
	JobID jobId = null;

	LOG.info("Waiting for job status running.");

	// Wait for running job
	while (jobId == null && deadline.hasTimeLeft()) {

		Collection<JobStatusMessage> jobs = clusterClient.listJobs().get(deadline.timeLeft().toMillis(), TimeUnit.MILLISECONDS);
		for (JobStatusMessage job : jobs) {
			if (job.getJobState() == JobStatus.RUNNING) {
				jobId = job.getJobId();
				LOG.info("Job running. ID: " + jobId);
				break;
			}
		}

		// Retry if job is not available yet
		if (jobId == null) {
			Thread.sleep(100L);
		}
	}

	// Trigger savepoint
	String savepointPath = null;
	for (int i = 0; i < 20; i++) {
		LOG.info("Triggering savepoint (" + (i + 1) + "/20).");
		try {
			savepointPath = clusterClient.triggerSavepoint(jobId, null)
				.get(deadline.timeLeft().toMillis(), TimeUnit.MILLISECONDS);
		} catch (Exception cause) {
			LOG.info("Failed to trigger savepoint. Retrying...", cause);
			// This can fail if the operators are not opened yet
			Thread.sleep(500);
		}
	}

	assertNotNull("Failed to trigger savepoint", savepointPath);

	clusterClient.disposeSavepoint(savepointPath).get();

	clusterClient.cancel(jobId);

	// make sure, the execution is finished to not influence other test methods
	invokeThread.join(deadline.timeLeft().toMillis());
	assertFalse("Program invoke thread still running", invokeThread.isAlive());
}
 
Example #9
Source File: FlinkPravegaReaderSavepointITCase.java    From flink-connectors with Apache License 2.0 4 votes vote down vote up
@Test
public void testPravegaWithSavepoint() throws Exception {
    final int sourceParallelism = 4;
    final int numPravegaSegments = 4;
    final int numElements = NUM_STREAM_ELEMENTS;

    // set up the stream
    final String streamName = RandomStringUtils.randomAlphabetic(20);
    SETUP_UTILS.createTestStream(streamName, numPravegaSegments);

    // we create two independent Flink jobs (that come from the same program)
    final JobGraph program1 = getFlinkJob(sourceParallelism, streamName, numElements);

    try (
            final EventStreamWriter<Integer> eventWriter = SETUP_UTILS.getIntegerWriter(streamName);

            // create the producer that writes to the stream
            final ThrottledIntegerWriter producer = new ThrottledIntegerWriter(
                    eventWriter,
                    numElements,
                    numElements / 2,  // the latest when the thread must be un-throttled
                    1,                 // the initial sleep time per element
                    false
            )

    ) {
        // the object on which we block while waiting for the checkpoint completion
        final OneShotLatch sync = new OneShotLatch();
        NotifyingMapper.TO_CALL_ON_COMPLETION.set( sync::trigger );

        // launch the Flink program from a separate thread
        final CheckedThread flinkRunner = new CheckedThread() {
            @Override
            public void go() throws Exception {
                MINI_CLUSTER.submitJob(program1);
            }
        };

        producer.start();
        flinkRunner.start();

        // wait until at least one checkpoint is complete before triggering the safepoints
        sync.await();

        // now that we are comfortably into the program, trigger a savepoint
        String savepointPath = null;

        // since with the short timeouts we configure in these tests, Pravega Checkpoints
        // sometimes don't complete in time, we retry a bit here
        for (int attempt = 1; savepointPath == null && attempt <= 5; attempt++) {
            savepointPath = MINI_CLUSTER.triggerSavepoint(program1.getJobID(), tmpFolder.newFolder().getAbsolutePath(), false).get();
        }

        assertNotNull("Failed to trigger a savepoint", savepointPath);

        // now cancel the job and relaunch a new one
        MINI_CLUSTER.cancelJob(program1.getJobID());

        try {
            // this throws an exception that the job was cancelled
            flinkRunner.sync();
        } catch (JobCancellationException ignored) {
        }

        producer.unthrottle();

        // now, resume with a new program
        final JobGraph program2 = getFlinkJob(sourceParallelism, streamName, numElements);
        program2.setSavepointRestoreSettings(SavepointRestoreSettings.forPath(savepointPath, false));

        // if these calls complete without exception, then the test passes
        try {
            MINI_CLUSTER.executeJobBlocking(program2);
        } catch (Exception e) {
            if (!(ExceptionUtils.getRootCause(e) instanceof SuccessException)) {
                throw e;
            }
        }
    }
}
 
Example #10
Source File: ClassLoaderITCase.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Tests disposal of a savepoint, which contains custom user code KvState.
 */
@Test
public void testDisposeSavepointWithCustomKvState() throws Exception {
	ClusterClient<?> clusterClient = new MiniClusterClient(new Configuration(), miniClusterResource.getMiniCluster());

	Deadline deadline = new FiniteDuration(100, TimeUnit.SECONDS).fromNow();

	File checkpointDir = FOLDER.newFolder();
	File outputDir = FOLDER.newFolder();

	final PackagedProgram program = PackagedProgram.newBuilder()
		.setJarFile(new File(CUSTOM_KV_STATE_JAR_PATH))
		.setArguments(new String[] {
			String.valueOf(parallelism),
			checkpointDir.toURI().toString(),
			"5000",
			outputDir.toURI().toString(),
			"false" // Disable unaligned checkpoints as this test is triggering concurrent savepoints/checkpoints
		})
		.build();

	TestStreamEnvironment.setAsContext(
		miniClusterResource.getMiniCluster(),
		parallelism,
		Collections.singleton(new Path(CUSTOM_KV_STATE_JAR_PATH)),
		Collections.emptyList()
	);

	// Execute detached
	Thread invokeThread = new Thread(() -> {
		try {
			program.invokeInteractiveModeForExecution();
		} catch (ProgramInvocationException ex) {
			if (ex.getCause() == null ||
				!(ex.getCause() instanceof JobCancellationException)) {
				ex.printStackTrace();
			}
		}
	});

	LOG.info("Starting program invoke thread");
	invokeThread.start();

	// The job ID
	JobID jobId = null;

	LOG.info("Waiting for job status running.");

	// Wait for running job
	while (jobId == null && deadline.hasTimeLeft()) {

		Collection<JobStatusMessage> jobs = clusterClient.listJobs().get(deadline.timeLeft().toMillis(), TimeUnit.MILLISECONDS);
		for (JobStatusMessage job : jobs) {
			if (job.getJobState() == JobStatus.RUNNING) {
				jobId = job.getJobId();
				LOG.info("Job running. ID: " + jobId);
				break;
			}
		}

		// Retry if job is not available yet
		if (jobId == null) {
			Thread.sleep(100L);
		}
	}

	// Trigger savepoint
	String savepointPath = null;
	for (int i = 0; i < 20; i++) {
		LOG.info("Triggering savepoint (" + (i + 1) + "/20).");
		try {
			savepointPath = clusterClient.triggerSavepoint(jobId, null)
				.get(deadline.timeLeft().toMillis(), TimeUnit.MILLISECONDS);
		} catch (Exception cause) {
			LOG.info("Failed to trigger savepoint. Retrying...", cause);
			// This can fail if the operators are not opened yet
			Thread.sleep(500);
		}
	}

	assertNotNull("Failed to trigger savepoint", savepointPath);

	clusterClient.disposeSavepoint(savepointPath).get();

	clusterClient.cancel(jobId).get();

	// make sure, the execution is finished to not influence other test methods
	invokeThread.join(deadline.timeLeft().toMillis());
	assertFalse("Program invoke thread still running", invokeThread.isAlive());
}