Skip to content

Commit cfdb33a

Browse files
authored
Fixup resource limit checks (#8935)
1 parent 8923110 commit cfdb33a

File tree

10 files changed

+417
-174
lines changed

10 files changed

+417
-174
lines changed

api/src/main/java/com/cloud/user/ResourceLimitService.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,13 +239,35 @@ public interface ResourceLimitService {
239239
void updateTaggedResourceLimitsAndCountsForAccounts(List<AccountResponse> responses, String tag);
240240
void updateTaggedResourceLimitsAndCountsForDomains(List<DomainResponse> responses, String tag);
241241
void checkVolumeResourceLimit(Account owner, Boolean display, Long size, DiskOffering diskOffering) throws ResourceAllocationException;
242+
243+
void checkVolumeResourceLimitForDiskOfferingChange(Account owner, Boolean display, Long currentSize, Long newSize,
244+
DiskOffering currentOffering, DiskOffering newOffering) throws ResourceAllocationException;
245+
242246
void incrementVolumeResourceCount(long accountId, Boolean display, Long size, DiskOffering diskOffering);
243247
void decrementVolumeResourceCount(long accountId, Boolean display, Long size, DiskOffering diskOffering);
248+
249+
void updateVmResourceCountForTemplateChange(long accountId, Boolean display, ServiceOffering offering, VirtualMachineTemplate currentTemplate, VirtualMachineTemplate newTemplate);
250+
251+
void updateVmResourceCountForServiceOfferingChange(long accountId, Boolean display, Long currentCpu, Long newCpu, Long currentMemory,
252+
Long newMemory,
253+
ServiceOffering currentOffering, ServiceOffering newOffering,
254+
VirtualMachineTemplate template);
255+
256+
void updateVolumeResourceCountForDiskOfferingChange(long accountId, Boolean display, Long currentSize, Long newSize,
257+
DiskOffering currentDiskOffering, DiskOffering newDiskOffering);
258+
244259
void incrementVolumePrimaryStorageResourceCount(long accountId, Boolean display, Long size, DiskOffering diskOffering);
245260
void decrementVolumePrimaryStorageResourceCount(long accountId, Boolean display, Long size, DiskOffering diskOffering);
246261
void checkVmResourceLimit(Account owner, Boolean display, ServiceOffering serviceOffering, VirtualMachineTemplate template) throws ResourceAllocationException;
247262
void incrementVmResourceCount(long accountId, Boolean display, ServiceOffering serviceOffering, VirtualMachineTemplate template);
248263
void decrementVmResourceCount(long accountId, Boolean display, ServiceOffering serviceOffering, VirtualMachineTemplate template);
264+
265+
void checkVmResourceLimitsForServiceOfferingChange(Account owner, Boolean display, Long currentCpu, Long newCpu,
266+
Long currentMemory, Long newMemory, ServiceOffering currentOffering, ServiceOffering newOffering, VirtualMachineTemplate template) throws ResourceAllocationException;
267+
268+
void checkVmResourceLimitsForTemplateChange(Account owner, Boolean display, ServiceOffering offering,
269+
VirtualMachineTemplate currentTemplate, VirtualMachineTemplate newTemplate) throws ResourceAllocationException;
270+
249271
void checkVmCpuResourceLimit(Account owner, Boolean display, ServiceOffering serviceOffering, VirtualMachineTemplate template, Long cpu) throws ResourceAllocationException;
250272
void incrementVmCpuResourceCount(long accountId, Boolean display, ServiceOffering serviceOffering, VirtualMachineTemplate template, Long cpu);
251273
void decrementVmCpuResourceCount(long accountId, Boolean display, ServiceOffering serviceOffering, VirtualMachineTemplate template, Long cpu);

api/src/main/java/com/cloud/vm/UserVmService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public interface UserVmService {
110110
UserVm startVirtualMachine(StartVMCmd cmd) throws StorageUnavailableException, ExecutionException, ConcurrentOperationException, ResourceUnavailableException,
111111
InsufficientCapacityException, ResourceAllocationException;
112112

113-
UserVm rebootVirtualMachine(RebootVMCmd cmd) throws InsufficientCapacityException, ResourceUnavailableException;
113+
UserVm rebootVirtualMachine(RebootVMCmd cmd) throws InsufficientCapacityException, ResourceUnavailableException, ResourceAllocationException;
114114

115115
UserVm updateVirtualMachine(UpdateVMCmd cmd) throws ResourceUnavailableException, InsufficientCapacityException;
116116

@@ -490,7 +490,7 @@ UserVm moveVMToUser(AssignVMCmd moveUserVMCmd) throws ResourceAllocationExceptio
490490

491491
VirtualMachine vmStorageMigration(Long vmId, Map<String, String> volumeToPool);
492492

493-
UserVm restoreVM(RestoreVMCmd cmd) throws InsufficientCapacityException, ResourceUnavailableException;
493+
UserVm restoreVM(RestoreVMCmd cmd) throws InsufficientCapacityException, ResourceUnavailableException, ResourceAllocationException;
494494

495495
UserVm restoreVirtualMachine(Account caller, long vmId, Long newTemplateId, Long rootDiskOfferingId, boolean expunge, Map<String, String> details) throws InsufficientCapacityException, ResourceUnavailableException;
496496

api/src/main/java/org/apache/cloudstack/api/command/user/vm/RebootVMCmd.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.apache.cloudstack.api.command.user.vm;
1818

1919

20+
import com.cloud.exception.ResourceAllocationException;
2021
import org.apache.cloudstack.acl.SecurityChecker.AccessType;
2122
import org.apache.cloudstack.api.ACL;
2223
import org.apache.cloudstack.api.APICommand;
@@ -113,7 +114,7 @@ public Long getApiResourceId() {
113114
}
114115

115116
@Override
116-
public void execute() throws ResourceUnavailableException, InsufficientCapacityException {
117+
public void execute() throws ResourceUnavailableException, InsufficientCapacityException, ResourceAllocationException {
117118
CallContext.current().setEventDetails("Vm Id: " + this._uuidMgr.getUuid(VirtualMachine.class, getId()));
118119
UserVm result;
119120
result = _userVmService.rebootVirtualMachine(this);

engine/api/src/main/java/com/cloud/vm/VirtualMachineManager.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.List;
2323
import java.util.Map;
2424

25+
import com.cloud.exception.ResourceAllocationException;
2526
import org.apache.cloudstack.context.CallContext;
2627
import org.apache.cloudstack.framework.config.ConfigKey;
2728

@@ -254,7 +255,7 @@ static String getHypervisorHostname(String name) {
254255
*/
255256
boolean unmanage(String vmUuid);
256257

257-
UserVm restoreVirtualMachine(long vmId, Long newTemplateId, Long rootDiskOfferingId, boolean expunge, Map<String, String> details) throws ResourceUnavailableException, InsufficientCapacityException;
258+
UserVm restoreVirtualMachine(long vmId, Long newTemplateId, Long rootDiskOfferingId, boolean expunge, Map<String, String> details) throws ResourceUnavailableException, InsufficientCapacityException, ResourceAllocationException;
258259

259260
boolean checkIfVmHasClusterWideVolumes(Long vmId);
260261

server/src/main/java/com/cloud/api/query/QueryManagerImpl.java

Lines changed: 24 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2978,7 +2978,7 @@ private ListResponse<StoragePoolResponse> createStoragesPoolResponse(Pair<List<S
29782978
ListResponse<StoragePoolResponse> response = new ListResponse<>();
29792979

29802980
List<StoragePoolResponse> poolResponses = ViewResponseHelper.createStoragePoolResponse(storagePools.first().toArray(new StoragePoolJoinVO[storagePools.first().size()]));
2981-
Map<String, Long> poolUuidToIdMap = storagePools.first().stream().collect(Collectors.toMap(StoragePoolJoinVO::getUuid, StoragePoolJoinVO::getId));
2981+
Map<String, Long> poolUuidToIdMap = storagePools.first().stream().collect(Collectors.toMap(StoragePoolJoinVO::getUuid, StoragePoolJoinVO::getId, (a, b) -> a));
29822982
for (StoragePoolResponse poolResponse : poolResponses) {
29832983
DataStore store = dataStoreManager.getPrimaryDataStore(poolResponse.getId());
29842984
if (store != null) {
@@ -3821,9 +3821,8 @@ private Pair<List<Long>, Integer> searchForServiceOfferingIdsAndCount(ListServic
38213821
List<String> storageTags = com.cloud.utils.StringUtils.csvTagsToList(diskOffering.getTags());
38223822
if (!storageTags.isEmpty() && VolumeApiServiceImpl.MatchStoragePoolTagsWithDiskOffering.value()) {
38233823
for (String tag : storageTags) {
3824-
diskOfferingSearch.and(tag, diskOfferingSearch.entity().getTags(), Op.EQ);
3824+
diskOfferingSearch.and("storageTag" + tag, diskOfferingSearch.entity().getTags(), Op.FIND_IN_SET);
38253825
}
3826-
diskOfferingSearch.done();
38273826
}
38283827
}
38293828

@@ -3935,18 +3934,24 @@ private Pair<List<Long>, Integer> searchForServiceOfferingIdsAndCount(ListServic
39353934
srvOffrDomainDetailSearch.entity().getName(), serviceOfferingSearch.entity().setString(ApiConstants.DOMAIN_ID));
39363935
}
39373936

3937+
List<String> hostTags = getHostTagsFromTemplateForServiceOfferingsListing(caller, templateId);
39383938
if (currentVmOffering != null) {
3939-
List<String> hostTags = com.cloud.utils.StringUtils.csvTagsToList(currentVmOffering.getHostTag());
3940-
if (!hostTags.isEmpty()) {
3941-
3942-
serviceOfferingSearch.and().op("hostTag", serviceOfferingSearch.entity().getHostTag(), Op.NULL);
3943-
serviceOfferingSearch.or().op();
3939+
hostTags.addAll(com.cloud.utils.StringUtils.csvTagsToList(currentVmOffering.getHostTag()));
3940+
}
39443941

3945-
for(String tag : hostTags) {
3946-
serviceOfferingSearch.and(tag, serviceOfferingSearch.entity().getHostTag(), Op.EQ);
3942+
if (!hostTags.isEmpty()) {
3943+
serviceOfferingSearch.and().op("hostTag", serviceOfferingSearch.entity().getHostTag(), Op.NULL);
3944+
serviceOfferingSearch.or();
3945+
boolean flag = true;
3946+
for(String tag : hostTags) {
3947+
if (flag) {
3948+
flag = false;
3949+
serviceOfferingSearch.op("hostTag" + tag, serviceOfferingSearch.entity().getHostTag(), Op.FIND_IN_SET);
3950+
} else {
3951+
serviceOfferingSearch.and("hostTag" + tag, serviceOfferingSearch.entity().getHostTag(), Op.FIND_IN_SET);
39473952
}
3948-
serviceOfferingSearch.cp().cp().done();
39493953
}
3954+
serviceOfferingSearch.cp().cp();
39503955
}
39513956

39523957
SearchCriteria<ServiceOfferingVO> sc = serviceOfferingSearch.create();
@@ -4063,41 +4068,19 @@ private Pair<List<Long>, Integer> searchForServiceOfferingIdsAndCount(ListServic
40634068
sc.setJoinParameters("domainDetailSearchNormalUser", "domainIdIN", domainIds.toArray());
40644069
}
40654070

4066-
List<String> hostTags = getHostTagsFromTemplateForServiceOfferingsListing(caller, templateId);
4067-
4068-
if (currentVmOffering != null) {
4069-
4070-
if (diskOffering != null) {
4071-
List<String> storageTags = com.cloud.utils.StringUtils.csvTagsToList(diskOffering.getTags());
4072-
if (!storageTags.isEmpty() && VolumeApiServiceImpl.MatchStoragePoolTagsWithDiskOffering.value()) {
4073-
for(String tag : storageTags) {
4074-
sc.setJoinParameters("diskOfferingSearch", tag, tag);
4075-
}
4071+
if (diskOffering != null) {
4072+
List<String> storageTags = com.cloud.utils.StringUtils.csvTagsToList(diskOffering.getTags());
4073+
if (!storageTags.isEmpty() && VolumeApiServiceImpl.MatchStoragePoolTagsWithDiskOffering.value()) {
4074+
for (String tag : storageTags) {
4075+
sc.setJoinParameters("diskOfferingSearch", "storageTag" + tag, tag);
40764076
}
40774077
}
4078-
4079-
List<String> offeringHostTags = com.cloud.utils.StringUtils.csvTagsToList(currentVmOffering.getHostTag());
4080-
if (!offeringHostTags.isEmpty()) {
4081-
hostTags.addAll(offeringHostTags);
4082-
}
40834078
}
4084-
if (CollectionUtils.isNotEmpty(hostTags)) {
4085-
SearchBuilder<ServiceOfferingJoinVO> hostTagsSearchBuilder = _srvOfferingJoinDao.createSearchBuilder();
4086-
for(String tag : hostTags) {
4087-
hostTagsSearchBuilder.and(tag, hostTagsSearchBuilder.entity().getHostTag(), Op.FIND_IN_SET);
4088-
}
4089-
hostTagsSearchBuilder.done();
40904079

4091-
SearchCriteria<ServiceOfferingJoinVO> hostTagsSearchCriteria = hostTagsSearchBuilder.create();
4092-
for(String tag : hostTags) {
4093-
hostTagsSearchCriteria.setParameters(tag, tag);
4080+
if (CollectionUtils.isNotEmpty(hostTags)) {
4081+
for (String tag : hostTags) {
4082+
sc.setParameters("hostTag" + tag, tag);
40944083
}
4095-
4096-
SearchCriteria<ServiceOfferingJoinVO> finalHostTagsSearchCriteria = _srvOfferingJoinDao.createSearchCriteria();
4097-
finalHostTagsSearchCriteria.addOr("hostTag", Op.NULL);
4098-
finalHostTagsSearchCriteria.addOr("hostTag", Op.SC, hostTagsSearchCriteria);
4099-
4100-
sc.addAnd("hostTagsConstraint", SearchCriteria.Op.SC, finalHostTagsSearchCriteria);
41014084
}
41024085

41034086
Pair<List<ServiceOfferingVO>, Integer> uniquePair = _srvOfferingDao.searchAndCount(sc, searchFilter);

0 commit comments

Comments
 (0)