Skip to content

Commit a5054c1

Browse files
Harikrishna PatnalaKishan Kavala
authored andcommitted
CLOUDSTACK-3848: Usage events for dynamic scaling of cpu/ram operation on VM
1 parent 6ec4e57 commit a5054c1

6 files changed

Lines changed: 155 additions & 37 deletions

File tree

‎api/src/com/cloud/event/EventTypes.java‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public class EventTypes {
6969
public static final String EVENT_VM_REBOOT = "VM.REBOOT";
7070
public static final String EVENT_VM_UPDATE = "VM.UPDATE";
7171
public static final String EVENT_VM_UPGRADE = "VM.UPGRADE";
72-
public static final String EVENT_VM_SCALE = "VM.SCALE";
72+
public static final String EVENT_VM_DYNAMIC_SCALE = "VM.DYNAMIC.SCALE";
7373
public static final String EVENT_VM_RESETPASSWORD = "VM.RESETPASSWORD";
7474
public static final String EVENT_VM_RESETSSHKEY = "VM.RESETSSHKEY";
7575
public static final String EVENT_VM_MIGRATE = "VM.MIGRATE";
@@ -92,7 +92,6 @@ public class EventTypes {
9292
public static final String EVENT_PROXY_STOP = "PROXY.STOP";
9393
public static final String EVENT_PROXY_REBOOT = "PROXY.REBOOT";
9494
public static final String EVENT_PROXY_HA = "PROXY.HA";
95-
public static final String EVENT_PROXY_SCALE = "PROXY.SCALE";
9695

9796

9897
// VNC Console Events
@@ -217,7 +216,6 @@ public class EventTypes {
217216
public static final String EVENT_SSVM_STOP = "SSVM.STOP";
218217
public static final String EVENT_SSVM_REBOOT = "SSVM.REBOOT";
219218
public static final String EVENT_SSVM_HA = "SSVM.HA";
220-
public static final String EVENT_SSVM_SCALE = "SSVM.SCALE";
221219

222220
// Service Offerings
223221
public static final String EVENT_SERVICE_OFFERING_CREATE = "SERVICE.OFFERING.CREATE";

‎api/src/org/apache/cloudstack/api/command/admin/systemvm/ScaleSystemVMCmd.java‎

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,23 +109,17 @@ public void execute(){
109109
response.setResponseName(getCommandName());
110110
this.setResponseObject(response);
111111
} else {
112-
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to scale system vm");
112+
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to upgrade system vm");
113113
}
114114
}
115115

116116
@Override
117117
public String getEventType() {
118-
VirtualMachine.Type type = _mgr.findSystemVMTypeById(getId());
119-
if(type == VirtualMachine.Type.ConsoleProxy){
120-
return EventTypes.EVENT_PROXY_SCALE;
121-
}
122-
else{
123-
return EventTypes.EVENT_SSVM_SCALE;
124-
}
118+
return EventTypes.EVENT_VM_UPGRADE;
125119
}
126120

127121
@Override
128122
public String getEventDescription() {
129-
return "scaling system vm: " + getId() + " to service offering: " + getServiceOfferingId();
123+
return "Upgrading system vm: " + getId() + " to service offering: " + getServiceOfferingId();
130124
}
131125
}

‎api/src/org/apache/cloudstack/api/command/user/vm/ScaleVMCmd.java‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.cloud.user.Account;
2222
import com.cloud.user.UserContext;
2323
import com.cloud.uservm.UserVm;
24+
import com.cloud.vm.VirtualMachine;
2425
import org.apache.cloudstack.api.*;
2526
import org.apache.cloudstack.api.response.ServiceOfferingResponse;
2627
import org.apache.cloudstack.api.response.SuccessResponse;
@@ -84,9 +85,18 @@ public long getEntityOwnerId() {
8485
return Account.ACCOUNT_ID_SYSTEM; // no account info given, parent this command to SYSTEM so ERROR events are tracked
8586
}
8687

88+
@Override
89+
public String getEventType() {
90+
return EventTypes.EVENT_VM_UPGRADE;
91+
}
92+
93+
@Override
94+
public String getEventDescription() {
95+
return "upgrading vm: " + getId() + " to service offering: " + getServiceOfferingId();
96+
}
97+
8798
@Override
8899
public void execute(){
89-
//UserContext.current().setEventDetails("Vm Id: "+getId());
90100
UserVm result;
91101
try {
92102
result = _userVmService.upgradeVirtualMachine(this);
@@ -112,14 +122,4 @@ public void execute(){
112122
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to scale vm");
113123
}
114124
}
115-
116-
@Override
117-
public String getEventType() {
118-
return EventTypes.EVENT_VM_SCALE;
119-
}
120-
121-
@Override
122-
public String getEventDescription() {
123-
return "scaling volume: " + getId() + " to service offering: " + getServiceOfferingId();
124-
}
125125
}

‎server/src/com/cloud/server/ManagementServerImpl.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3189,6 +3189,7 @@ public ArrayList<String> getCloudIdentifierResponse(long userId) {
31893189
public Map<String, Object> listCapabilities(ListCapabilitiesCmd cmd) {
31903190
Map<String, Object> capabilities = new HashMap<String, Object>();
31913191

3192+
Account caller = UserContext.current().getCaller();
31923193
boolean securityGroupsEnabled = false;
31933194
boolean elasticLoadBalancerEnabled = false;
31943195
String supportELB = "false";
@@ -3716,6 +3717,7 @@ public HypervisorCapabilities updateHypervisorCapabilities(Long id, Long maxGues
37163717
}
37173718

37183719
@Override
3720+
@ActionEvent(eventType = EventTypes.EVENT_VM_UPGRADE, eventDescription = "Upgrading system VM", async = true)
37193721
public VirtualMachine upgradeSystemVM(ScaleSystemVMCmd cmd) throws ResourceUnavailableException, ManagementServerException, VirtualMachineMigrationException, ConcurrentOperationException {
37203722

37213723
VMInstanceVO vmInstance = _vmInstanceDao.findById(cmd.getId());
@@ -3727,7 +3729,7 @@ public VirtualMachine upgradeSystemVM(ScaleSystemVMCmd cmd) throws ResourceUnava
37273729
VirtualMachine vm = _vmInstanceDao.findById(cmd.getId());
37283730
return vm;
37293731
}else{
3730-
return null;
3732+
throw new CloudRuntimeException("Failed to upgrade System VM");
37313733
}
37323734
}
37333735

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

Lines changed: 92 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -763,14 +763,10 @@ private UserVm rebootVirtualMachine(long userId, long vmId)
763763
/*
764764
* TODO: cleanup eventually - Refactored API call
765765
*/
766+
// This method will be deprecated as we use ScaleVMCmd for both stopped VMs and running VMs
766767
public UserVm upgradeVirtualMachine(UpgradeVMCmd cmd) throws ResourceAllocationException {
767768
Long vmId = cmd.getId();
768769
Long svcOffId = cmd.getServiceOfferingId();
769-
return upgradeStoppedVirtualMachine(vmId, svcOffId);
770-
}
771-
772-
773-
private UserVm upgradeStoppedVirtualMachine(Long vmId, Long svcOffId) throws ResourceAllocationException {
774770
Account caller = UserContext.current().getCaller();
775771

776772
// Verify input parameters
@@ -836,6 +832,70 @@ private UserVm upgradeStoppedVirtualMachine(Long vmId, Long svcOffId) throws Res
836832
vmInstance.getServiceOfferingId(), vmInstance.getTemplateId(), vmInstance.getHypervisorType().toString(), VirtualMachine.class.getName(), vmInstance.getUuid());
837833

838834
return _vmDao.findById(vmInstance.getId());
835+
}
836+
837+
private UserVm upgradeStoppedVirtualMachine(Long vmId, Long svcOffId) throws ResourceAllocationException {
838+
Account caller = UserContext.current().getCaller();
839+
840+
// Verify input parameters
841+
//UserVmVO vmInstance = _vmDao.findById(vmId);
842+
VMInstanceVO vmInstance = _vmInstanceDao.findById(vmId);
843+
if (vmInstance == null) {
844+
throw new InvalidParameterValueException(
845+
"unable to find a virtual machine with id " + vmId);
846+
}
847+
848+
_accountMgr.checkAccess(caller, null, true, vmInstance);
849+
850+
// Check resource limits for CPU and Memory.
851+
ServiceOfferingVO newServiceOffering = _offeringDao.findById(svcOffId);
852+
ServiceOfferingVO currentServiceOffering = _offeringDao.findByIdIncludingRemoved(vmInstance.getServiceOfferingId());
853+
854+
int newCpu = newServiceOffering.getCpu();
855+
int newMemory = newServiceOffering.getRamSize();
856+
int currentCpu = currentServiceOffering.getCpu();
857+
int currentMemory = currentServiceOffering.getRamSize();
858+
859+
if (newCpu > currentCpu) {
860+
_resourceLimitMgr.checkResourceLimit(caller, ResourceType.cpu,
861+
newCpu - currentCpu);
862+
}
863+
if (newMemory > currentMemory) {
864+
_resourceLimitMgr.checkResourceLimit(caller, ResourceType.memory,
865+
newMemory - currentMemory);
866+
}
867+
868+
// Check that the specified service offering ID is valid
869+
_itMgr.checkIfCanUpgrade(vmInstance, svcOffId);
870+
871+
// remove diskAndMemory VM snapshots
872+
List<VMSnapshotVO> vmSnapshots = _vmSnapshotDao.findByVm(vmId);
873+
for (VMSnapshotVO vmSnapshotVO : vmSnapshots) {
874+
if(vmSnapshotVO.getType() == VMSnapshot.Type.DiskAndMemory){
875+
if(!_vmSnapshotMgr.deleteAllVMSnapshots(vmId, VMSnapshot.Type.DiskAndMemory)){
876+
String errMsg = "Failed to remove VM snapshot during upgrading, snapshot id " + vmSnapshotVO.getId();
877+
s_logger.debug(errMsg);
878+
throw new CloudRuntimeException(errMsg);
879+
}
880+
881+
}
882+
}
883+
884+
_itMgr.upgradeVmDb(vmId, svcOffId);
885+
886+
// Increment or decrement CPU and Memory count accordingly.
887+
if (newCpu > currentCpu) {
888+
_resourceLimitMgr.incrementResourceCount(caller.getAccountId(), ResourceType.cpu, new Long (newCpu - currentCpu));
889+
} else if (currentCpu > newCpu) {
890+
_resourceLimitMgr.decrementResourceCount(caller.getAccountId(), ResourceType.cpu, new Long (currentCpu - newCpu));
891+
}
892+
if (newMemory > currentMemory) {
893+
_resourceLimitMgr.incrementResourceCount(caller.getAccountId(), ResourceType.memory, new Long (newMemory - currentMemory));
894+
} else if (currentMemory > newMemory) {
895+
_resourceLimitMgr.decrementResourceCount(caller.getAccountId(), ResourceType.memory, new Long (currentMemory - newMemory));
896+
}
897+
898+
return _vmDao.findById(vmInstance.getId());
839899

840900
}
841901

@@ -1088,17 +1148,30 @@ public UserVm updateDefaultNicForVirtualMachine(UpdateDefaultNicForVMCmd cmd) th
10881148
}
10891149

10901150
@Override
1091-
@ActionEvent(eventType = EventTypes.EVENT_VM_SCALE, eventDescription = "scaling Vm")
1151+
@ActionEvent(eventType = EventTypes.EVENT_VM_UPGRADE, eventDescription = "Upgrading VM", async = true)
10921152
public UserVm
10931153
upgradeVirtualMachine(ScaleVMCmd cmd) throws ResourceUnavailableException, ConcurrentOperationException, ManagementServerException, VirtualMachineMigrationException{
10941154

10951155
Long vmId = cmd.getId();
10961156
Long newServiceOfferingId = cmd.getServiceOfferingId();
1157+
UserContext.current().setEventDetails("Vm Id: " + vmId);
1158+
10971159
boolean result = upgradeVirtualMachine(vmId, newServiceOfferingId);
10981160
if(result){
1099-
return _vmDao.findById(vmId);
1100-
}else{
1101-
return null;
1161+
UserVmVO vmInstance = _vmDao.findById(vmId);
1162+
if(vmInstance.getState().equals(State.Stopped)){
1163+
// Generate usage event for VM upgrade
1164+
UsageEventUtils.publishUsageEvent(EventTypes.EVENT_VM_UPGRADE, vmInstance.getAccountId(), vmInstance.getDataCenterId(), vmInstance.getId(), vmInstance.getHostName(),
1165+
vmInstance.getServiceOfferingId(), vmInstance.getTemplateId(), vmInstance.getHypervisorType().toString(), VirtualMachine.class.getName(), vmInstance.getUuid());
1166+
}
1167+
if(vmInstance.getState().equals(State.Running)){
1168+
// Generate usage event for Dynamic scaling of VM
1169+
UsageEventUtils.publishUsageEvent(EventTypes.EVENT_VM_DYNAMIC_SCALE, vmInstance.getAccountId(), vmInstance.getDataCenterId(), vmInstance.getId(), vmInstance.getHostName(),
1170+
vmInstance.getServiceOfferingId(), vmInstance.getTemplateId(), vmInstance.getHypervisorType().toString(), VirtualMachine.class.getName(), vmInstance.getUuid());
1171+
}
1172+
return vmInstance;
1173+
} else {
1174+
throw new CloudRuntimeException("Failed to scale the VM");
11021175
}
11031176

11041177
}
@@ -1140,7 +1213,6 @@ public HashMap<Long, List<VmDiskStatsEntry>> getVmDiskStatistics(long hostId, St
11401213

11411214
@Override
11421215
public boolean upgradeVirtualMachine(Long vmId, Long newServiceOfferingId) throws ResourceUnavailableException, ConcurrentOperationException, ManagementServerException, VirtualMachineMigrationException{
1143-
Account caller = UserContext.current().getCaller();
11441216

11451217
// Verify input parameters
11461218
VMInstanceVO vmInstance = _vmInstanceDao.findById(vmId);
@@ -1149,7 +1221,16 @@ public boolean upgradeVirtualMachine(Long vmId, Long newServiceOfferingId) throw
11491221
upgradeStoppedVirtualMachine(vmId, newServiceOfferingId);
11501222
return true;
11511223
}
1224+
if(vmInstance.getState().equals(State.Running)){
1225+
return upgradeRunningVirtualMachine(vmId, newServiceOfferingId);
1226+
}
1227+
return false;
1228+
}
11521229

1230+
private boolean upgradeRunningVirtualMachine(Long vmId, Long newServiceOfferingId) throws ResourceUnavailableException, ConcurrentOperationException, ManagementServerException, VirtualMachineMigrationException{
1231+
1232+
Account caller = UserContext.current().getCaller();
1233+
VMInstanceVO vmInstance = _vmInstanceDao.findById(vmId);
11531234
if(vmInstance.getHypervisorType() != HypervisorType.XenServer && vmInstance.getHypervisorType() != HypervisorType.VMware){
11541235
throw new InvalidParameterValueException("This operation not permitted for this hypervisor of the vm");
11551236
}
@@ -1247,13 +1328,11 @@ public boolean upgradeVirtualMachine(Long vmId, Long newServiceOfferingId) throw
12471328
// Decrement CPU and Memory count accordingly.
12481329
if (newCpu > currentCpu) {
12491330
_resourceLimitMgr.decrementResourceCount(caller.getAccountId(), ResourceType.cpu, new Long (newCpu - currentCpu));
1250-
}
1331+
}
12511332
if (newMemory > currentMemory) {
12521333
_resourceLimitMgr.decrementResourceCount(caller.getAccountId(), ResourceType.memory, new Long (newMemory - currentMemory));
12531334
}
12541335
}
1255-
1256-
12571336
}
12581337
}
12591338
}

‎usage/src/com/cloud/usage/UsageManagerImpl.java‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,6 +1096,51 @@ private void createVMHelperEvent(UsageEventVO event) {
10961096
UsageVMInstanceVO usageInstanceNew = new UsageVMInstanceVO(UsageTypes.ALLOCATED_VM, zoneId, event.getAccountId(), vmId, vmName,
10971097
soId, templateId, hypervisorType, event.getCreateDate(), null);
10981098
m_usageInstanceDao.persist(usageInstanceNew);
1099+
} else if (EventTypes.EVENT_VM_DYNAMIC_SCALE.equals(event.getType())) {
1100+
// Ending the running vm event
1101+
SearchCriteria<UsageVMInstanceVO> sc = m_usageInstanceDao.createSearchCriteria();
1102+
sc.addAnd("vmInstanceId", SearchCriteria.Op.EQ, Long.valueOf(vmId));
1103+
sc.addAnd("endDate", SearchCriteria.Op.NULL);
1104+
sc.addAnd("usageType", SearchCriteria.Op.EQ, UsageTypes.RUNNING_VM);
1105+
List<UsageVMInstanceVO> usageInstances = m_usageInstanceDao.search(sc, null);
1106+
if (usageInstances != null) {
1107+
if (usageInstances.size() > 1) {
1108+
s_logger.warn("found multiple entries for a vm running with id: " + vmId + ", ending them all...");
1109+
}
1110+
for (UsageVMInstanceVO usageInstance : usageInstances) {
1111+
usageInstance.setEndDate(event.getCreateDate());
1112+
m_usageInstanceDao.update(usageInstance);
1113+
}
1114+
}
1115+
1116+
sc = m_usageInstanceDao.createSearchCriteria();
1117+
sc.addAnd("vmInstanceId", SearchCriteria.Op.EQ, Long.valueOf(vmId));
1118+
sc.addAnd("endDate", SearchCriteria.Op.NULL);
1119+
sc.addAnd("usageType", SearchCriteria.Op.EQ, UsageTypes.ALLOCATED_VM);
1120+
usageInstances = m_usageInstanceDao.search(sc, null);
1121+
if (usageInstances == null || (usageInstances.size() == 0)) {
1122+
s_logger.error("Cannot find allocated vm entry for a vm running with id: " + vmId);
1123+
} else if (usageInstances.size() == 1) {
1124+
UsageVMInstanceVO usageInstance = usageInstances.get(0);
1125+
if(usageInstance.getSerivceOfferingId() != soId){
1126+
//Service Offering changed after Vm creation
1127+
//End current Allocated usage and create new Allocated Vm entry with new soId
1128+
usageInstance.setEndDate(event.getCreateDate());
1129+
m_usageInstanceDao.update(usageInstance);
1130+
usageInstance.setServiceOfferingId(soId);
1131+
usageInstance.setStartDate(event.getCreateDate());
1132+
usageInstance.setEndDate(null);
1133+
m_usageInstanceDao.persist(usageInstance);
1134+
}
1135+
}
1136+
1137+
Long templateId = event.getTemplateId();
1138+
String hypervisorType = event.getResourceType();
1139+
1140+
// add this VM to the usage helper table with new service offering Id
1141+
UsageVMInstanceVO usageInstanceNew = new UsageVMInstanceVO(UsageTypes.RUNNING_VM, zoneId, event.getAccountId(), vmId, vmName,
1142+
soId, templateId, hypervisorType, event.getCreateDate(), null);
1143+
m_usageInstanceDao.persist(usageInstanceNew);
10991144
}
11001145
}
11011146

0 commit comments

Comments
 (0)