Search in sources :

Example 46 with LogSegmentMetadata

use of org.apache.distributedlog.LogSegmentMetadata in project bookkeeper by apache.

the class BKLogSegmentEntryReader method openComplete.

@Override
public void openComplete(int rc, LedgerHandle lh, Object ctx) {
    LogSegmentMetadata segment = (LogSegmentMetadata) ctx;
    if (BKException.Code.OK != rc) {
        // fail current reader or retry opening the reader
        failOrRetryOpenLedger(rc, segment);
        return;
    }
    // switch to new ledger handle if the log segment is moved to completed.
    CacheEntry longPollRead = null;
    synchronized (this) {
        if (isClosed()) {
            lh.asyncClose(new AsyncCallback.CloseCallback() {

                @Override
                public void closeComplete(int rc, LedgerHandle lh, Object ctx) {
                    logger.debug("Close the open ledger {} since the log segment reader is already closed", lh.getId());
                }
            }, null);
            return;
        }
        this.metadata = segment;
        this.lh = lh;
        this.openLedgerHandles.add(lh);
        longPollRead = outstandingLongPoll;
    }
    if (null != longPollRead) {
        // reissue the long poll read when the log segment state is changed
        issueRead(longPollRead);
    }
    // notify readers
    notifyReaders();
}
Also used : LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) LogSegmentMetadata(org.apache.distributedlog.LogSegmentMetadata) AsyncCallback(org.apache.bookkeeper.client.AsyncCallback)

Example 47 with LogSegmentMetadata

use of org.apache.distributedlog.LogSegmentMetadata in project bookkeeper by apache.

the class DLAuditor method calculateStreamSpaceUsage.

private long calculateStreamSpaceUsage(final Namespace namespace, final String stream) throws IOException {
    DistributedLogManager dlm = namespace.openLog(stream);
    long totalBytes = 0;
    try {
        List<LogSegmentMetadata> segments = dlm.getLogSegments();
        for (LogSegmentMetadata segment : segments) {
            try {
                LedgerHandle lh = getBookKeeperClient(namespace).get().openLedgerNoRecovery(segment.getLogSegmentId(), BookKeeper.DigestType.CRC32, conf.getBKDigestPW().getBytes(UTF_8));
                totalBytes += lh.getLength();
                lh.close();
            } catch (BKException e) {
                logger.error("Failed to open ledger {} : ", segment.getLogSegmentId(), e);
                throw new IOException("Failed to open ledger " + segment.getLogSegmentId(), e);
            } catch (InterruptedException e) {
                logger.warn("Interrupted on opening ledger {} : ", segment.getLogSegmentId(), e);
                Thread.currentThread().interrupt();
                throw new DLInterruptedException("Interrupted on opening ledger " + segment.getLogSegmentId(), e);
            }
        }
    } finally {
        dlm.close();
    }
    return totalBytes;
}
Also used : LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) DistributedLogManager(org.apache.distributedlog.api.DistributedLogManager) LogSegmentMetadata(org.apache.distributedlog.LogSegmentMetadata) BKException(org.apache.bookkeeper.client.BKException) DLInterruptedException(org.apache.distributedlog.exceptions.DLInterruptedException) IOException(java.io.IOException) DLInterruptedException(org.apache.distributedlog.exceptions.DLInterruptedException)

Example 48 with LogSegmentMetadata

use of org.apache.distributedlog.LogSegmentMetadata in project bookkeeper by apache.

the class DLAuditor method collectLedgersFromStream.

private List<Long> collectLedgersFromStream(Namespace namespace, String stream, Set<Long> ledgers) throws IOException {
    DistributedLogManager dlm = namespace.openLog(stream);
    try {
        List<LogSegmentMetadata> segments = dlm.getLogSegments();
        List<Long> sLedgers = new ArrayList<Long>();
        for (LogSegmentMetadata segment : segments) {
            synchronized (ledgers) {
                ledgers.add(segment.getLogSegmentId());
            }
            sLedgers.add(segment.getLogSegmentId());
        }
        return sLedgers;
    } finally {
        dlm.close();
    }
}
Also used : DistributedLogManager(org.apache.distributedlog.api.DistributedLogManager) LogSegmentMetadata(org.apache.distributedlog.LogSegmentMetadata) AtomicLong(java.util.concurrent.atomic.AtomicLong) ArrayList(java.util.ArrayList)

Example 49 with LogSegmentMetadata

use of org.apache.distributedlog.LogSegmentMetadata in project bookkeeper by apache.

the class PerStreamLogSegmentCache method getLogSegments.

/**
 * Retrieve log segments from the cache.
 *- first sort the log segments in ascending order
 * - do validation and assign corresponding sequence id
 * - apply comparator after validation
 *
 * @param comparator
 *          comparator to sort the returned log segments.
 * @return list of sorted and filtered log segments.
 * @throws UnexpectedException if unexpected condition detected (e.g. ledger sequence number gap)
 */
public List<LogSegmentMetadata> getLogSegments(Comparator<LogSegmentMetadata> comparator) throws UnexpectedException {
    List<LogSegmentMetadata> segmentsToReturn;
    synchronized (logSegments) {
        segmentsToReturn = new ArrayList<LogSegmentMetadata>(logSegments.size());
        segmentsToReturn.addAll(logSegments.values());
    }
    Collections.sort(segmentsToReturn, LogSegmentMetadata.COMPARATOR);
    LogSegmentMetadata prevSegment = null;
    if (validateLogSegmentSequenceNumber) {
        // validation ledger sequence number to ensure the log segments are unique.
        for (int i = 0; i < segmentsToReturn.size(); i++) {
            LogSegmentMetadata segment = segmentsToReturn.get(i);
            if (null != prevSegment && prevSegment.getVersion() >= LogSegmentMetadata.LogSegmentMetadataVersion.VERSION_V2_LEDGER_SEQNO.value && segment.getVersion() >= LogSegmentMetadata.LogSegmentMetadataVersion.VERSION_V2_LEDGER_SEQNO.value && prevSegment.getLogSegmentSequenceNumber() + 1 != segment.getLogSegmentSequenceNumber()) {
                LOG.error("{} found ledger sequence number gap between log segment {} and {}", new Object[] { streamName, prevSegment, segment });
                throw new UnexpectedException(streamName + " found ledger sequence number gap between log segment " + prevSegment.getLogSegmentSequenceNumber() + " and " + segment.getLogSegmentSequenceNumber());
            }
            prevSegment = segment;
        }
    }
    prevSegment = null;
    long startSequenceId = DistributedLogConstants.UNASSIGNED_SEQUENCE_ID;
    for (int i = 0; i < segmentsToReturn.size(); i++) {
        LogSegmentMetadata segment = segmentsToReturn.get(i);
        // assign sequence id
        if (!segment.isInProgress()) {
            if (segment.supportsSequenceId()) {
                startSequenceId = segment.getStartSequenceId() + segment.getRecordCount();
                if (null != prevSegment && prevSegment.supportsSequenceId() && prevSegment.getStartSequenceId() > segment.getStartSequenceId()) {
                    LOG.warn("{} found decreasing start sequence id in log segment {}, previous is {}", new Object[] { streamName, segment, prevSegment });
                }
            } else {
                startSequenceId = DistributedLogConstants.UNASSIGNED_SEQUENCE_ID;
            }
        } else {
            if (segment.supportsSequenceId()) {
                LogSegmentMetadata newSegment = segment.mutator().setStartSequenceId(startSequenceId == DistributedLogConstants.UNASSIGNED_SEQUENCE_ID ? 0L : startSequenceId).build();
                segmentsToReturn.set(i, newSegment);
            }
            break;
        }
        prevSegment = segment;
    }
    if (comparator != LogSegmentMetadata.COMPARATOR) {
        Collections.sort(segmentsToReturn, comparator);
    }
    return segmentsToReturn;
}
Also used : UnexpectedException(org.apache.distributedlog.exceptions.UnexpectedException) LogSegmentMetadata(org.apache.distributedlog.LogSegmentMetadata)

Example 50 with LogSegmentMetadata

use of org.apache.distributedlog.LogSegmentMetadata in project bookkeeper by apache.

the class LogSegmentMetadataStoreUpdater method changeSequenceNumber.

@Override
public CompletableFuture<LogSegmentMetadata> changeSequenceNumber(LogSegmentMetadata segment, long logSegmentSeqNo) {
    String newZkPath = segment.getZkPath().replace(formatLogSegmentSequenceNumber(segment.getLogSegmentSequenceNumber()), formatLogSegmentSequenceNumber(logSegmentSeqNo));
    final LogSegmentMetadata newSegment = segment.mutator().setLogSegmentSequenceNumber(logSegmentSeqNo).setZkPath(newZkPath).build();
    return addNewSegmentAndDeleteOldSegment(newSegment, segment);
}
Also used : LogSegmentMetadata(org.apache.distributedlog.LogSegmentMetadata)

Aggregations

LogSegmentMetadata (org.apache.distributedlog.LogSegmentMetadata)50 Test (org.junit.Test)31 DistributedLogManager (org.apache.distributedlog.api.DistributedLogManager)12 List (java.util.List)9 DLSN (org.apache.distributedlog.DLSN)9 DistributedLogConfiguration (org.apache.distributedlog.DistributedLogConfiguration)9 LogRecordWithDLSN (org.apache.distributedlog.LogRecordWithDLSN)8 Entry (org.apache.distributedlog.Entry)7 Versioned (org.apache.bookkeeper.versioning.Versioned)5 LogSegmentNamesListener (org.apache.distributedlog.callback.LogSegmentNamesListener)5 ZKException (org.apache.distributedlog.exceptions.ZKException)5 CompletableFuture (java.util.concurrent.CompletableFuture)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 URI (java.net.URI)2 ArrayList (java.util.ArrayList)2 LedgerHandle (org.apache.bookkeeper.client.LedgerHandle)2 OrderedScheduler (org.apache.bookkeeper.common.util.OrderedScheduler)2 DLIllegalStateException (org.apache.distributedlog.exceptions.DLIllegalStateException)2