Example usage for java.util Collections rotate

List of usage examples for java.util Collections rotate

Introduction

In this page you can find the example usage for java.util Collections rotate.

Prototype

public static void rotate(List<?> list, int distance) 

Source Link

Document

Rotates the elements in the specified list by the specified distance.

Usage

From source file:org.onosproject.intentperf.IntentPerfInstaller.java

private List<NodeId> getNeighbors() {
    List<NodeId> nodes = clusterService.getNodes().stream().map(ControllerNode::id)
            .collect(Collectors.toCollection(ArrayList::new));
    // sort neighbors by id
    Collections.sort(nodes, (node1, node2) -> node1.toString().compareTo(node2.toString()));
    // rotate the local node to index 0
    Collections.rotate(nodes, -1 * nodes.indexOf(clusterService.getLocalNode().id()));
    log.debug("neighbors (raw): {}", nodes); //TODO remove
    // generate the sub-list that will contain local node and selected neighbors
    nodes = nodes.subList(0, numNeighbors + 1);
    log.debug("neighbors: {}", nodes); //TODO remove
    return nodes;
}

From source file:org.rhq.enterprise.server.cloud.FailoverListManagerBean.java

private Map<Agent, FailoverListComposite> getForAgents(PartitionEvent event, List<Server> servers,
        List<Agent> agents, List<FailoverListDetailsComposite> existingLoads) {
    Map<Agent, FailoverListComposite> result = new HashMap<Agent, FailoverListComposite>(agents.size());

    // create a bucket for each server to which we will assign agents 
    List<ServerBucket> buckets = new ArrayList<ServerBucket>(servers.size());
    for (Server next : servers) {
        buckets.add(new ServerBucket(next));
    }/*from w  ww.ja va  2  s. com*/

    // initialize the result map
    Map<Agent, List<ServerBucket>> agentServerListMap = new HashMap<Agent, List<ServerBucket>>(agents.size());
    for (Agent next : agents) {
        agentServerListMap.put(next, new ArrayList<ServerBucket>(servers.size()));
    }

    // assign server lists level by level: primary, then secondary, the tertiary, etc        
    for (int level = 0; (level < servers.size()); ++level) {

        // Initialize the bucket loads for the next round
        initBuckets(buckets, existingLoads, level);

        // assign a server for this level to each agent, balancing as we go
        for (Agent next : agents) {

            List<ServerBucket> serverList = agentServerListMap.get(next);

            // When assigning primary (i.e. level 0), supply the current primary as the preferred server.
            // This should reduce connection churn by letting most agents stay put (but affects balancing, we'll deal with
            // that below)
            ServerBucket bestBucket = null;

            // Rotate the list (makes the last entry the first entry) on each iteration. This
            // enhances bucket distribution amongst the levels and ensures that we don't starve
            // buckets at the end of the list.
            Collections.rotate(buckets, 1);

            if ((0 == level) && (null != next.getServer())) {
                bestBucket = ServerBucket.getBestBucket(buckets, serverList, next.getAffinityGroup(),
                        next.getServer().getName());
            } else {
                bestBucket = ServerBucket.getBestBucket(buckets, serverList, next.getAffinityGroup(), null);
            }

            if (null == bestBucket) {
                // this should never happen but let's defensively check and log
                log.error("Unexpected Condition! null bucket in getForAllAgents()");
                continue;
            }

            serverList.add(bestBucket);
            // note that assigned load takes into consideration compute power of the server
            bestBucket.assignedLoad += (getAgentLoad(next) / bestBucket.computePower);
            bestBucket.assignedAgents.add(next);
        }

        // For debugging logServerList("Level " + level, agentServerListMap);

        // The first pass does a best-effort balancing as it goes but may need further balancing because:
        // - the assignment of primary servers tries to retain the current primary server of an existing agent.
        //   This disrupts the load balancing (but reduces churn).
        // - the algorithm is greedy, assigning servers as they are available, this can overload a server near the
        //   end of assignments (due to, for example, constraints avoiding server duplication in a server list).
        // Now, if necessary for load balance, force some agents to new servers.
        if (balanceLoad(buckets, agentServerListMap)) {
            // for debugging logServerList("Forced Rebalance!", agentServerListMap);
        }
    }

    // generate the result Map
    for (Agent next : agentServerListMap.keySet()) {
        List<ServerEntry> serverEntries = new ArrayList<ServerEntry>(servers.size());

        for (ServerBucket bucket : agentServerListMap.get(next)) {
            serverEntries.add(bucket.serverEntry);
        }

        result.put(next, new FailoverListComposite(serverEntries));
    }

    return result;
}