Search in sources :

Example 1 with DistributedLock

use of com.twitter.distributedlog.lock.DistributedLock in project distributedlog by twitter.

the class BKDistributedLogManager method createWriteHandler.

private void createWriteHandler(ZKLogMetadataForWriter logMetadata, boolean lockHandler, final Promise<BKLogWriteHandler> createPromise) {
    OrderedScheduler lockStateExecutor = getLockStateExecutor(true);
    // Build the locks
    DistributedLock lock;
    if (conf.isWriteLockEnabled()) {
        lock = new ZKDistributedLock(lockStateExecutor, getLockFactory(true), logMetadata.getLockPath(), conf.getLockTimeoutMilliSeconds(), statsLogger);
    } else {
        lock = NopDistributedLock.INSTANCE;
    }
    // Build the ledger allocator
    LedgerAllocator allocator;
    try {
        allocator = createLedgerAllocator(logMetadata);
    } catch (IOException e) {
        FutureUtils.setException(createPromise, e);
        return;
    }
    // Make sure writer handler created before resources are initialized
    final BKLogWriteHandler writeHandler = new BKLogWriteHandler(logMetadata, conf, writerZKCBuilder, writerBKCBuilder, writerMetadataStore, scheduler, allocator, statsLogger, perLogStatsLogger, alertStatsLogger, clientId, regionId, writeLimiter, featureProvider, dynConf, lock);
    PermitManager manager = getLogSegmentRollingPermitManager();
    if (manager instanceof Watcher) {
        writeHandler.register((Watcher) manager);
    }
    if (lockHandler) {
        writeHandler.lockHandler().addEventListener(new FutureEventListener<DistributedLock>() {

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

            @Override
            public void onFailure(final Throwable cause) {
                writeHandler.asyncClose().ensure(new AbstractFunction0<BoxedUnit>() {

                    @Override
                    public BoxedUnit apply() {
                        FutureUtils.setException(createPromise, cause);
                        return BoxedUnit.UNIT;
                    }
                });
            }
        });
    } else {
        FutureUtils.setValue(createPromise, writeHandler);
    }
}
Also used : DistributedLock(com.twitter.distributedlog.lock.DistributedLock) NopDistributedLock(com.twitter.distributedlog.lock.NopDistributedLock) ZKDistributedLock(com.twitter.distributedlog.lock.ZKDistributedLock) PermitManager(com.twitter.distributedlog.util.PermitManager) LedgerAllocator(com.twitter.distributedlog.bk.LedgerAllocator) SimpleLedgerAllocator(com.twitter.distributedlog.bk.SimpleLedgerAllocator) Watcher(org.apache.zookeeper.Watcher) IOException(java.io.IOException) AbstractFunction0(scala.runtime.AbstractFunction0) ZKDistributedLock(com.twitter.distributedlog.lock.ZKDistributedLock) OrderedScheduler(com.twitter.distributedlog.util.OrderedScheduler)

Example 2 with DistributedLock

use of com.twitter.distributedlog.lock.DistributedLock in project distributedlog by twitter.

the class BKLogReadHandler method lockStream.

/**
 * Elective stream lock--readers are not required to acquire the lock before using the stream.
 */
synchronized Future<Void> lockStream() {
    if (null == lockAcquireFuture) {
        final Function0<DistributedLock> lockFunction = new ExceptionalFunction0<DistributedLock>() {

            @Override
            public DistributedLock applyE() throws IOException {
                // Unfortunately this has a blocking call which we should not execute on the
                // ZK completion thread
                BKLogReadHandler.this.readLock = new ZKDistributedLock(lockStateExecutor, lockFactory, readLockPath, conf.getLockTimeoutMilliSeconds(), statsLogger.scope("read_lock"));
                LOG.info("acquiring readlock {} at {}", getLockClientId(), readLockPath);
                return BKLogReadHandler.this.readLock;
            }
        };
        lockAcquireFuture = ensureReadLockPathExist().flatMap(new ExceptionalFunction<Void, Future<Void>>() {

            @Override
            public Future<Void> applyE(Void in) throws Throwable {
                return scheduler.apply(lockFunction).flatMap(new ExceptionalFunction<DistributedLock, Future<Void>>() {

                    @Override
                    public Future<Void> applyE(DistributedLock lock) throws IOException {
                        return acquireLockOnExecutorThread(lock);
                    }
                });
            }
        });
    }
    return lockAcquireFuture;
}
Also used : DistributedLock(com.twitter.distributedlog.lock.DistributedLock) ZKDistributedLock(com.twitter.distributedlog.lock.ZKDistributedLock) ExceptionalFunction0(com.twitter.util.ExceptionalFunction0) Future(com.twitter.util.Future) IOException(java.io.IOException) ExceptionalFunction(com.twitter.util.ExceptionalFunction) ZKDistributedLock(com.twitter.distributedlog.lock.ZKDistributedLock)

Example 3 with DistributedLock

use of com.twitter.distributedlog.lock.DistributedLock in project distributedlog by twitter.

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);
    DistributedLogNamespace namespace = DistributedLogNamespaceBuilder.newBuilder().conf(confLocal).uri(uri).clientId("gabbagoo").build();
    DistributedLogManager dlm = namespace.openLog(name);
    DistributedLogNamespace namespace1 = DistributedLogNamespaceBuilder.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
    Await.result(writer.write(DLMTestUtil.getLogRecordInstance(txid++)));
    writer.flushAndCommit();
    BKLogSegmentWriter perStreamWriter = writer.getCachedLogWriter();
    DistributedLock lock = perStreamWriter.getLock();
    FutureUtils.result(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);
        Await.result(writer.write(DLMTestUtil.getLogRecordInstance(txid++)));
        fail("should have thrown");
    } catch (LockingException ex) {
        LOG.debug("caught exception ", ex);
    }
    writer.close();
    dlm.close();
}
Also used : DynamicDistributedLogConfiguration(com.twitter.distributedlog.config.DynamicDistributedLogConfiguration) DistributedLock(com.twitter.distributedlog.lock.DistributedLock) LockingException(com.twitter.distributedlog.exceptions.LockingException) DistributedLogNamespace(com.twitter.distributedlog.namespace.DistributedLogNamespace) URI(java.net.URI) Test(org.junit.Test)

Aggregations

DistributedLock (com.twitter.distributedlog.lock.DistributedLock)3 ZKDistributedLock (com.twitter.distributedlog.lock.ZKDistributedLock)2 IOException (java.io.IOException)2 LedgerAllocator (com.twitter.distributedlog.bk.LedgerAllocator)1 SimpleLedgerAllocator (com.twitter.distributedlog.bk.SimpleLedgerAllocator)1 DynamicDistributedLogConfiguration (com.twitter.distributedlog.config.DynamicDistributedLogConfiguration)1 LockingException (com.twitter.distributedlog.exceptions.LockingException)1 NopDistributedLock (com.twitter.distributedlog.lock.NopDistributedLock)1 DistributedLogNamespace (com.twitter.distributedlog.namespace.DistributedLogNamespace)1 OrderedScheduler (com.twitter.distributedlog.util.OrderedScheduler)1 PermitManager (com.twitter.distributedlog.util.PermitManager)1 ExceptionalFunction (com.twitter.util.ExceptionalFunction)1 ExceptionalFunction0 (com.twitter.util.ExceptionalFunction0)1 Future (com.twitter.util.Future)1 URI (java.net.URI)1 Watcher (org.apache.zookeeper.Watcher)1 Test (org.junit.Test)1 AbstractFunction0 (scala.runtime.AbstractFunction0)1