From 97b6b117e1448115613318aef1679ac00c93375e Mon Sep 17 00:00:00 2001 From: rashmidixit Date: Mon, 5 Dec 2016 14:25:27 +0530 Subject: [PATCH 1/3] The system capacity was not getting calculated correctly --- .../cloud/capacity/dao/CapacityDaoImpl.java | 22 +++---------------- 1 file changed, 3 insertions(+), 19 deletions(-) diff --git a/engine/schema/src/com/cloud/capacity/dao/CapacityDaoImpl.java b/engine/schema/src/com/cloud/capacity/dao/CapacityDaoImpl.java index 1b1b856af7f..95d57d0eb47 100644 --- a/engine/schema/src/com/cloud/capacity/dao/CapacityDaoImpl.java +++ b/engine/schema/src/com/cloud/capacity/dao/CapacityDaoImpl.java @@ -23,7 +23,6 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Map.Entry; import javax.inject.Inject; @@ -356,11 +355,11 @@ public class CapacityDaoImpl extends GenericDaoBase implements switch (level) { case 1: // List all the capacities grouped by zone, capacity Type - finalQuery.append(LIST_CAPACITY_GROUP_BY_CLUSTER_TYPE_PART2); + finalQuery.append(LIST_CAPACITY_GROUP_BY_ZONE_TYPE_PART2); break; case 2: // List all the capacities grouped by pod, capacity Type - finalQuery.append(LIST_CAPACITY_GROUP_BY_CLUSTER_TYPE_PART2); + finalQuery.append(LIST_CAPACITY_GROUP_BY_POD_TYPE_PART2); break; case 3: // List all the capacities grouped by cluster, capacity Type @@ -392,22 +391,7 @@ public class CapacityDaoImpl extends GenericDaoBase implements results.add(summedCapacity); } - HashMap capacityMap = new HashMap(); - for (SummedCapacity result: results) { - if (capacityMap.containsKey(result.getCapacityType().intValue())) { - SummedCapacity tempCapacity = capacityMap.get(result.getCapacityType().intValue()); - tempCapacity.setUsedCapacity(tempCapacity.getUsedCapacity()+result.getUsedCapacity()); - tempCapacity.setReservedCapacity(tempCapacity.getReservedCapacity()+result.getReservedCapacity()); - tempCapacity.setSumTotal(tempCapacity.getTotalCapacity()+result.getTotalCapacity()); - }else { - capacityMap.put(result.getCapacityType().intValue(),result); - } - } - List summedCapacityList = new ArrayList(); - for (Entry entry : capacityMap.entrySet()) { - summedCapacityList.add(entry.getValue()); - } - return summedCapacityList; + return results; } catch (SQLException e) { throw new CloudRuntimeException("DB Exception on: " + finalQuery, e); } catch (Throwable e) { From 91bfedd2c7ed7db86a5b1167bb955977da0f1817 Mon Sep 17 00:00:00 2001 From: Nathan Johnson Date: Mon, 20 Feb 2017 14:58:20 -0600 Subject: [PATCH 2/3] CLOUDSTACK-9796 - Fix NPE in VirtualMachineManagerImpl.java This checks the work variable for NULL in all cases where it is used. Fixes CLOUDSTACK-9796. --- .../src/com/cloud/vm/VirtualMachineManagerImpl.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/engine/orchestration/src/com/cloud/vm/VirtualMachineManagerImpl.java b/engine/orchestration/src/com/cloud/vm/VirtualMachineManagerImpl.java index bea98cb25d4..03a37987752 100644 --- a/engine/orchestration/src/com/cloud/vm/VirtualMachineManagerImpl.java +++ b/engine/orchestration/src/com/cloud/vm/VirtualMachineManagerImpl.java @@ -744,14 +744,17 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac protected boolean changeState(final T vm, final Event event, final Long hostId, final ItWorkVO work, final Step step) throws NoTransitionException { // FIXME: We should do this better. - final Step previousStep = work.getStep(); - _workDao.updateStep(work, step); + Step previousStep = null; + if (work != null) { + previousStep = work.getStep(); + _workDao.updateStep(work, step); + } boolean result = false; try { result = stateTransitTo(vm, event, hostId); return result; } finally { - if (!result) { + if (!result && work != null) { _workDao.updateStep(work, previousStep); } } @@ -1507,12 +1510,13 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac if (doCleanup) { if (cleanup(vmGuru, new VirtualMachineProfileImpl(vm), work, Event.StopRequested, cleanUpEvenIfUnableToStop)) { try { - if (s_logger.isDebugEnabled()) { + if (s_logger.isDebugEnabled() && work != null) { s_logger.debug("Updating work item to Done, id:" + work.getId()); } if (!changeState(vm, Event.AgentReportStopped, null, work, Step.Done)) { throw new CloudRuntimeException("Unable to stop " + vm); } + } catch (final NoTransitionException e) { s_logger.warn("Unable to cleanup " + vm); throw new CloudRuntimeException("Unable to stop " + vm, e); From bf77c07797518bfd8dd05836fced25615f1d8375 Mon Sep 17 00:00:00 2001 From: Suresh Kumar Anaparti Date: Fri, 30 Dec 2016 01:57:02 +0530 Subject: [PATCH 3/3] CLOUDSTACK-9719: [VMware] VR loses DHCP settings and VMs cannot obtain IP after HA recovery - Set high restart priority for the VR. Test scenarios: - Enable cluster HA after VR is created. Now stop and start VR and check its restart priority, should be High. - Enable cluster HA before VR is created. Now create some VM and verify that VR created must have High restart priority. --- .../vmware/VmwareServerDiscoverer.java | 4 +- .../vmware/resource/VmwareResource.java | 14 +- .../cloud/hypervisor/vmware/mo/ClusterMO.java | 121 +++++++++++++++++- .../cloud/hypervisor/vmware/mo/HostMO.java | 20 +++ .../vmware/mo/VmwareHypervisorHost.java | 4 + 5 files changed, 155 insertions(+), 8 deletions(-) diff --git a/plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/VmwareServerDiscoverer.java b/plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/VmwareServerDiscoverer.java index 35063b05859..4b2f830a3ee 100644 --- a/plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/VmwareServerDiscoverer.java +++ b/plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/VmwareServerDiscoverer.java @@ -29,7 +29,6 @@ import javax.naming.ConfigurationException; import org.apache.log4j.Logger; -import com.vmware.vim25.ClusterDasConfigInfo; import com.vmware.vim25.ManagedObjectReference; import org.apache.cloudstack.api.ApiConstants; @@ -344,8 +343,7 @@ public class VmwareServerDiscoverer extends DiscovererBase implements Discoverer return null; } else { ClusterMO clusterMo = new ClusterMO(context, morCluster); - ClusterDasConfigInfo dasConfig = clusterMo.getDasConfig(); - if (dasConfig != null && dasConfig.isEnabled() != null && dasConfig.isEnabled().booleanValue()) { + if (clusterMo.isHAEnabled()) { clusterDetails.put("NativeHA", "true"); _clusterDetailsDao.persist(clusterId, clusterDetails); } diff --git a/plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/resource/VmwareResource.java b/plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/resource/VmwareResource.java index ad4ae267528..56ca4ea0966 100644 --- a/plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/resource/VmwareResource.java +++ b/plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/resource/VmwareResource.java @@ -48,9 +48,9 @@ import org.apache.log4j.NDC; import com.google.gson.Gson; import com.vmware.vim25.AboutInfo; import com.vmware.vim25.BoolPolicy; -import com.vmware.vim25.ClusterDasConfigInfo; import com.vmware.vim25.ComputeResourceSummary; import com.vmware.vim25.CustomFieldStringValue; +import com.vmware.vim25.DasVmPriority; import com.vmware.vim25.DVPortConfigInfo; import com.vmware.vim25.DVPortConfigSpec; import com.vmware.vim25.DatastoreSummary; @@ -1634,6 +1634,11 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa if (vmFolderExists && vmxFileFullPath != null) { // VM can be registered only if .vmx is present. registerVm(vmNameOnVcenter, dsRootVolumeIsOn); vmMo = hyperHost.findVmOnHyperHost(vmInternalCSName); + if (vmMo != null) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Found registered vm " + vmInternalCSName + " at host " + hyperHost.getHyperHostName()); + } + } tearDownVm(vmMo); }else if (!hyperHost.createBlankVm(vmNameOnVcenter, vmInternalCSName, vmSpec.getCpus(), vmSpec.getMaxSpeed().intValue(), getReservedCpuMHZ(vmSpec), vmSpec.getLimitCpuUse(), (int)(vmSpec.getMaxRam() / (1024 * 1024)), getReservedMemoryMb(vmSpec), @@ -1981,6 +1986,10 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa throw new Exception("Failed to configure VM before start. vmName: " + vmInternalCSName); } + if (vmSpec.getType() == VirtualMachine.Type.DomainRouter) { + hyperHost.setRestartPriorityForVM(vmMo, DasVmPriority.HIGH.value()); + } + // // Post Configuration // @@ -4805,8 +4814,7 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa private void fillHostDetailsInfo(VmwareContext serviceContext, Map details) throws Exception { VmwareHypervisorHost hyperHost = getHyperHost(getServiceContext()); - ClusterDasConfigInfo dasConfig = hyperHost.getDasConfig(); - if (dasConfig != null && dasConfig.isEnabled() != null && dasConfig.isEnabled().booleanValue()) { + if (hyperHost.isHAEnabled()) { details.put("NativeHA", "true"); } } diff --git a/vmware-base/src/com/cloud/hypervisor/vmware/mo/ClusterMO.java b/vmware-base/src/com/cloud/hypervisor/vmware/mo/ClusterMO.java index b5f0500d391..a6e42850c5e 100644 --- a/vmware-base/src/com/cloud/hypervisor/vmware/mo/ClusterMO.java +++ b/vmware-base/src/com/cloud/hypervisor/vmware/mo/ClusterMO.java @@ -22,16 +22,20 @@ import java.util.Arrays; import java.util.HashMap; import java.util.List; +import org.apache.commons.lang.StringUtils; import org.apache.log4j.Logger; import com.google.gson.Gson; import com.vmware.vim25.ArrayOfHostIpRouteEntry; +import com.vmware.vim25.ArrayUpdateOperation; import com.vmware.vim25.ClusterComputeResourceSummary; import com.vmware.vim25.ClusterConfigInfoEx; import com.vmware.vim25.ClusterDasConfigInfo; +import com.vmware.vim25.ClusterDasVmSettingsRestartPriority; import com.vmware.vim25.ClusterHostRecommendation; import com.vmware.vim25.ComputeResourceSummary; import com.vmware.vim25.CustomFieldStringValue; +import com.vmware.vim25.DasVmPriority; import com.vmware.vim25.DatastoreInfo; import com.vmware.vim25.DynamicProperty; import com.vmware.vim25.GuestOsDescriptor; @@ -49,6 +53,10 @@ import com.vmware.vim25.PropertySpec; import com.vmware.vim25.TraversalSpec; import com.vmware.vim25.VirtualMachineConfigOption; import com.vmware.vim25.VirtualMachineConfigSpec; +import com.vmware.vim25.ClusterDasVmConfigInfo; +import com.vmware.vim25.ClusterDasVmConfigSpec; +import com.vmware.vim25.ClusterDasVmSettings; +import com.vmware.vim25.ClusterConfigSpecEx; import com.cloud.hypervisor.vmware.util.VmwareContext; import com.cloud.hypervisor.vmware.util.VmwareHelper; @@ -77,9 +85,118 @@ public class ClusterMO extends BaseMO implements VmwareHypervisorHost { @Override public ClusterDasConfigInfo getDasConfig() throws Exception { - // Note getDynamicProperty() with "configurationEx.dasConfig" does not work here because of that dasConfig is a property in subclass + ClusterConfigInfoEx configInfo = getClusterConfigInfo(); + if (configInfo != null) { + // Note getDynamicProperty() with "configurationEx.dasConfig" does not work here because of that dasConfig is a property in subclass + return configInfo.getDasConfig(); + } + + return null; + } + + private ClusterConfigInfoEx getClusterConfigInfo() throws Exception { ClusterConfigInfoEx configInfo = (ClusterConfigInfoEx)_context.getVimClient().getDynamicProperty(_mor, "configurationEx"); - return configInfo.getDasConfig(); + return configInfo; + } + + @Override + public boolean isHAEnabled() throws Exception { + ClusterDasConfigInfo dasConfig = getDasConfig(); + if (dasConfig != null && dasConfig.isEnabled() != null && dasConfig.isEnabled().booleanValue()) { + return true; + } + + return false; + } + + private String getRestartPriorityForVM(VirtualMachineMO vmMo) throws Exception { + if (vmMo == null) { + s_logger.debug("Failed to get restart priority for VM, invalid VM object reference"); + return null; + } + + ManagedObjectReference vmMor = vmMo.getMor(); + if (vmMor == null || !vmMor.getType().equals("VirtualMachine")) { + s_logger.debug("Failed to get restart priority for VM: " + vmMo.getName() + ", invalid VM object reference"); + return null; + } + + ClusterConfigInfoEx configInfo = getClusterConfigInfo(); + if (configInfo == null) { + s_logger.debug("Failed to get restart priority for VM: " + vmMo.getName() + ", no cluster config information"); + return null; + } + + List dasVmConfig = configInfo.getDasVmConfig(); + for (int dasVmConfigIndex = 0; dasVmConfigIndex < dasVmConfig.size(); dasVmConfigIndex++) { + ClusterDasVmConfigInfo dasVmConfigInfo = dasVmConfig.get(dasVmConfigIndex); + if (dasVmConfigInfo != null && dasVmConfigInfo.getKey().getValue().equals(vmMor.getValue())) { + DasVmPriority dasVmPriority = dasVmConfigInfo.getRestartPriority(); + if (dasVmPriority != null) { + return dasVmPriority.value(); + } else { + //VM uses cluster restart priority when DasVmPriority for the VM is null. + return ClusterDasVmSettingsRestartPriority.CLUSTER_RESTART_PRIORITY.value(); + } + } + } + + s_logger.debug("VM: " + vmMo.getName() + " uses default restart priority in the cluster: " + getName()); + return null; + } + + @Override + public void setRestartPriorityForVM(VirtualMachineMO vmMo, String priority) throws Exception { + if (vmMo == null || StringUtils.isBlank(priority)) { + return; + } + + if (!isHAEnabled()) { + s_logger.debug("Couldn't set restart priority for VM: " + vmMo.getName() + ", HA disabled in the cluster"); + return; + } + + ManagedObjectReference vmMor = vmMo.getMor(); + if (vmMor == null || !vmMor.getType().equals("VirtualMachine")) { + s_logger.debug("Failed to set restart priority for VM: " + vmMo.getName() + ", invalid VM object reference"); + return; + } + + String currentVmRestartPriority = getRestartPriorityForVM(vmMo); + if (StringUtils.isNotBlank(currentVmRestartPriority) && currentVmRestartPriority.equalsIgnoreCase(priority)) { + return; + } + + ClusterDasVmSettings clusterDasVmSettings = new ClusterDasVmSettings(); + clusterDasVmSettings.setRestartPriority(priority); + + ClusterDasVmConfigInfo clusterDasVmConfigInfo = new ClusterDasVmConfigInfo(); + clusterDasVmConfigInfo.setKey(vmMor); + clusterDasVmConfigInfo.setDasSettings(clusterDasVmSettings); + + ClusterDasVmConfigSpec clusterDasVmConfigSpec = new ClusterDasVmConfigSpec(); + clusterDasVmConfigSpec.setOperation((StringUtils.isNotBlank(currentVmRestartPriority)) ? ArrayUpdateOperation.EDIT : ArrayUpdateOperation.ADD); + clusterDasVmConfigSpec.setInfo(clusterDasVmConfigInfo); + + ClusterConfigSpecEx clusterConfigSpecEx = new ClusterConfigSpecEx(); + ClusterDasConfigInfo clusterDasConfigInfo = new ClusterDasConfigInfo(); + clusterConfigSpecEx.setDasConfig(clusterDasConfigInfo); + clusterConfigSpecEx.getDasVmConfigSpec().add(clusterDasVmConfigSpec); + + ManagedObjectReference morTask = _context.getService().reconfigureComputeResourceTask(_mor, clusterConfigSpecEx, true); + + boolean result = _context.getVimClient().waitForTask(morTask); + + if (result) { + _context.waitForTaskProgressDone(morTask); + + if (s_logger.isTraceEnabled()) + s_logger.trace("vCenter API trace - setRestartPriority done(successfully)"); + } else { + if (s_logger.isTraceEnabled()) + s_logger.trace("vCenter API trace - setRestartPriority done(failed)"); + s_logger.error("Set restart priority failed for VM: " + vmMo.getName() + " due to " + TaskMO.getTaskFailureInfo(_context, morTask)); + } } @Override diff --git a/vmware-base/src/com/cloud/hypervisor/vmware/mo/HostMO.java b/vmware-base/src/com/cloud/hypervisor/vmware/mo/HostMO.java index c008e6b01e7..5f64e83c770 100644 --- a/vmware-base/src/com/cloud/hypervisor/vmware/mo/HostMO.java +++ b/vmware-base/src/com/cloud/hypervisor/vmware/mo/HostMO.java @@ -143,6 +143,26 @@ public class HostMO extends BaseMO implements VmwareHypervisorHost { return null; } + @Override + public boolean isHAEnabled() throws Exception { + ManagedObjectReference morParent = getParentMor(); + if (morParent.getType().equals("ClusterComputeResource")) { + ClusterMO clusterMo = new ClusterMO(_context, morParent); + return clusterMo.isHAEnabled(); + } + + return false; + } + + @Override + public void setRestartPriorityForVM(VirtualMachineMO vmMo, String priority) throws Exception { + ManagedObjectReference morParent = getParentMor(); + if (morParent.getType().equals("ClusterComputeResource")) { + ClusterMO clusterMo = new ClusterMO(_context, morParent); + clusterMo.setRestartPriorityForVM(vmMo, priority); + } + } + @Override public String getHyperHostDefaultGateway() throws Exception { List entries = getHostIpRouteEntries(); diff --git a/vmware-base/src/com/cloud/hypervisor/vmware/mo/VmwareHypervisorHost.java b/vmware-base/src/com/cloud/hypervisor/vmware/mo/VmwareHypervisorHost.java index fdfc254278b..0767ec09f9a 100644 --- a/vmware-base/src/com/cloud/hypervisor/vmware/mo/VmwareHypervisorHost.java +++ b/vmware-base/src/com/cloud/hypervisor/vmware/mo/VmwareHypervisorHost.java @@ -37,6 +37,10 @@ public interface VmwareHypervisorHost { ClusterDasConfigInfo getDasConfig() throws Exception; + boolean isHAEnabled() throws Exception; + + void setRestartPriorityForVM(VirtualMachineMO vmMo, String priority) throws Exception; + ManagedObjectReference getHyperHostDatacenter() throws Exception; ManagedObjectReference getHyperHostOwnerResourcePool() throws Exception;