Search in sources :

Example 1 with DistributedLock

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

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();
}
Also used : DynamicDistributedLogConfiguration(org.apache.distributedlog.config.DynamicDistributedLogConfiguration) DistributedLock(org.apache.distributedlog.lock.DistributedLock) LockingException(org.apache.distributedlog.exceptions.LockingException) DistributedLogManager(org.apache.distributedlog.api.DistributedLogManager) URI(java.net.URI) Namespace(org.apache.distributedlog.api.namespace.Namespace) Test(org.junit.Test)

Aggregations

DistributedLock (org.apache.distributedlog.lock.DistributedLock)2 IOException (java.io.IOException)1 URI (java.net.URI)1 DistributedLogManager (org.apache.distributedlog.api.DistributedLogManager)1 Namespace (org.apache.distributedlog.api.namespace.Namespace)1 DynamicDistributedLogConfiguration (org.apache.distributedlog.config.DynamicDistributedLogConfiguration)1 LockingException (org.apache.distributedlog.exceptions.LockingException)1 NopDistributedLock (org.apache.distributedlog.lock.NopDistributedLock)1 ZKDistributedLock (org.apache.distributedlog.lock.ZKDistributedLock)1 LogSegmentEntryWriter (org.apache.distributedlog.logsegment.LogSegmentEntryWriter)1 Test (org.junit.Test)1