Search in sources :

Example 1 with LogSegmentNotFoundException

use of org.apache.distributedlog.exceptions.LogSegmentNotFoundException in project bookkeeper by apache.

the class BKLogHandler method readLogSegmentsFromStore.

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());
        }
        // 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) {
                // 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);
            }
        });
    }
}
Also used : UnexpectedException(org.apache.distributedlog.exceptions.UnexpectedException) HashSet(java.util.HashSet) Set(java.util.Set) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) List(java.util.List) LogSegmentNotFoundException(org.apache.distributedlog.exceptions.LogSegmentNotFoundException) HashSet(java.util.HashSet)

Example 2 with LogSegmentNotFoundException

use of org.apache.distributedlog.exceptions.LogSegmentNotFoundException in project bookkeeper by apache.

the class ZKLogSegmentMetadataStore method deleteLogSegment.

@Override
public void deleteLogSegment(Transaction<Object> txn, final LogSegmentMetadata segment, final OpListener<Void> listener) {
    Op deleteOp = Op.delete(segment.getZkPath(), -1);
    logger.info("Delete segment : {}", segment);
    txn.addOp(DefaultZKOp.of(deleteOp, new OpListener<Void>() {

        @Override
        public void onCommit(Void r) {
            if (null != listener) {
                listener.onCommit(r);
            }
        }

        @Override
        public void onAbort(Throwable t) {
            logger.info("Aborted transaction on deleting segment {}", segment);
            KeeperException.Code kc;
            if (t instanceof KeeperException) {
                kc = ((KeeperException) t).code();
            } else if (t instanceof ZKException) {
                kc = ((ZKException) t).getKeeperExceptionCode();
            } else {
                abortListener(t);
                return;
            }
            if (KeeperException.Code.NONODE == kc) {
                abortListener(new LogSegmentNotFoundException(segment.getZkPath()));
                return;
            }
            abortListener(t);
        }

        private void abortListener(Throwable t) {
            if (null != listener) {
                listener.onAbort(t);
            }
        }
    }));
}
Also used : ZKOp(org.apache.distributedlog.zk.ZKOp) Op(org.apache.zookeeper.Op) DefaultZKOp(org.apache.distributedlog.zk.DefaultZKOp) ZKVersionedSetOp(org.apache.distributedlog.zk.ZKVersionedSetOp) ZKException(org.apache.distributedlog.exceptions.ZKException) OpListener(org.apache.distributedlog.util.Transaction.OpListener) KeeperException(org.apache.zookeeper.KeeperException) LogSegmentNotFoundException(org.apache.distributedlog.exceptions.LogSegmentNotFoundException)

Aggregations

LogSegmentNotFoundException (org.apache.distributedlog.exceptions.LogSegmentNotFoundException)2 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 UnexpectedException (org.apache.distributedlog.exceptions.UnexpectedException)1 ZKException (org.apache.distributedlog.exceptions.ZKException)1 OpListener (org.apache.distributedlog.util.Transaction.OpListener)1 DefaultZKOp (org.apache.distributedlog.zk.DefaultZKOp)1 ZKOp (org.apache.distributedlog.zk.ZKOp)1 ZKVersionedSetOp (org.apache.distributedlog.zk.ZKVersionedSetOp)1 KeeperException (org.apache.zookeeper.KeeperException)1 Op (org.apache.zookeeper.Op)1