static <R> Supplier<R> unchecked(CheckedSupplier<R> checkedSupplier) { return () -> { try { return checkedSupplier.get(); } catch (Exception e) { throw new RuntimeException(e); } }; }
@Override @SuppressWarnings("unchecked") protected AbstractFetcher<T, ?> createFetcher( SourceContext<T> sourceContext, Map<KafkaTopicPartition, Long> thisSubtaskPartitionsWithStartOffsets, SerializedValue<AssignerWithPeriodicWatermarks<T>> watermarksPeriodic, SerializedValue<AssignerWithPunctuatedWatermarks<T>> watermarksPunctuated, StreamingRuntimeContext runtimeContext, OffsetCommitMode offsetCommitMode, MetricGroup consumerMetricGroup, boolean useMetrics) throws Exception { return testFetcherSupplier.get(); }
public static RefCountedBufferingFileStream restore( final FunctionWithException<File, RefCountedFile, IOException> tmpFileProvider, final File initialTmpFile) throws IOException { return new RefCountedBufferingFileStream( tmpFileProvider.apply(initialTmpFile), BUFFER_SIZE); } }
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); } } }
private Map<StateHandleID, CompletableFuture<StreamStateHandle>> createUploadFutures( Map<StateHandleID, Path> files, CheckpointStreamFactory checkpointStreamFactory, CloseableRegistry closeableRegistry) { Map<StateHandleID, CompletableFuture<StreamStateHandle>> futures = new HashMap<>(files.size()); for (Map.Entry<StateHandleID, Path> entry : files.entrySet()) { final Supplier<StreamStateHandle> supplier = CheckedSupplier.unchecked(() -> uploadLocalFileToCheckpointFs(entry.getValue(), checkpointStreamFactory, closeableRegistry)); futures.put(entry.getKey(), CompletableFuture.supplyAsync(supplier, executorService)); } return futures; }
/** * Converts a {@link ThrowingConsumer} into a {@link Consumer} which throws checked exceptions * as unchecked. * * @param throwingConsumer to convert into a {@link Consumer} * @param <A> input type * @return {@link Consumer} which throws all checked exceptions as unchecked */ public static <A> Consumer<A> uncheckedConsumer(ThrowingConsumer<A, ?> throwingConsumer) { return (A value) -> { try { throwingConsumer.accept(value); } catch (Throwable t) { ExceptionUtils.rethrow(t); } }; } }
/** * 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); } }
private void testNormalConsumerLifecycle(FlinkKafkaConsumerBase<String> testKafkaConsumer) throws Exception { setupConsumer(testKafkaConsumer); final CompletableFuture<Void> runFuture = CompletableFuture.runAsync(ThrowingRunnable.unchecked(() -> testKafkaConsumer.run(new TestSourceContext<>()))); testKafkaConsumer.close(); runFuture.get(); }
/** * Convert a {@link BiConsumerWithException} into a {@link BiConsumer}. * * @param biConsumerWithException BiConsumer with exception to convert into a {@link BiConsumer}. * @param <A> first input type * @param <B> second input type * @return {@link BiConsumer} which rethrows all checked exceptions as unchecked. */ static <A, B> BiConsumer<A, B> unchecked(BiConsumerWithException<A, B, ?> biConsumerWithException) { return (A a, B b) -> { try { biConsumerWithException.accept(a, b); } catch (Throwable t) { ExceptionUtils.rethrow(t); } }; } }
/** * Convert at {@link BiFunctionWithException} into a {@link BiFunction}. * * @param biFunctionWithException function with exception to convert into a function * @param <A> input type * @param <B> output type * @return {@link BiFunction} which throws all checked exception as an unchecked exception. */ static <A, B, C> BiFunction<A, B, C> unchecked(BiFunctionWithException<A, B, C, ?> biFunctionWithException) { return (A a, B b) -> { try { return biFunctionWithException.apply(a, b); } catch (Throwable t) { ExceptionUtils.rethrow(t); // we need this to appease the compiler :-( return null; } }; } }
/** * 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 <R, E extends Throwable> R withContextClassLoader( final ClassLoader cl, final SupplierWithException<R, E> s) throws E { final Thread currentThread = Thread.currentThread(); final ClassLoader oldClassLoader = currentThread.getContextClassLoader(); try { currentThread.setContextClassLoader(cl); return s.get(); } finally { currentThread.setContextClassLoader(oldClassLoader); } } }
/** * 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(); } } }
private <C> CompletableFuture<C> retry( CheckedSupplier<CompletableFuture<C>> operation, Predicate<Throwable> retryPredicate) { return FutureUtils.retryWithDelay( CheckedSupplier.unchecked(operation), restClusterClientConfiguration.getRetryMaxAttempts(), Time.milliseconds(restClusterClientConfiguration.getRetryDelay()), retryPredicate, new ScheduledExecutorServiceAdapter(retryExecutorService)); }
/** * This method supplies all elements from the input to the consumer. Exceptions that happen on elements are * suppressed until all elements are processed. If exceptions happened for one or more of the inputs, they are * reported in a combining suppressed exception. * * @param inputs iterator for all inputs to the throwingConsumer. * @param throwingConsumer this consumer will be called for all elements delivered by the input iterator. * @param <T> the type of input. * @throws Exception collected exceptions that happened during the invocation of the consumer on the input elements. */ public static <T> void applyToAllWhileSuppressingExceptions( Iterable<T> inputs, ThrowingConsumer<T, ? extends Exception> throwingConsumer) throws Exception { if (inputs != null && throwingConsumer != null) { Exception exception = null; for (T input : inputs) { if (input != null) { try { throwingConsumer.accept(input); } catch (Exception ex) { exception = ExceptionUtils.firstOrSuppressed(ex, exception); } } } if (exception != null) { throw exception; } } }
public static RefCountedBufferingFileStream openNew( final FunctionWithException<File, RefCountedFile, IOException> tmpFileProvider) throws IOException { return new RefCountedBufferingFileStream( tmpFileProvider.apply(null), BUFFER_SIZE); }
/** * Converts a {@link ThrowingRunnable} into a {@link Runnable} which throws all checked exceptions * as unchecked. * * @param throwingRunnable to convert into a {@link Runnable} * @return {@link Runnable} which throws all checked exceptions as unchecked. */ static Runnable unchecked(ThrowingRunnable<?> throwingRunnable) { return () -> { try { throwingRunnable.run(); } catch (Throwable t) { ExceptionUtils.rethrow(t); } }; } }
/** * @return wrapped {@link OptionalFailure} returned by {@code valueSupplier} or wrapped failure if * {@code valueSupplier} has thrown an {@link Exception}. */ public static <T> OptionalFailure<T> createFrom(CheckedSupplier<T> valueSupplier) { try { return of(valueSupplier.get()); } catch (Exception ex) { return ofFailure(ex); } }
private FSDataInputStream createInputStream( final SupplierWithException<FSDataInputStream, IOException> streamOpener) throws IOException { final SupplierWithException<InStream, IOException> wrappedStreamOpener = () -> new InStream(streamOpener.get(), this); return createStream(wrappedStreamOpener, openInputStreams, false); }
/** * Convert at {@link FunctionWithException} into a {@link Function}. * * @param functionWithException function with exception to convert into a function * @param <A> input type * @param <B> output type * @return {@link Function} which throws all checked exception as an unchecked exception. */ public static <A, B> Function<A, B> uncheckedFunction(FunctionWithException<A, B, ?> functionWithException) { return (A value) -> { try { return functionWithException.apply(value); } catch (Throwable t) { ExceptionUtils.rethrow(t); // we need this to appease the compiler :-( return null; } }; }
private FSDataOutputStream createOutputStream( final SupplierWithException<FSDataOutputStream, IOException> streamOpener) throws IOException { final SupplierWithException<OutStream, IOException> wrappedStreamOpener = () -> new OutStream(streamOpener.get(), this); return createStream(wrappedStreamOpener, openOutputStreams, true); }