Example usage for java.util.concurrent ConcurrentLinkedDeque size

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

Introduction

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

Prototype

public int size() 

Source Link

Document

Returns the number of elements in this deque.

Usage

From source file:gobblin.ingestion.google.webmaster.GoogleWebmasterDataFetcherImpl.java

/**
 * Get all pages in an async mode.//  ww w .jav  a2s .  co  m
 */
private Collection<String> getPages(String startDate, String endDate, List<Dimension> dimensions,
        ApiDimensionFilter countryFilter, Queue<Pair<String, FilterOperator>> toProcess) throws IOException {
    String country = GoogleWebmasterFilter.countryFilterToString(countryFilter);

    ConcurrentLinkedDeque<String> allPages = new ConcurrentLinkedDeque<>();
    int r = 0;
    while (r <= RETRY) {
        ++r;
        log.info(String.format("Get pages at round %d with size %d.", r, toProcess.size()));
        ConcurrentLinkedDeque<Pair<String, FilterOperator>> nextRound = new ConcurrentLinkedDeque<>();
        ExecutorService es = Executors.newFixedThreadPool(10, ExecutorsUtils
                .newDaemonThreadFactory(Optional.of(log), Optional.of(this.getClass().getSimpleName())));

        while (!toProcess.isEmpty()) {
            submitJob(toProcess.poll(), countryFilter, startDate, endDate, dimensions, es, allPages, nextRound);
        }
        //wait for jobs to finish and start next round if necessary.
        try {
            es.shutdown();
            boolean terminated = es.awaitTermination(5, TimeUnit.MINUTES);
            if (!terminated) {
                es.shutdownNow();
                log.warn(String.format(
                        "Timed out while getting all pages for country-%s at round %d. Next round now has size %d.",
                        country, r, nextRound.size()));
            }
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        if (nextRound.isEmpty()) {
            break;
        }
        toProcess = nextRound;
    }
    if (r == RETRY) {
        throw new RuntimeException(String.format(
                "Getting all pages reaches the maximum number of retires %d. Date range: %s ~ %s. Country: %s.",
                RETRY, startDate, endDate, country));
    }
    return allPages;
}

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 {/*from w  w w .  jav  a2s. com*/
        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());
    }
}

From source file:org.apache.eagle.alert.engine.publisher.dedup.DedupCache.java

private DedupValue updateCount(EventUniq eventEniq) {
    ConcurrentLinkedDeque<DedupValue> dedupValues = events.get(eventEniq);
    if (dedupValues == null || dedupValues.size() <= 0) {
        LOG.warn("{} No dedup values found for {}, cannot update count", this.publishName, eventEniq);
        return null;
    } else {//from   w w  w . j a v a  2  s  .  c  o  m
        DedupValue dedupValue = dedupValues.getLast();
        dedupValue.setCount(dedupValue.getCount() + 1);
        String updateMsg = String.format("%s Update count for dedup key %s, value %s and count %s",
                this.publishName, eventEniq, dedupValue.getStateFieldValue(), dedupValue.getCount());
        if (LOG.isDebugEnabled()) {
            LOG.debug(updateMsg);
        }
        return dedupValue;
    }
}

From source file:org.apache.gobblin.ingestion.google.webmaster.GoogleWebmasterDataFetcherImpl.java

/**
 * Get all pages in an async mode./*from   w  ww . j av a  2s  .  c om*/
 */
private Collection<String> getPages(String startDate, String endDate, List<Dimension> dimensions,
        ApiDimensionFilter countryFilter, Queue<Pair<String, FilterOperator>> toProcess, int rowLimit)
        throws IOException {
    String country = GoogleWebmasterFilter.countryFilterToString(countryFilter);

    ConcurrentLinkedDeque<String> allPages = new ConcurrentLinkedDeque<>();
    int r = 0;
    while (r <= GET_PAGES_RETRIES) {
        ++r;
        log.info(String.format("Get pages at round %d with size %d.", r, toProcess.size()));
        ConcurrentLinkedDeque<Pair<String, FilterOperator>> nextRound = new ConcurrentLinkedDeque<>();
        ExecutorService es = Executors.newFixedThreadPool(10, ExecutorsUtils
                .newDaemonThreadFactory(Optional.of(log), Optional.of(this.getClass().getSimpleName())));

        while (!toProcess.isEmpty()) {
            submitJob(toProcess.poll(), countryFilter, startDate, endDate, dimensions, es, allPages, nextRound,
                    rowLimit);
        }
        //wait for jobs to finish and start next round if necessary.
        try {
            es.shutdown();
            boolean terminated = es.awaitTermination(5, TimeUnit.MINUTES);
            if (!terminated) {
                es.shutdownNow();
                log.warn(String.format(
                        "Timed out while getting all pages for country-%s at round %d. Next round now has size %d.",
                        country, r, nextRound.size()));
            }
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        if (nextRound.isEmpty()) {
            break;
        }
        toProcess = nextRound;
    }
    if (r == GET_PAGES_RETRIES) {
        throw new RuntimeException(String.format(
                "Getting all pages reaches the maximum number of retires %d. Date range: %s ~ %s. Country: %s.",
                GET_PAGES_RETRIES, startDate, endDate, country));
    }
    return allPages;
}