use of org.apache.distributedlog.lock.DistributedLock 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.lock.DistributedLock in project bookkeeper by apache.
the class TestAsyncReaderWriter method testAsyncWriteWithMinDelayBetweenFlushesFlushFailure.
@Test(timeout = 60000)
public void testAsyncWriteWithMinDelayBetweenFlushesFlushFailure() throws Exception {
String name = runtime.getMethodName();
DistributedLogConfiguration confLocal = new DistributedLogConfiguration();
confLocal.loadConf(testConf);
confLocal.setOutputBufferSize(0);
confLocal.setImmediateFlushEnabled(true);
confLocal.setMinDelayBetweenImmediateFlushMs(1);
URI uri = createDLMURI("/" + name);
ensureURICreated(uri);
Namespace namespace = NamespaceBuilder.newBuilder().conf(confLocal).uri(uri).clientId("gabbagoo").build();
DistributedLogManager dlm = namespace.openLog(name);
Namespace namespace1 = NamespaceBuilder.newBuilder().conf(confLocal).uri(uri).clientId("tortellini").build();
DistributedLogManager dlm1 = namespace1.openLog(name);
int txid = 1;
BKAsyncLogWriter writer = (BKAsyncLogWriter) (dlm.startAsyncLogSegmentNonPartitioned());
// First write succeeds since lock isnt checked until transmit, which is scheduled
Utils.ioResult(writer.write(DLMTestUtil.getLogRecordInstance(txid++)));
writer.flushAndCommit();
BKLogSegmentWriter perStreamWriter = writer.getCachedLogWriter();
DistributedLock lock = perStreamWriter.getLock();
Utils.ioResult(lock.asyncClose());
// Get second writer, steal lock
BKAsyncLogWriter writer2 = (BKAsyncLogWriter) (dlm1.startAsyncLogSegmentNonPartitioned());
try {
// Succeeds, kicks off scheduked flush
writer.write(DLMTestUtil.getLogRecordInstance(txid++));
// Succeeds, kicks off scheduled flush
Thread.sleep(100);
Utils.ioResult(writer.write(DLMTestUtil.getLogRecordInstance(txid++)));
fail("should have thrown");
} catch (LockingException ex) {
LOG.debug("caught exception ", ex);
}
writer.close();
dlm.close();
}
Aggregations