org.apache.flink.util.function.ThrowingRunnable Java Examples

The following examples show how to use org.apache.flink.util.function.ThrowingRunnable. 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: LambdaUtil.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * Runs the given runnable with the given ClassLoader as the thread's
 * {@link Thread#setContextClassLoader(ClassLoader) context class loader}.
 *
 * <p>The method will make sure to set the context class loader of the calling thread
 * back to what it was before after the runnable completed.
 */
public static <E extends Throwable> void withContextClassLoader(
		final ClassLoader cl,
		final ThrowingRunnable<E> r) throws E {

	final Thread currentThread = Thread.currentThread();
	final ClassLoader oldClassLoader = currentThread.getContextClassLoader();

	try {
		currentThread.setContextClassLoader(cl);
		r.run();
	}
	finally {
		currentThread.setContextClassLoader(oldClassLoader);
	}
}
 
Example #2
Source File: MemoryManager.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Acquires a shared resource, identified by a type string. If the resource already exists, this
 * returns a descriptor to the resource. If the resource does not yet exist, the method initializes
 * a new resource using the initializer function and given size.
 *
 * <p>The resource opaque, meaning the memory manager does not understand its structure.
 *
 * <p>The OpaqueMemoryResource object returned from this method must be closed once not used any further.
 * Once all acquisitions have closed the object, the resource itself is closed.
 */
public <T extends AutoCloseable> OpaqueMemoryResource<T> getExternalSharedMemoryResource(
		String type,
		LongFunctionWithException<T, Exception> initializer,
		long numBytes) throws Exception {

	// This object identifies the lease in this request. It is used only to identify the release operation.
	// Using the object to represent the lease is a bit nicer safer than just using a reference counter.
	final Object leaseHolder = new Object();

	final SharedResources.ResourceAndSize<T> resource =
			sharedResources.getOrAllocateSharedResource(type, leaseHolder, initializer, numBytes);

	final ThrowingRunnable<Exception> disposer = () -> sharedResources.release(type, leaseHolder);

	return new OpaqueMemoryResource<>(resource.resourceHandle(), resource.size(), disposer);
}
 
Example #3
Source File: RocksDBStateDownloader.java    From flink with Apache License 2.0 6 votes vote down vote up
private List<Runnable> createDownloadRunnables(
	Map<StateHandleID, StreamStateHandle> stateHandleMap,
	Path restoreInstancePath,
	CloseableRegistry closeableRegistry) {
	List<Runnable> runnables = new ArrayList<>(stateHandleMap.size());
	for (Map.Entry<StateHandleID, StreamStateHandle> entry : stateHandleMap.entrySet()) {
		StateHandleID stateHandleID = entry.getKey();
		StreamStateHandle remoteFileHandle = entry.getValue();

		Path path = restoreInstancePath.resolve(stateHandleID.toString());

		runnables.add(ThrowingRunnable.unchecked(
			() -> downloadDataForStateHandle(path, remoteFileHandle, closeableRegistry)));
	}
	return runnables;
}
 
Example #4
Source File: AbstractTtlDecorator.java    From flink with Apache License 2.0 6 votes vote down vote up
<SE extends Throwable, CE extends Throwable, CLE extends Throwable, V> TtlValue<V> getWrappedWithTtlCheckAndUpdate(
	SupplierWithException<TtlValue<V>, SE> getter,
	ThrowingConsumer<TtlValue<V>, CE> updater,
	ThrowingRunnable<CLE> stateClear) throws SE, CE, CLE {
	TtlValue<V> ttlValue = getter.get();
	if (ttlValue == null) {
		return null;
	} else if (expired(ttlValue)) {
		stateClear.run();
		if (!returnExpired) {
			return null;
		}
	} else if (updateTsOnRead) {
		updater.accept(rewrapWithNewTs(ttlValue));
	}
	return ttlValue;
}
 
Example #5
Source File: SlotPoolPendingRequestFailureTest.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that a pending slot request is failed with a timeout.
 */
@Test
public void testPendingSlotRequestTimeout() throws Exception {
	final ScheduledExecutorService singleThreadExecutor = Executors.newSingleThreadScheduledExecutor();
	final ComponentMainThreadExecutor componentMainThreadExecutor = ComponentMainThreadExecutorServiceAdapter.forSingleThreadExecutor(singleThreadExecutor);

	final SlotPoolImpl slotPool = setUpSlotPool(componentMainThreadExecutor);

	try {
		final Time timeout = Time.milliseconds(5L);

		final CompletableFuture<PhysicalSlot> slotFuture = CompletableFuture
			.supplyAsync(() -> requestNewAllocatedSlot(slotPool, new SlotRequestId(), timeout), componentMainThreadExecutor)
			.thenCompose(Function.identity());

		try {
			slotFuture.get();
			fail("Expected that the future completes with a TimeoutException.");
		} catch (ExecutionException ee) {
			assertThat(ExceptionUtils.stripExecutionException(ee), instanceOf(TimeoutException.class));
		}
	} finally {
		CompletableFuture.runAsync(ThrowingRunnable.unchecked(slotPool::close), componentMainThreadExecutor).get();
		singleThreadExecutor.shutdownNow();
	}
}
 
Example #6
Source File: SlotPoolPendingRequestFailureTest.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that a pending slot request is failed with a timeout.
 */
@Test
public void testPendingSlotRequestTimeout() throws Exception {
	final ScheduledExecutorService singleThreadExecutor = Executors.newSingleThreadScheduledExecutor();
	final ComponentMainThreadExecutor componentMainThreadExecutor = ComponentMainThreadExecutorServiceAdapter.forSingleThreadExecutor(singleThreadExecutor);

	final SlotPoolImpl slotPool = setUpSlotPool(componentMainThreadExecutor);

	try {
		final Time timeout = Time.milliseconds(5L);

		final CompletableFuture<PhysicalSlot> slotFuture = CompletableFuture
			.supplyAsync(() -> requestNewAllocatedSlot(slotPool, new SlotRequestId(), timeout), componentMainThreadExecutor)
			.thenCompose(Function.identity());

		try {
			slotFuture.get();
			fail("Expected that the future completes with a TimeoutException.");
		} catch (ExecutionException ee) {
			assertThat(ExceptionUtils.stripExecutionException(ee), instanceOf(TimeoutException.class));
		}
	} finally {
		CompletableFuture.runAsync(ThrowingRunnable.unchecked(slotPool::close), componentMainThreadExecutor).get();
		singleThreadExecutor.shutdownNow();
	}
}
 
Example #7
Source File: AbstractTtlDecorator.java    From flink with Apache License 2.0 6 votes vote down vote up
<SE extends Throwable, CE extends Throwable, CLE extends Throwable, V> TtlValue<V> getWrappedWithTtlCheckAndUpdate(
	SupplierWithException<TtlValue<V>, SE> getter,
	ThrowingConsumer<TtlValue<V>, CE> updater,
	ThrowingRunnable<CLE> stateClear) throws SE, CE, CLE {
	TtlValue<V> ttlValue = getter.get();
	if (ttlValue == null) {
		return null;
	} else if (expired(ttlValue)) {
		stateClear.run();
		if (!returnExpired) {
			return null;
		}
	} else if (updateTsOnRead) {
		updater.accept(rewrapWithNewTs(ttlValue));
	}
	return ttlValue;
}
 
Example #8
Source File: RocksDBStateDownloader.java    From flink with Apache License 2.0 6 votes vote down vote up
private List<Runnable> createDownloadRunnables(
	Map<StateHandleID, StreamStateHandle> stateHandleMap,
	Path restoreInstancePath,
	CloseableRegistry closeableRegistry) {
	List<Runnable> runnables = new ArrayList<>(stateHandleMap.size());
	for (Map.Entry<StateHandleID, StreamStateHandle> entry : stateHandleMap.entrySet()) {
		StateHandleID stateHandleID = entry.getKey();
		StreamStateHandle remoteFileHandle = entry.getValue();

		Path path = new Path(restoreInstancePath, stateHandleID.toString());

		runnables.add(ThrowingRunnable.unchecked(
			() -> downloadDataForStateHandle(path, remoteFileHandle, closeableRegistry)));
	}
	return runnables;
}
 
Example #9
Source File: TestingJobGraphStore.java    From flink with Apache License 2.0 6 votes vote down vote up
private TestingJobGraphStore(
		ThrowingConsumer<JobGraphListener, ? extends Exception> startConsumer,
		ThrowingRunnable<? extends Exception> stopRunnable,
		FunctionWithException<Collection<JobID>, Collection<JobID>, ? extends Exception> jobIdsFunction,
		BiFunctionWithException<JobID, Map<JobID, JobGraph>, JobGraph, ? extends Exception> recoverJobGraphFunction,
		ThrowingConsumer<JobGraph, ? extends Exception> putJobGraphConsumer,
		ThrowingConsumer<JobID, ? extends Exception> removeJobGraphConsumer,
		ThrowingConsumer<JobID, ? extends Exception> releaseJobGraphConsumer,
		Collection<JobGraph> initialJobGraphs) {
	this.startConsumer = startConsumer;
	this.stopRunnable = stopRunnable;
	this.jobIdsFunction = jobIdsFunction;
	this.recoverJobGraphFunction = recoverJobGraphFunction;
	this.putJobGraphConsumer = putJobGraphConsumer;
	this.removeJobGraphConsumer = removeJobGraphConsumer;
	this.releaseJobGraphConsumer = releaseJobGraphConsumer;

	for (JobGraph initialJobGraph : initialJobGraphs) {
		storedJobs.put(initialJobGraph.getJobID(), initialJobGraph);
	}
}
 
Example #10
Source File: AbstractTtlDecorator.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
<SE extends Throwable, CE extends Throwable, CLE extends Throwable, V> TtlValue<V> getWrappedWithTtlCheckAndUpdate(
	SupplierWithException<TtlValue<V>, SE> getter,
	ThrowingConsumer<TtlValue<V>, CE> updater,
	ThrowingRunnable<CLE> stateClear) throws SE, CE, CLE {
	TtlValue<V> ttlValue = getter.get();
	if (ttlValue == null) {
		return null;
	} else if (expired(ttlValue)) {
		stateClear.run();
		if (!returnExpired) {
			return null;
		}
	} else if (updateTsOnRead) {
		updater.accept(rewrapWithNewTs(ttlValue));
	}
	return ttlValue;
}
 
Example #11
Source File: RocksDBStateDownloader.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
private List<Runnable> createDownloadRunnables(
	Map<StateHandleID, StreamStateHandle> stateHandleMap,
	Path restoreInstancePath,
	CloseableRegistry closeableRegistry) {
	List<Runnable> runnables = new ArrayList<>(stateHandleMap.size());
	for (Map.Entry<StateHandleID, StreamStateHandle> entry : stateHandleMap.entrySet()) {
		StateHandleID stateHandleID = entry.getKey();
		StreamStateHandle remoteFileHandle = entry.getValue();

		Path path = new Path(restoreInstancePath, stateHandleID.toString());

		runnables.add(ThrowingRunnable.unchecked(
			() -> downloadDataForStateHandle(path, remoteFileHandle, closeableRegistry)));
	}
	return runnables;
}
 
Example #12
Source File: StreamTask.java    From flink with Apache License 2.0 5 votes vote down vote up
private Exception runAndSuppressThrowable(ThrowingRunnable<?> runnable, @Nullable Exception originalException) {
	try {
		runnable.run();
	} catch (Throwable t) {
		// TODO: investigate why Throwable instead of Exception is used here.
		Exception e = t instanceof Exception ? (Exception) t : new Exception(t);
		return ExceptionUtils.firstOrSuppressed(e, originalException);
	}

	return originalException;
}
 
Example #13
Source File: StreamTask.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public <E extends Exception> void executeInTaskThread(
		ThrowingRunnable<E> runnable,
		String descriptionFormat,
		Object... descriptionArgs) throws E {
	if (mailboxProcessor.isMailboxThread()) {
		runnable.run();
	} else {
		mainMailboxExecutor.execute(runnable, descriptionFormat, descriptionArgs);
	}
}
 
Example #14
Source File: TestingComponentMainThreadExecutor.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Executes the given runnable with the main thread executor and blocks until completion.
 */
public void execute(@Nonnull ThrowingRunnable<Throwable> throwingRunnable) {
	execute(() -> {
		throwingRunnable.run();
		return null;
	});
}
 
Example #15
Source File: Mail.java    From flink with Apache License 2.0 5 votes vote down vote up
public Mail(ThrowingRunnable<? extends Exception> runnable, int priority, StreamTaskActionExecutor actionExecutor, String descriptionFormat, Object... descriptionArgs) {
	this.runnable = Preconditions.checkNotNull(runnable);
	this.priority = priority;
	this.descriptionFormat = descriptionFormat == null ? runnable.toString() : descriptionFormat;
	this.descriptionArgs = Preconditions.checkNotNull(descriptionArgs);
	this.actionExecutor = actionExecutor;
}
 
Example #16
Source File: CoordinatorTestUtils.java    From flink with Apache License 2.0 5 votes vote down vote up
static void verifyException(ThrowingRunnable<Throwable> runnable, String failureMessage, String errorMessage) {
	try {
		runnable.run();
		fail(failureMessage);
	} catch (Throwable t) {
		Throwable rootCause = t;
		while (rootCause.getCause() != null) {
			rootCause = rootCause.getCause();
		}
		assertThat(rootCause.getMessage(), Matchers.startsWith(errorMessage));
	}
}
 
Example #17
Source File: MailboxExecutorImpl.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void execute(
		final ThrowingRunnable<? extends Exception> command,
		final String descriptionFormat,
		final Object... descriptionArgs) {
	try {
		mailbox.put(new Mail(command, priority, actionExecutor, descriptionFormat, descriptionArgs));
	} catch (IllegalStateException mbex) {
		throw new RejectedExecutionException(mbex);
	}
}
 
Example #18
Source File: DummyInvokable.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public <E extends Exception> void executeInTaskThread(
		ThrowingRunnable<E> runnable,
		String descriptionFormat,
		Object... descriptionArgs) throws E {
	runnable.run();
}
 
Example #19
Source File: TestingJobLeaderService.java    From flink with Apache License 2.0 5 votes vote down vote up
TestingJobLeaderService(
		QuadConsumer<String, RpcService, HighAvailabilityServices, JobLeaderListener> startConsumer,
		ThrowingRunnable<? extends Exception> stopRunnable,
		Consumer<JobID> removeJobConsumer,
		BiConsumerWithException<JobID, String, ? extends Exception> addJobConsumer,
		Consumer<JobID> reconnectConsumer,
		Function<JobID, Boolean> containsJobFunction) {
	this.startConsumer = startConsumer;
	this.stopRunnable = stopRunnable;
	this.removeJobConsumer = removeJobConsumer;
	this.addJobConsumer = addJobConsumer;
	this.reconnectConsumer = reconnectConsumer;
	this.containsJobFunction = containsJobFunction;
}
 
Example #20
Source File: AbstractInvokable.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * This method performs some action asynchronously in the task thread.
 *
 * @param runnable the action to perform
 * @param descriptionFormat the optional description for the command that is used for debugging and error-reporting.
 * @param descriptionArgs the parameters used to format the final description string.
 */
public <E extends Exception> void executeInTaskThread(
		ThrowingRunnable<E> runnable,
		String descriptionFormat,
		Object... descriptionArgs) throws E {
	throw new UnsupportedOperationException(
		String.format("executeInTaskThread not supported by %s", getClass().getName()));
}
 
Example #21
Source File: CheckpointBarrierUnalignerTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public <E extends Exception> void executeInTaskThread(
		ThrowingRunnable<E> runnable,
		String descriptionFormat,
		Object... descriptionArgs) throws E {
	runnable.run();
}
 
Example #22
Source File: AbstractTtlDecorator.java    From flink with Apache License 2.0 5 votes vote down vote up
<SE extends Throwable, CE extends Throwable, CLE extends Throwable, V> V getWithTtlCheckAndUpdate(
	SupplierWithException<TtlValue<V>, SE> getter,
	ThrowingConsumer<TtlValue<V>, CE> updater,
	ThrowingRunnable<CLE> stateClear) throws SE, CE, CLE {
	TtlValue<V> ttlValue = getWrappedWithTtlCheckAndUpdate(getter, updater, stateClear);
	return ttlValue == null ? null : ttlValue.getUserValue();
}
 
Example #23
Source File: CheckpointBarrierUnalignerCancellationTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public <E extends Exception> void executeInTaskThread(
		ThrowingRunnable<E> runnable,
		String descriptionFormat,
		Object... descriptionArgs) {
	try {
		runnable.run();
	} catch (Exception e) {
		throw new RuntimeException(e);
	}
}
 
Example #24
Source File: JsonResponseHistoryServerArchivist.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<Acknowledge> archiveExecutionGraph(AccessExecutionGraph executionGraph) {
	return CompletableFuture
		.runAsync(
			ThrowingRunnable.unchecked(() ->
				FsJobArchivist.archiveJob(archivePath, executionGraph.getJobID(), jsonArchivist.archiveJsonWithPath(executionGraph))),
			ioExecutor)
		.thenApply(ignored -> Acknowledge.get());
}
 
Example #25
Source File: RocksDBResourceContainerTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testFreeSharedResourcesAfterClose() throws Exception {
	LRUCache cache = new LRUCache(1024L);
	WriteBufferManager wbm = new WriteBufferManager(1024L, cache);
	RocksDBSharedResources sharedResources = new RocksDBSharedResources(cache, wbm);
	final ThrowingRunnable<Exception> disposer = sharedResources::close;
	OpaqueMemoryResource<RocksDBSharedResources> opaqueResource =
		new OpaqueMemoryResource<>(sharedResources, 1024L, disposer);

	RocksDBResourceContainer container = new RocksDBResourceContainer(PredefinedOptions.DEFAULT, null, opaqueResource);

	container.close();
	assertThat(cache.isOwningHandle(), is(false));
	assertThat(wbm.isOwningHandle(), is(false));
}
 
Example #26
Source File: TestingComponentMainThreadExecutor.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Executes the given runnable with the main thread executor and blocks until completion.
 */
public void execute(@Nonnull ThrowingRunnable<Throwable> throwingRunnable) {
	execute(() -> {
		throwingRunnable.run();
		return null;
	});
}
 
Example #27
Source File: LambdaUtil.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Runs the given runnable with the given ClassLoader as the thread's
 * {@link Thread#setContextClassLoader(ClassLoader) context class loader}.
 *
 * <p>The method will make sure to set the context class loader of the calling thread
 * back to what it was before after the runnable completed.
 */
public static <E extends Throwable> void withContextClassLoader(
		final ClassLoader cl,
		final ThrowingRunnable<E> r) throws E {

	try (TemporaryClassLoaderContext tmpCl = new TemporaryClassLoaderContext(cl)) {
		r.run();
	}
}
 
Example #28
Source File: AbstractTtlDecorator.java    From flink with Apache License 2.0 5 votes vote down vote up
<SE extends Throwable, CE extends Throwable, CLE extends Throwable, V> V getWithTtlCheckAndUpdate(
	SupplierWithException<TtlValue<V>, SE> getter,
	ThrowingConsumer<TtlValue<V>, CE> updater,
	ThrowingRunnable<CLE> stateClear) throws SE, CE, CLE {
	TtlValue<V> ttlValue = getWrappedWithTtlCheckAndUpdate(getter, updater, stateClear);
	return ttlValue == null ? null : ttlValue.getUserValue();
}
 
Example #29
Source File: TestingComponentMainThreadExecutor.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Executes the given runnable with the main thread executor and blocks until completion.
 */
public void execute(@Nonnull ThrowingRunnable<Throwable> throwingRunnable) {
	execute(() -> {
		throwingRunnable.run();
		return null;
	});
}
 
Example #30
Source File: AbstractTtlDecorator.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
<SE extends Throwable, CE extends Throwable, CLE extends Throwable, V> V getWithTtlCheckAndUpdate(
	SupplierWithException<TtlValue<V>, SE> getter,
	ThrowingConsumer<TtlValue<V>, CE> updater,
	ThrowingRunnable<CLE> stateClear) throws SE, CE, CLE {
	TtlValue<V> ttlValue = getWrappedWithTtlCheckAndUpdate(getter, updater, stateClear);
	return ttlValue == null ? null : ttlValue.getUserValue();
}