List of usage examples for java.util.concurrent CompletableFuture completeExceptionally
public boolean completeExceptionally(Throwable ex)
From source file:org.apache.distributedlog.auditor.DLAuditor.java
private long calculateLedgerSpaceUsage(BookKeeperClient bkc, final ExecutorService executorService) throws IOException { final AtomicLong totalBytes = new AtomicLong(0); final AtomicLong totalEntries = new AtomicLong(0); final AtomicLong numLedgers = new AtomicLong(0); LedgerManager lm = BookKeeperAccessor.getLedgerManager(bkc.get()); final CompletableFuture<Void> doneFuture = FutureUtils.createFuture(); final BookKeeper bk = bkc.get(); BookkeeperInternalCallbacks.Processor<Long> collector = new BookkeeperInternalCallbacks.Processor<Long>() { @Override/*from ww w .j a va2 s . c om*/ public void process(final Long lid, final AsyncCallback.VoidCallback cb) { numLedgers.incrementAndGet(); executorService.submit(new Runnable() { @Override public void run() { bk.asyncOpenLedgerNoRecovery(lid, BookKeeper.DigestType.CRC32, conf.getBKDigestPW().getBytes(UTF_8), new org.apache.bookkeeper.client.AsyncCallback.OpenCallback() { @Override public void openComplete(int rc, LedgerHandle lh, Object ctx) { final int cbRc; if (BKException.Code.OK == rc) { totalBytes.addAndGet(lh.getLength()); totalEntries.addAndGet(lh.getLastAddConfirmed() + 1); cbRc = rc; } else { cbRc = BKException.Code.ZKException; } executorService.submit(new Runnable() { @Override public void run() { cb.processResult(cbRc, null, null); } }); } }, null); } }); } }; AsyncCallback.VoidCallback finalCb = new AsyncCallback.VoidCallback() { @Override public void processResult(int rc, String path, Object ctx) { if (BKException.Code.OK == rc) { doneFuture.complete(null); } else { doneFuture.completeExceptionally(BKException.create(rc)); } } }; lm.asyncProcessLedgers(collector, finalCb, null, BKException.Code.OK, BKException.Code.ZKException); try { doneFuture.get(); logger.info("calculated {} ledgers\n\ttotal bytes = {}\n\ttotal entries = {}", new Object[] { numLedgers.get(), totalBytes.get(), totalEntries.get() }); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new DLInterruptedException("Interrupted on calculating ledger space : ", e); } catch (ExecutionException e) { if (e.getCause() instanceof IOException) { throw (IOException) (e.getCause()); } else { throw new IOException("Failed to calculate ledger space : ", e.getCause()); } } return totalBytes.get(); }
From source file:org.apache.distributedlog.BKLogHandler.java
public CompletableFuture<LogRecordWithDLSN> asyncGetFirstLogRecord() { final CompletableFuture<LogRecordWithDLSN> promise = new CompletableFuture<LogRecordWithDLSN>(); streamMetadataStore.logExists(logMetadata.getUri(), logMetadata.getLogName()) .whenComplete(new FutureEventListener<Void>() { @Override//w ww .j a va2s. com public void onSuccess(Void value) { readLogSegmentsFromStore(LogSegmentMetadata.COMPARATOR, LogSegmentFilter.DEFAULT_FILTER, null).whenComplete(new FutureEventListener<Versioned<List<LogSegmentMetadata>>>() { @Override public void onSuccess(Versioned<List<LogSegmentMetadata>> ledgerList) { if (ledgerList.getValue().isEmpty()) { promise.completeExceptionally(new LogEmptyException( "Log " + getFullyQualifiedName() + " has no records")); return; } CompletableFuture<LogRecordWithDLSN> firstRecord = null; for (LogSegmentMetadata ledger : ledgerList.getValue()) { if (!ledger.isTruncated() && (ledger.getRecordCount() > 0 || ledger.isInProgress())) { firstRecord = asyncReadFirstUserRecord(ledger, DLSN.InitialDLSN); break; } } if (null != firstRecord) { FutureUtils.proxyTo(firstRecord, promise); } else { promise.completeExceptionally(new LogEmptyException( "Log " + getFullyQualifiedName() + " has no records")); } } @Override public void onFailure(Throwable cause) { promise.completeExceptionally(cause); } }); } @Override public void onFailure(Throwable cause) { promise.completeExceptionally(cause); } }); return promise; }
From source file:org.apache.distributedlog.BKLogHandler.java
public CompletableFuture<LogRecordWithDLSN> getLastLogRecordAsync(final boolean recover, final boolean includeEndOfStream) { final CompletableFuture<LogRecordWithDLSN> promise = new CompletableFuture<LogRecordWithDLSN>(); streamMetadataStore.logExists(logMetadata.getUri(), logMetadata.getLogName()) .whenComplete(new FutureEventListener<Void>() { @Override//from w ww .j av a2 s. c om public void onSuccess(Void value) { readLogSegmentsFromStore(LogSegmentMetadata.DESC_COMPARATOR, LogSegmentFilter.DEFAULT_FILTER, null).whenComplete( new FutureEventListener<Versioned<List<LogSegmentMetadata>>>() { @Override public void onSuccess(Versioned<List<LogSegmentMetadata>> ledgerList) { if (ledgerList.getValue().isEmpty()) { promise.completeExceptionally(new LogEmptyException( "Log " + getFullyQualifiedName() + " has no records")); return; } asyncGetLastLogRecord(ledgerList.getValue().iterator(), promise, recover, false, includeEndOfStream); } @Override public void onFailure(Throwable cause) { promise.completeExceptionally(cause); } }); } @Override public void onFailure(Throwable cause) { promise.completeExceptionally(cause); } }); return promise; }
From source file:org.apache.distributedlog.BKLogHandler.java
private void asyncGetLastLogRecord(final Iterator<LogSegmentMetadata> ledgerIter, final CompletableFuture<LogRecordWithDLSN> promise, final boolean fence, final boolean includeControlRecord, final boolean includeEndOfStream) { if (ledgerIter.hasNext()) { LogSegmentMetadata metadata = ledgerIter.next(); asyncReadLastRecord(metadata, fence, includeControlRecord, includeEndOfStream) .whenComplete(new FutureEventListener<LogRecordWithDLSN>() { @Override/* ww w . j a v a 2s . c o m*/ public void onSuccess(LogRecordWithDLSN record) { if (null == record) { asyncGetLastLogRecord(ledgerIter, promise, fence, includeControlRecord, includeEndOfStream); } else { promise.complete(record); } } @Override public void onFailure(Throwable cause) { promise.completeExceptionally(cause); } }); } else { promise.completeExceptionally( new LogEmptyException("Log " + getFullyQualifiedName() + " has no records")); } }
From source file:org.apache.distributedlog.BKLogHandler.java
/** * Read the log segments from the store and register a listener. * @param comparator//from ww w. j av a 2 s. co m * @param segmentFilter * @param logSegmentNamesListener * @return future represents the result of log segments */ public CompletableFuture<Versioned<List<LogSegmentMetadata>>> readLogSegmentsFromStore( final Comparator<LogSegmentMetadata> comparator, final LogSegmentFilter segmentFilter, final LogSegmentNamesListener logSegmentNamesListener) { final CompletableFuture<Versioned<List<LogSegmentMetadata>>> readResult = new CompletableFuture<Versioned<List<LogSegmentMetadata>>>(); metadataStore.getLogSegmentNames(logMetadata.getLogSegmentsPath(), logSegmentNamesListener) .whenComplete(new FutureEventListener<Versioned<List<String>>>() { @Override public void onFailure(Throwable cause) { readResult.completeExceptionally(cause); } @Override public void onSuccess(Versioned<List<String>> logSegmentNames) { readLogSegmentsFromStore(logSegmentNames, comparator, segmentFilter, readResult); } }); return readResult; }
From source file:org.apache.distributedlog.BKLogHandler.java
protected void readLogSegmentsFromStore(final Versioned<List<String>> logSegmentNames, final Comparator<LogSegmentMetadata> comparator, final LogSegmentFilter segmentFilter, final CompletableFuture<Versioned<List<LogSegmentMetadata>>> readResult) { Set<String> segmentsReceived = new HashSet<String>(); segmentsReceived.addAll(segmentFilter.filter(logSegmentNames.getValue())); Set<String> segmentsAdded; final Set<String> removedSegments = Collections.synchronizedSet(new HashSet<String>()); final Map<String, LogSegmentMetadata> addedSegments = Collections .synchronizedMap(new HashMap<String, LogSegmentMetadata>()); Pair<Set<String>, Set<String>> segmentChanges = logSegmentCache.diff(segmentsReceived); segmentsAdded = segmentChanges.getLeft(); removedSegments.addAll(segmentChanges.getRight()); if (segmentsAdded.isEmpty()) { if (LOG.isTraceEnabled()) { LOG.trace("No segments added for {}.", getFullyQualifiedName()); }/*from w ww . j a v a 2 s . com*/ // update the cache before #getCachedLogSegments to return updateLogSegmentCache(removedSegments, addedSegments); List<LogSegmentMetadata> segmentList; try { segmentList = getCachedLogSegments(comparator); } catch (UnexpectedException e) { readResult.completeExceptionally(e); return; } readResult.complete(new Versioned<List<LogSegmentMetadata>>(segmentList, logSegmentNames.getVersion())); return; } final AtomicInteger numChildren = new AtomicInteger(segmentsAdded.size()); final AtomicInteger numFailures = new AtomicInteger(0); for (final String segment : segmentsAdded) { String logSegmentPath = logMetadata.getLogSegmentPath(segment); LogSegmentMetadata cachedSegment = metadataCache.get(logSegmentPath); if (null != cachedSegment) { addedSegments.put(segment, cachedSegment); completeReadLogSegmentsFromStore(removedSegments, addedSegments, comparator, readResult, logSegmentNames.getVersion(), numChildren, numFailures); continue; } metadataStore.getLogSegment(logSegmentPath).whenComplete(new FutureEventListener<LogSegmentMetadata>() { @Override public void onSuccess(LogSegmentMetadata result) { addedSegments.put(segment, result); complete(); } @Override public void onFailure(Throwable cause) { // LogSegmentNotFoundException exception is possible in two cases // 1. A log segment was deleted by truncation between the call to getChildren and read // attempt on the znode corresponding to the segment // 2. In progress segment has been completed => inprogress ZNode does not exist if (cause instanceof LogSegmentNotFoundException) { removedSegments.add(segment); complete(); } else { // fail fast if (1 == numFailures.incrementAndGet()) { readResult.completeExceptionally(cause); return; } } } private void complete() { completeReadLogSegmentsFromStore(removedSegments, addedSegments, comparator, readResult, logSegmentNames.getVersion(), numChildren, numFailures); } }); } }
From source file:org.apache.distributedlog.BKLogHandler.java
private void completeReadLogSegmentsFromStore(final Set<String> removedSegments, final Map<String, LogSegmentMetadata> addedSegments, final Comparator<LogSegmentMetadata> comparator, final CompletableFuture<Versioned<List<LogSegmentMetadata>>> readResult, final Version logSegmentNamesVersion, final AtomicInteger numChildren, final AtomicInteger numFailures) { if (0 != numChildren.decrementAndGet()) { return;// ww w . j a v a 2 s . c o m } if (numFailures.get() > 0) { return; } // update the cache only when fetch completed and before #getCachedLogSegments updateLogSegmentCache(removedSegments, addedSegments); List<LogSegmentMetadata> segmentList; try { segmentList = getCachedLogSegments(comparator); } catch (UnexpectedException e) { readResult.completeExceptionally(e); return; } readResult.complete(new Versioned<List<LogSegmentMetadata>>(segmentList, logSegmentNamesVersion)); }
From source file:org.apache.distributedlog.BookKeeperClient.java
public CompletableFuture<LedgerHandle> createLedger(int ensembleSize, int writeQuorumSize, int ackQuorumSize) { BookKeeper bk;/*from www.j a v a 2 s. com*/ try { bk = get(); } catch (IOException ioe) { return FutureUtils.exception(ioe); } final CompletableFuture<LedgerHandle> promise = new CompletableFuture<LedgerHandle>(); bk.asyncCreateLedger(ensembleSize, writeQuorumSize, ackQuorumSize, BookKeeper.DigestType.CRC32, passwd, new AsyncCallback.CreateCallback() { @Override public void createComplete(int rc, LedgerHandle lh, Object ctx) { if (BKException.Code.OK == rc) { promise.complete(lh); } else { promise.completeExceptionally(BKException.create(rc)); } } }, null, Collections.emptyMap()); return promise; }
From source file:org.apache.distributedlog.BookKeeperClient.java
public CompletableFuture<Void> deleteLedger(long lid, final boolean ignoreNonExistentLedger) { BookKeeper bk;/*from w w w.j av a2 s . c o m*/ try { bk = get(); } catch (IOException ioe) { return FutureUtils.exception(ioe); } final CompletableFuture<Void> promise = new CompletableFuture<Void>(); bk.asyncDeleteLedger(lid, new AsyncCallback.DeleteCallback() { @Override public void deleteComplete(int rc, Object ctx) { if (BKException.Code.OK == rc) { promise.complete(null); } else if (BKException.Code.NoSuchLedgerExistsException == rc) { if (ignoreNonExistentLedger) { promise.complete(null); } else { promise.completeExceptionally(BKException.create(rc)); } } else { promise.completeExceptionally(BKException.create(rc)); } } }, null); return promise; }
From source file:org.apache.distributedlog.impl.subscription.ZKSubscriptionsStore.java
@Override public CompletableFuture<Map<String, DLSN>> getLastCommitPositions() { final CompletableFuture<Map<String, DLSN>> result = new CompletableFuture<Map<String, DLSN>>(); try {// w ww . ja v a 2 s .c o m this.zkc.get().getChildren(this.zkPath, false, new AsyncCallback.Children2Callback() { @Override public void processResult(int rc, String path, Object ctx, List<String> children, Stat stat) { if (KeeperException.Code.NONODE.intValue() == rc) { result.complete(new HashMap<String, DLSN>()); } else if (KeeperException.Code.OK.intValue() != rc) { result.completeExceptionally(KeeperException.create(KeeperException.Code.get(rc), path)); } else { getLastCommitPositions(result, children); } } }, null); } catch (ZooKeeperClient.ZooKeeperConnectionException zkce) { result.completeExceptionally(zkce); } catch (InterruptedException ie) { Thread.currentThread().interrupt(); result.completeExceptionally(new DLInterruptedException("getLastCommitPositions was interrupted", ie)); } return result; }