Java Code Examples for com.twitter.util.Duration#fromMilliseconds()

The following examples show how to use com.twitter.util.Duration#fromMilliseconds() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StreamImpl.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
/**
 * Shouldn't call close directly. The callers should call #requestClose instead
 *
 * @param shouldAbort shall we abort the stream instead of closing
 */
private Future<Void> close(boolean shouldAbort, final boolean uncache) {
    boolean abort;
    closeLock.writeLock().lock();
    try {
        if (StreamStatus.CLOSED == status) {
            return closePromise;
        }
        abort = shouldAbort || (StreamStatus.INITIALIZED != status && StreamStatus.CLOSING != status);
        status = StreamStatus.CLOSED;
        streamManager.notifyReleased(this);
    } finally {
        closeLock.writeLock().unlock();
    }
    logger.info("Closing stream {} ...", name);
    // Close the writers to release the locks before failing the requests
    CompletableFuture<Void> closeWriterFuture;
    if (abort) {
        closeWriterFuture = Abortables.asyncAbort(writer, true);
    } else {
        closeWriterFuture = Utils.asyncClose(writer, true);
    }
    // close the manager and error out pending requests after close writer
    Duration closeWaitDuration;
    if (writerCloseTimeoutMs <= 0) {
        closeWaitDuration = Duration.Top();
    } else {
        closeWaitDuration = Duration.fromMilliseconds(writerCloseTimeoutMs);
    }

    CompletableFuture<Void> maskedFuture = FutureUtils.createFuture();
    FutureUtils.proxyTo(
        FutureUtils.stats(
            closeWriterFuture,
            writerCloseStatLogger,
            Stopwatch.createStarted()
        ),
        maskedFuture);

    FutureUtils.within(
        maskedFuture,
        closeWaitDuration.inMillis(),
        TimeUnit.MILLISECONDS,
        new java.util.concurrent.TimeoutException("Timeout on closing"),
        scheduler,
        name
    ).whenCompleteAsync(
        new org.apache.distributedlog.common.concurrent.FutureEventListener<Void>() {
            @Override
            public void onSuccess(Void value) {
                postClose(uncache);
            }

            @Override
            public void onFailure(Throwable cause) {
                if (cause instanceof java.util.concurrent.TimeoutException) {
                    writerCloseTimeoutCounter.inc();
                }
            }
        },
        scheduler.chooseExecutor(name)
    );
    return closePromise;
}
 
Example 2
Source File: TestMovingAverageRate.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
private void advance(TimeControl time, MockTimer timer, int timeMs) {
    Duration duration = Duration.fromMilliseconds(timeMs);
    time.advance(duration);
    timer.tick();
}