Search in sources :

Example 11 with BoxedUnit

use of scala.runtime.BoxedUnit in project distributedlog by twitter.

the class SafeQueueingFuturePool method apply.

public synchronized Future<T> apply(final Function0<T> fn) {
    Preconditions.checkNotNull(fn);
    if (closed) {
        return Future.exception(new RejectedExecutionException("Operation submitted to closed SafeQueueingFuturePool"));
    }
    ++outstanding;
    queue.add(fn);
    Future<T> result = orderedFuturePool.apply(new Function0<T>() {

        @Override
        public T apply() {
            return queue.poll().apply();
        }

        @Override
        public String toString() {
            return fn.toString();
        }
    }).ensure(new Function0<BoxedUnit>() {

        public BoxedUnit apply() {
            if (decrOutstandingAndCheckDone()) {
                applyAll();
            }
            return null;
        }
    });
    return result;
}
Also used : Function0(com.twitter.util.Function0) BoxedUnit(scala.runtime.BoxedUnit) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Example 12 with BoxedUnit

use of scala.runtime.BoxedUnit in project distributedlog by twitter.

the class ZKDistributedLock method asyncAcquire.

/**
     * Asynchronously acquire the lock. Technically the try phase of this operation--which adds us to the waiter
     * list--is executed synchronously, but the lock wait itself doesn't block.
     */
public synchronized Future<ZKDistributedLock> asyncAcquire() {
    if (null != lockAcquireFuture) {
        return Future.exception(new UnexpectedException("Someone is already acquiring/acquired lock " + lockPath));
    }
    final Promise<ZKDistributedLock> promise = new Promise<ZKDistributedLock>(new Function<Throwable, BoxedUnit>() {

        @Override
        public BoxedUnit apply(Throwable cause) {
            lockStateExecutor.submit(lockPath, new Runnable() {

                @Override
                public void run() {
                    asyncClose();
                }
            });
            return BoxedUnit.UNIT;
        }
    });
    final Stopwatch stopwatch = Stopwatch.createStarted();
    promise.addEventListener(new FutureEventListener<ZKDistributedLock>() {

        @Override
        public void onSuccess(ZKDistributedLock lock) {
            acquireStats.registerSuccessfulEvent(stopwatch.stop().elapsed(TimeUnit.MICROSECONDS));
        }

        @Override
        public void onFailure(Throwable cause) {
            acquireStats.registerFailedEvent(stopwatch.stop().elapsed(TimeUnit.MICROSECONDS));
            // release the lock if fail to acquire
            asyncClose();
        }
    });
    this.lockAcquireFuture = promise;
    lockStateExecutor.submit(lockPath, new Runnable() {

        @Override
        public void run() {
            doAsyncAcquireWithSemaphore(promise, lockTimeout);
        }
    });
    return promise;
}
Also used : Promise(com.twitter.util.Promise) UnexpectedException(com.twitter.distributedlog.exceptions.UnexpectedException) Stopwatch(com.google.common.base.Stopwatch) BoxedUnit(scala.runtime.BoxedUnit)

Example 13 with BoxedUnit

use of scala.runtime.BoxedUnit in project distributedlog by twitter.

the class ZKSessionLockFactory method createLock.

@Override
public Future<SessionLock> createLock(String lockPath, DistributedLockContext context) {
    AtomicInteger numRetries = new AtomicInteger(lockCreationRetries);
    final AtomicReference<Throwable> interruptedException = new AtomicReference<Throwable>(null);
    Promise<SessionLock> createPromise = new Promise<SessionLock>(new com.twitter.util.Function<Throwable, BoxedUnit>() {

        @Override
        public BoxedUnit apply(Throwable t) {
            interruptedException.set(t);
            return BoxedUnit.UNIT;
        }
    });
    createLock(lockPath, context, interruptedException, numRetries, createPromise, 0L);
    return createPromise;
}
Also used : Promise(com.twitter.util.Promise) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicReference(java.util.concurrent.atomic.AtomicReference) BoxedUnit(scala.runtime.BoxedUnit)

Example 14 with BoxedUnit

use of scala.runtime.BoxedUnit in project distributedlog by twitter.

the class ReadUtils method getLogRecordNotLessThanTxId.

//
// Search Functions
//
/**
     * Get the log record whose transaction id is not less than provided <code>transactionId</code>.
     *
     * <p>
     * It uses a binary-search like algorithm to find the log record whose transaction id is not less than
     * provided <code>transactionId</code> within a log <code>segment</code>. You could think of a log segment
     * in terms of a sequence of records whose transaction ids are non-decreasing.
     *
     * - The sequence of records within a log segment is divided into N pieces.
     * - Find the piece of records that contains a record whose transaction id is not less than provided
     *   <code>transactionId</code>.
     *
     * N could be chosen based on trading off concurrency and latency.
     * </p>
     *
     * @param logName
     *          name of the log
     * @param segment
     *          metadata of the log segment
     * @param transactionId
     *          transaction id
     * @param executorService
     *          executor service used for processing entries
     * @param handleCache
     *          ledger handle cache
     * @param nWays
     *          how many number of entries to search in parallel
     * @return found log record. none if all transaction ids are less than provided <code>transactionId</code>.
     */
public static Future<Optional<LogRecordWithDLSN>> getLogRecordNotLessThanTxId(final String logName, final LogSegmentMetadata segment, final long transactionId, final ExecutorService executorService, final LedgerHandleCache handleCache, final int nWays) {
    if (!segment.isInProgress()) {
        if (segment.getLastTxId() < transactionId) {
            // all log records whose transaction id is less than provided transactionId
            // then return none
            Optional<LogRecordWithDLSN> noneRecord = Optional.absent();
            return Future.value(noneRecord);
        }
    }
    final Promise<Optional<LogRecordWithDLSN>> promise = new Promise<Optional<LogRecordWithDLSN>>();
    final FutureEventListener<LedgerDescriptor> openLedgerListener = new FutureEventListener<LedgerDescriptor>() {

        @Override
        public void onSuccess(final LedgerDescriptor ld) {
            promise.ensure(new AbstractFunction0<BoxedUnit>() {

                @Override
                public BoxedUnit apply() {
                    handleCache.asyncCloseLedger(ld);
                    return BoxedUnit.UNIT;
                }
            });
            long lastEntryId;
            try {
                lastEntryId = handleCache.getLastAddConfirmed(ld);
            } catch (BKException e) {
                promise.setException(e);
                return;
            }
            if (lastEntryId < 0) {
                // it means that the log segment is created but not written yet or an empty log segment.
                // it is equivalent to 'all log records whose transaction id is less than provided transactionId'
                Optional<LogRecordWithDLSN> nonRecord = Optional.absent();
                promise.setValue(nonRecord);
                return;
            }
            // all log records whose transaction id is not less than provided transactionId
            if (segment.getFirstTxId() >= transactionId) {
                final FirstTxIdNotLessThanSelector selector = new FirstTxIdNotLessThanSelector(transactionId);
                asyncReadRecordFromEntries(logName, ld, handleCache, segment, executorService, new SingleEntryScanContext(0L), selector).addEventListener(new FutureEventListener<LogRecordWithDLSN>() {

                    @Override
                    public void onSuccess(LogRecordWithDLSN value) {
                        promise.setValue(Optional.of(selector.result()));
                    }

                    @Override
                    public void onFailure(Throwable cause) {
                        promise.setException(cause);
                    }
                });
                return;
            }
            getLogRecordNotLessThanTxIdFromEntries(logName, ld, segment, transactionId, executorService, handleCache, Lists.newArrayList(0L, lastEntryId), nWays, Optional.<LogRecordWithDLSN>absent(), promise);
        }

        @Override
        public void onFailure(final Throwable cause) {
            String errMsg = "Error opening log segment [" + segment + "] for find record from " + logName;
            promise.setException(new IOException(errMsg, BKException.create(FutureUtils.bkResultCode(cause))));
        }
    };
    handleCache.asyncOpenLedger(segment, false).addEventListener(FutureEventListenerRunnable.of(openLedgerListener, executorService));
    return promise;
}
Also used : Optional(com.google.common.base.Optional) IOException(java.io.IOException) FirstTxIdNotLessThanSelector(com.twitter.distributedlog.selector.FirstTxIdNotLessThanSelector) Promise(com.twitter.util.Promise) FutureEventListener(com.twitter.util.FutureEventListener) BKException(org.apache.bookkeeper.client.BKException) BoxedUnit(scala.runtime.BoxedUnit)

Example 15 with BoxedUnit

use of scala.runtime.BoxedUnit in project distributedlog by twitter.

the class ReadUtils method asyncReadRecord.

private static Future<LogRecordWithDLSN> asyncReadRecord(final String streamName, final LogSegmentMetadata l, final boolean fence, final boolean includeControl, final boolean includeEndOfStream, final int scanStartBatchSize, final int scanMaxBatchSize, final AtomicInteger numRecordsScanned, final ExecutorService executorService, final LedgerHandleCache handleCache, final LogRecordSelector selector, final boolean backward, final long startEntryId) {
    final Promise<LogRecordWithDLSN> promise = new Promise<LogRecordWithDLSN>();
    FutureEventListener<LedgerDescriptor> openLedgerListener = new FutureEventListener<LedgerDescriptor>() {

        @Override
        public void onSuccess(final LedgerDescriptor ledgerDescriptor) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("{} Opened logsegment {} for reading record", streamName, l);
            }
            promise.ensure(new AbstractFunction0<BoxedUnit>() {

                @Override
                public BoxedUnit apply() {
                    handleCache.asyncCloseLedger(ledgerDescriptor);
                    return BoxedUnit.UNIT;
                }
            });
            if (LOG.isDebugEnabled()) {
                LOG.debug("{} {} scanning {}.", new Object[] { (backward ? "backward" : "forward"), streamName, l });
            }
            asyncReadRecordFromLogSegment(streamName, ledgerDescriptor, handleCache, l, executorService, scanStartBatchSize, scanMaxBatchSize, includeControl, includeEndOfStream, promise, numRecordsScanned, selector, backward, startEntryId);
        }

        @Override
        public void onFailure(final Throwable cause) {
            String errMsg = "Error opening log segment [" + l + "] for reading record of " + streamName;
            promise.setException(new IOException(errMsg, BKException.create(FutureUtils.bkResultCode(cause))));
        }
    };
    handleCache.asyncOpenLedger(l, fence).addEventListener(FutureEventListenerRunnable.of(openLedgerListener, executorService));
    return promise;
}
Also used : Promise(com.twitter.util.Promise) FutureEventListener(com.twitter.util.FutureEventListener) BoxedUnit(scala.runtime.BoxedUnit) IOException(java.io.IOException)

Aggregations

BoxedUnit (scala.runtime.BoxedUnit)15 Promise (com.twitter.util.Promise)9 UnexpectedException (com.twitter.distributedlog.exceptions.UnexpectedException)4 FutureEventListener (com.twitter.util.FutureEventListener)4 IOException (java.io.IOException)4 Stopwatch (com.google.common.base.Stopwatch)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 SafeRunnable (org.apache.bookkeeper.util.SafeRunnable)3 DLInterruptedException (com.twitter.distributedlog.exceptions.DLInterruptedException)2 OwnershipAcquireFailedException (com.twitter.distributedlog.exceptions.OwnershipAcquireFailedException)2 BulkWriteOp (com.twitter.distributedlog.service.stream.BulkWriteOp)2 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)2 Optional (com.google.common.base.Optional)1 AlreadyClosedException (com.twitter.distributedlog.exceptions.AlreadyClosedException)1 DLException (com.twitter.distributedlog.exceptions.DLException)1 InvalidStreamNameException (com.twitter.distributedlog.exceptions.InvalidStreamNameException)1 LockCancelledException (com.twitter.distributedlog.exceptions.LockCancelledException)1 LockingException (com.twitter.distributedlog.exceptions.LockingException)1 LogNotFoundException (com.twitter.distributedlog.exceptions.LogNotFoundException)1 OverCapacityException (com.twitter.distributedlog.exceptions.OverCapacityException)1