List of usage examples for java.util.concurrent CompletableFuture completeExceptionally
public boolean completeExceptionally(Throwable ex)
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 . ja va2 s . c o 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 scheduleTimeout(CompletableFuture<RawMessage> future) { Future<?> timeout = scheduler.schedule(() -> { future.completeExceptionally(new TimeoutException("Timeout")); }, 10, TimeUnit.SECONDS);//from w w w.j a va 2 s . c om future.whenComplete((res, exception) -> { timeout.cancel(true); }); }
From source file:org.apache.pulsar.compaction.TwoPhaseCompactor.java
private CompletableFuture<Long> phaseTwoSeekThenLoop(RawReader reader, MessageId from, MessageId to, MessageId lastReadId, Map<String, MessageId> latestForKey, BookKeeper bk, LedgerHandle ledger) { CompletableFuture<Long> promise = new CompletableFuture<>(); reader.seekAsync(from).thenCompose((v) -> { Semaphore outstanding = new Semaphore(MAX_OUTSTANDING); CompletableFuture<Void> loopPromise = new CompletableFuture<Void>(); phaseTwoLoop(reader, to, latestForKey, ledger, outstanding, loopPromise); return loopPromise; }).thenCompose((v) -> closeLedger(ledger)) .thenCompose((v) -> reader.acknowledgeCumulativeAsync(lastReadId, ImmutableMap.of(COMPACTED_TOPIC_LEDGER_PROPERTY, ledger.getId()))) .whenComplete((res, exception) -> { if (exception != null) { deleteLedger(bk, ledger).whenComplete((res2, exception2) -> { if (exception2 != null) { log.warn("Cleanup of ledger {} for failed", ledger, exception2); }/* w ww . ja va 2 s . c o m*/ // complete with original exception promise.completeExceptionally(exception); }); } else { promise.complete(ledger.getId()); } }); return promise; }
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; }/* w w w. j av a2 s . c o 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.pulsar.compaction.TwoPhaseCompactor.java
private CompletableFuture<LedgerHandle> createLedger(BookKeeper bk, Map<String, byte[]> metadata) { CompletableFuture<LedgerHandle> bkf = new CompletableFuture<>(); bk.asyncCreateLedger(conf.getManagedLedgerDefaultEnsembleSize(), conf.getManagedLedgerDefaultWriteQuorum(), conf.getManagedLedgerDefaultAckQuorum(), Compactor.COMPACTED_TOPIC_LEDGER_DIGEST_TYPE, Compactor.COMPACTED_TOPIC_LEDGER_PASSWORD, (rc, ledger, ctx) -> { if (rc != BKException.Code.OK) { bkf.completeExceptionally(BKException.create(rc)); } else { bkf.complete(ledger); }/*from w w w . j a v a 2s. c o m*/ }, null, metadata); return bkf; }
From source file:org.apache.pulsar.compaction.TwoPhaseCompactor.java
private CompletableFuture<Void> deleteLedger(BookKeeper bk, LedgerHandle lh) { CompletableFuture<Void> bkf = new CompletableFuture<>(); bk.asyncDeleteLedger(lh.getId(), (rc, ctx) -> { if (rc != BKException.Code.OK) { bkf.completeExceptionally(BKException.create(rc)); } else {//from w ww . ja v a2 s. com bkf.complete(null); } }, null); return bkf; }
From source file:org.apache.pulsar.compaction.TwoPhaseCompactor.java
private CompletableFuture<Void> closeLedger(LedgerHandle lh) { CompletableFuture<Void> bkf = new CompletableFuture<>(); lh.asyncClose((rc, ledger, ctx) -> { if (rc != BKException.Code.OK) { bkf.completeExceptionally(BKException.create(rc)); } else {/*from w ww. j a v a2s . c om*/ bkf.complete(null); } }, null); return bkf; }
From source file:org.apache.pulsar.compaction.TwoPhaseCompactor.java
private CompletableFuture<Void> addToCompactedLedger(LedgerHandle lh, RawMessage m) { CompletableFuture<Void> bkf = new CompletableFuture<>(); ByteBuf serialized = m.serialize();// ww w . j ava 2 s. c om lh.asyncAddEntry(serialized, (rc, ledger, eid, ctx) -> { if (rc != BKException.Code.OK) { bkf.completeExceptionally(BKException.create(rc)); } else { bkf.complete(null); } }, null); return bkf; }
From source file:org.apache.pulsar.functions.runtime.ProcessRuntime.java
@Override public CompletableFuture<FunctionStatus> getFunctionStatus(int instanceId) { CompletableFuture<FunctionStatus> retval = new CompletableFuture<>(); if (stub == null) { retval.completeExceptionally(new RuntimeException("Not alive")); return retval; }//from w w w . j a va2 s . c o m ListenableFuture<FunctionStatus> response = stub.withDeadlineAfter(GRPC_TIMEOUT_SECS, TimeUnit.SECONDS) .getFunctionStatus(Empty.newBuilder().build()); Futures.addCallback(response, new FutureCallback<FunctionStatus>() { @Override public void onFailure(Throwable throwable) { FunctionStatus.Builder builder = FunctionStatus.newBuilder(); builder.setRunning(false); if (deathException != null) { builder.setFailureException(deathException.getMessage()); } else { builder.setFailureException(throwable.getMessage()); } retval.complete(builder.build()); } @Override public void onSuccess(InstanceCommunication.FunctionStatus t) { retval.complete(t); } }); return retval; }
From source file:org.apache.pulsar.functions.runtime.ProcessRuntime.java
@Override public CompletableFuture<InstanceCommunication.MetricsData> getAndResetMetrics() { CompletableFuture<InstanceCommunication.MetricsData> retval = new CompletableFuture<>(); if (stub == null) { retval.completeExceptionally(new RuntimeException("Not alive")); return retval; }//from w w w . ja va2 s . c om ListenableFuture<InstanceCommunication.MetricsData> response = stub .withDeadlineAfter(GRPC_TIMEOUT_SECS, TimeUnit.SECONDS) .getAndResetMetrics(Empty.newBuilder().build()); Futures.addCallback(response, new FutureCallback<InstanceCommunication.MetricsData>() { @Override public void onFailure(Throwable throwable) { retval.completeExceptionally(throwable); } @Override public void onSuccess(InstanceCommunication.MetricsData t) { retval.complete(t); } }); return retval; }