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