Search in sources :

Example 1 with FutureEventListener

use of com.twitter.util.FutureEventListener in project storm by apache.

the class DruidBeamBolt method process.

@Override
protected void process(final Tuple tuple) {
    Future future = tranquilizer.send((druidEventMapper.getEvent(tuple)));
    LOG.debug("Sent tuple : [{}]", tuple);
    future.addEventListener(new FutureEventListener() {

        @Override
        public void onFailure(Throwable cause) {
            if (cause instanceof MessageDroppedException) {
                collector.ack(tuple);
                LOG.debug("Tuple Dropped due to MessageDroppedException : [{}]", tuple);
                if (druidConfig.getDiscardStreamId() != null)
                    collector.emit(druidConfig.getDiscardStreamId(), new Values(tuple, System.currentTimeMillis()));
            } else {
                collector.fail(tuple);
                LOG.debug("Tuple Processing Failed : [{}]", tuple);
            }
        }

        @Override
        public void onSuccess(Object value) {
            collector.ack(tuple);
            LOG.debug("Tuple Processing Success : [{}]", tuple);
        }
    });
}
Also used : MessageDroppedException(com.metamx.tranquility.tranquilizer.MessageDroppedException) Values(org.apache.storm.tuple.Values) Future(com.twitter.util.Future) FutureEventListener(com.twitter.util.FutureEventListener)

Example 2 with FutureEventListener

use of com.twitter.util.FutureEventListener in project distributedlog by twitter.

the class BKLogHandler method asyncGetLedgerListInternal.

private void asyncGetLedgerListInternal(final Comparator<LogSegmentMetadata> comparator, final LogSegmentFilter segmentFilter, final Watcher watcher, final GenericCallback<List<LogSegmentMetadata>> finalCallback, final AtomicInteger numAttemptsLeft, final AtomicLong backoffMillis) {
    final Stopwatch stopwatch = Stopwatch.createStarted();
    try {
        if (LOG.isTraceEnabled()) {
            LOG.trace("Async getting ledger list for {}.", getFullyQualifiedName());
        }
        final GenericCallback<List<LogSegmentMetadata>> callback = new GenericCallback<List<LogSegmentMetadata>>() {

            @Override
            public void operationComplete(int rc, List<LogSegmentMetadata> result) {
                long elapsedMicros = stopwatch.stop().elapsed(TimeUnit.MICROSECONDS);
                if (KeeperException.Code.OK.intValue() != rc) {
                    getListStat.registerFailedEvent(elapsedMicros);
                } else {
                    if (LogSegmentFilter.DEFAULT_FILTER == segmentFilter) {
                        isFullListFetched.set(true);
                    }
                    getListStat.registerSuccessfulEvent(elapsedMicros);
                }
                finalCallback.operationComplete(rc, result);
            }
        };
        zooKeeperClient.get().getChildren(logMetadata.getLogSegmentsPath(), watcher, new AsyncCallback.Children2Callback() {

            @Override
            public void processResult(final int rc, final String path, final Object ctx, final List<String> children, final Stat stat) {
                if (KeeperException.Code.OK.intValue() != rc) {
                    if ((KeeperException.Code.CONNECTIONLOSS.intValue() == rc || KeeperException.Code.SESSIONEXPIRED.intValue() == rc || KeeperException.Code.SESSIONMOVED.intValue() == rc) && numAttemptsLeft.decrementAndGet() > 0) {
                        long backoffMs = backoffMillis.get();
                        backoffMillis.set(Math.min(conf.getZKRetryBackoffMaxMillis(), 2 * backoffMs));
                        scheduler.schedule(new Runnable() {

                            @Override
                            public void run() {
                                asyncGetLedgerListInternal(comparator, segmentFilter, watcher, finalCallback, numAttemptsLeft, backoffMillis);
                            }
                        }, backoffMs, TimeUnit.MILLISECONDS);
                        return;
                    }
                    callback.operationComplete(rc, null);
                    return;
                }
                if (LOG.isTraceEnabled()) {
                    LOG.trace("Got ledger list from {} : {}", logMetadata.getLogSegmentsPath(), children);
                }
                ledgerListWatchSet.set(true);
                Set<String> segmentsReceived = new HashSet<String>();
                segmentsReceived.addAll(segmentFilter.filter(children));
                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());
                    }
                    // update the cache before fetch
                    logSegmentCache.update(removedSegments, addedSegments);
                    List<LogSegmentMetadata> segmentList;
                    try {
                        segmentList = getCachedLogSegments(comparator);
                    } catch (UnexpectedException e) {
                        callback.operationComplete(KeeperException.Code.DATAINCONSISTENCY.intValue(), null);
                        return;
                    }
                    callback.operationComplete(KeeperException.Code.OK.intValue(), segmentList);
                    notifyUpdatedLogSegments(segmentList);
                    if (!removedSegments.isEmpty()) {
                        notifyOnOperationComplete();
                    }
                    return;
                }
                final AtomicInteger numChildren = new AtomicInteger(segmentsAdded.size());
                final AtomicInteger numFailures = new AtomicInteger(0);
                for (final String segment : segmentsAdded) {
                    metadataStore.getLogSegment(logMetadata.getLogSegmentPath(segment)).addEventListener(new FutureEventListener<LogSegmentMetadata>() {

                        @Override
                        public void onSuccess(LogSegmentMetadata result) {
                            addedSegments.put(segment, result);
                            complete();
                        }

                        @Override
                        public void onFailure(Throwable cause) {
                            // 2. In progress segment has been completed => inprogress ZNode does not exist
                            if (cause instanceof KeeperException && KeeperException.Code.NONODE == ((KeeperException) cause).code()) {
                                removedSegments.add(segment);
                                complete();
                            } else {
                                // fail fast
                                if (1 == numFailures.incrementAndGet()) {
                                    int rcToReturn = KeeperException.Code.SYSTEMERROR.intValue();
                                    if (cause instanceof KeeperException) {
                                        rcToReturn = ((KeeperException) cause).code().intValue();
                                    } else if (cause instanceof ZKException) {
                                        rcToReturn = ((ZKException) cause).getKeeperExceptionCode().intValue();
                                    }
                                    // :( properly we need dlog related response code.
                                    callback.operationComplete(rcToReturn, null);
                                    return;
                                }
                            }
                        }

                        private void complete() {
                            if (0 == numChildren.decrementAndGet() && numFailures.get() == 0) {
                                // update the cache only when fetch completed
                                logSegmentCache.update(removedSegments, addedSegments);
                                List<LogSegmentMetadata> segmentList;
                                try {
                                    segmentList = getCachedLogSegments(comparator);
                                } catch (UnexpectedException e) {
                                    callback.operationComplete(KeeperException.Code.DATAINCONSISTENCY.intValue(), null);
                                    return;
                                }
                                callback.operationComplete(KeeperException.Code.OK.intValue(), segmentList);
                                notifyUpdatedLogSegments(segmentList);
                                notifyOnOperationComplete();
                            }
                        }
                    });
                }
            }
        }, null);
    } catch (ZooKeeperClient.ZooKeeperConnectionException e) {
        getListStat.registerFailedEvent(stopwatch.stop().elapsed(TimeUnit.MICROSECONDS));
        finalCallback.operationComplete(KeeperException.Code.CONNECTIONLOSS.intValue(), null);
    } catch (InterruptedException e) {
        getListStat.registerFailedEvent(stopwatch.stop().elapsed(TimeUnit.MICROSECONDS));
        finalCallback.operationComplete(KeeperException.Code.CONNECTIONLOSS.intValue(), null);
    }
}
Also used : Set(java.util.Set) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) HashSet(java.util.HashSet) HashMap(java.util.HashMap) AsyncCallback(org.apache.zookeeper.AsyncCallback) Stopwatch(com.google.common.base.Stopwatch) DLInterruptedException(com.twitter.distributedlog.exceptions.DLInterruptedException) Stat(org.apache.zookeeper.data.Stat) List(java.util.List) ArrayList(java.util.ArrayList) GenericCallback(org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.GenericCallback) HashSet(java.util.HashSet) Pair(org.apache.commons.lang3.tuple.Pair) UnexpectedException(com.twitter.distributedlog.exceptions.UnexpectedException) ZKException(com.twitter.distributedlog.exceptions.ZKException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FutureEventListener(com.twitter.util.FutureEventListener) Map(java.util.Map) HashMap(java.util.HashMap) KeeperException(org.apache.zookeeper.KeeperException)

Example 3 with FutureEventListener

use of com.twitter.util.FutureEventListener in project distributedlog by twitter.

the class BKLogHandler method asyncReadLastRecord.

public Future<LogRecordWithDLSN> asyncReadLastRecord(final LogSegmentMetadata l, final boolean fence, final boolean includeControl, final boolean includeEndOfStream) {
    final AtomicInteger numRecordsScanned = new AtomicInteger(0);
    final Stopwatch stopwatch = Stopwatch.createStarted();
    final LedgerHandleCache handleCache = LedgerHandleCache.newBuilder().bkc(bookKeeperClient).conf(conf).build();
    return ReadUtils.asyncReadLastRecord(getFullyQualifiedName(), l, fence, includeControl, includeEndOfStream, firstNumEntriesPerReadLastRecordScan, maxNumEntriesPerReadLastRecordScan, numRecordsScanned, scheduler, handleCache).addEventListener(new FutureEventListener<LogRecordWithDLSN>() {

        @Override
        public void onSuccess(LogRecordWithDLSN value) {
            recoverLastEntryStats.registerSuccessfulEvent(stopwatch.stop().elapsed(TimeUnit.MICROSECONDS));
            recoverScannedEntriesStats.registerSuccessfulEvent(numRecordsScanned.get());
        }

        @Override
        public void onFailure(Throwable cause) {
            recoverLastEntryStats.registerFailedEvent(stopwatch.stop().elapsed(TimeUnit.MICROSECONDS));
        }
    }).ensure(new AbstractFunction0<BoxedUnit>() {

        @Override
        public BoxedUnit apply() {
            handleCache.clear();
            return BoxedUnit.UNIT;
        }
    });
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Stopwatch(com.google.common.base.Stopwatch) FutureEventListener(com.twitter.util.FutureEventListener) BoxedUnit(scala.runtime.BoxedUnit)

Example 4 with FutureEventListener

use of com.twitter.util.FutureEventListener in project distributedlog by twitter.

the class BKLogHandler method getLastLogRecordAsync.

public Future<LogRecordWithDLSN> getLastLogRecordAsync(final boolean recover, final boolean includeEndOfStream) {
    final Promise<LogRecordWithDLSN> promise = new Promise<LogRecordWithDLSN>();
    checkLogStreamExistsAsync().addEventListener(new FutureEventListener<Void>() {

        @Override
        public void onSuccess(Void value) {
            asyncGetFullLedgerListDesc(true, true).addEventListener(new FutureEventListener<List<LogSegmentMetadata>>() {

                @Override
                public void onSuccess(List<LogSegmentMetadata> ledgerList) {
                    if (ledgerList.isEmpty()) {
                        promise.setException(new LogEmptyException("Log " + getFullyQualifiedName() + " has no records"));
                        return;
                    }
                    asyncGetLastLogRecord(ledgerList.iterator(), promise, recover, false, includeEndOfStream);
                }

                @Override
                public void onFailure(Throwable cause) {
                    promise.setException(cause);
                }
            });
        }

        @Override
        public void onFailure(Throwable cause) {
            promise.setException(cause);
        }
    });
    return promise;
}
Also used : Promise(com.twitter.util.Promise) LogEmptyException(com.twitter.distributedlog.exceptions.LogEmptyException) FutureEventListener(com.twitter.util.FutureEventListener) List(java.util.List) ArrayList(java.util.ArrayList)

Example 5 with FutureEventListener

use of com.twitter.util.FutureEventListener in project distributedlog by twitter.

the class BKLogHandler method asyncGetFirstLogRecord.

public Future<LogRecordWithDLSN> asyncGetFirstLogRecord() {
    final Promise<LogRecordWithDLSN> promise = new Promise<LogRecordWithDLSN>();
    checkLogStreamExistsAsync().addEventListener(new FutureEventListener<Void>() {

        @Override
        public void onSuccess(Void value) {
            asyncGetFullLedgerList(true, true).addEventListener(new FutureEventListener<List<LogSegmentMetadata>>() {

                @Override
                public void onSuccess(List<LogSegmentMetadata> ledgerList) {
                    if (ledgerList.isEmpty()) {
                        promise.setException(new LogEmptyException("Log " + getFullyQualifiedName() + " has no records"));
                        return;
                    }
                    Future<LogRecordWithDLSN> firstRecord = null;
                    for (LogSegmentMetadata ledger : ledgerList) {
                        if (!ledger.isTruncated() && (ledger.getRecordCount() > 0 || ledger.isInProgress())) {
                            firstRecord = asyncReadFirstUserRecord(ledger, DLSN.InitialDLSN);
                            break;
                        }
                    }
                    if (null != firstRecord) {
                        promise.become(firstRecord);
                    } else {
                        promise.setException(new LogEmptyException("Log " + getFullyQualifiedName() + " has no records"));
                    }
                }

                @Override
                public void onFailure(Throwable cause) {
                    promise.setException(cause);
                }
            });
        }

        @Override
        public void onFailure(Throwable cause) {
            promise.setException(cause);
        }
    });
    return promise;
}
Also used : Promise(com.twitter.util.Promise) LogEmptyException(com.twitter.distributedlog.exceptions.LogEmptyException) FutureEventListener(com.twitter.util.FutureEventListener) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

FutureEventListener (com.twitter.util.FutureEventListener)23 Promise (com.twitter.util.Promise)9 IOException (java.io.IOException)6 List (java.util.List)6 Future (com.twitter.util.Future)5 Stopwatch (com.google.common.base.Stopwatch)4 UnexpectedException (com.twitter.distributedlog.exceptions.UnexpectedException)4 ArrayList (java.util.ArrayList)4 BoxedUnit (scala.runtime.BoxedUnit)4 DLInterruptedException (com.twitter.distributedlog.exceptions.DLInterruptedException)3 LogEmptyException (com.twitter.distributedlog.exceptions.LogEmptyException)3 Set (java.util.Set)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 LockingException (com.twitter.distributedlog.exceptions.LockingException)2 OwnershipAcquireFailedException (com.twitter.distributedlog.exceptions.OwnershipAcquireFailedException)2 ZKException (com.twitter.distributedlog.exceptions.ZKException)2 FirstTxIdNotLessThanSelector (com.twitter.distributedlog.selector.FirstTxIdNotLessThanSelector)2 ZKTransaction (com.twitter.distributedlog.zk.ZKTransaction)2 HashSet (java.util.HashSet)2