Skip to content

Commit a4762b0

Browse files
committed
CLOUDSTACK-4464,CLOUDSTACK-4529: use property collector to collect all information in one round, we have a few other places that need to do it the same way
1 parent d6bac14 commit a4762b0

4 files changed

Lines changed: 74 additions & 43 deletions

File tree

‎plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/resource/VmwareResource.java‎

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import com.vmware.vim25.BoolPolicy;
5555
import com.vmware.vim25.ClusterDasConfigInfo;
5656
import com.vmware.vim25.ComputeResourceSummary;
57+
import com.vmware.vim25.CustomFieldStringValue;
5758
import com.vmware.vim25.DVPortConfigInfo;
5859
import com.vmware.vim25.DVPortConfigSpec;
5960
import com.vmware.vim25.DatastoreSummary;
@@ -5740,10 +5741,16 @@ public PingCommand getCurrentStatus(long id) {
57405741

57415742
if(_recycleHungWorker) {
57425743
s_logger.info("Scan hung worker VM to recycle");
5744+
5745+
int key = ((HostMO)hyperHost).getCustomFieldKey("VirtualMachine", CustomFieldConstants.CLOUD_VM_INTERNAL_NAME);
5746+
if(key == 0) {
5747+
s_logger.warn("Custom field " + CustomFieldConstants.CLOUD_VM_INTERNAL_NAME + " is not registered ?!");
5748+
}
5749+
String instanceNameCustomField = "value[" + key + "]";
57435750

57445751
// GC worker that has been running for too long
57455752
ObjectContent[] ocs = hyperHost.getVmPropertiesOnHyperHost(
5746-
new String[] {"name", "config.template", "runtime.powerState", "runtime.bootTime"});
5753+
new String[] {"name", "config.template", "runtime.powerState", "runtime.bootTime", instanceNameCustomField });
57475754
if(ocs != null) {
57485755
for(ObjectContent oc : ocs) {
57495756
List<DynamicProperty> props = oc.getPropSet();
@@ -5757,6 +5764,10 @@ public PingCommand getCurrentStatus(long id) {
57575764
for(DynamicProperty prop : props) {
57585765
if (prop.getName().equals("name"))
57595766
vmName = prop.getVal().toString();
5767+
else if(prop.getName().startsWith("value[")) {
5768+
if(prop.getVal() != null)
5769+
internalName = ((CustomFieldStringValue)prop.getVal()).getValue();
5770+
}
57605771
else if(prop.getName().equals("config.template"))
57615772
template = (Boolean)prop.getVal();
57625773
else if(prop.getName().equals("runtime.powerState"))
@@ -5766,15 +5777,13 @@ else if(prop.getName().equals("runtime.bootTime"))
57665777
}
57675778

57685779
VirtualMachineMO vmMo = new VirtualMachineMO(hyperHost.getContext(), oc.getObj());
5769-
// Check if vmMo has the custom property CLOUD_VM_INTERNAL_NAME set.
5770-
internalName = vmMo.getCustomFieldValue(CustomFieldConstants.CLOUD_VM_INTERNAL_NAME);
5771-
57725780
String name = null;
57735781
if (internalName != null) {
57745782
name = internalName;
57755783
} else {
57765784
name = vmName;
57775785
}
5786+
57785787
if(!template && name.matches("[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}")) {
57795788
boolean recycle = false;
57805789

@@ -6215,9 +6224,16 @@ private VirtualMachineGuestOsIdentifier translateGuestOsIdentifier(String cpuArc
62156224

62166225
private HashMap<String, State> getVmStates() throws Exception {
62176226
VmwareHypervisorHost hyperHost = getHyperHost(getServiceContext());
6227+
6228+
int key = ((HostMO)hyperHost).getCustomFieldKey("VirtualMachine", CustomFieldConstants.CLOUD_VM_INTERNAL_NAME);
6229+
if(key == 0) {
6230+
s_logger.warn("Custom field " + CustomFieldConstants.CLOUD_VM_INTERNAL_NAME + " is not registered ?!");
6231+
}
6232+
String instanceNameCustomField = "value[" + key + "]";
6233+
62186234
// CLOUD_VM_INTERNAL_NAME stores the internal CS generated vm name. This was earlier stored in name. Now, name can be either the hostname or
62196235
// the internal CS name, but the custom field CLOUD_VM_INTERNAL_NAME always stores the internal CS name.
6220-
ObjectContent[] ocs = hyperHost.getVmPropertiesOnHyperHost(new String[] { "name", "runtime.powerState", "config.template" });
6236+
ObjectContent[] ocs = hyperHost.getVmPropertiesOnHyperHost(new String[] { "name", "runtime.powerState", "config.template", instanceNameCustomField });
62216237

62226238
HashMap<String, State> newStates = new HashMap<String, State>();
62236239
if (ocs != null && ocs.length > 0) {
@@ -6238,13 +6254,15 @@ private HashMap<String, State> getVmStates() throws Exception {
62386254
powerState = (VirtualMachinePowerState) objProp.getVal();
62396255
} else if (objProp.getName().equals("name")) {
62406256
name = (String) objProp.getVal();
6241-
} else {
6257+
} else if(objProp.getName().contains(instanceNameCustomField)) {
6258+
if(objProp.getVal() != null)
6259+
VMInternalCSName = ((CustomFieldStringValue)objProp.getVal()).getValue();
6260+
}
6261+
else {
62426262
assert (false);
62436263
}
62446264
}
6245-
VirtualMachineMO vmMo = new VirtualMachineMO(hyperHost.getContext(), oc.getObj());
6246-
// Check if vmMo has the custom property CLOUD_VM_INTERNAL_NAME set.
6247-
VMInternalCSName = vmMo.getCustomFieldValue(CustomFieldConstants.CLOUD_VM_INTERNAL_NAME);
6265+
62486266
if (VMInternalCSName != null)
62496267
name = VMInternalCSName;
62506268

@@ -6276,8 +6294,14 @@ private HashMap<String, VmStatsEntry> getVmStats(List<String> vmNames) throws Ex
62766294
}
62776295
}
62786296
}
6297+
6298+
int key = ((HostMO)hyperHost).getCustomFieldKey("VirtualMachine", CustomFieldConstants.CLOUD_VM_INTERNAL_NAME);
6299+
if(key == 0) {
6300+
s_logger.warn("Custom field " + CustomFieldConstants.CLOUD_VM_INTERNAL_NAME + " is not registered ?!");
6301+
}
6302+
String instanceNameCustomField = "value[" + key + "]";
62796303

6280-
ObjectContent[] ocs = hyperHost.getVmPropertiesOnHyperHost(new String[] {"name", "summary.config.numCpu", "summary.quickStats.overallCpuUsage"});
6304+
ObjectContent[] ocs = hyperHost.getVmPropertiesOnHyperHost(new String[] {"name", "summary.config.numCpu", "summary.quickStats.overallCpuUsage", instanceNameCustomField});
62816305
if (ocs != null && ocs.length > 0) {
62826306
for (ObjectContent oc : ocs) {
62836307
List<DynamicProperty> objProps = oc.getPropSet();
@@ -6290,16 +6314,17 @@ private HashMap<String, VmStatsEntry> getVmStats(List<String> vmNames) throws Ex
62906314
for (DynamicProperty objProp : objProps) {
62916315
if (objProp.getName().equals("name")) {
62926316
vmNameOnVcenter = objProp.getVal().toString();
6293-
} else if (objProp.getName().equals("summary.config.numCpu")) {
6317+
} else if(objProp.getName().contains(instanceNameCustomField)) {
6318+
if(objProp.getVal() != null)
6319+
vmInternalCSName = ((CustomFieldStringValue)objProp.getVal()).getValue();
6320+
}
6321+
else if (objProp.getName().equals("summary.config.numCpu")) {
62946322
numberCPUs = objProp.getVal().toString();
62956323
} else if (objProp.getName().equals("summary.quickStats.overallCpuUsage")) {
62966324
maxCpuUsage = objProp.getVal().toString();
62976325
}
62986326
}
62996327
VirtualMachineMO vmMo = new VirtualMachineMO(hyperHost.getContext(), oc.getObj());
6300-
// Check if vmMo has the custom property CLOUD_VM_INTERNAL_NAME set.
6301-
vmInternalCSName = vmMo.getCustomFieldValue(CustomFieldConstants.CLOUD_VM_INTERNAL_NAME);
6302-
63036328
if (vmInternalCSName != null) {
63046329
name = vmInternalCSName;
63056330
} else {

‎vmware-base/src/com/cloud/hypervisor/vmware/mo/ClusterMO.java‎

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -98,25 +98,28 @@ public ManagedObjectReference getHyperHostCluster() throws Exception {
9898

9999
@Override
100100
public VirtualMachineMO findVmOnHyperHost(String name) throws Exception {
101-
ObjectContent[] ocs = getVmPropertiesOnHyperHost(new String[] { "name" });
102-
return HypervisorHostHelper.findVmFromObjectContent(_context, ocs, name);
101+
102+
int key = getCustomFieldKey("VirtualMachine", CustomFieldConstants.CLOUD_VM_INTERNAL_NAME);
103+
if(key == 0) {
104+
s_logger.warn("Custom field " + CustomFieldConstants.CLOUD_VM_INTERNAL_NAME + " is not registered ?!");
105+
}
106+
107+
String instanceNameCustomField = "value[" + key + "]";
108+
ObjectContent[] ocs = getVmPropertiesOnHyperHost(new String[] { "name", instanceNameCustomField });
109+
return HypervisorHostHelper.findVmFromObjectContent(_context, ocs, name, instanceNameCustomField);
103110
}
104111

105112
@Override
106113
public VirtualMachineMO findVmOnPeerHyperHost(String name) throws Exception {
107-
ObjectContent[] ocs = getVmPropertiesOnHyperHost(new String[] { "name" });
108-
if(ocs != null && ocs.length > 0) {
109-
for(ObjectContent oc : ocs) {
110-
List<DynamicProperty> props = oc.getPropSet();
111-
if(props != null) {
112-
for(DynamicProperty prop : props) {
113-
if(prop.getVal().toString().equals(name))
114-
return new VirtualMachineMO(_context, oc.getObj());
115-
}
116-
}
117-
}
114+
int key = getCustomFieldKey("VirtualMachine", CustomFieldConstants.CLOUD_VM_INTERNAL_NAME);
115+
if(key == 0) {
116+
s_logger.warn("Custom field " + CustomFieldConstants.CLOUD_VM_INTERNAL_NAME + " is not registered ?!");
118117
}
119-
return null;
118+
119+
String instanceNameCustomField = "value[" + key + "]";
120+
121+
ObjectContent[] ocs = getVmPropertiesOnHyperHost(new String[] { "name", instanceNameCustomField });
122+
return HypervisorHostHelper.findVmFromObjectContent(_context, ocs, name, instanceNameCustomField);
120123
}
121124

122125
@Override

‎vmware-base/src/com/cloud/hypervisor/vmware/mo/HostMO.java‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -510,9 +510,14 @@ private void loadVmCache() throws Exception {
510510

511511
_vmCache.clear();
512512

513+
int key = getCustomFieldKey("VirtualMachine", CustomFieldConstants.CLOUD_VM_INTERNAL_NAME);
514+
if(key == 0) {
515+
s_logger.warn("Custom field " + CustomFieldConstants.CLOUD_VM_INTERNAL_NAME + " is not registered ?!");
516+
}
517+
513518
// name is the name of the VM as it appears in vCenter. The CLOUD_VM_INTERNAL_NAME custom
514519
// field value contains the name of the VM as it is maintained internally by cloudstack (i-x-y).
515-
ObjectContent[] ocs = getVmPropertiesOnHyperHost(new String[] { "name" });
520+
ObjectContent[] ocs = getVmPropertiesOnHyperHost(new String[] { "name", "value[" + key + "]" });
516521
if(ocs != null && ocs.length > 0) {
517522
for(ObjectContent oc : ocs) {
518523
List<DynamicProperty> props = oc.getPropSet();
@@ -522,11 +527,11 @@ private void loadVmCache() throws Exception {
522527
for (DynamicProperty prop : props) {
523528
if (prop.getName().equals("name")) {
524529
vmVcenterName = prop.getVal().toString();
525-
}
530+
} else if(prop.getName().startsWith("value[")) {
531+
if(prop.getVal() != null)
532+
vmInternalCSName = ((CustomFieldStringValue)prop.getVal()).getValue();
533+
}
526534
}
527-
VirtualMachineMO vmMo = new VirtualMachineMO(_context, oc.getObj());
528-
// Check if vmMo has the custom property CLOUD_VM_INTERNAL_NAME set.
529-
vmInternalCSName = vmMo.getCustomFieldValue(CustomFieldConstants.CLOUD_VM_INTERNAL_NAME);
530535
String vmName = null;
531536
if (vmInternalCSName != null && isUserVMInternalCSName(vmInternalCSName)) {
532537
vmName = vmInternalCSName;

‎vmware-base/src/com/cloud/hypervisor/vmware/mo/HypervisorHostHelper.java‎

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
import com.vmware.vim25.AlreadyExistsFaultMsg;
3131
import com.vmware.vim25.BoolPolicy;
32+
import com.vmware.vim25.CustomFieldStringValue;
3233
import com.vmware.vim25.DVPortSetting;
3334
import com.vmware.vim25.DVPortgroupConfigInfo;
3435
import com.vmware.vim25.DVPortgroupConfigSpec;
@@ -46,7 +47,6 @@
4647
import com.vmware.vim25.LongPolicy;
4748
import com.vmware.vim25.ManagedObjectReference;
4849
import com.vmware.vim25.ObjectContent;
49-
import com.vmware.vim25.OptionValue;
5050
import com.vmware.vim25.OvfCreateImportSpecParams;
5151
import com.vmware.vim25.OvfCreateImportSpecResult;
5252
import com.vmware.vim25.OvfFileItem;
@@ -91,8 +91,8 @@ public class HypervisorHostHelper {
9191
private static final String UNTAGGED_VLAN_NAME = "untagged";
9292

9393
public static VirtualMachineMO findVmFromObjectContent(VmwareContext context,
94-
ObjectContent[] ocs, String name) {
95-
94+
ObjectContent[] ocs, String name, String instanceNameCustomField) {
95+
9696
if(ocs != null && ocs.length > 0) {
9797
for(ObjectContent oc : ocs) {
9898
String vmNameInvCenter = null;
@@ -102,16 +102,14 @@ public static VirtualMachineMO findVmFromObjectContent(VmwareContext context,
102102
for(DynamicProperty objProp : objProps) {
103103
if(objProp.getName().equals("name")) {
104104
vmNameInvCenter = (String)objProp.getVal();
105+
} else if(objProp.getName().contains(instanceNameCustomField)) {
106+
if(objProp.getVal() != null)
107+
vmInternalCSName = ((CustomFieldStringValue)objProp.getVal()).getValue();
105108
}
106-
VirtualMachineMO vmMo = new VirtualMachineMO(context, oc.getObj());
107-
// Check if vmMo has the custom property CLOUD_VM_INTERNAL_NAME set.
108-
try {
109-
vmInternalCSName = vmMo.getCustomFieldValue(CustomFieldConstants.CLOUD_VM_INTERNAL_NAME);
110-
} catch (Exception e) {
111-
s_logger.error("Unable to retrieve custom field value for internal VM name");
112-
}
109+
113110
if ( (vmNameInvCenter != null && name.equalsIgnoreCase(vmNameInvCenter))
114111
|| (vmInternalCSName != null && name.equalsIgnoreCase(vmInternalCSName)) ) {
112+
VirtualMachineMO vmMo = new VirtualMachineMO(context, oc.getObj());
115113
return vmMo;
116114
}
117115
}

0 commit comments

Comments
 (0)