Skip to content

Commit a31449b

Browse files
GaOrtigaGabrielhsato03JoaoJandre
authored
Allow altering only either CPU or memory during VM live scale (#8234)
* allow change only one parameter during live scale * Update server/src/main/java/com/cloud/vm/UserVmManagerImpl.java Co-authored-by: sato03 <henriquesato2003@gmail.com> * apply change method name * Update server/src/main/java/com/cloud/vm/UserVmManagerImpl.java Co-authored-by: João Jandre <48719461+JoaoJandre@users.noreply.github.com> --------- Co-authored-by: Gabriel <gabriel.fernandes@scclouds.com.br> Co-authored-by: sato03 <henriquesato2003@gmail.com> Co-authored-by: João Jandre <48719461+JoaoJandre@users.noreply.github.com>
1 parent 4a0ca20 commit a31449b

File tree

2 files changed

+115
-2
lines changed

2 files changed

+115
-2
lines changed

server/src/main/java/com/cloud/vm/UserVmManagerImpl.java

+39-2
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir
399399
@Inject
400400
private VMTemplateZoneDao _templateZoneDao;
401401
@Inject
402-
private TemplateDataStoreDao _templateStoreDao;
402+
protected TemplateDataStoreDao _templateStoreDao;
403403
@Inject
404404
private DomainDao _domainDao;
405405
@Inject
@@ -1226,6 +1226,39 @@ public UserVm upgradeVirtualMachine(UpgradeVMCmd cmd) throws ResourceAllocationE
12261226
return userVm;
12271227
}
12281228

1229+
/**
1230+
Updates the instance details map with the current values of the instance for the CPU speed, memory, and CPU number if they have not been specified.
1231+
@param details Map containing the instance details.
1232+
@param vmInstance The virtual machine instance.
1233+
@param newServiceOfferingId The ID of the new service offering.
1234+
*/
1235+
1236+
protected void updateInstanceDetails (Map<String, String> details, VirtualMachine vmInstance, Long newServiceOfferingId) {
1237+
ServiceOfferingVO currentServiceOffering = serviceOfferingDao.findByIdIncludingRemoved(vmInstance.getId(), vmInstance.getServiceOfferingId());
1238+
ServiceOfferingVO newServiceOffering = serviceOfferingDao.findById(newServiceOfferingId);
1239+
updateInstanceDetailsKeepCurrentValueIfNull(newServiceOffering.getSpeed(), details, VmDetailConstants.CPU_SPEED, currentServiceOffering.getSpeed());
1240+
updateInstanceDetailsKeepCurrentValueIfNull(newServiceOffering.getRamSize(), details, VmDetailConstants.MEMORY, currentServiceOffering.getRamSize());
1241+
updateInstanceDetailsKeepCurrentValueIfNull(newServiceOffering.getCpu(), details, VmDetailConstants.CPU_NUMBER, currentServiceOffering.getCpu());
1242+
}
1243+
1244+
/**
1245+
* Updates a specific instance detail with the current instance value if the new value is null.
1246+
*
1247+
* @param newValue the new value to be set
1248+
* @param details a map of instance details
1249+
* @param detailsConstant the name of the detail constant to be updated
1250+
* @param currentValue the current value of the detail constant
1251+
*/
1252+
1253+
protected void updateInstanceDetailsKeepCurrentValueIfNull(Integer newValue, Map<String, String> details, String detailsConstant, Integer currentValue) {
1254+
if (newValue == null && details.get(detailsConstant) == null) {
1255+
String currentValueString = String.valueOf(currentValue);
1256+
logger.debug("{} was not specified, keeping the current value: {}.", detailsConstant, currentValueString);
1257+
details.put(detailsConstant, currentValueString);
1258+
}
1259+
}
1260+
1261+
12291262
private void validateOfferingMaxResource(ServiceOfferingVO offering) {
12301263
Integer maxCPUCores = ConfigurationManagerImpl.VM_SERVICE_OFFERING_MAX_CPU_CORES.value() == 0 ? Integer.MAX_VALUE: ConfigurationManagerImpl.VM_SERVICE_OFFERING_MAX_CPU_CORES.value();
12311264
if (offering.getCpu() > maxCPUCores) {
@@ -1891,7 +1924,11 @@ public UserVm upgradeVirtualMachine(ScaleVMCmd cmd) throws ResourceUnavailableEx
18911924
}
18921925
CallContext.current().setEventDetails("Vm Id: " + vm.getUuid());
18931926

1894-
boolean result = upgradeVirtualMachine(vmId, newServiceOfferingId, cmd.getDetails());
1927+
Map<String, String> cmdDetails = cmd.getDetails();
1928+
1929+
updateInstanceDetails(cmdDetails, vm, newServiceOfferingId);
1930+
1931+
boolean result = upgradeVirtualMachine(vmId, newServiceOfferingId, cmdDetails);
18951932
if (result) {
18961933
UserVmVO vmInstance = _vmDao.findById(vmId);
18971934
if (vmInstance.getState().equals(State.Stopped)) {

server/src/test/java/com/cloud/vm/UserVmManagerImplTest.java

+76
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,9 @@ public class UserVmManagerImplTest {
267267
@Mock
268268
ServiceOfferingJoinDao serviceOfferingJoinDao;
269269

270+
@Mock
271+
private VMInstanceVO vmInstanceMock;
272+
270273
private static final long vmId = 1l;
271274
private static final long zoneId = 2L;
272275
private static final long accountId = 3L;
@@ -277,6 +280,8 @@ public class UserVmManagerImplTest {
277280

278281
private Map<String, String> customParameters = new HashMap<>();
279282

283+
String[] detailsConstants = {VmDetailConstants.MEMORY, VmDetailConstants.CPU_NUMBER, VmDetailConstants.CPU_SPEED};
284+
280285
private DiskOfferingVO smallerDisdkOffering = prepareDiskOffering(5l * GiB_TO_BYTES, 1l, 1L, 2L);
281286
private DiskOfferingVO largerDisdkOffering = prepareDiskOffering(10l * GiB_TO_BYTES, 2l, 10L, 20L);
282287

@@ -293,6 +298,10 @@ public void beforeTest() {
293298
CallContext.register(callerUser, callerAccount);
294299

295300
customParameters.put(VmDetailConstants.ROOT_DISK_SIZE, "123");
301+
customParameters.put(VmDetailConstants.MEMORY, "2048");
302+
customParameters.put(VmDetailConstants.CPU_NUMBER, "4");
303+
customParameters.put(VmDetailConstants.CPU_SPEED, "1000");
304+
296305
lenient().doNothing().when(resourceLimitMgr).incrementResourceCount(anyLong(), any(Resource.ResourceType.class));
297306
lenient().doNothing().when(resourceLimitMgr).decrementResourceCount(anyLong(), any(Resource.ResourceType.class), anyLong());
298307

@@ -1443,4 +1452,71 @@ public void testRestoreVirtualMachineWithVMSnapshots() throws ResourceUnavailabl
14431452

14441453
userVmManagerImpl.restoreVirtualMachine(accountMock, vmId, newTemplateId);
14451454
}
1455+
1456+
@Test
1457+
public void updateInstanceDetailsKeepCurrentValueIfNullTestDetailsConstantIsNotNullDoNothing() {
1458+
int currentValue = 123;
1459+
1460+
for (String detailsConstant : detailsConstants) {
1461+
userVmManagerImpl.updateInstanceDetailsKeepCurrentValueIfNull(null, customParameters, detailsConstant, currentValue);
1462+
}
1463+
1464+
Assert.assertEquals(customParameters.get(VmDetailConstants.MEMORY), "2048");
1465+
Assert.assertEquals(customParameters.get(VmDetailConstants.CPU_NUMBER), "4");
1466+
Assert.assertEquals(customParameters.get(VmDetailConstants.CPU_SPEED), "1000");
1467+
}
1468+
1469+
@Test
1470+
public void updateInstanceDetailsKeepCurrentValueIfNullTestNewValueIsNotNullDoNothing() {
1471+
Map<String, String> details = new HashMap<>();
1472+
int currentValue = 123;
1473+
1474+
for (String detailsConstant : detailsConstants) {
1475+
userVmManagerImpl.updateInstanceDetailsKeepCurrentValueIfNull(321, details, detailsConstant, currentValue);
1476+
}
1477+
1478+
Assert.assertNull(details.get(VmDetailConstants.MEMORY));
1479+
Assert.assertNull(details.get(VmDetailConstants.CPU_NUMBER));
1480+
Assert.assertNull(details.get(VmDetailConstants.CPU_SPEED));
1481+
}
1482+
1483+
@Test
1484+
public void updateInstanceDetailsKeepCurrentValueIfNullTestBothValuesAreNullKeepCurrentValue() {
1485+
Map<String, String> details = new HashMap<>();
1486+
int currentValue = 123;
1487+
1488+
for (String detailsConstant : detailsConstants) {
1489+
userVmManagerImpl.updateInstanceDetailsKeepCurrentValueIfNull(null, details, detailsConstant, currentValue);
1490+
}
1491+
1492+
Assert.assertEquals(details.get(VmDetailConstants.MEMORY), String.valueOf(currentValue));
1493+
Assert.assertEquals(details.get(VmDetailConstants.CPU_NUMBER), String.valueOf(currentValue));
1494+
Assert.assertEquals(details.get(VmDetailConstants.CPU_SPEED),String.valueOf(currentValue));
1495+
}
1496+
1497+
@Test
1498+
public void updateInstanceDetailsKeepCurrentValueIfNullTestNeitherValueIsNullDoNothing() {
1499+
int currentValue = 123;
1500+
1501+
for (String detailsConstant : detailsConstants) {
1502+
userVmManagerImpl.updateInstanceDetailsKeepCurrentValueIfNull(321, customParameters, detailsConstant, currentValue);
1503+
}
1504+
1505+
Assert.assertEquals(customParameters.get(VmDetailConstants.MEMORY), "2048");
1506+
Assert.assertEquals(customParameters.get(VmDetailConstants.CPU_NUMBER), "4");
1507+
Assert.assertEquals(customParameters.get(VmDetailConstants.CPU_SPEED),"1000");
1508+
}
1509+
1510+
@Test
1511+
public void updateInstanceDetailsTestAllConstantsAreUpdated() {
1512+
Mockito.doReturn(serviceOffering).when(_serviceOfferingDao).findById(Mockito.anyLong());
1513+
Mockito.doReturn(1L).when(vmInstanceMock).getId();
1514+
Mockito.doReturn(1L).when(vmInstanceMock).getServiceOfferingId();
1515+
Mockito.doReturn(serviceOffering).when(_serviceOfferingDao).findByIdIncludingRemoved(Mockito.anyLong(), Mockito.anyLong());
1516+
userVmManagerImpl.updateInstanceDetails(null, vmInstanceMock, 0l);
1517+
1518+
Mockito.verify(userVmManagerImpl).updateInstanceDetailsKeepCurrentValueIfNull(Mockito.any(), Mockito.any(), Mockito.eq(VmDetailConstants.CPU_SPEED), Mockito.any());
1519+
Mockito.verify(userVmManagerImpl).updateInstanceDetailsKeepCurrentValueIfNull(Mockito.any(), Mockito.any(), Mockito.eq(VmDetailConstants.MEMORY), Mockito.any());
1520+
Mockito.verify(userVmManagerImpl).updateInstanceDetailsKeepCurrentValueIfNull(Mockito.any(), Mockito.any(), Mockito.eq(VmDetailConstants.CPU_NUMBER), Mockito.any());
1521+
}
14461522
}

0 commit comments

Comments
 (0)