Search in sources :

Example 1 with LogSegmentEntryWriter

use of org.apache.distributedlog.logsegment.LogSegmentEntryWriter in project bookkeeper by apache.

the class BKDistributedLogManager method createWriteHandler.

private void createWriteHandler(LogMetadataForWriter logMetadata, boolean lockHandler, final CompletableFuture<BKLogWriteHandler> createPromise) {
    // Build the locks
    DistributedLock lock;
    if (conf.isWriteLockEnabled()) {
        lock = driver.getLogStreamMetadataStore(WRITER).createWriteLock(logMetadata);
    } else {
        lock = NopDistributedLock.INSTANCE;
    }
    Allocator<LogSegmentEntryWriter, Object> segmentAllocator;
    try {
        segmentAllocator = driver.getLogSegmentEntryStore(WRITER).newLogSegmentAllocator(logMetadata, dynConf);
    } catch (IOException ioe) {
        FutureUtils.completeExceptionally(createPromise, ioe);
        return;
    }
    // Make sure writer handler created before resources are initialized
    final BKLogWriteHandler writeHandler = new BKLogWriteHandler(logMetadata, conf, driver.getLogStreamMetadataStore(WRITER), logSegmentMetadataCache, driver.getLogSegmentEntryStore(WRITER), scheduler, segmentAllocator, statsLogger, perLogStatsLogger, alertStatsLogger, clientId, regionId, writeLimiter, featureProvider, dynConf, lock);
    if (lockHandler) {
        writeHandler.lockHandler().whenComplete(new FutureEventListener<DistributedLock>() {

            @Override
            public void onSuccess(DistributedLock lock) {
                FutureUtils.complete(createPromise, writeHandler);
            }

            @Override
            public void onFailure(final Throwable cause) {
                FutureUtils.ensure(writeHandler.asyncClose(), () -> FutureUtils.completeExceptionally(createPromise, cause));
            }
        });
    } else {
        FutureUtils.complete(createPromise, writeHandler);
    }
}
Also used : LogSegmentEntryWriter(org.apache.distributedlog.logsegment.LogSegmentEntryWriter) ZKDistributedLock(org.apache.distributedlog.lock.ZKDistributedLock) NopDistributedLock(org.apache.distributedlog.lock.NopDistributedLock) DistributedLock(org.apache.distributedlog.lock.DistributedLock) IOException(java.io.IOException)

Example 2 with LogSegmentEntryWriter

use of org.apache.distributedlog.logsegment.LogSegmentEntryWriter in project bookkeeper by apache.

the class BKLogWriteHandler method doStartLogSegment.

protected void doStartLogSegment(final long txId, final boolean bestEffort, final boolean allowMaxTxID, final CompletableFuture<BKLogSegmentWriter> promise) {
    // validate the tx id
    if ((txId < 0) || (!allowMaxTxID && (txId == DistributedLogConstants.MAX_TXID))) {
        FutureUtils.completeExceptionally(promise, new IOException("Invalid Transaction Id " + txId));
        return;
    }
    long highestTxIdWritten = maxTxId.get();
    if (txId < highestTxIdWritten) {
        if (highestTxIdWritten == DistributedLogConstants.MAX_TXID) {
            LOG.error("We've already marked the stream as ended and attempting to start a new log segment");
            FutureUtils.completeExceptionally(promise, new EndOfStreamException("Writing to a stream after it has been marked as completed"));
            return;
        } else {
            LOG.error("We've already seen TxId {} the max TXId is {}", txId, highestTxIdWritten);
            FutureUtils.completeExceptionally(promise, new TransactionIdOutOfOrderException(txId, highestTxIdWritten));
            return;
        }
    }
    try {
        logSegmentAllocator.allocate();
    } catch (IOException e) {
        // failed to issue an allocation request
        failStartLogSegment(promise, bestEffort, e);
        return;
    }
    // start the transaction from zookeeper
    final Transaction<Object> txn = streamMetadataStore.newTransaction();
    // failpoint injected before creating ledger
    try {
        FailpointUtils.checkFailPoint(FailpointUtils.FailPointName.FP_StartLogSegmentBeforeLedgerCreate);
    } catch (IOException ioe) {
        failStartLogSegment(promise, bestEffort, ioe);
        return;
    }
    logSegmentAllocator.tryObtain(txn, NULL_OP_LISTENER).whenComplete(new FutureEventListener<LogSegmentEntryWriter>() {

        @Override
        public void onSuccess(LogSegmentEntryWriter entryWriter) {
            // try-obtain succeed
            createInprogressLogSegment(txn, txId, entryWriter, bestEffort, promise);
        }

        @Override
        public void onFailure(Throwable cause) {
            failStartLogSegment(promise, bestEffort, cause);
        }
    });
}
Also used : LogSegmentEntryWriter(org.apache.distributedlog.logsegment.LogSegmentEntryWriter) EndOfStreamException(org.apache.distributedlog.exceptions.EndOfStreamException) TransactionIdOutOfOrderException(org.apache.distributedlog.exceptions.TransactionIdOutOfOrderException) IOException(java.io.IOException)

Example 3 with LogSegmentEntryWriter

use of org.apache.distributedlog.logsegment.LogSegmentEntryWriter in project bookkeeper by apache.

the class TestDistributedLogBase method getLedgerHandle.

protected LedgerHandle getLedgerHandle(BKLogSegmentWriter segmentWriter) {
    LogSegmentEntryWriter entryWriter = segmentWriter.getEntryWriter();
    assertTrue(entryWriter instanceof BKLogSegmentEntryWriter);
    return ((BKLogSegmentEntryWriter) entryWriter).getLedgerHandle();
}
Also used : BKLogSegmentEntryWriter(org.apache.distributedlog.impl.logsegment.BKLogSegmentEntryWriter) LogSegmentEntryWriter(org.apache.distributedlog.logsegment.LogSegmentEntryWriter) BKLogSegmentEntryWriter(org.apache.distributedlog.impl.logsegment.BKLogSegmentEntryWriter)

Aggregations

LogSegmentEntryWriter (org.apache.distributedlog.logsegment.LogSegmentEntryWriter)3 IOException (java.io.IOException)2 EndOfStreamException (org.apache.distributedlog.exceptions.EndOfStreamException)1 TransactionIdOutOfOrderException (org.apache.distributedlog.exceptions.TransactionIdOutOfOrderException)1 BKLogSegmentEntryWriter (org.apache.distributedlog.impl.logsegment.BKLogSegmentEntryWriter)1 DistributedLock (org.apache.distributedlog.lock.DistributedLock)1 NopDistributedLock (org.apache.distributedlog.lock.NopDistributedLock)1 ZKDistributedLock (org.apache.distributedlog.lock.ZKDistributedLock)1