Skip to content

Commit 4ad9ac5

Browse files
author
prachi
committed
Bug 11200 - maximum number of guests per host
Changes: To make sure migration does not attempt to pick a host that has running VMs more than the max guest VM's limit: - Changed manual migration to call host allocators to return a list of hosts suitable for migration. Host allocators check for the max guest VM limit. - Earlier we returned hosts with enough capacity but now Host Allocators make other checks along with capacity. So the list of hosts returned are hosts that have enough capacity AND satisfy all other conditions like host tags, max guests limit etc. Or in other words Allocators dont return the hosts that dont satisfy all conditions even if they have capacity. -Therefore, now we mark the list of hosts returned for manual migration as 'suitable' hosts instead of 'hasenoughCapacity' in the HostResponse. - HA migration already calls allocators, so no change is needed there.
1 parent 980c1ca commit 4ad9ac5

16 files changed

Lines changed: 165 additions & 63 deletions

File tree

‎api/src/com/cloud/api/commands/ListHostsCmd.java‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,16 +127,16 @@ public AsyncJob.Type getInstanceType() {
127127
@Override
128128
public void execute(){
129129
List<? extends Host> result = new ArrayList<Host>();
130-
List<Long> hostIdsWithCapacity = new ArrayList<Long>();
130+
List<? extends Host> hostsWithCapacity = new ArrayList<Host>();
131131

132132
if(getVirtualMachineId() != null){
133133
UserVm userVm = _userVmService.getUserVm(getVirtualMachineId());
134134
if (userVm == null) {
135135
throw new InvalidParameterValueException("Unable to find the VM by id=" + getVirtualMachineId());
136136
}
137-
Pair<List<? extends Host>, List<Long>> hostsForMigration = _mgr.listHostsForMigrationOfVM(userVm, this.getStartIndex(), this.getPageSizeVal());
137+
Pair<List<? extends Host>, List<? extends Host>> hostsForMigration = _mgr.listHostsForMigrationOfVM(userVm, this.getStartIndex(), this.getPageSizeVal());
138138
result = hostsForMigration.first();
139-
hostIdsWithCapacity = hostsForMigration.second();
139+
hostsWithCapacity = hostsForMigration.second();
140140
}else{
141141
result = _mgr.searchForServers(this);
142142
}
@@ -145,11 +145,11 @@ public void execute(){
145145
List<HostResponse> hostResponses = new ArrayList<HostResponse>();
146146
for (Host host : result) {
147147
HostResponse hostResponse = _responseGenerator.createHostResponse(host);
148-
Boolean hasEnoughCapacity = false;
149-
if(hostIdsWithCapacity.contains(host.getId())){
150-
hasEnoughCapacity = true;
148+
Boolean suitableForMigration = false;
149+
if(hostsWithCapacity.contains(host)){
150+
suitableForMigration = true;
151151
}
152-
hostResponse.setHasEnoughCapacity(hasEnoughCapacity);
152+
hostResponse.setSuitableForMigration(suitableForMigration);
153153
hostResponse.setObjectName("host");
154154
hostResponses.add(hostResponse);
155155
}

‎api/src/com/cloud/api/response/HostResponse.java‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,9 @@ public class HostResponse extends BaseResponse {
150150
@SerializedName("hasEnoughCapacity") @Param(description="true if this host has enough CPU and RAM capacity to migrate a VM to it, false otherwise")
151151
private Boolean hasEnoughCapacity;
152152

153+
@SerializedName("suitableForMigration") @Param(description="true if this host is suitable(has enough capacity and satisfies all conditions like hosttags, max guests vm limit etc) to migrate a VM to it , false otherwise")
154+
private Boolean suitableForMigration;
155+
153156
@SerializedName("allocationstate") @Param(description="the allocation state of the host")
154157
private String allocationState;
155158

@@ -485,6 +488,14 @@ public Boolean hasEnoughCapacity() {
485488
public void setHasEnoughCapacity(Boolean hasEnoughCapacity) {
486489
this.hasEnoughCapacity = hasEnoughCapacity;
487490
}
491+
492+
public Boolean isSuitableForMigration() {
493+
return suitableForMigration;
494+
}
495+
496+
public void setSuitableForMigration(Boolean suitableForMigration) {
497+
this.suitableForMigration = suitableForMigration;
498+
}
488499

489500
public String getAllocationState() {
490501
return allocationState;

‎api/src/com/cloud/server/ManagementService.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -501,13 +501,13 @@ public interface ManagementService {
501501

502502
/**
503503
* List hosts for migrating the given VM. The API returns list of all hosts in the VM's cluster minus the current host and
504-
* also a list of hostIds that seem to have enough CPU and RAM capacity to host this VM.
504+
* also a list of hosts that seem to have enough CPU and RAM capacity to host this VM.
505505
*
506506
* @param UserVm
507507
* vm The VM to migrate
508-
* @return Pair<List<? extends Host>, List<Long>> List of all Hosts in VM's cluster and list of HostIds with enough capacity
508+
* @return Pair<List<? extends Host>, List<? extends Host>> List of all Hosts in VM's cluster and list of Hosts with enough capacity
509509
*/
510-
Pair<List<? extends Host>, List<Long>> listHostsForMigrationOfVM(UserVm vm, Long startIndex, Long pageSize);
510+
Pair<List<? extends Host>, List<? extends Host>> listHostsForMigrationOfVM(UserVm vm, Long startIndex, Long pageSize);
511511

512512
String[] listEventTypes();
513513

‎core/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4879,6 +4879,9 @@ protected void fillHostInfo(Connection conn, StartupRoutingCommand cmd) {
48794879
details.put("product_version", hr.softwareVersion.get("product_version"));
48804880
if( hr.softwareVersion.get("product_version_text_short") != null ) {
48814881
details.put("product_version_text_short", hr.softwareVersion.get("product_version_text_short"));
4882+
cmd.setHypervisorVersion(hr.softwareVersion.get("product_version_text_short"));
4883+
}else{
4884+
cmd.setHypervisorVersion(hr.softwareVersion.get("product_version"));
48824885
}
48834886
if (_privateNetworkName != null) {
48844887
details.put("private.network.device", _privateNetworkName);

‎server/src/com/cloud/agent/manager/allocator/HostAllocator.java‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package com.cloud.agent.manager.allocator;
1919

2020
import java.util.List;
21+
2122
import com.cloud.deploy.DeploymentPlan;
2223
import com.cloud.deploy.DeploymentPlanner.ExcludeList;
2324
import com.cloud.host.Host;
@@ -51,5 +52,23 @@ public interface HostAllocator extends Adapter {
5152
**/
5253

5354
public List<Host> allocateTo(VirtualMachineProfile<?extends VirtualMachine> vmProfile, DeploymentPlan plan, Type type, ExcludeList avoid, int returnUpTo);
55+
56+
/**
57+
* Determines which physical hosts are suitable to
58+
* allocate the guest virtual machines on
59+
*
60+
* @param VirtualMachineProfile vmProfile
61+
* @param DeploymentPlan plan
62+
* @param Type type
63+
* @param ExcludeList avoid
64+
* @param int returnUpTo (use -1 to return all possible hosts)
65+
* @param boolean considerReservedCapacity (default should be true, set to false if host capacity calculation should not look at reserved capacity)
66+
* @return List<Host> List of hosts that are suitable for VM allocation
67+
**/
68+
69+
public List<Host> allocateTo(VirtualMachineProfile<?extends VirtualMachine> vmProfile, DeploymentPlan plan, Type type, ExcludeList avoid, int returnUpTo, boolean considerReservedCapacity);
70+
71+
72+
public static int RETURN_UPTO_ALL = -1;
5473

5574
}

‎server/src/com/cloud/agent/manager/allocator/impl/FirstFitAllocator.java‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,17 @@ public class FirstFitAllocator implements HostAllocator {
8282
protected String _allocationAlgorithm = "random";
8383
@Inject CapacityManager _capacityMgr;
8484

85+
8586
@Override
8687
public List<Host> allocateTo(VirtualMachineProfile<? extends VirtualMachine> vmProfile, DeploymentPlan plan, Type type,
8788
ExcludeList avoid, int returnUpTo) {
88-
89-
long dcId = plan.getDataCenterId();
89+
return allocateTo(vmProfile, plan, type, avoid, returnUpTo, true);
90+
}
91+
92+
@Override
93+
public List<Host> allocateTo(VirtualMachineProfile<? extends VirtualMachine> vmProfile, DeploymentPlan plan, Type type, ExcludeList avoid, int returnUpTo, boolean considerReservedCapacity) {
94+
95+
long dcId = plan.getDataCenterId();
9096
Long podId = plan.getPodId();
9197
Long clusterId = plan.getClusterId();
9298
ServiceOffering offering = vmProfile.getServiceOffering();
@@ -150,10 +156,10 @@ public List<Host> allocateTo(VirtualMachineProfile<? extends VirtualMachine> vmP
150156

151157
}
152158

153-
return allocateTo(offering, template, avoid, clusterHosts, returnUpTo);
159+
return allocateTo(offering, template, avoid, clusterHosts, returnUpTo, considerReservedCapacity);
154160
}
155161

156-
protected List<Host> allocateTo(ServiceOffering offering, VMTemplateVO template, ExcludeList avoid, List<HostVO> hosts, int returnUpTo) {
162+
protected List<Host> allocateTo(ServiceOffering offering, VMTemplateVO template, ExcludeList avoid, List<HostVO> hosts, int returnUpTo, boolean considerReservedCapacity) {
157163
if (_allocationAlgorithm.equals("random")) {
158164
// Shuffle this so that we don't check the hosts in the same order.
159165
Collections.shuffle(hosts);
@@ -208,7 +214,7 @@ protected List<Host> allocateTo(ServiceOffering offering, VMTemplateVO template,
208214
boolean numCpusGood = host.getCpus().intValue() >= offering.getCpu();
209215
int cpu_requested = offering.getCpu() * offering.getSpeed();
210216
long ram_requested = offering.getRamSize() * 1024L * 1024L;
211-
boolean hostHasCapacity = _capacityMgr.checkIfHostHasCapacity(host.getId(), cpu_requested, ram_requested, false, _factor);
217+
boolean hostHasCapacity = _capacityMgr.checkIfHostHasCapacity(host.getId(), cpu_requested, ram_requested, false, _factor, considerReservedCapacity);
212218

213219
if (numCpusGood && hostHasCapacity) {
214220
if (s_logger.isDebugEnabled()) {
@@ -382,5 +388,4 @@ public boolean stop() {
382388
return true;
383389
}
384390

385-
386391
}

‎server/src/com/cloud/agent/manager/allocator/impl/RandomAllocator.java‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,19 @@
2121
import java.util.Collections;
2222
import java.util.List;
2323
import java.util.Map;
24-
import java.util.Set;
2524

2625
import javax.ejb.Local;
2726

2827
import org.apache.log4j.Logger;
29-
import com.cloud.host.HostVO;
30-
import com.cloud.host.Host.Type;
28+
3129
import com.cloud.agent.manager.allocator.HostAllocator;
32-
import com.cloud.dc.DataCenterVO;
33-
import com.cloud.dc.HostPodVO;
3430
import com.cloud.deploy.DeploymentPlan;
3531
import com.cloud.deploy.DeploymentPlanner.ExcludeList;
3632
import com.cloud.host.Host;
33+
import com.cloud.host.Host.Type;
34+
import com.cloud.host.HostVO;
3735
import com.cloud.host.dao.HostDao;
3836
import com.cloud.offering.ServiceOffering;
39-
import com.cloud.storage.VMTemplateVO;
4037
import com.cloud.uservm.UserVm;
4138
import com.cloud.utils.component.ComponentLocator;
4239
import com.cloud.vm.VirtualMachine;
@@ -50,7 +47,13 @@ public class RandomAllocator implements HostAllocator {
5047

5148
@Override
5249
public List<Host> allocateTo(VirtualMachineProfile<? extends VirtualMachine> vmProfile, DeploymentPlan plan, Type type,
53-
ExcludeList avoid, int returnUpTo) {
50+
ExcludeList avoid, int returnUpTo) {
51+
return allocateTo(vmProfile, plan, type, avoid, returnUpTo, true);
52+
}
53+
54+
@Override
55+
public List<Host> allocateTo(VirtualMachineProfile<? extends VirtualMachine> vmProfile, DeploymentPlan plan, Type type,
56+
ExcludeList avoid, int returnUpTo, boolean considerReservedCapacity) {
5457

5558
long dcId = plan.getDataCenterId();
5659
Long podId = plan.getPodId();

‎server/src/com/cloud/agent/manager/allocator/impl/TestingAllocator.java‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,13 @@ public class TestingAllocator implements HostAllocator {
4949

5050
@Override
5151
public List<Host> allocateTo(VirtualMachineProfile<? extends VirtualMachine> vmProfile, DeploymentPlan plan, Type type,
52-
ExcludeList avoid, int returnUpTo) {
52+
ExcludeList avoid, int returnUpTo) {
53+
return allocateTo(vmProfile, plan, type, avoid, returnUpTo, true);
54+
}
55+
56+
@Override
57+
public List<Host> allocateTo(VirtualMachineProfile<? extends VirtualMachine> vmProfile, DeploymentPlan plan, Type type,
58+
ExcludeList avoid, int returnUpTo, boolean considerReservedCapacity) {
5359
List<Host> availableHosts = new ArrayList<Host>();
5460
Host host = null;
5561
if (type == Host.Type.Routing && _routingHost != null) {

‎server/src/com/cloud/capacity/CapacityManager.java‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,15 @@ public interface CapacityManager extends Manager {
3232

3333
void allocateVmCapacity(VirtualMachine vm, boolean fromLastHost);
3434

35-
boolean checkIfHostHasCapacity(long hostId, Integer cpu, long ram, boolean checkFromReservedCapacity, float cpuOverprovisioningFactor);
36-
35+
/**
36+
* @param hostId Id of the host to check capacity
37+
* @param cpu required CPU
38+
* @param ram required RAM
39+
* @param checkFromReservedCapacity set to true if ONLY reserved capacity of the host should be looked at
40+
* @param cpuOverprovisioningFactor factor to apply to the actual host cpu
41+
* @param considerReservedCapacity (default should be true, set to false if host capacity calculation should not look at reserved capacity at all)
42+
*/
43+
boolean checkIfHostHasCapacity(long hostId, Integer cpu, long ram, boolean checkFromReservedCapacity, float cpuOverprovisioningFactor, boolean considerReservedCapacity);
44+
3745
void updateCapacityForHost(HostVO host);
3846
}

‎server/src/com/cloud/capacity/CapacityManagerImpl.java‎

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ public void allocateVmCapacity(VirtualMachine vm, boolean fromLastHost) {
293293
}
294294

295295
@Override
296-
public boolean checkIfHostHasCapacity(long hostId, Integer cpu, long ram, boolean checkFromReservedCapacity, float cpuOverprovisioningFactor) {
296+
public boolean checkIfHostHasCapacity(long hostId, Integer cpu, long ram, boolean checkFromReservedCapacity, float cpuOverprovisioningFactor, boolean considerReservedCapacity) {
297297
boolean hasCapacity = false;
298298

299299
if (s_logger.isDebugEnabled()) {
@@ -352,16 +352,27 @@ public boolean checkIfHostHasCapacity(long hostId, Integer cpu, long ram, boolea
352352
failureReason = "Host does not have enough reserved CPU available";
353353
}
354354
} else {
355-
long freeCpu = totalCpu - (reservedCpu + usedCpu);
356-
long freeMem = totalMem - (reservedMem + usedMem);
355+
356+
long reservedCpuValueToUse = reservedCpu;
357+
long reservedMemValueToUse = reservedMem;
358+
359+
if(!considerReservedCapacity){
360+
if (s_logger.isDebugEnabled()) {
361+
s_logger.debug("considerReservedCapacity is" + considerReservedCapacity + " , not considering reserved capacity for calculating free capacity");
362+
}
363+
reservedCpuValueToUse = 0;
364+
reservedMemValueToUse = 0;
365+
}
366+
long freeCpu = totalCpu - (reservedCpuValueToUse + usedCpu);
367+
long freeMem = totalMem - (reservedMemValueToUse + usedMem);
357368

358369
if (s_logger.isDebugEnabled()) {
359370
s_logger.debug("Free CPU: " + freeCpu + " , Requested CPU: " + cpu);
360371
s_logger.debug("Free RAM: " + freeMem + " , Requested RAM: " + ram);
361372
}
362373
/* alloc from free resource */
363-
if ((reservedCpu + usedCpu + cpu <= totalCpu)) {
364-
if ((reservedMem + usedMem + ram <= totalMem)) {
374+
if ((reservedCpuValueToUse + usedCpu + cpu <= totalCpu)) {
375+
if ((reservedMemValueToUse + usedMem + ram <= totalMem)) {
365376
hasCapacity = true;
366377
} else {
367378
failureReason = "Host does not have enough RAM available";
@@ -378,10 +389,10 @@ public boolean checkIfHostHasCapacity(long hostId, Integer cpu, long ram, boolea
378389

379390
s_logger.debug("STATS: Can alloc CPU from host: " + hostId + ", used: " + usedCpu + ", reserved: " + reservedCpu + ", actual total: "
380391
+ actualTotalCpu + ", total with overprovisioning: " + totalCpu + "; requested cpu:" + cpu + ",alloc_from_last_host?:"
381-
+ checkFromReservedCapacity);
392+
+ checkFromReservedCapacity + " ,considerReservedCapacity?: "+considerReservedCapacity);
382393

383394
s_logger.debug("STATS: Can alloc MEM from host: " + hostId + ", used: " + usedMem + ", reserved: " + reservedMem + ", total: " + totalMem
384-
+ "; requested mem: " + ram + ",alloc_from_last_host?:" + checkFromReservedCapacity);
395+
+ "; requested mem: " + ram + ",alloc_from_last_host?:" + checkFromReservedCapacity+ " ,considerReservedCapacity?: "+considerReservedCapacity);
385396
} else {
386397

387398
if (checkFromReservedCapacity) {
@@ -390,7 +401,7 @@ public boolean checkIfHostHasCapacity(long hostId, Integer cpu, long ram, boolea
390401
} else {
391402
s_logger.debug("STATS: Failed to alloc resource from host: " + hostId + " reservedCpu: " + reservedCpu + ", used cpu: " + usedCpu
392403
+ ", requested cpu: " + cpu + ", actual total cpu: " + actualTotalCpu + ", total cpu with overprovisioning: " + totalCpu
393-
+ ", reservedMem: " + reservedMem + ", used Mem: " + usedMem + ", requested mem: " + ram + ", total Mem:" + totalMem);
404+
+ ", reservedMem: " + reservedMem + ", used Mem: " + usedMem + ", requested mem: " + ram + ", total Mem:" + totalMem+ " ,considerReservedCapacity?: "+considerReservedCapacity);
394405
}
395406

396407
if (s_logger.isDebugEnabled()) {

0 commit comments

Comments
 (0)