Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/4.13' into 4.14
Browse files Browse the repository at this point in the history
Signed-off-by: Rohit Yadav <[email protected]>
  • Loading branch information
rohityadavcloud committed Jun 16, 2020
2 parents e94a54f + 5054766 commit 77947f2
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 108 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ void advanceReboot(String vmUuid, Map<VirtualMachineProfile.Param, Object> param
* @param serviceOfferingId
* @return
*/
boolean upgradeVmDb(long vmId, long serviceOfferingId);
boolean upgradeVmDb(long vmId, ServiceOffering newServiceOffering, ServiceOffering currentServiceOffering);

/**
* @param vm
Expand Down Expand Up @@ -204,7 +204,7 @@ NicProfile addVmToNetwork(VirtualMachine vm, Network network, NicProfile request
boolean replugNic(Network network, NicTO nic, VirtualMachineTO vm, ReservationContext context, DeployDestination dest) throws ConcurrentOperationException,
ResourceUnavailableException, InsufficientCapacityException;

VirtualMachine reConfigureVm(String vmUuid, ServiceOffering newServiceOffering, boolean sameHost) throws ResourceUnavailableException, ConcurrentOperationException,
VirtualMachine reConfigureVm(String vmUuid, ServiceOffering oldServiceOffering, ServiceOffering newServiceOffering, Map<String, String> customParameters, boolean sameHost) throws ResourceUnavailableException, ConcurrentOperationException,
InsufficientServerCapacityException;

void findHostAndMigrate(String vmUuid, Long newSvcOfferingId, DeploymentPlanner.ExcludeList excludeHostList) throws InsufficientCapacityException,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@
import javax.inject.Inject;
import javax.naming.ConfigurationException;

import com.cloud.network.dao.NetworkDetailVO;
import com.cloud.network.dao.NetworkDetailsDao;
import org.apache.cloudstack.affinity.dao.AffinityGroupVMMapDao;
import org.apache.cloudstack.api.ApiConstants;
import org.apache.cloudstack.api.command.admin.vm.MigrateVMCmd;
Expand Down Expand Up @@ -143,6 +141,7 @@
import com.cloud.deploy.DeploymentPlanningManager;
import com.cloud.event.EventTypes;
import com.cloud.event.UsageEventUtils;
import com.cloud.event.UsageEventVO;
import com.cloud.exception.AffinityConflictException;
import com.cloud.exception.AgentUnavailableException;
import com.cloud.exception.ConcurrentOperationException;
Expand All @@ -165,6 +164,8 @@
import com.cloud.network.Network;
import com.cloud.network.NetworkModel;
import com.cloud.network.dao.NetworkDao;
import com.cloud.network.dao.NetworkDetailVO;
import com.cloud.network.dao.NetworkDetailsDao;
import com.cloud.network.dao.NetworkVO;
import com.cloud.network.router.VirtualRouter;
import com.cloud.network.security.SecurityGroupManager;
Expand Down Expand Up @@ -239,6 +240,8 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac

private static final String VM_SYNC_ALERT_SUBJECT = "VM state sync alert";

@Inject
private UserVmManager _userVmMgr;
@Inject
private DataStoreManager dataStoreMgr;
@Inject
Expand Down Expand Up @@ -3482,13 +3485,20 @@ public void checkIfCanUpgrade(final VirtualMachine vmInstance, final ServiceOffe
}

@Override
public boolean upgradeVmDb(final long vmId, final long serviceOfferingId) {
final VMInstanceVO vmForUpdate = _vmDao.createForUpdate();
vmForUpdate.setServiceOfferingId(serviceOfferingId);
final ServiceOffering newSvcOff = _entityMgr.findById(ServiceOffering.class, serviceOfferingId);
public boolean upgradeVmDb(final long vmId, final ServiceOffering newServiceOffering, ServiceOffering currentServiceOffering) {

final VMInstanceVO vmForUpdate = _vmDao.findById(vmId);
vmForUpdate.setServiceOfferingId(newServiceOffering.getId());
final ServiceOffering newSvcOff = _entityMgr.findById(ServiceOffering.class, newServiceOffering.getId());
vmForUpdate.setHaEnabled(newSvcOff.isOfferHA());
vmForUpdate.setLimitCpuUse(newSvcOff.getLimitCpuUse());
vmForUpdate.setServiceOfferingId(newSvcOff.getId());
if (newServiceOffering.isDynamic()) {
saveCustomOfferingDetails(vmId, newServiceOffering);
}
if (currentServiceOffering.isDynamic() && !newServiceOffering.isDynamic()) {
removeCustomOfferingDetails(vmId);
}
return _vmDao.update(vmId, vmForUpdate);
}

Expand Down Expand Up @@ -4170,8 +4180,8 @@ public boolean unplugNic(final Network network, final NicTO nic, final VirtualMa
}

@Override
public VMInstanceVO reConfigureVm(final String vmUuid, final ServiceOffering oldServiceOffering,
final boolean reconfiguringOnExistingHost)
public VMInstanceVO reConfigureVm(final String vmUuid, final ServiceOffering oldServiceOffering, final ServiceOffering newServiceOffering,
Map<String, String> customParameters, final boolean reconfiguringOnExistingHost)
throws ResourceUnavailableException, InsufficientServerCapacityException, ConcurrentOperationException {

final AsyncJobExecutionContext jobContext = AsyncJobExecutionContext.getCurrentExecutionContext();
Expand All @@ -4181,14 +4191,14 @@ public VMInstanceVO reConfigureVm(final String vmUuid, final ServiceOffering old
final VirtualMachine vm = _vmDao.findByUuid(vmUuid);
placeHolder = createPlaceHolderWork(vm.getId());
try {
return orchestrateReConfigureVm(vmUuid, oldServiceOffering, reconfiguringOnExistingHost);
return orchestrateReConfigureVm(vmUuid, oldServiceOffering, newServiceOffering, reconfiguringOnExistingHost);
} finally {
if (placeHolder != null) {
_workJobDao.expunge(placeHolder.getId());
}
}
} else {
final Outcome<VirtualMachine> outcome = reconfigureVmThroughJobQueue(vmUuid, oldServiceOffering, reconfiguringOnExistingHost);
final Outcome<VirtualMachine> outcome = reconfigureVmThroughJobQueue(vmUuid, oldServiceOffering, newServiceOffering, customParameters, reconfiguringOnExistingHost);

VirtualMachine vm = null;
try {
Expand Down Expand Up @@ -4217,14 +4227,12 @@ public VMInstanceVO reConfigureVm(final String vmUuid, final ServiceOffering old
}
}

private VMInstanceVO orchestrateReConfigureVm(final String vmUuid, final ServiceOffering oldServiceOffering, final boolean reconfiguringOnExistingHost) throws ResourceUnavailableException,
ConcurrentOperationException {
private VMInstanceVO orchestrateReConfigureVm(final String vmUuid, final ServiceOffering oldServiceOffering, final ServiceOffering newServiceOffering,
final boolean reconfiguringOnExistingHost) throws ResourceUnavailableException, ConcurrentOperationException {
final VMInstanceVO vm = _vmDao.findByUuid(vmUuid);
upgradeVmDb(vm.getId(), newServiceOffering, oldServiceOffering);

final long newServiceofferingId = vm.getServiceOfferingId();
final ServiceOffering newServiceOffering = _offeringDao.findById(vm.getId(), newServiceofferingId);
final HostVO hostVo = _hostDao.findById(vm.getHostId());

final Float memoryOvercommitRatio = CapacityManager.MemOverprovisioningFactor.valueIn(hostVo.getClusterId());
final Float cpuOvercommitRatio = CapacityManager.CpuOverprovisioningFactor.valueIn(hostVo.getClusterId());
final long minMemory = (long)(newServiceOffering.getRamSize() / memoryOvercommitRatio);
Expand All @@ -4251,7 +4259,7 @@ private VMInstanceVO orchestrateReConfigureVm(final String vmUuid, final Service
if (reconfiguringOnExistingHost) {
vm.setServiceOfferingId(oldServiceOffering.getId());
_capacityMgr.releaseVmCapacity(vm, false, false, vm.getHostId()); //release the old capacity
vm.setServiceOfferingId(newServiceofferingId);
vm.setServiceOfferingId(newServiceOffering.getId());
_capacityMgr.allocateVmCapacity(vm, false); // lock the new capacity
}

Expand All @@ -4260,7 +4268,9 @@ private VMInstanceVO orchestrateReConfigureVm(final String vmUuid, final Service
s_logger.error("Unable to scale vm due to " + (reconfigureAnswer == null ? "" : reconfigureAnswer.getDetails()));
throw new CloudRuntimeException("Unable to scale vm due to " + (reconfigureAnswer == null ? "" : reconfigureAnswer.getDetails()));
}

if (vm.getType().equals(VirtualMachine.Type.User)) {
_userVmMgr.generateUsageEvent(vm, vm.isDisplayVm(), EventTypes.EVENT_VM_DYNAMIC_SCALE);
}
success = true;
} catch (final OperationTimedoutException e) {
throw new AgentUnavailableException("Operation timed out on reconfiguring " + vm, dstHostId);
Expand All @@ -4269,7 +4279,7 @@ private VMInstanceVO orchestrateReConfigureVm(final String vmUuid, final Service
} finally {
if (!success) {
_capacityMgr.releaseVmCapacity(vm, false, false, vm.getHostId()); // release the new capacity
vm.setServiceOfferingId(oldServiceOffering.getId());
upgradeVmDb(vm.getId(), oldServiceOffering, newServiceOffering); // rollback
_capacityMgr.allocateVmCapacity(vm, false); // allocate the old capacity
}
}
Expand All @@ -4278,6 +4288,33 @@ private VMInstanceVO orchestrateReConfigureVm(final String vmUuid, final Service

}

private void removeCustomOfferingDetails(long vmId) {
Map<String, String> details = userVmDetailsDao.listDetailsKeyPairs(vmId);
details.remove(UsageEventVO.DynamicParameters.cpuNumber.name());
details.remove(UsageEventVO.DynamicParameters.cpuSpeed.name());
details.remove(UsageEventVO.DynamicParameters.memory.name());
List<UserVmDetailVO> detailList = new ArrayList<UserVmDetailVO>();
for(Map.Entry<String, String> entry: details.entrySet()) {
UserVmDetailVO detailVO = new UserVmDetailVO(vmId, entry.getKey(), entry.getValue(), true);
detailList.add(detailVO);
}
userVmDetailsDao.saveDetails(detailList);
}

private void saveCustomOfferingDetails(long vmId, ServiceOffering serviceOffering) {
//save the custom values to the database.
Map<String, String> details = userVmDetailsDao.listDetailsKeyPairs(vmId);
details.put(UsageEventVO.DynamicParameters.cpuNumber.name(), serviceOffering.getCpu().toString());
details.put(UsageEventVO.DynamicParameters.cpuSpeed.name(), serviceOffering.getSpeed().toString());
details.put(UsageEventVO.DynamicParameters.memory.name(), serviceOffering.getRamSize().toString());
List<UserVmDetailVO> detailList = new ArrayList<UserVmDetailVO>();
for (Map.Entry<String, String> entry: details.entrySet()) {
UserVmDetailVO detailVO = new UserVmDetailVO(vmId, entry.getKey(), entry.getValue(), true);
detailList.add(detailVO);
}
userVmDetailsDao.saveDetails(detailList);
}

@Override
public String getConfigComponentName() {
return VirtualMachineManager.class.getSimpleName();
Expand Down Expand Up @@ -5174,7 +5211,7 @@ public Outcome<VirtualMachine> removeVmFromNetworkThroughJobQueue(
}

public Outcome<VirtualMachine> reconfigureVmThroughJobQueue(
final String vmUuid, final ServiceOffering newServiceOffering, final boolean reconfiguringOnExistingHost) {
final String vmUuid, final ServiceOffering oldServiceOffering, final ServiceOffering newServiceOffering, Map<String, String> customParameters, final boolean reconfiguringOnExistingHost) {

final CallContext context = CallContext.current();
final User user = context.getCallingUser();
Expand Down Expand Up @@ -5205,7 +5242,7 @@ public Outcome<VirtualMachine> reconfigureVmThroughJobQueue(

// save work context info (there are some duplications)
final VmWorkReconfigure workInfo = new VmWorkReconfigure(user.getId(), account.getId(), vm.getId(),
VirtualMachineManagerImpl.VM_WORK_JOB_HANDLER, newServiceOffering.getId(), reconfiguringOnExistingHost);
VirtualMachineManagerImpl.VM_WORK_JOB_HANDLER, oldServiceOffering.getId(), newServiceOffering.getId(), customParameters, reconfiguringOnExistingHost);
workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo));

_jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vm.getId());
Expand Down Expand Up @@ -5364,10 +5401,14 @@ private Pair<JobInfo.Status, String> orchestrateReconfigure(final VmWorkReconfig
s_logger.info("Unable to find vm " + work.getVmId());
}
assert vm != null;
ServiceOfferingVO oldServiceOffering = _offeringDao.findById(work.getOldServiceOfferingId());
ServiceOfferingVO newServiceOffering = _offeringDao.findById(work.getNewServiceOfferingId());
if (newServiceOffering.isDynamic()) {
// update the service offering object with the custom parameters like cpu, memory
newServiceOffering = _offeringDao.getComputeOffering(newServiceOffering, work.getCustomParameters());
}

final ServiceOffering newServiceOffering = _offeringDao.findById(vm.getId(), work.getNewServiceOfferingId());

reConfigureVm(vm.getUuid(), newServiceOffering,
reConfigureVm(vm.getUuid(), oldServiceOffering, newServiceOffering, work.getCustomParameters(),
work.isSameHost());
return new Pair<JobInfo.Status, String>(JobInfo.Status.SUCCEEDED, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,40 @@
package com.cloud.vm;


import java.util.Map;

public class VmWorkReconfigure extends VmWork {
private static final long serialVersionUID = -4517030323758086615L;

Long oldServiceOfferingId;
Long newServiceOfferingId;

Map<String, String> customParameters;
boolean sameHost;

public VmWorkReconfigure(long userId, long accountId, long vmId, String handlerName,
Long newServiceOfferingId, boolean sameHost) {
public VmWorkReconfigure(long userId, long accountId, long vmId, String handlerName, Long oldServiceOfferingId,
Long newServiceOfferingId, Map<String, String> customParameters, boolean sameHost) {

super(userId, accountId, vmId, handlerName);

this.oldServiceOfferingId = oldServiceOfferingId;
this.newServiceOfferingId = newServiceOfferingId;
this.customParameters = customParameters;
this.sameHost = sameHost;
}

public Long getOldServiceOfferingId() {
return oldServiceOfferingId;
}

public Long getNewServiceOfferingId() {
return newServiceOfferingId;
}

public Map<String, String> getCustomParameters() {
return customParameters;
}

public boolean isSameHost() {
return sameHost;
}
Expand Down
10 changes: 1 addition & 9 deletions server/src/main/java/com/cloud/server/ManagementServerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -4126,15 +4126,7 @@ private VirtualMachine upgradeStoppedSystemVm(final Long systemVmId, final Long
}
_itMgr.checkIfCanUpgrade(systemVm, newServiceOffering);

final boolean result = _itMgr.upgradeVmDb(systemVmId, serviceOfferingId);

if (newServiceOffering.isDynamic()) {
//save the custom values to the database.
_userVmMgr.saveCustomOfferingDetails(systemVmId, newServiceOffering);
}
if (currentServiceOffering.isDynamic() && !newServiceOffering.isDynamic()) {
_userVmMgr.removeCustomOfferingDetails(systemVmId);
}
final boolean result = _itMgr.upgradeVmDb(systemVmId, newServiceOffering, currentServiceOffering);

if (result) {
return _vmInstanceDao.findById(systemVmId);
Expand Down
5 changes: 0 additions & 5 deletions server/src/main/java/com/cloud/vm/UserVmManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import com.cloud.exception.ResourceAllocationException;
import com.cloud.exception.ResourceUnavailableException;
import com.cloud.exception.VirtualMachineMigrationException;
import com.cloud.offering.ServiceOffering;
import com.cloud.service.ServiceOfferingVO;
import com.cloud.storage.Storage.StoragePoolType;
import com.cloud.user.Account;
Expand Down Expand Up @@ -118,10 +117,6 @@ UserVm updateVirtualMachine(long id, String displayName, String group, Boolean h
//find a common place for all the scaling and upgrading code of both user and systemvms.
void validateCustomParameters(ServiceOfferingVO serviceOffering, Map<String, String> customParameters);

public void saveCustomOfferingDetails(long vmId, ServiceOffering serviceOffering);

public void removeCustomOfferingDetails(long vmId);

void generateUsageEvent(VirtualMachine vm, boolean isDisplay, String eventType);

void persistDeviceBusInfo(UserVmVO paramUserVmVO, String paramString);
Expand Down
Loading

0 comments on commit 77947f2

Please sign in to comment.