Example usage for java.util.concurrent ConcurrentLinkedDeque addFirst

List of usage examples for java.util.concurrent ConcurrentLinkedDeque addFirst

Introduction

In this page you can find the example usage for java.util.concurrent ConcurrentLinkedDeque addFirst.

Prototype

public void addFirst(E e) 

Source Link

Document

Inserts the specified element at the front of this deque.

Usage

From source file:org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl.java

private void maybeOffload(CompletableFuture<PositionImpl> finalPromise) {
    if (!offloadMutex.tryLock()) {
        scheduledExecutor.schedule(safeRun(() -> maybeOffloadInBackground(finalPromise)), 100,
                TimeUnit.MILLISECONDS);
    } else {//  w  w  w . j  a  v  a 2 s .c  om
        CompletableFuture<PositionImpl> unlockingPromise = new CompletableFuture<>();
        unlockingPromise.whenComplete((res, ex) -> {
            offloadMutex.unlock();
            if (ex != null) {
                finalPromise.completeExceptionally(ex);
            } else {
                finalPromise.complete(res);
            }
        });

        long threshold = config.getOffloadAutoTriggerSizeThresholdBytes();
        long sizeSummed = 0;
        long alreadyOffloadedSize = 0;
        long toOffloadSize = 0;

        ConcurrentLinkedDeque<LedgerInfo> toOffload = new ConcurrentLinkedDeque();

        // go through ledger list from newest to oldest and build a list to offload in oldest to newest order
        for (Map.Entry<Long, LedgerInfo> e : ledgers.descendingMap().entrySet()) {
            long size = e.getValue().getSize();
            sizeSummed += size;
            boolean alreadyOffloaded = e.getValue().hasOffloadContext()
                    && e.getValue().getOffloadContext().getComplete();
            if (alreadyOffloaded) {
                alreadyOffloadedSize += size;
            } else if (sizeSummed > threshold) {
                toOffloadSize += size;
                toOffload.addFirst(e.getValue());
            }
        }

        if (toOffload.size() > 0) {
            log.info(
                    "[{}] Going to automatically offload ledgers {}"
                            + ", total size = {}, already offloaded = {}, to offload = {}",
                    name, toOffload.stream().map(l -> l.getLedgerId()).collect(Collectors.toList()), sizeSummed,
                    alreadyOffloadedSize, toOffloadSize);
        } else {
            // offloadLoop will complete immediately with an empty list to offload
            log.debug("[{}] Nothing to offload, total size = {}, already offloaded = {}, threshold = {}", name,
                    sizeSummed, alreadyOffloadedSize, threshold);
        }

        offloadLoop(unlockingPromise, toOffload, PositionImpl.latest, Optional.empty());
    }
}