Search in sources :

Example 1 with ExceptionalFunction0

use of com.twitter.util.ExceptionalFunction0 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)

Aggregations

DistributedLock (com.twitter.distributedlog.lock.DistributedLock)1 ZKDistributedLock (com.twitter.distributedlog.lock.ZKDistributedLock)1 ExceptionalFunction (com.twitter.util.ExceptionalFunction)1 ExceptionalFunction0 (com.twitter.util.ExceptionalFunction0)1 Future (com.twitter.util.Future)1 IOException (java.io.IOException)1