bug 10732: fix console proxy assignment issue

This commit is contained in:
Kelven Yang 2011-07-14 21:52:16 -07:00
parent 281201be8e
commit 028d235716
2 changed files with 31 additions and 11 deletions

View File

@ -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;
@ -31,28 +32,47 @@ 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<ConsoleProxyVO> candidates, Map<Long, Integer> loadInfo, long dataCenterId) {
public ConsoleProxyVO allocProxy(List<ConsoleProxyVO> candidates, final Map<Long, Integer> loadInfo, long dataCenterId) {
if(candidates != null) {
List<ConsoleProxyVO> allocationList = new ArrayList<ConsoleProxyVO>();
for(ConsoleProxyVO proxy : candidates) {
Integer load = loadInfo.get(proxy.getId());
if(load == null || load < _maxSessionCount) {
allocationList.add(proxy);
}
Collections.sort(candidates, new Comparator<ConsoleProxyVO> () {
@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;
}

View File

@ -50,10 +50,10 @@ public class ConsoleProxyDaoImpl extends GenericDaoBase<ConsoleProxyVO, Long> 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";
//