CLOUDSTACK-4734: Validate and Fail-over to another VmwareContext object when it is from the pool

This commit is contained in:
Kelven Yang 2013-09-27 15:11:07 -07:00
parent 08c3b6596d
commit f3c917db05
5 changed files with 71 additions and 11 deletions

View File

@ -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);

View File

@ -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) {

View File

@ -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 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);

View File

@ -107,7 +107,6 @@ public class VmwareClient {
}
private ManagedObjectReference SVC_INST_REF = new ManagedObjectReference();
private ManagedObjectReference propCollectorRef;
private static VimService vimService;
private VimPortType vimPort;
private String serviceCookie;
@ -151,8 +150,6 @@ public class VmwareClient {
vimPort.login(serviceContent.getSessionManager(), userName, password, null);
isConnected = true;
propCollectorRef = serviceContent.getPropertyCollector();
}
/**
@ -197,7 +194,7 @@ public class VmwareClient {
* @return Service property collector
*/
public ManagedObjectReference getPropCol() {
return propCollectorRef;
return getServiceContent().getPropertyCollector();
}
/**
@ -207,6 +204,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.
*
@ -266,7 +300,7 @@ public class VmwareClient {
List<PropertyFilterSpec> specArr = new ArrayList<PropertyFilterSpec>();
specArr.add(spec);
return vimPort.retrieveProperties(propCollectorRef, specArr);
return vimPort.retrieveProperties(getPropCol(), specArr);
}
/**
@ -334,7 +368,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;
@ -343,7 +378,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;
}
@ -546,7 +581,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;

View File

@ -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) {