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);
}
});
}
}
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);
}
}
}));
}
Aggregations