List of usage examples for java.util.concurrent CompletableFuture isDone
public boolean isDone()
From source file:org.apache.pulsar.client.impl.ClientCnx.java
private void addPendingLookupRequests(long requestId, CompletableFuture<LookupDataResult> future) { pendingLookupRequests.put(requestId, future); eventLoopGroup.schedule(() -> {/*from www. ja v a 2 s.c om*/ if (!future.isDone()) { future.completeExceptionally(new TimeoutException( requestId + " lookup request timedout after ms " + operationTimeoutMs)); } }, operationTimeoutMs, TimeUnit.MILLISECONDS); }
From source file:org.apache.pulsar.client.impl.ClientCnx.java
private void checkRequestTimeout() { while (!requestTimeoutQueue.isEmpty()) { RequestTime request = requestTimeoutQueue.peek(); if (request == null || (System.currentTimeMillis() - request.creationTimeMs) < operationTimeoutMs) { // if there is no request that is timed out then exit the loop break; }/*from ww w .j a v a 2 s .co m*/ request = requestTimeoutQueue.poll(); CompletableFuture<ProducerResponse> requestFuture = pendingRequests.remove(request.requestId); if (requestFuture != null && !requestFuture.isDone() && requestFuture.completeExceptionally(new TimeoutException( request.requestId + " lookup request timedout after ms " + operationTimeoutMs))) { log.warn("{} request {} timed out after {} ms", ctx.channel(), request.requestId, operationTimeoutMs); } else { // request is already completed successfully. } } }
From source file:org.apache.pulsar.compaction.TwoPhaseCompactor.java
private void phaseOneLoop(RawReader reader, Optional<MessageId> firstMessageId, Optional<MessageId> toMessageId, MessageId lastMessageId, Map<String, MessageId> latestForKey, CompletableFuture<PhaseOneResult> loopPromise) { if (loopPromise.isDone()) { return;/*from w w w .j a va 2 s . co m*/ } CompletableFuture<RawMessage> future = reader.readNextAsync(); scheduleTimeout(future); future.whenCompleteAsync((m, exception) -> { try { if (exception != null) { loopPromise.completeExceptionally(exception); return; } MessageId id = m.getMessageId(); boolean deletedMessage = false; if (RawBatchConverter.isReadableBatch(m)) { try { RawBatchConverter.extractIdsAndKeys(m) .forEach(e -> latestForKey.put(e.getRight(), e.getLeft())); } catch (IOException ioe) { log.info("Error decoding batch for message {}. Whole batch will be included in output", id, ioe); } } else { Pair<String, Integer> keyAndSize = extractKeyAndSize(m); if (keyAndSize != null) { if (keyAndSize.getRight() > 0) { latestForKey.put(keyAndSize.getLeft(), id); } else { deletedMessage = true; latestForKey.remove(keyAndSize.getLeft()); } } } MessageId first = firstMessageId.orElse(deletedMessage ? null : id); MessageId to = deletedMessage ? toMessageId.orElse(null) : id; if (id.compareTo(lastMessageId) == 0) { loopPromise.complete(new PhaseOneResult(first, to, lastMessageId, latestForKey)); } else { phaseOneLoop(reader, Optional.ofNullable(first), Optional.ofNullable(to), lastMessageId, latestForKey, loopPromise); } } finally { m.close(); } }, scheduler); }
From source file:org.apache.pulsar.compaction.TwoPhaseCompactor.java
private void phaseTwoLoop(RawReader reader, MessageId to, Map<String, MessageId> latestForKey, LedgerHandle lh, Semaphore outstanding, CompletableFuture<Void> promise) { reader.readNextAsync().whenCompleteAsync((m, exception) -> { if (exception != null) { promise.completeExceptionally(exception); return; } else if (promise.isDone()) { return; }/*from w ww .ja va 2 s. co m*/ MessageId id = m.getMessageId(); Optional<RawMessage> messageToAdd = Optional.empty(); if (RawBatchConverter.isReadableBatch(m)) { try { messageToAdd = RawBatchConverter.rebatchMessage(m, (key, subid) -> latestForKey.get(key).equals(subid)); } catch (IOException ioe) { log.info("Error decoding batch for message {}. Whole batch will be included in output", id, ioe); messageToAdd = Optional.of(m); } } else { Pair<String, Integer> keyAndSize = extractKeyAndSize(m); MessageId msg; if (keyAndSize == null) { // pass through messages without a key messageToAdd = Optional.of(m); } else if ((msg = latestForKey.get(keyAndSize.getLeft())) != null && msg.equals(id)) { // consider message only if present into latestForKey map if (keyAndSize.getRight() <= 0) { promise.completeExceptionally(new IllegalArgumentException( "Compaction phase found empty record from sorted key-map")); } messageToAdd = Optional.of(m); } else { m.close(); // Reached to last-id and phase-one found it deleted-message while iterating on ledger so, not // present under latestForKey. Complete the compaction. if (to.equals(id)) { promise.complete(null); } } } messageToAdd.ifPresent((toAdd) -> { try { outstanding.acquire(); CompletableFuture<Void> addFuture = addToCompactedLedger(lh, toAdd) .whenComplete((res, exception2) -> { outstanding.release(); if (exception2 != null) { promise.completeExceptionally(exception2); } }); if (to.equals(id)) { addFuture.whenComplete((res, exception2) -> { if (exception2 == null) { promise.complete(null); } }); } } catch (InterruptedException ie) { Thread.currentThread().interrupt(); promise.completeExceptionally(ie); } }); phaseTwoLoop(reader, to, latestForKey, lh, outstanding, promise); }, scheduler); }
From source file:org.apache.tinkerpop.gremlin.driver.ResultQueueTest.java
@Test public void shouldAwaitEverythingAndFlushOnMarkCompleted() throws Exception { final CompletableFuture<List<Result>> future = resultQueue.await(4); resultQueue.add(new Result("test1")); resultQueue.add(new Result("test2")); resultQueue.add(new Result("test3")); assertThat(future.isDone(), is(false)); resultQueue.markComplete();/*from w w w .j av a 2 s. c o m*/ assertThat(future.isDone(), is(true)); final List<Result> results = future.get(); assertEquals("test1", results.get(0).getString()); assertEquals("test2", results.get(1).getString()); assertEquals("test3", results.get(2).getString()); assertEquals(3, results.size()); assertThat(resultQueue.isEmpty(), is(true)); }
From source file:org.apache.tinkerpop.gremlin.driver.ResultQueueTest.java
@Test public void shouldAwaitFailTheFutureOnMarkError() throws Exception { final CompletableFuture<List<Result>> future = resultQueue.await(4); resultQueue.add(new Result("test1")); resultQueue.add(new Result("test2")); resultQueue.add(new Result("test3")); assertThat(future.isDone(), is(false)); resultQueue.markError(new Exception("no worky")); assertThat(future.isDone(), is(true)); try {/*from ww w .j a v a 2s .co m*/ future.get(); } catch (Exception ex) { final Throwable t = ExceptionUtils.getRootCause(ex); assertEquals("no worky", t.getMessage()); } }
From source file:org.apache.tinkerpop.gremlin.driver.ResultQueueTest.java
@Test public void shouldAwaitToExpectedValueAndDrainOnAdd() throws Exception { final CompletableFuture<List<Result>> future = resultQueue.await(3); resultQueue.add(new Result("test1")); resultQueue.add(new Result("test2")); // shouldn't complete until the third item is in play assertThat(future.isDone(), is(false)); resultQueue.add(new Result("test3")); final List<Result> results = future.get(); assertEquals("test1", results.get(0).getString()); assertEquals("test2", results.get(1).getString()); assertEquals("test3", results.get(2).getString()); assertEquals(3, results.size());//from w w w . ja va 2 s .c om assertThat(resultQueue.isEmpty(), is(true)); }
From source file:org.apache.tinkerpop.gremlin.driver.ResultQueueTest.java
@Test public void shouldAwaitMultipleToExpectedValueAndDrainOnAdd() throws Exception { final CompletableFuture<List<Result>> future1 = resultQueue.await(3); final CompletableFuture<List<Result>> future2 = resultQueue.await(1); resultQueue.add(new Result("test1")); resultQueue.add(new Result("test2")); // shouldn't complete the first future until the third item is in play assertThat(future1.isDone(), is(false)); assertThat(future2.isDone(), is(false)); resultQueue.add(new Result("test3")); final List<Result> results1 = future1.get(); assertEquals("test1", results1.get(0).getString()); assertEquals("test2", results1.get(1).getString()); assertEquals("test3", results1.get(2).getString()); assertEquals(3, results1.size());/* w w w . java 2 s . c om*/ assertThat(future1.isDone(), is(true)); assertThat(future2.isDone(), is(false)); resultQueue.add(new Result("test4")); assertThat(future1.isDone(), is(true)); assertThat(future2.isDone(), is(true)); final List<Result> results2 = future2.get(); assertEquals("test4", results2.get(0).getString()); assertEquals(1, results2.size()); assertThat(resultQueue.isEmpty(), is(true)); }
From source file:org.apache.tinkerpop.gremlin.driver.ResultQueueTest.java
@Test public void shouldAwaitToExpectedValueAndDrainOnAwait() throws Exception { resultQueue.add(new Result("test1")); resultQueue.add(new Result("test2")); resultQueue.add(new Result("test3")); final CompletableFuture<List<Result>> future = resultQueue.await(3); assertThat(future.isDone(), is(true)); final List<Result> results = future.get(); assertEquals("test1", results.get(0).getString()); assertEquals("test2", results.get(1).getString()); assertEquals("test3", results.get(2).getString()); assertEquals(3, results.size());/* w w w. j a va2 s. c o m*/ assertThat(resultQueue.isEmpty(), is(true)); }
From source file:org.apache.tinkerpop.gremlin.driver.ResultQueueTest.java
@Test public void shouldAwaitToReadCompletedAndDrainOnAwait() throws Exception { resultQueue.add(new Result("test1")); resultQueue.add(new Result("test2")); resultQueue.add(new Result("test3")); resultQueue.markComplete();/*from w ww .jav a 2 s . co m*/ // you might want 30 but there are only three final CompletableFuture<List<Result>> future = resultQueue.await(30); assertThat(future.isDone(), is(true)); final List<Result> results = future.get(); assertEquals("test1", results.get(0).getString()); assertEquals("test2", results.get(1).getString()); assertEquals("test3", results.get(2).getString()); assertEquals(3, results.size()); assertThat(resultQueue.isEmpty(), is(true)); }