Java Code Examples for org.apache.flink.util.function.RunnableWithException#run()

The following examples show how to use org.apache.flink.util.function.RunnableWithException#run() . 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: KubernetesResourceManagerTest.java    From flink with Apache License 2.0 6 votes vote down vote up
void runTest(RunnableWithException testMethod) throws Exception {
	if (slotManager == null) {
		WorkerResourceSpec workerResourceSpec = KubernetesWorkerResourceSpecFactory.INSTANCE
			.createDefaultWorkerResourceSpec(flinkConfig);
		slotManager = SlotManagerBuilder.newBuilder()
			.setDefaultWorkerResourceSpec(workerResourceSpec)
			.build();
		registerSlotProfile = SlotManagerImpl.generateDefaultSlotResourceProfile(workerResourceSpec, 1);
	}

	if (flinkKubeClient == null) {
		flinkKubeClient = KubernetesResourceManagerTest.this.flinkKubeClient;
	}

	resourceManager = createAndStartResourceManager(flinkConfig, slotManager, flinkKubeClient);

	try {
		testMethod.run();
	} finally {
		resourceManager.close();
	}
}
 
Example 2
Source File: ContinuousFileReaderOperator.java    From flink with Apache License 2.0 6 votes vote down vote up
private void cleanUp() throws Exception {
	LOG.debug("cleanup, state={}", state);

	RunnableWithException[] runClose = {
			() -> sourceContext.close(),
			() -> output.close(),
			() -> format.close(),
			() -> {
				if (this.format instanceof RichInputFormat) {
					((RichInputFormat) this.format).closeInputFormat();
				}
			}};
	Exception firstException = null;

	for (RunnableWithException r : runClose) {
		try {
			r.run();
		} catch (Exception e) {
			firstException = ExceptionUtils.firstOrSuppressed(e, firstException);
		}
	}
	currentSplit = null;
	if (firstException != null) {
		throw firstException;
	}
}
 
Example 3
Source File: YarnResourceManagerTest.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * A wrapper function for running test. Deal with setup and teardown logic
 * in Context.
 * @param testMethod the real test body.
 */
void runTest(RunnableWithException testMethod) throws Exception {
	startResourceManager();
	try {
		testMethod.run();
	} finally {
		stopResourceManager();
	}
}
 
Example 4
Source File: TaskManagerReleaseInSlotManagerTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private void checkTaskManagerTimeoutWithCustomCanBeReleasedResponse(
		SlotManagerImpl slotManager,
		boolean canBeReleased,
		RunnableWithException doAfterCheckTriggerBeforeCanBeReleasedResponse) throws Exception {
	canBeReleasedFuture.set(new CompletableFuture<>());
	mainThreadExecutor.execute(slotManager::checkTaskManagerTimeouts); // trigger TM.canBeReleased request
	mainThreadExecutor.triggerAll();
	doAfterCheckTriggerBeforeCanBeReleasedResponse.run();
	canBeReleasedFuture.get().complete(canBeReleased); // finish TM.canBeReleased request
	mainThreadExecutor.triggerAll();
}
 
Example 5
Source File: ChannelStateWriterImplTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private static <T extends Throwable> void unwrappingError(Class<T> clazz, RunnableWithException r) throws Exception {
	try {
		r.run();
	} catch (Exception e) {
		throw findThrowable(e, clazz).map(te -> (Exception) te).orElse(e);
	}
}
 
Example 6
Source File: ChannelStateCheckpointWriter.java    From flink with Apache License 2.0 5 votes vote down vote up
private void runWithChecks(RunnableWithException r) throws Exception {
	try {
		checkState(!result.isDone(), "result is already completed", result);
		r.run();
	} catch (Exception e) {
		fail(e);
		throw e;
	}
}
 
Example 7
Source File: ChannelStateCheckpointWriter.java    From flink with Apache License 2.0 5 votes vote down vote up
private void doComplete(boolean precondition, RunnableWithException complete, RunnableWithException... callbacks) throws Exception {
	Preconditions.checkArgument(precondition);
	complete.run();
	if (allInputsReceived && allOutputsReceived) {
		for (RunnableWithException callback : callbacks) {
			callback.run();
		}
	}
}
 
Example 8
Source File: MapRNotInClassPathTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testInstantiationWhenMapRClassesAreMissing() throws Exception {
	final String testClassName = "org.apache.flink.runtime.fs.maprfs.MapRNotInClassPathTest$TestRunner";
	final ClassLoader cl = new MapRFreeClassLoader(getClass().getClassLoader());

	final RunnableWithException testRunner = Class
		.forName(testClassName, false, cl)
		.asSubclass(RunnableWithException.class)
		.newInstance();

	testRunner.run();
}
 
Example 9
Source File: ExceptionUtils.java    From flink with Apache License 2.0 5 votes vote down vote up
public static void suppressExceptions(RunnableWithException action) {
	try {
		action.run();
	}
	catch (InterruptedException e) {
		// restore interrupted state
		Thread.currentThread().interrupt();
	}
	catch (Throwable t) {
		if (isJvmFatalError(t)) {
			rethrow(t);
		}
	}
}
 
Example 10
Source File: ExceptionUtils.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
public static void suppressExceptions(RunnableWithException action) {
	try {
		action.run();
	}
	catch (InterruptedException e) {
		// restore interrupted state
		Thread.currentThread().interrupt();
	}
	catch (Throwable t) {
		if (isJvmFatalError(t)) {
			rethrow(t);
		}
	}
}
 
Example 11
Source File: RetryingCallback.java    From flink-statefun with Apache License 2.0 5 votes vote down vote up
/**
 * Executes the runnable, and completes {@link #resultFuture} with any exceptions thrown, during
 * its execution.
 */
private void tryWithFuture(RunnableWithException runnable) {
  try {
    runnable.run();
  } catch (Throwable t) {
    resultFuture.completeExceptionally(t);
  }
}
 
Example 12
Source File: MailboxImplTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private void testUnblocksInternal(
	RunnableWithException testMethod,
	Consumer<Mailbox> unblockMethod) throws InterruptedException {
	final Thread[] blockedThreads = new Thread[8];
	final Exception[] exceptions = new Exception[blockedThreads.length];

	CountDownLatch countDownLatch = new CountDownLatch(blockedThreads.length);

	for (int i = 0; i < blockedThreads.length; ++i) {
		final int id = i;
		Thread blocked = new Thread(() -> {
			try {
				countDownLatch.countDown();
				while (true) {
					testMethod.run();
				}
			} catch (Exception ex) {
				exceptions[id] = ex;
			}
		});
		blockedThreads[i] = blocked;
		blocked.start();
	}

	countDownLatch.await();
	unblockMethod.accept(mailbox);

	for (Thread blockedThread : blockedThreads) {
		blockedThread.join();
	}

	for (Exception exception : exceptions) {
		Assert.assertEquals(MailboxStateException.class, exception.getClass());
	}

}
 
Example 13
Source File: SynchronousSavepointLatch.java    From flink with Apache License 2.0 5 votes vote down vote up
void acknowledgeCheckpointAndTrigger(final long checkpointId, RunnableWithException runnable) throws Exception {
	synchronized (synchronizationPoint) {
		if (completionResult == null && this.checkpointId == checkpointId) {
			completionResult = CompletionResult.COMPLETED;
			try {
				runnable.run();
			} finally {
				synchronizationPoint.notifyAll();
			}
		}
	}
}
 
Example 14
Source File: TaskManagerReleaseInSlotManagerTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private void checkTaskManagerTimeoutWithCustomCanBeReleasedResponse(
		SlotManagerImpl slotManager,
		boolean canBeReleased,
		RunnableWithException doAfterCheckTriggerBeforeCanBeReleasedResponse) throws Exception {
	canBeReleasedFuture.set(new CompletableFuture<>());
	mainThreadExecutor.execute(slotManager::checkTaskManagerTimeouts); // trigger TM.canBeReleased request
	mainThreadExecutor.triggerAll();
	doAfterCheckTriggerBeforeCanBeReleasedResponse.run();
	canBeReleasedFuture.get().complete(canBeReleased); // finish TM.canBeReleased request
	mainThreadExecutor.triggerAll();
}
 
Example 15
Source File: MapRNotInClassPathTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testInstantiationWhenMapRClassesAreMissing() throws Exception {
	final String testClassName = "org.apache.flink.runtime.fs.maprfs.MapRNotInClassPathTest$TestRunner";
	final ClassLoader cl = new MapRFreeClassLoader(getClass().getClassLoader());

	final RunnableWithException testRunner = Class
		.forName(testClassName, false, cl)
		.asSubclass(RunnableWithException.class)
		.newInstance();

	testRunner.run();
}
 
Example 16
Source File: ExceptionUtils.java    From flink with Apache License 2.0 5 votes vote down vote up
public static void suppressExceptions(RunnableWithException action) {
	try {
		action.run();
	}
	catch (InterruptedException e) {
		// restore interrupted state
		Thread.currentThread().interrupt();
	}
	catch (Throwable t) {
		if (isJvmFatalError(t)) {
			rethrow(t);
		}
	}
}
 
Example 17
Source File: YarnTestBase.java    From flink with Apache License 2.0 4 votes vote down vote up
protected void runTest(RunnableWithException test) throws Exception {
	// wrapping the cleanup logic in an AutoClosable automatically suppresses additional exceptions
	try (final CleanupYarnApplication ignored = new CleanupYarnApplication()) {
		test.run();
	}
}
 
Example 18
Source File: YarnTestBase.java    From flink with Apache License 2.0 4 votes vote down vote up
protected void runTest(RunnableWithException test) throws Exception {
	// wrapping the cleanup logic in an AutoClosable automatically suppresses additional exceptions
	try (final CleanupYarnApplication ignored = new CleanupYarnApplication()) {
		test.run();
	}
}
 
Example 19
Source File: StreamTaskActionExecutor.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public void run(RunnableWithException runnable) throws Exception {
	synchronized (mutex) {
		runnable.run();
	}
}
 
Example 20
Source File: StreamTaskExecutionDecorationTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public void run(RunnableWithException runnable) throws Exception {
	calls.incrementAndGet();
	runnable.run();
}