CLOUDSTACK-4734: Validate and Fail-over to another VmwareContext object when it is from the pool
Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/94c8e28f Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/94c8e28f Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/94c8e28f Branch: refs/heads/4.2 Commit: 94c8e28f8d92ce711a5aaeabed52bb69ef3a6700 Parents: c9aa880 Author: Kelven Yang <kelv...@gmail.com> Authored: Fri Sep 27 15:11:07 2013 -0700 Committer: Kelven Yang <kelv...@gmail.com> Committed: Fri Sep 27 15:24:16 2013 -0700 ---------------------------------------------------------------------- .../vmware/resource/VmwareContextFactory.java | 9 +++- .../VmwareSecondaryStorageContextFactory.java | 10 ++++ .../hypervisor/vmware/mo/DatacenterMO.java | 8 ++- .../hypervisor/vmware/util/VmwareClient.java | 51 +++++++++++++++++--- .../hypervisor/vmware/util/VmwareContext.java | 4 ++ 5 files changed, 71 insertions(+), 11 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cloudstack/blob/94c8e28f/plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/resource/VmwareContextFactory.java ---------------------------------------------------------------------- diff --git a/plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/resource/VmwareContextFactory.java b/plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/resource/VmwareContextFactory.java index ed607e1..3079998 100755 --- a/plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/resource/VmwareContextFactory.java +++ b/plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/resource/VmwareContextFactory.java @@ -80,8 +80,15 @@ public class VmwareContextFactory { public static VmwareContext getContext(String vCenterAddress, String vCenterUserName, String vCenterPassword) throws Exception { VmwareContext context = s_pool.getContext(vCenterAddress, vCenterUserName); - if(context == null) + if(context == null) { context = create(vCenterAddress, vCenterUserName, vCenterPassword); + } else { + if(!context.validate()) { + s_logger.info("Validation of the context faild. dispose and create a new one"); + context.close(); + context = create(vCenterAddress, vCenterUserName, vCenterPassword); + } + } if(context != null) { context.registerStockObject(VmwareManager.CONTEXT_STOCK_NAME, s_vmwareMgr); http://git-wip-us.apache.org/repos/asf/cloudstack/blob/94c8e28f/plugins/hypervisors/vmware/src/com/cloud/storage/resource/VmwareSecondaryStorageContextFactory.java ---------------------------------------------------------------------- diff --git a/plugins/hypervisors/vmware/src/com/cloud/storage/resource/VmwareSecondaryStorageContextFactory.java b/plugins/hypervisors/vmware/src/com/cloud/storage/resource/VmwareSecondaryStorageContextFactory.java index 5365e58..253d6fd 100644 --- a/plugins/hypervisors/vmware/src/com/cloud/storage/resource/VmwareSecondaryStorageContextFactory.java +++ b/plugins/hypervisors/vmware/src/com/cloud/storage/resource/VmwareSecondaryStorageContextFactory.java @@ -16,11 +16,15 @@ // under the License. package com.cloud.storage.resource; +import org.apache.log4j.Logger; + import com.cloud.hypervisor.vmware.util.VmwareClient; import com.cloud.hypervisor.vmware.util.VmwareContext; import com.cloud.hypervisor.vmware.util.VmwareContextPool; public class VmwareSecondaryStorageContextFactory { + private static final Logger s_logger = Logger.getLogger(VmwareSecondaryStorageContextFactory.class); + private static volatile int s_seq = 1; private static VmwareContextPool s_pool; @@ -51,6 +55,12 @@ public class VmwareSecondaryStorageContextFactory { VmwareContext context = s_pool.getContext(vCenterAddress, vCenterUserName); if(context == null) { context = create(vCenterAddress, vCenterUserName, vCenterPassword); + } else { + if(!context.validate()) { + s_logger.info("Validation of the context faild. dispose and create a new one"); + context.close(); + context = create(vCenterAddress, vCenterUserName, vCenterPassword); + } } if(context != null) { http://git-wip-us.apache.org/repos/asf/cloudstack/blob/94c8e28f/vmware-base/src/com/cloud/hypervisor/vmware/mo/DatacenterMO.java ---------------------------------------------------------------------- diff --git a/vmware-base/src/com/cloud/hypervisor/vmware/mo/DatacenterMO.java b/vmware-base/src/com/cloud/hypervisor/vmware/mo/DatacenterMO.java index cabb60a..6d82aef 100755 --- a/vmware-base/src/com/cloud/hypervisor/vmware/mo/DatacenterMO.java +++ b/vmware-base/src/com/cloud/hypervisor/vmware/mo/DatacenterMO.java @@ -20,6 +20,8 @@ package com.cloud.hypervisor.vmware.mo; import java.util.ArrayList; import java.util.List; +import org.apache.log4j.Logger; + import com.cloud.hypervisor.vmware.util.VmwareContext; import com.cloud.utils.Pair; import com.vmware.vim25.CustomFieldStringValue; @@ -37,6 +39,7 @@ import com.vmware.vim25.VirtualEthernetCardDistributedVirtualPortBackingInfo; import edu.emory.mathcs.backport.java.util.Arrays; public class DatacenterMO extends BaseMO { + private static final Logger s_logger = Logger.getLogger(DatacenterMO.class); public DatacenterMO(VmwareContext context, ManagedObjectReference morDc) { super(context, morDc); @@ -50,7 +53,9 @@ public class DatacenterMO extends BaseMO { super(context, null); _mor = _context.getVimClient().getDecendentMoRef(_context.getRootFolder(), "Datacenter", dcName); - assert(_mor != null); + if(_mor == null) { + s_logger.error("Unable to locate DC " + dcName); + } } @Override @@ -61,7 +66,6 @@ public class DatacenterMO extends BaseMO { public void registerTemplate(ManagedObjectReference morHost, String datastoreName, String templateName, String templateFileName) throws Exception { - ManagedObjectReference morFolder = (ManagedObjectReference)_context.getVimClient().getDynamicProperty( _mor, "vmFolder"); assert(morFolder != null); http://git-wip-us.apache.org/repos/asf/cloudstack/blob/94c8e28f/vmware-base/src/com/cloud/hypervisor/vmware/util/VmwareClient.java ---------------------------------------------------------------------- diff --git a/vmware-base/src/com/cloud/hypervisor/vmware/util/VmwareClient.java b/vmware-base/src/com/cloud/hypervisor/vmware/util/VmwareClient.java index 6ab9700..ff13b1c 100644 --- a/vmware-base/src/com/cloud/hypervisor/vmware/util/VmwareClient.java +++ b/vmware-base/src/com/cloud/hypervisor/vmware/util/VmwareClient.java @@ -109,7 +109,6 @@ public class VmwareClient { } private ManagedObjectReference SVC_INST_REF = new ManagedObjectReference(); - private ManagedObjectReference propCollectorRef; private static VimService vimService; private VimPortType vimPort; private String serviceCookie; @@ -153,8 +152,6 @@ public class VmwareClient { vimPort.login(serviceContent.getSessionManager(), userName, password, null); isConnected = true; - - propCollectorRef = serviceContent.getPropertyCollector(); } /** @@ -199,7 +196,7 @@ public class VmwareClient { * @return Service property collector */ public ManagedObjectReference getPropCol() { - return propCollectorRef; + return getServiceContent().getPropertyCollector(); } /** @@ -209,6 +206,43 @@ public class VmwareClient { return getServiceContent().getRootFolder(); } + public boolean validate() { + // + // There is no official API to validate an open vCenter API session. This is hacking way to tell if + // an open vCenter API session is still valid for making calls. + // + // It will give false result if there really does not exist data-center in the inventory, however, I consider + // this really is not possible in production deployment + // + + // Create PropertySpecs + PropertySpec pSpec = new PropertySpec(); + pSpec.setType("Datacenter"); + pSpec.setAll(false); + pSpec.getPathSet().add("name"); + + ObjectSpec oSpec = new ObjectSpec(); + oSpec.setObj(getRootFolder()); + oSpec.setSkip(false); + oSpec.getSelectSet().addAll(constructCompleteTraversalSpec()); + + PropertyFilterSpec spec = new PropertyFilterSpec(); + spec.getPropSet().add(pSpec); + spec.getObjectSet().add(oSpec); + List<PropertyFilterSpec> specArr = new ArrayList<PropertyFilterSpec>(); + specArr.add(spec); + + try { + List<ObjectContent> ocary = vimPort.retrieveProperties(getPropCol(), specArr); + if(ocary != null && ocary.size() > 0) + return true; + } catch(Exception e) { + return false; + } + + return false; + } + /** * Get the property value of a managed object. * @@ -268,7 +302,7 @@ public class VmwareClient { List<PropertyFilterSpec> specArr = new ArrayList<PropertyFilterSpec>(); specArr.add(spec); - return vimPort.retrieveProperties(propCollectorRef, specArr); + return vimPort.retrieveProperties(getPropCol(), specArr); } public boolean waitForTask2(ManagedObjectReference task) throws RuntimeFaultFaultMsg, RemoteException, InterruptedException { @@ -416,7 +450,8 @@ public class VmwareClient { pSpec.setType(objmor.getType()); spec.getPropSet().add(pSpec); - ManagedObjectReference filterSpecRef = vimPort.createFilter(propCollectorRef, spec, true); + ManagedObjectReference propertyCollector = this.getPropCol(); + ManagedObjectReference filterSpecRef = vimPort.createFilter(propertyCollector, spec, true); boolean reached = false; @@ -425,7 +460,7 @@ public class VmwareClient { List<ObjectUpdate> objupary = null; List<PropertyChange> propchgary = null; while (!reached) { - updateset = vimPort.waitForUpdates(propCollectorRef, version); + updateset = vimPort.waitForUpdates(propertyCollector, version); if (updateset == null || updateset.getFilterSet() == null) { continue; } @@ -628,7 +663,7 @@ public class VmwareClient { List<PropertyFilterSpec> specArr = new ArrayList<PropertyFilterSpec>(); specArr.add(spec); - List<ObjectContent> ocary = vimPort.retrieveProperties(propCollectorRef, specArr); + List<ObjectContent> ocary = vimPort.retrieveProperties(getPropCol(), specArr); if (ocary == null || ocary.size() == 0) { return null; http://git-wip-us.apache.org/repos/asf/cloudstack/blob/94c8e28f/vmware-base/src/com/cloud/hypervisor/vmware/util/VmwareContext.java ---------------------------------------------------------------------- diff --git a/vmware-base/src/com/cloud/hypervisor/vmware/util/VmwareContext.java b/vmware-base/src/com/cloud/hypervisor/vmware/util/VmwareContext.java index 6c3dab7..c499576 100755 --- a/vmware-base/src/com/cloud/hypervisor/vmware/util/VmwareContext.java +++ b/vmware-base/src/com/cloud/hypervisor/vmware/util/VmwareContext.java @@ -102,6 +102,10 @@ public class VmwareContext { if(s_logger.isInfoEnabled()) s_logger.info("New VmwareContext object, current outstanding count: " + getOutstandingContextCount()); } + + public boolean validate() { + return _vimClient.validate(); + } public void registerStockObject(String name, Object obj) { synchronized(_stockMap) {