diff --git a/server/src/com/cloud/consoleproxy/ConsoleProxyBalanceAllocator.java b/server/src/com/cloud/consoleproxy/ConsoleProxyBalanceAllocator.java index d1a9a96aaa1..0a445cb0678 100644 --- a/server/src/com/cloud/consoleproxy/ConsoleProxyBalanceAllocator.java +++ b/server/src/com/cloud/consoleproxy/ConsoleProxyBalanceAllocator.java @@ -19,6 +19,7 @@ package com.cloud.consoleproxy; import java.util.ArrayList; +import java.util.Comparator; import java.util.List; import java.util.Map; import java.util.Random; @@ -30,29 +31,48 @@ import com.cloud.configuration.dao.ConfigurationDao; import com.cloud.utils.NumbersUtil; import com.cloud.utils.component.ComponentLocator; import com.cloud.vm.ConsoleProxyVO; + +import edu.emory.mathcs.backport.java.util.Collections; @Local(value={ConsoleProxyAllocator.class}) public class ConsoleProxyBalanceAllocator implements ConsoleProxyAllocator { private String _name; - private int _maxSessionCount = ConsoleProxyManager.DEFAULT_PROXY_CAPACITY; private final Random _rand = new Random(System.currentTimeMillis()); @Override - public ConsoleProxyVO allocProxy(List candidates, Map loadInfo, long dataCenterId) { + public ConsoleProxyVO allocProxy(List candidates, final Map loadInfo, long dataCenterId) { if(candidates != null) { List allocationList = new ArrayList(); - for(ConsoleProxyVO proxy : candidates) { - Integer load = loadInfo.get(proxy.getId()); - if(load == null || load < _maxSessionCount) { - allocationList.add(proxy); - } - } + allocationList.add(proxy); + } + + Collections.sort(candidates, new Comparator () { + @Override + public int compare(ConsoleProxyVO x, ConsoleProxyVO y) { + Integer loadOfX = loadInfo.get(x.getId()); + Integer loadOfY = loadInfo.get(y.getId()); + + if(loadOfX != null && loadOfY != null) { + if(loadOfX < loadOfY) + return -1; + else if(loadOfX > loadOfY) + return 1; + return 0; + } else if(loadOfX == null && loadOfY == null) { + return 0; + } else { + if(loadOfX == null) + return -1; + return 1; + } + } + }); if(allocationList.size() > 0) - return allocationList.get(_rand.nextInt(allocationList.size())); + return allocationList.get(0); } return null; } diff --git a/server/src/com/cloud/vm/dao/ConsoleProxyDaoImpl.java b/server/src/com/cloud/vm/dao/ConsoleProxyDaoImpl.java index a9f8dcc7f5e..1a3dc91603e 100644 --- a/server/src/com/cloud/vm/dao/ConsoleProxyDaoImpl.java +++ b/server/src/com/cloud/vm/dao/ConsoleProxyDaoImpl.java @@ -50,10 +50,10 @@ public class ConsoleProxyDaoImpl extends GenericDaoBase im // private static final String PROXY_ASSIGNMENT_MATRIX = "SELECT c.id, count(runningVm.id) AS count " + - " FROM console_proxy AS c LEFT JOIN" + + " FROM console_proxy AS c LEFT JOIN vm_instance AS i ON c.id=i.id LEFT JOIN" + " (SELECT v.id AS id, v.proxy_id AS proxy_id FROM vm_instance AS v WHERE " + " (v.state='Running' OR v.state='Creating' OR v.state='Starting' OR v.state='Migrating')) " + - " AS runningVm ON c.id = runningVm.proxy_id" + + " AS runningVm ON c.id = runningVm.proxy_id WHERE i.state='Running' " + " GROUP BY c.id"; //