Search in sources :

Example 1 with Deque

use of java.util.Deque in project hbase by apache.

the class StochasticLoadBalancer method updateRegionLoad.

/**
   * Store the current region loads.
   */
private synchronized void updateRegionLoad() {
    // We create a new hashmap so that regions that are no longer there are removed.
    // However we temporarily need the old loads so we can use them to keep the rolling average.
    Map<String, Deque<BalancerRegionLoad>> oldLoads = loads;
    loads = new HashMap<>();
    for (ServerName sn : clusterStatus.getServers()) {
        ServerLoad sl = clusterStatus.getLoad(sn);
        if (sl == null) {
            continue;
        }
        for (Entry<byte[], RegionLoad> entry : sl.getRegionsLoad().entrySet()) {
            Deque<BalancerRegionLoad> rLoads = oldLoads.get(Bytes.toString(entry.getKey()));
            if (rLoads == null) {
                // There was nothing there
                rLoads = new ArrayDeque<>();
            } else if (rLoads.size() >= numRegionLoadsToRemember) {
                rLoads.remove();
            }
            rLoads.add(new BalancerRegionLoad(entry.getValue()));
            loads.put(Bytes.toString(entry.getKey()), rLoads);
        }
    }
    for (CostFromRegionLoadFunction cost : regionLoadFunctions) {
        cost.setLoads(loads);
    }
}
Also used : ServerLoad(org.apache.hadoop.hbase.ServerLoad) RegionLoad(org.apache.hadoop.hbase.RegionLoad) ServerName(org.apache.hadoop.hbase.ServerName) Deque(java.util.Deque) ArrayDeque(java.util.ArrayDeque)

Example 2 with Deque

use of java.util.Deque in project kafka by apache.

the class RecordAccumulator method ready.

/**
     * Get a list of nodes whose partitions are ready to be sent, and the earliest time at which any non-sendable
     * partition will be ready; Also return the flag for whether there are any unknown leaders for the accumulated
     * partition batches.
     * <p>
     * A destination node is ready to send data if:
     * <ol>
     * <li>There is at least one partition that is not backing off its send
     * <li><b>and</b> those partitions are not muted (to prevent reordering if
     *   {@value org.apache.kafka.clients.producer.ProducerConfig#MAX_IN_FLIGHT_REQUESTS_PER_CONNECTION}
     *   is set to one)</li>
     * <li><b>and <i>any</i></b> of the following are true</li>
     * <ul>
     *     <li>The record set is full</li>
     *     <li>The record set has sat in the accumulator for at least lingerMs milliseconds</li>
     *     <li>The accumulator is out of memory and threads are blocking waiting for data (in this case all partitions
     *     are immediately considered ready).</li>
     *     <li>The accumulator has been closed</li>
     * </ul>
     * </ol>
     */
public ReadyCheckResult ready(Cluster cluster, long nowMs) {
    Set<Node> readyNodes = new HashSet<>();
    long nextReadyCheckDelayMs = Long.MAX_VALUE;
    Set<String> unknownLeaderTopics = new HashSet<>();
    boolean exhausted = this.free.queued() > 0;
    for (Map.Entry<TopicPartition, Deque<ProducerBatch>> entry : this.batches.entrySet()) {
        TopicPartition part = entry.getKey();
        Deque<ProducerBatch> deque = entry.getValue();
        Node leader = cluster.leaderFor(part);
        synchronized (deque) {
            if (leader == null && !deque.isEmpty()) {
                // This is a partition for which leader is not known, but messages are available to send.
                // Note that entries are currently not removed from batches when deque is empty.
                unknownLeaderTopics.add(part.topic());
            } else if (!readyNodes.contains(leader) && !muted.contains(part)) {
                ProducerBatch batch = deque.peekFirst();
                if (batch != null) {
                    long waitedTimeMs = batch.waitedTimeMs(nowMs);
                    boolean backingOff = batch.attempts() > 0 && waitedTimeMs < retryBackoffMs;
                    long timeToWaitMs = backingOff ? retryBackoffMs : lingerMs;
                    boolean full = deque.size() > 1 || batch.isFull();
                    boolean expired = waitedTimeMs >= timeToWaitMs;
                    boolean sendable = full || expired || exhausted || closed || flushInProgress();
                    if (sendable && !backingOff) {
                        readyNodes.add(leader);
                    } else {
                        long timeLeftMs = Math.max(timeToWaitMs - waitedTimeMs, 0);
                        // Note that this results in a conservative estimate since an un-sendable partition may have
                        // a leader that will later be found to have sendable data. However, this is good enough
                        // since we'll just wake up and then sleep again for the remaining time.
                        nextReadyCheckDelayMs = Math.min(timeLeftMs, nextReadyCheckDelayMs);
                    }
                }
            }
        }
    }
    return new ReadyCheckResult(readyNodes, nextReadyCheckDelayMs, unknownLeaderTopics);
}
Also used : Node(org.apache.kafka.common.Node) Deque(java.util.Deque) ArrayDeque(java.util.ArrayDeque) TopicPartition(org.apache.kafka.common.TopicPartition) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) CopyOnWriteMap(org.apache.kafka.common.utils.CopyOnWriteMap) HashSet(java.util.HashSet)

Example 3 with Deque

use of java.util.Deque in project mapdb by jankotek.

the class ArrayDequeTest method testEmptyIterator.

/**
     * iterator of empty collection has no elements
     */
public void testEmptyIterator() {
    Deque c = new ArrayDeque();
    assertIteratorExhausted(c.iterator());
    assertIteratorExhausted(c.descendingIterator());
}
Also used : ArrayDeque(java.util.ArrayDeque) Deque(java.util.Deque) ArrayDeque(java.util.ArrayDeque)

Example 4 with Deque

use of java.util.Deque in project mapdb by jankotek.

the class ConcurrentLinkedDequeTest method testEmptyIterator.

/**
     * iterator of empty collection has no elements
     */
public void testEmptyIterator() {
    Deque c = new ConcurrentLinkedDeque();
    assertIteratorExhausted(c.iterator());
    assertIteratorExhausted(c.descendingIterator());
}
Also used : Deque(java.util.Deque) ConcurrentLinkedDeque(java.util.concurrent.ConcurrentLinkedDeque) ConcurrentLinkedDeque(java.util.concurrent.ConcurrentLinkedDeque)

Example 5 with Deque

use of java.util.Deque in project mapdb by jankotek.

the class LinkedBlockingDequeTest method testEmptyIterator.

/**
     * iterator of empty collection has no elements
     */
public void testEmptyIterator() {
    Deque c = new LinkedBlockingDeque();
    assertIteratorExhausted(c.iterator());
    assertIteratorExhausted(c.descendingIterator());
}
Also used : LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) BlockingDeque(java.util.concurrent.BlockingDeque) Deque(java.util.Deque) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque)

Aggregations

Deque (java.util.Deque)77 Map (java.util.Map)49 HashMap (java.util.HashMap)35 Test (org.junit.Test)33 ArrayDeque (java.util.ArrayDeque)14 Span (com.nike.wingtips.Span)13 IOException (java.io.IOException)12 ArrayList (java.util.ArrayList)12 LinkedList (java.util.LinkedList)9 List (java.util.List)8 BiConsumer (java.util.function.BiConsumer)8 HashSet (java.util.HashSet)6 Consumer (java.util.function.Consumer)6 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)5 BiFunction (java.util.function.BiFunction)5 DataProvider (com.tngtech.java.junit.dataprovider.DataProvider)4 Set (java.util.Set)4 AbstractIterator (com.google.common.collect.AbstractIterator)3 Collection (java.util.Collection)3 Random (java.util.Random)3