Java Code Examples for com.twitter.util.Promise#setException()
The following examples show how to use
com.twitter.util.Promise#setException() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DistributedLogClientImpl.java From distributedlog with Apache License 2.0 | 6 votes |
void complete(SocketAddress address, BulkWriteResponse bulkWriteResponse) { super.complete(address); Iterator<WriteResponse> writeResponseIterator = bulkWriteResponse.getWriteResponses().iterator(); Iterator<Promise<DLSN>> resultIterator = results.iterator(); // Fill in errors from thrift responses. while (resultIterator.hasNext() && writeResponseIterator.hasNext()) { Promise<DLSN> result = resultIterator.next(); WriteResponse writeResponse = writeResponseIterator.next(); if (StatusCode.SUCCESS == writeResponse.getHeader().getCode()) { result.setValue(DLSN.deserialize(writeResponse.getDlsn())); } else { result.setException(ProtocolUtils.exception(writeResponse.getHeader())); } } // Should never happen, but just in case so there's some record. if (bulkWriteResponse.getWriteResponses().size() != data.size()) { logger.error("wrong number of results, response = {} records = {}", bulkWriteResponse.getWriteResponses().size(), data.size()); } }
Example 2
Source File: DistributedLogClientImpl.java From distributedlog with Apache License 2.0 | 6 votes |
void complete(SocketAddress address, BulkWriteResponse bulkWriteResponse) { super.complete(address); Iterator<WriteResponse> writeResponseIterator = bulkWriteResponse.getWriteResponses().iterator(); Iterator<Promise<DLSN>> resultIterator = results.iterator(); // Fill in errors from thrift responses. while (resultIterator.hasNext() && writeResponseIterator.hasNext()) { Promise<DLSN> result = resultIterator.next(); WriteResponse writeResponse = writeResponseIterator.next(); if (StatusCode.SUCCESS == writeResponse.getHeader().getCode()) { result.setValue(DLSN.deserialize(writeResponse.getDlsn())); } else { result.setException(DLException.of(writeResponse.getHeader())); } } // Should never happen, but just in case so there's some record. if (bulkWriteResponse.getWriteResponses().size() != data.size()) { logger.error("wrong number of results, response = {} records = ", bulkWriteResponse.getWriteResponses().size(), data.size()); } }
Example 3
Source File: ZKSubscriptionsStore.java From distributedlog with Apache License 2.0 | 6 votes |
@Override public Future<Map<String, DLSN>> getLastCommitPositions() { final Promise<Map<String, DLSN>> result = new Promise<Map<String, DLSN>>(); try { 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.setValue(new HashMap<String, DLSN>()); } else if (KeeperException.Code.OK.intValue() != rc) { result.setException(KeeperException.create(KeeperException.Code.get(rc), path)); } else { getLastCommitPositions(result, children); } } }, null); } catch (ZooKeeperClient.ZooKeeperConnectionException zkce) { result.setException(zkce); } catch (InterruptedException ie) { result.setException(new DLInterruptedException("getLastCommitPositions was interrupted", ie)); } return result; }
Example 4
Source File: ZKSubscriptionStateStore.java From distributedlog with Apache License 2.0 | 5 votes |
Future<DLSN> getLastCommitPositionFromZK() { final Promise<DLSN> result = new Promise<DLSN>(); try { logger.debug("Reading last commit position from path {}", zkPath); zooKeeperClient.get().getData(zkPath, false, new AsyncCallback.DataCallback() { @Override public void processResult(int rc, String path, Object ctx, byte[] data, Stat stat) { logger.debug("Read last commit position from path {}: rc = {}", zkPath, rc); if (KeeperException.Code.NONODE.intValue() == rc) { result.setValue(DLSN.NonInclusiveLowerBound); } else if (KeeperException.Code.OK.intValue() != rc) { result.setException(KeeperException.create(KeeperException.Code.get(rc), path)); } else { try { DLSN dlsn = DLSN.deserialize(new String(data, Charsets.UTF_8)); result.setValue(dlsn); } catch (Exception t) { logger.warn("Invalid last commit position found from path {}", zkPath, t); // invalid dlsn recorded in subscription state store result.setValue(DLSN.NonInclusiveLowerBound); } } } }, null); } catch (ZooKeeperClient.ZooKeeperConnectionException zkce) { result.setException(zkce); } catch (InterruptedException ie) { result.setException(new DLInterruptedException("getLastCommitPosition was interrupted", ie)); } return result; }
Example 5
Source File: ReadUtils.java From distributedlog with Apache License 2.0 | 5 votes |
private static void asyncReadRecordFromLogSegment( final String streamName, final LedgerDescriptor ledgerDescriptor, final LedgerHandleCache handleCache, final LogSegmentMetadata metadata, final ExecutorService executorService, final int scanStartBatchSize, final int scanMaxBatchSize, final boolean includeControl, final boolean includeEndOfStream, final Promise<LogRecordWithDLSN> promise, final AtomicInteger numRecordsScanned, final LogRecordSelector selector, final boolean backward, final long startEntryId) { final long lastAddConfirmed; try { lastAddConfirmed = handleCache.getLastAddConfirmed(ledgerDescriptor); } catch (BKException e) { promise.setException(e); return; } if (lastAddConfirmed < 0) { if (LOG.isDebugEnabled()) { LOG.debug("Ledger {} is empty for {}.", new Object[] { ledgerDescriptor, streamName }); } promise.setValue(null); return; } final ScanContext context = new ScanContext( startEntryId, lastAddConfirmed, scanStartBatchSize, scanMaxBatchSize, includeControl, includeEndOfStream, backward, numRecordsScanned); asyncReadRecordFromEntries(streamName, ledgerDescriptor, handleCache, metadata, executorService, promise, context, selector); }
Example 6
Source File: Utils.java From distributedlog with Apache License 2.0 | 5 votes |
private static void handleKeeperExceptionCode(int rc, String pathOrMessage, Promise<BoxedUnit> result) { if (KeeperException.Code.OK.intValue() == rc) { result.setValue(BoxedUnit.UNIT); } else if (DistributedLogConstants.ZK_CONNECTION_EXCEPTION_RESULT_CODE == rc) { result.setException(new ZooKeeperClient.ZooKeeperConnectionException(pathOrMessage)); } else if (DistributedLogConstants.DL_INTERRUPTED_EXCEPTION_RESULT_CODE == rc) { result.setException(new DLInterruptedException(pathOrMessage)); } else { result.setException(KeeperException.create(KeeperException.Code.get(rc), pathOrMessage)); } }
Example 7
Source File: ZKLogSegmentMetadataStore.java From distributedlog with Apache License 2.0 | 5 votes |
@Override @SuppressWarnings("unchecked") public void processResult(int rc, String path, Object ctx, List<String> children, Stat stat) { Promise<List<String>> result = ((Promise<List<String>>) ctx); if (KeeperException.Code.OK.intValue() == rc) { result.setValue(children); } else { result.setException(KeeperException.create(KeeperException.Code.get(rc))); } }
Example 8
Source File: BKLogHandler.java From distributedlog with Apache License 2.0 | 5 votes |
private void asyncGetLastLogRecord(final Iterator<LogSegmentMetadata> ledgerIter, final Promise<LogRecordWithDLSN> promise, final boolean fence, final boolean includeControlRecord, final boolean includeEndOfStream) { if (ledgerIter.hasNext()) { LogSegmentMetadata metadata = ledgerIter.next(); asyncReadLastRecord(metadata, fence, includeControlRecord, includeEndOfStream).addEventListener( new FutureEventListener<LogRecordWithDLSN>() { @Override public void onSuccess(LogRecordWithDLSN record) { if (null == record) { asyncGetLastLogRecord(ledgerIter, promise, fence, includeControlRecord, includeEndOfStream); } else { promise.setValue(record); } } @Override public void onFailure(Throwable cause) { promise.setException(cause); } } ); } else { promise.setException(new LogEmptyException("Log " + getFullyQualifiedName() + " has no records")); } }
Example 9
Source File: BKLogWriteHandler.java From distributedlog with Apache License 2.0 | 4 votes |
private Future<LogSegmentMetadata> deleteLogSegment( final LogSegmentMetadata ledgerMetadata) { LOG.info("Deleting ledger {} for {}", ledgerMetadata, getFullyQualifiedName()); final Promise<LogSegmentMetadata> promise = new Promise<LogSegmentMetadata>(); final Stopwatch stopwatch = Stopwatch.createStarted(); promise.addEventListener(new FutureEventListener<LogSegmentMetadata>() { @Override public void onSuccess(LogSegmentMetadata segment) { deleteOpStats.registerSuccessfulEvent(stopwatch.stop().elapsed(TimeUnit.MICROSECONDS)); } @Override public void onFailure(Throwable cause) { deleteOpStats.registerFailedEvent(stopwatch.stop().elapsed(TimeUnit.MICROSECONDS)); } }); try { bookKeeperClient.get().asyncDeleteLedger(ledgerMetadata.getLedgerId(), new AsyncCallback.DeleteCallback() { @Override public void deleteComplete(int rc, Object ctx) { if (BKException.Code.NoSuchLedgerExistsException == rc) { LOG.warn("No ledger {} found to delete for {} : {}.", new Object[]{ledgerMetadata.getLedgerId(), getFullyQualifiedName(), ledgerMetadata}); } else if (BKException.Code.OK != rc) { BKException bke = BKException.create(rc); LOG.error("Couldn't delete ledger {} from bookkeeper for {} : ", new Object[]{ledgerMetadata.getLedgerId(), getFullyQualifiedName(), bke}); promise.setException(bke); return; } // after the ledger is deleted, we delete the metadata znode scheduler.submit(new Runnable() { @Override public void run() { deleteLogSegmentMetadata(ledgerMetadata, promise); } }); } }, null); } catch (IOException e) { promise.setException(BKException.create(BKException.Code.BookieHandleNotAvailableException)); } return promise; }
Example 10
Source File: ReadUtils.java From distributedlog with Apache License 2.0 | 4 votes |
/** * Read record from a given range of ledger entries. * * @param streamName * fully qualified stream name (used for logging) * @param ledgerDescriptor * ledger descriptor. * @param handleCache * ledger handle cache. * @param executorService * executor service used for processing entries * @param context * scan context * @return a future with the log record. */ private static Future<LogRecordWithDLSN> asyncReadRecordFromEntries( final String streamName, final LedgerDescriptor ledgerDescriptor, LedgerHandleCache handleCache, final LogSegmentMetadata metadata, final ExecutorService executorService, final ScanContext context, final LogRecordSelector selector) { final Promise<LogRecordWithDLSN> promise = new Promise<LogRecordWithDLSN>(); final long startEntryId = context.curStartEntryId.get(); final long endEntryId = context.curEndEntryId.get(); if (LOG.isDebugEnabled()) { LOG.debug("{} reading entries [{} - {}] from {}.", new Object[] { streamName, startEntryId, endEntryId, ledgerDescriptor }); } FutureEventListener<Enumeration<LedgerEntry>> readEntriesListener = new FutureEventListener<Enumeration<LedgerEntry>>() { @Override public void onSuccess(final Enumeration<LedgerEntry> entries) { if (LOG.isDebugEnabled()) { LOG.debug("{} finished reading entries [{} - {}] from {}", new Object[]{ streamName, startEntryId, endEntryId, ledgerDescriptor }); } LogRecordWithDLSN record = null; while (entries.hasMoreElements()) { LedgerEntry entry = entries.nextElement(); try { visitEntryRecords( streamName, metadata, ledgerDescriptor.getLogSegmentSequenceNo(), entry, context, selector); } catch (IOException ioe) { // exception is only thrown due to bad ledger entry, so it might be corrupted // we shouldn't do anything beyond this point. throw the exception to application promise.setException(ioe); return; } } record = selector.result(); if (LOG.isDebugEnabled()) { LOG.debug("{} got record from entries [{} - {}] of {} : {}", new Object[]{streamName, startEntryId, endEntryId, ledgerDescriptor, record}); } promise.setValue(record); } @Override public void onFailure(final Throwable cause) { String errMsg = "Error reading entries [" + startEntryId + "-" + endEntryId + "] for reading record of " + streamName; promise.setException(new IOException(errMsg, BKException.create(FutureUtils.bkResultCode(cause)))); } }; handleCache.asyncReadEntries(ledgerDescriptor, startEntryId, endEntryId) .addEventListener(FutureEventListenerRunnable.of(readEntriesListener, executorService)); return promise; }
Example 11
Source File: ReadUtils.java From distributedlog with Apache License 2.0 | 4 votes |
/** * Scan entries for the given record. * * @param streamName * fully qualified stream name (used for logging) * @param ledgerDescriptor * ledger descriptor. * @param handleCache * ledger handle cache. * @param executorService * executor service used for processing entries * @param promise * promise to return desired record. * @param context * scan context */ private static void asyncReadRecordFromEntries( final String streamName, final LedgerDescriptor ledgerDescriptor, final LedgerHandleCache handleCache, final LogSegmentMetadata metadata, final ExecutorService executorService, final Promise<LogRecordWithDLSN> promise, final ScanContext context, final LogRecordSelector selector) { FutureEventListener<LogRecordWithDLSN> readEntriesListener = new FutureEventListener<LogRecordWithDLSN>() { @Override public void onSuccess(LogRecordWithDLSN value) { if (LOG.isDebugEnabled()) { LOG.debug("{} read record from [{} - {}] of {} : {}", new Object[]{streamName, context.curStartEntryId.get(), context.curEndEntryId.get(), ledgerDescriptor, value}); } if (null != value) { promise.setValue(value); return; } if (!context.moveToNextRange()) { // no entries to read again promise.setValue(null); return; } // scan next range asyncReadRecordFromEntries(streamName, ledgerDescriptor, handleCache, metadata, executorService, promise, context, selector); } @Override public void onFailure(Throwable cause) { promise.setException(cause); } }; asyncReadRecordFromEntries(streamName, ledgerDescriptor, handleCache, metadata, executorService, context, selector) .addEventListener(FutureEventListenerRunnable.of(readEntriesListener, executorService)); }
Example 12
Source File: ReadUtils.java From distributedlog with Apache License 2.0 | 4 votes |
private static Future<LogRecordWithDLSN> asyncReadRecord( final String streamName, final LogSegmentMetadata l, final boolean fence, final boolean includeControl, final boolean includeEndOfStream, final int scanStartBatchSize, final int scanMaxBatchSize, final AtomicInteger numRecordsScanned, final ExecutorService executorService, final LedgerHandleCache handleCache, final LogRecordSelector selector, final boolean backward, final long startEntryId) { final Promise<LogRecordWithDLSN> promise = new Promise<LogRecordWithDLSN>(); FutureEventListener<LedgerDescriptor> openLedgerListener = new FutureEventListener<LedgerDescriptor>() { @Override public void onSuccess(final LedgerDescriptor ledgerDescriptor) { if (LOG.isDebugEnabled()) { LOG.debug("{} Opened logsegment {} for reading record", streamName, l); } promise.ensure(new AbstractFunction0<BoxedUnit>() { @Override public BoxedUnit apply() { handleCache.asyncCloseLedger(ledgerDescriptor); return BoxedUnit.UNIT; } }); if (LOG.isDebugEnabled()) { LOG.debug("{} {} scanning {}.", new Object[]{ (backward ? "backward" : "forward"), streamName, l}); } asyncReadRecordFromLogSegment( streamName, ledgerDescriptor, handleCache, l, executorService, scanStartBatchSize, scanMaxBatchSize, includeControl, includeEndOfStream, promise, numRecordsScanned, selector, backward, startEntryId); } @Override public void onFailure(final Throwable cause) { String errMsg = "Error opening log segment [" + l + "] for reading record of " + streamName; promise.setException(new IOException(errMsg, BKException.create(FutureUtils.bkResultCode(cause)))); } }; handleCache.asyncOpenLedger(l, fence) .addEventListener(FutureEventListenerRunnable.of(openLedgerListener, executorService)); return promise; }
Example 13
Source File: ReadUtils.java From distributedlog with Apache License 2.0 | 4 votes |
/** * Find the log record whose transaction id is not less than provided <code>transactionId</code> from * entries between <code>startEntryId</code> and <code>endEntryId</code>. * * @param logName * name of the log * @param segment * log segment * @param transactionId * provided transaction id to search * @param executorService * executor service * @param handleCache * handle cache * @param entriesToSearch * list of entries to search * @param nWays * how many entries to search in parallel * @param prevFoundRecord * the log record found in previous search * @param promise * promise to satisfy the result */ private static void getLogRecordNotLessThanTxIdFromEntries( final String logName, final LedgerDescriptor ld, final LogSegmentMetadata segment, final long transactionId, final ExecutorService executorService, final LedgerHandleCache handleCache, final List<Long> entriesToSearch, final int nWays, final Optional<LogRecordWithDLSN> prevFoundRecord, final Promise<Optional<LogRecordWithDLSN>> promise) { final List<Future<LogRecordWithDLSN>> searchResults = Lists.newArrayListWithExpectedSize(entriesToSearch.size()); for (Long entryId : entriesToSearch) { LogRecordSelector selector = new FirstTxIdNotLessThanSelector(transactionId); Future<LogRecordWithDLSN> searchResult = asyncReadRecordFromEntries( logName, ld, handleCache, segment, executorService, new SingleEntryScanContext(entryId), selector); searchResults.add(searchResult); } FutureEventListener<List<LogRecordWithDLSN>> processSearchResultsListener = new FutureEventListener<List<LogRecordWithDLSN>>() { @Override public void onSuccess(List<LogRecordWithDLSN> resultList) { processSearchResults( logName, ld, segment, transactionId, executorService, handleCache, resultList, nWays, prevFoundRecord, promise); } @Override public void onFailure(Throwable cause) { promise.setException(cause); } }; Future.collect(searchResults).addEventListener( FutureEventListenerRunnable.of(processSearchResultsListener, executorService)); }
Example 14
Source File: EnvelopedRecordSetWriter.java From distributedlog with Apache License 2.0 | 4 votes |
private synchronized void cancelPromises(Throwable reason) { for (Promise<DLSN> promise : promiseList) { promise.setException(reason); } promiseList.clear(); }