Example usage for com.google.common.collect MinMaxPriorityQueue removeLast

List of usage examples for com.google.common.collect MinMaxPriorityQueue removeLast

Introduction

In this page you can find the example usage for com.google.common.collect MinMaxPriorityQueue removeLast.

Prototype

public E removeLast() 

Source Link

Document

Removes and returns the greatest element of this queue.

Usage

From source file:org.apache.hadoop.hbase.master.balancer.SimpleLoadBalancer.java

/**
 * Add a region from the head or tail to the List of regions to return.
 *//*  ww w .j av  a2s. c om*/
private void addRegionPlan(final MinMaxPriorityQueue<RegionPlan> regionsToMove, final boolean fetchFromTail,
        final ServerName sn, List<RegionPlan> regionsToReturn) {
    RegionPlan rp = null;
    if (!fetchFromTail)
        rp = regionsToMove.remove();
    else
        rp = regionsToMove.removeLast();
    rp.setDestination(sn);
    regionsToReturn.add(rp);
}

From source file:com.alibaba.wasp.master.balancer.DefaultLoadBalancer.java

/**
 * Add a entityGroup from the head or tail to the List of entityGroups to return.
 *///from w  w  w  . j ava 2 s  .  c o m
private void addEntityGroupPlan(final MinMaxPriorityQueue<EntityGroupPlan> entityGroupsToMove,
        final boolean fetchFromTail, final ServerName sn, List<EntityGroupPlan> entityGroupsToReturn) {
    EntityGroupPlan rp = null;
    if (!fetchFromTail)
        rp = entityGroupsToMove.remove();
    else
        rp = entityGroupsToMove.removeLast();
    rp.setDestination(sn);
    entityGroupsToReturn.add(rp);
}

From source file:co.cask.cdap.common.zookeeper.coordination.BalancedAssignmentStrategy.java

/**
 * Balance the assignment by spreading it across all handlers evenly.
 *
 * @param handlerQueue The priority queue for tracking number of resources assigned to a given handler.
 * @param assigner The assigner for changing the assignment.
 * @param maxDiff The maximum differences between the handlers that has the most resources assigned vs the one with
 *                the least resources assigned.
 *///  ww  w  .  ja  va2 s .c  o m
private <T> void balance(MinMaxPriorityQueue<HandlerSize<T>> handlerQueue, ResourceAssigner<T> assigner,
        int maxDiff) {
    HandlerSize<T> minHandler = handlerQueue.peekFirst();
    HandlerSize<T> maxHandler = handlerQueue.peekLast();

    // Move assignment from the handler that has the most assigned partition replica to the least one, until the
    // differences is within the desired range.
    Multimap<T, PartitionReplica> assignments = assigner.get();
    while (maxHandler.getSize() - minHandler.getSize() > maxDiff) {
        PartitionReplica partitionReplica = assignments.get(maxHandler.getHandler()).iterator().next();

        // Remove min and max from the queue, and perform the reassignment.
        handlerQueue.removeFirst();
        handlerQueue.removeLast();

        assigner.set(minHandler.getHandler(), partitionReplica);

        // After assignment, the corresponding size should get updated, hence put it back to the queue for next iteration.
        handlerQueue.add(minHandler);
        handlerQueue.add(maxHandler);

        minHandler = handlerQueue.peekFirst();
        maxHandler = handlerQueue.peekLast();
    }
}

From source file:org.apache.phoenix.schema.PMetaDataCache.java

/**
 * Used when the cache is growing past its max size to clone in a single pass.
 * Removes least recently used tables to get size of cache below its max size by
 * the overage amount./* www  . java2 s .  c  o m*/
 */
public PMetaDataCache cloneMinusOverage(long overage) {
    assert (overage > 0);
    int nToRemove = Math.max(MIN_REMOVAL_SIZE,
            (int) Math.ceil((currentByteSize - maxByteSize) / ((double) currentByteSize / size())) + 1);
    MinMaxPriorityQueue<PTableRef> toRemove = BUILDER.expectedSize(nToRemove).create();
    PMetaDataCache newCache = new PMetaDataCache(this.size(), this.maxByteSize, this.timeKeeper,
            this.tableRefFactory);

    long toRemoveBytes = 0;
    // Add to new cache, but track references to remove when done
    // to bring cache at least overage amount below it's max size.
    for (PTableRef tableRef : this.tables.values()) {
        newCache.put(tableRef.getTable().getKey(), tableRefFactory.makePTableRef(tableRef));
        toRemove.add(tableRef);
        toRemoveBytes += tableRef.getEstimatedSize();
        while (toRemoveBytes - toRemove.peekLast().getEstimatedSize() >= overage) {
            PTableRef removedRef = toRemove.removeLast();
            toRemoveBytes -= removedRef.getEstimatedSize();
        }
    }
    for (PTableRef toRemoveRef : toRemove) {
        newCache.remove(toRemoveRef.getTable().getKey());
    }
    return newCache;
}

From source file:org.apache.hadoop.hbase.master.DefaultLoadBalancer.java

/**
 * Add a region from the head or tail to the List of regions to return.
 *//*from   w ww . j ava2 s.  c  o m*/
void addRegionPlan(final MinMaxPriorityQueue<RegionPlan> regionsToMove, final boolean fetchFromTail,
        final ServerName sn, List<RegionPlan> regionsToReturn) {
    RegionPlan rp = null;
    if (!fetchFromTail)
        rp = regionsToMove.remove();
    else
        rp = regionsToMove.removeLast();
    rp.setDestination(sn);
    regionsToReturn.add(rp);
}

From source file:edu.brandeis.wisedb.scheduler.BestNFirstGraphSearch.java

@Override
public List<Action> schedule(Set<ModelQuery> toSched) {

    FullGraphState first = new FullGraphState(new TreeSet<ModelVM>(), toSched, sla, qtp);
    MinMaxPriorityQueue<StateCost> frontier = MinMaxPriorityQueue.create();
    frontier.add(new StateCost(first, 0, null, null));

    while (!frontier.isEmpty()) {
        log.fine("Frontier size: " + frontier.size());

        PriorityQueue<Action> pq = new PriorityQueue<Action>(new ActionComparator());
        StateCost next = frontier.poll();

        if (next.s.isGoalState()) {
            // we're done
            List<Action> toR = new LinkedList<Action>();
            StateCost last = next;//from   ww  w. ja va  2  s  .c  o  m
            while (last.action != null) {
                toR.add(0, last.action);
                last = last.prev;
            }
            log.fine("Reached goal state with following actions: " + toR);

            return toR;
        }

        for (Action a : next.s.getPossibleActions()) {
            int cost = 0;
            FullGraphState nextState = next.s.getNewStateForAction(a);

            cost += h.predictCostToEnd(nextState);
            //cost += nextState.getExecutionCost();

            a.computedCost = cost;
            log.finer("Added action " + a + " to the frontier");
            pq.add(a);
        }

        if (pq.isEmpty()) {
            log.severe("There was no selectable action for state: " + next);
            return null;
        }

        for (int i = 0; i < toTry; i++) {
            Action nextBest = pq.poll();
            if (nextBest == null) {
                log.fine("Unable to get " + (i + 1) + "th action for state " + next);
                break;
            }
            FullGraphState c = next.s.getNewStateForAction(nextBest);
            StateCost candidate = new StateCost(c, c.getExecutionCost(), nextBest, next);
            frontier.add(candidate);
        }

        while (frontier.size() > maxFrontierSize) {
            frontier.removeLast();
        }
    }

    return null;
}