break; case FAILED: return failedFuture(state.getThrowable()); case CLOSED: throw new IllegalStateException("HiveSplitSource is already closed");
public synchronized CompletableFuture<?> asyncAbort() { if (!completedSuccessfully.compareAndSet(null, false)) { if (completedSuccessfully.get()) { // Should not happen normally return failedFuture(new IllegalStateException("Current transaction already committed")); } // Already done return completedFuture(null); } return abortInternal(); }
private CompletableFuture<TransactionMetadata> removeTransactionMetadataAsFuture(TransactionId transactionId) { TransactionMetadata transactionMetadata = transactions.remove(transactionId); if (transactionMetadata == null) { return failedFuture(unknownTransactionError(transactionId)); } return completedFuture(transactionMetadata); }
@Override public CompletableFuture<List<ConnectorSplit>> getNextBatch(int maxSize) { checkState(!closed, "Provider is already closed"); CompletableFuture<List<ConnectorSplit>> future = queue.getBatchAsync(maxSize); // Before returning, check if there is a registered failure. // If so, we want to throw the error, instead of returning because the scheduler can block // while scheduling splits and wait for work to finish before continuing. In this case, // we want to end the query as soon as possible and abort the work if (throwable.get() != null) { return failedFuture(throwable.get()); } return future; }
return failedFuture(new PrestoException(TRANSACTION_ALREADY_ABORTED, "Current transaction has already been aborted"));
@Test public void testFailedFuture() throws Exception { CompletableFuture<Object> future = failedFuture(new SQLException("foo")); assertTrue(future.isCompletedExceptionally()); assertFailure(future::get, e -> { assertInstanceOf(e, ExecutionException.class); assertTrue(e.getCause() instanceof SQLException); assertEquals(e.getCause().getMessage(), "foo"); }); }
@Test public void testFailedFuture() throws Exception { CompletableFuture<Object> future = failedFuture(new SQLException("foo")); assertTrue(future.isCompletedExceptionally()); assertFailure(future::get, e -> { assertInstanceOf(e, ExecutionException.class); assertTrue(e.getCause() instanceof SQLException); assertEquals(e.getCause().getMessage(), "foo"); }); }
@Test public void testFailedFuture() throws Exception { CompletableFuture<Object> future = failedFuture(new SQLException("foo")); assertTrue(future.isCompletedExceptionally()); assertFailure(future::get, (e) -> { assertInstanceOf(e, ExecutionException.class); assertTrue(e.getCause() instanceof SQLException); assertEquals(e.getCause().getMessage(), "foo"); }); }
@Test public void testGetFutureValueWithExceptionType() throws Exception { assertGetUnchecked(future -> getFutureValue(future, IOException.class)); assertFailure(() -> getFutureValue(failedFuture(new SQLException("foo")), SQLException.class), e -> { assertInstanceOf(e, SQLException.class); assertEquals(e.getMessage(), "foo"); }); }
@Test public void testGetFutureValueWithExceptionType() throws Exception { assertGetUnchecked(future -> getFutureValue(future, IOException.class)); assertFailure(() -> getFutureValue(failedFuture(new SQLException("foo")), SQLException.class), (e) -> { assertInstanceOf(e, SQLException.class); assertEquals(e.getMessage(), "foo"); }); }
@Test public void testGetFutureValueWithExceptionType() throws Exception { assertGetUnchecked(future -> getFutureValue(future, IOException.class)); assertFailure(() -> getFutureValue(failedFuture(new SQLException("foo")), SQLException.class), e -> { assertInstanceOf(e, SQLException.class); assertEquals(e.getMessage(), "foo"); }); }
@Test public void testAlreadyCompleteExceptionallyUnmodifiableFuture() throws Exception { CompletableFuture<String> future = failedFuture(new SQLException("foo")); CompletableFuture<String> unmodifiableFuture = unmodifiableFuture(future); assertFailure(() -> getFutureValue(future, SQLException.class), e -> { assertInstanceOf(e, SQLException.class); assertEquals(e.getMessage(), "foo"); }); assertTrue(unmodifiableFuture.isDone()); assertFailure(() -> getFutureValue(unmodifiableFuture, SQLException.class), e -> { assertInstanceOf(e, SQLException.class); assertEquals(e.getMessage(), "foo"); }); }
@Test public void testAlreadyCompleteExceptionallyUnmodifiableFuture() throws Exception { CompletableFuture<String> future = failedFuture(new SQLException("foo")); CompletableFuture<String> unmodifiableFuture = unmodifiableFuture(future); assertFailure(() -> getFutureValue(future, SQLException.class), (e) -> { assertInstanceOf(e, SQLException.class); assertEquals(e.getMessage(), "foo"); }); assertTrue(unmodifiableFuture.isDone()); assertFailure(() -> getFutureValue(unmodifiableFuture, SQLException.class), (e) -> { assertInstanceOf(e, SQLException.class); assertEquals(e.getMessage(), "foo"); }); }
@Test public void testTryGetFutureValueWithExceptionType() throws Exception { assertGetUnchecked(future -> { Optional<?> optional = tryGetFutureValue(future, 100, MILLISECONDS, IOException.class); if (optional.isPresent()) { return optional.get(); } // null value is also absent assertNull(getFutureValue(future, IOException.class)); return null; }); assertEquals(tryGetFutureValue(new CompletableFuture<>(), 10, MILLISECONDS), Optional.empty()); assertFailure(() -> tryGetFutureValue(failedFuture(new SQLException("foo")), 10, MILLISECONDS, SQLException.class), (e) -> { assertInstanceOf(e, SQLException.class); assertEquals(e.getMessage(), "foo"); }); }
@Test public void testTryGetFutureValueWithExceptionType() throws Exception { assertGetUnchecked(future -> { Optional<?> optional = tryGetFutureValue(future, 100, MILLISECONDS, IOException.class); if (optional.isPresent()) { return optional.get(); } // null value is also absent assertNull(getFutureValue(future, IOException.class)); return null; }); assertEquals(tryGetFutureValue(new CompletableFuture<>(), 10, MILLISECONDS), Optional.empty()); assertFailure(() -> tryGetFutureValue(failedFuture(new SQLException("foo")), 10, MILLISECONDS, SQLException.class), e -> { assertInstanceOf(e, SQLException.class); assertEquals(e.getMessage(), "foo"); }); }
@Test public void testAlreadyCompleteExceptionallyUnmodifiableFuture() throws Exception { CompletableFuture<String> future = failedFuture(new SQLException("foo")); CompletableFuture<String> unmodifiableFuture = unmodifiableFuture(future); assertFailure(() -> getFutureValue(future, SQLException.class), e -> { assertInstanceOf(e, SQLException.class); assertEquals(e.getMessage(), "foo"); }); assertTrue(unmodifiableFuture.isDone()); assertFailure(() -> getFutureValue(unmodifiableFuture, SQLException.class), e -> { assertInstanceOf(e, SQLException.class); assertEquals(e.getMessage(), "foo"); }); }
@Test public void testTryGetFutureValueWithExceptionType() throws Exception { assertGetUnchecked(future -> { Optional<?> optional = tryGetFutureValue(future, 100, MILLISECONDS, IOException.class); if (optional.isPresent()) { return optional.get(); } // null value is also absent assertNull(getFutureValue(future, IOException.class)); return null; }); assertEquals(tryGetFutureValue(new CompletableFuture<>(), 10, MILLISECONDS), Optional.empty()); assertFailure(() -> tryGetFutureValue(failedFuture(new SQLException("foo")), 10, MILLISECONDS, SQLException.class), e -> { assertInstanceOf(e, SQLException.class); assertEquals(e.getMessage(), "foo"); }); }
public static void assertGetUncheckedInternal(UncheckedGetter getter) throws Exception { assertEquals(getter.get(completedFuture("foo")), "foo"); assertFailure(() -> getter.get(failedFuture(new IllegalArgumentException("foo"))), (e) -> { assertInstanceOf(e, IllegalArgumentException.class); assertEquals(e.getMessage(), "foo"); }); assertFailure(() -> getter.get(failedFuture(new SQLException("foo"))), (e) -> { assertInstanceOf(e, RuntimeException.class); assertInstanceOf(e.getCause(), SQLException.class); assertEquals(e.getCause().getMessage(), "foo"); }); Thread.currentThread().interrupt(); assertFailure(() -> getter.get(new CompletableFuture<>()), (e) -> { assertInstanceOf(e, RuntimeException.class); assertInstanceOf(e.getCause(), InterruptedException.class); assertTrue(Thread.interrupted()); }); assertFalse(Thread.currentThread().isInterrupted()); CompletableFuture<Object> canceledFuture = new CompletableFuture<>(); canceledFuture.cancel(true); assertFailure(() -> getter.get(canceledFuture), e -> assertInstanceOf(e, CancellationException.class)); assertEquals(getter.get(completedFuture(null)), null); }
private static void assertGetUncheckedInternal(UncheckedGetter getter) throws Exception { assertEquals(getter.get(completedFuture("foo")), "foo"); assertFailure(() -> getter.get(failedFuture(new IllegalArgumentException("foo"))), e -> { assertInstanceOf(e, IllegalArgumentException.class); assertEquals(e.getMessage(), "foo"); }); assertFailure(() -> getter.get(failedFuture(new SQLException("foo"))), e -> { assertInstanceOf(e, RuntimeException.class); assertInstanceOf(e.getCause(), SQLException.class); assertEquals(e.getCause().getMessage(), "foo"); }); Thread.currentThread().interrupt(); assertFailure(() -> getter.get(new CompletableFuture<>()), e -> { assertInstanceOf(e, RuntimeException.class); assertInstanceOf(e.getCause(), InterruptedException.class); assertTrue(Thread.interrupted()); }); assertFalse(Thread.currentThread().isInterrupted()); CompletableFuture<Object> canceledFuture = new CompletableFuture<>(); canceledFuture.cancel(true); assertFailure(() -> getter.get(canceledFuture), e -> assertInstanceOf(e, CancellationException.class)); assertEquals(getter.get(completedFuture(null)), null); }
private static void assertGetUncheckedInternal(UncheckedGetter getter) throws Exception { assertEquals(getter.get(completedFuture("foo")), "foo"); assertFailure(() -> getter.get(failedFuture(new IllegalArgumentException("foo"))), e -> { assertInstanceOf(e, IllegalArgumentException.class); assertEquals(e.getMessage(), "foo"); }); assertFailure(() -> getter.get(failedFuture(new SQLException("foo"))), e -> { assertInstanceOf(e, RuntimeException.class); assertInstanceOf(e.getCause(), SQLException.class); assertEquals(e.getCause().getMessage(), "foo"); }); Thread.currentThread().interrupt(); assertFailure(() -> getter.get(new CompletableFuture<>()), e -> { assertInstanceOf(e, RuntimeException.class); assertInstanceOf(e.getCause(), InterruptedException.class); assertTrue(Thread.interrupted()); }); assertFalse(Thread.currentThread().isInterrupted()); CompletableFuture<Object> canceledFuture = new CompletableFuture<>(); canceledFuture.cancel(true); assertFailure(() -> getter.get(canceledFuture), e -> assertInstanceOf(e, CancellationException.class)); assertEquals(getter.get(completedFuture(null)), null); }