Skip to content

Commit f3c917d

Browse files
committed
CLOUDSTACK-4734: Validate and Fail-over to another VmwareContext object when it is from the pool
1 parent 08c3b65 commit f3c917d

5 files changed

Lines changed: 71 additions & 11 deletions

File tree

plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/resource/VmwareContextFactory.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,15 @@ public static VmwareContext create(String vCenterAddress, String vCenterUserName
8080

8181
public static VmwareContext getContext(String vCenterAddress, String vCenterUserName, String vCenterPassword) throws Exception {
8282
VmwareContext context = s_pool.getContext(vCenterAddress, vCenterUserName);
83-
if(context == null)
83+
if(context == null) {
8484
context = create(vCenterAddress, vCenterUserName, vCenterPassword);
85+
} else {
86+
if(!context.validate()) {
87+
s_logger.info("Validation of the context faild. dispose and create a new one");
88+
context.close();
89+
context = create(vCenterAddress, vCenterUserName, vCenterPassword);
90+
}
91+
}
8592

8693
if(context != null) {
8794
context.registerStockObject(VmwareManager.CONTEXT_STOCK_NAME, s_vmwareMgr);

plugins/hypervisors/vmware/src/com/cloud/storage/resource/VmwareSecondaryStorageContextFactory.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@
1616
// under the License.
1717
package com.cloud.storage.resource;
1818

19+
import org.apache.log4j.Logger;
20+
1921
import com.cloud.hypervisor.vmware.util.VmwareClient;
2022
import com.cloud.hypervisor.vmware.util.VmwareContext;
2123
import com.cloud.hypervisor.vmware.util.VmwareContextPool;
2224

2325
public class VmwareSecondaryStorageContextFactory {
26+
private static final Logger s_logger = Logger.getLogger(VmwareSecondaryStorageContextFactory.class);
27+
2428
private static volatile int s_seq = 1;
2529

2630
private static VmwareContextPool s_pool;
@@ -51,6 +55,12 @@ public static VmwareContext getContext(String vCenterAddress, String vCenterUser
5155
VmwareContext context = s_pool.getContext(vCenterAddress, vCenterUserName);
5256
if(context == null) {
5357
context = create(vCenterAddress, vCenterUserName, vCenterPassword);
58+
} else {
59+
if(!context.validate()) {
60+
s_logger.info("Validation of the context faild. dispose and create a new one");
61+
context.close();
62+
context = create(vCenterAddress, vCenterUserName, vCenterPassword);
63+
}
5464
}
5565

5666
if(context != null) {

vmware-base/src/com/cloud/hypervisor/vmware/mo/DatacenterMO.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import java.util.ArrayList;
2121
import java.util.List;
2222

23+
import org.apache.log4j.Logger;
24+
2325
import com.cloud.hypervisor.vmware.util.VmwareContext;
2426
import com.cloud.utils.Pair;
2527
import com.vmware.vim25.CustomFieldStringValue;
@@ -37,6 +39,7 @@
3739
import java.util.Arrays;
3840

3941
public class DatacenterMO extends BaseMO {
42+
private static final Logger s_logger = Logger.getLogger(DatacenterMO.class);
4043

4144
public DatacenterMO(VmwareContext context, ManagedObjectReference morDc) {
4245
super(context, morDc);
@@ -50,7 +53,9 @@ public DatacenterMO(VmwareContext context, String dcName) throws Exception {
5053
super(context, null);
5154

5255
_mor = _context.getVimClient().getDecendentMoRef(_context.getRootFolder(), "Datacenter", dcName);
53-
assert(_mor != null);
56+
if(_mor == null) {
57+
s_logger.error("Unable to locate DC " + dcName);
58+
}
5459
}
5560

5661
@Override
@@ -61,7 +66,6 @@ public String getName() throws Exception {
6166
public void registerTemplate(ManagedObjectReference morHost, String datastoreName,
6267
String templateName, String templateFileName) throws Exception {
6368

64-
6569
ManagedObjectReference morFolder = (ManagedObjectReference)_context.getVimClient().getDynamicProperty(
6670
_mor, "vmFolder");
6771
assert(morFolder != null);

vmware-base/src/com/cloud/hypervisor/vmware/util/VmwareClient.java

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ private static void trustAllHttpsCertificates() throws Exception {
107107
}
108108

109109
private ManagedObjectReference SVC_INST_REF = new ManagedObjectReference();
110-
private ManagedObjectReference propCollectorRef;
111110
private static VimService vimService;
112111
private VimPortType vimPort;
113112
private String serviceCookie;
@@ -151,8 +150,6 @@ public void connect(String url, String userName, String password) throws Excepti
151150

152151
vimPort.login(serviceContent.getSessionManager(), userName, password, null);
153152
isConnected = true;
154-
155-
propCollectorRef = serviceContent.getPropertyCollector();
156153
}
157154

158155
/**
@@ -197,7 +194,7 @@ public String getServiceCookie() {
197194
* @return Service property collector
198195
*/
199196
public ManagedObjectReference getPropCol() {
200-
return propCollectorRef;
197+
return getServiceContent().getPropertyCollector();
201198
}
202199

203200
/**
@@ -207,6 +204,43 @@ public ManagedObjectReference getRootFolder() {
207204
return getServiceContent().getRootFolder();
208205
}
209206

207+
public boolean validate() {
208+
//
209+
// There is no official API to validate an open vCenter API session. This is hacking way to tell if
210+
// an open vCenter API session is still valid for making calls.
211+
//
212+
// It will give false result if there really does not exist data-center in the inventory, however, I consider
213+
// this really is not possible in production deployment
214+
//
215+
216+
// Create PropertySpecs
217+
PropertySpec pSpec = new PropertySpec();
218+
pSpec.setType("Datacenter");
219+
pSpec.setAll(false);
220+
pSpec.getPathSet().add("name");
221+
222+
ObjectSpec oSpec = new ObjectSpec();
223+
oSpec.setObj(getRootFolder());
224+
oSpec.setSkip(false);
225+
oSpec.getSelectSet().addAll(constructCompleteTraversalSpec());
226+
227+
PropertyFilterSpec spec = new PropertyFilterSpec();
228+
spec.getPropSet().add(pSpec);
229+
spec.getObjectSet().add(oSpec);
230+
List<PropertyFilterSpec> specArr = new ArrayList<PropertyFilterSpec>();
231+
specArr.add(spec);
232+
233+
try {
234+
List<ObjectContent> ocary = vimPort.retrieveProperties(getPropCol(), specArr);
235+
if(ocary != null && ocary.size() > 0)
236+
return true;
237+
} catch(Exception e) {
238+
return false;
239+
}
240+
241+
return false;
242+
}
243+
210244
/**
211245
* Get the property value of a managed object.
212246
*
@@ -266,7 +300,7 @@ private List<ObjectContent> retrieveMoRefProperties(ManagedObjectReference mObj,
266300
List<PropertyFilterSpec> specArr = new ArrayList<PropertyFilterSpec>();
267301
specArr.add(spec);
268302

269-
return vimPort.retrieveProperties(propCollectorRef, specArr);
303+
return vimPort.retrieveProperties(getPropCol(), specArr);
270304
}
271305

272306
/**
@@ -334,7 +368,8 @@ private Object[] waitForValues(ManagedObjectReference objmor, String[] filterPro
334368
pSpec.setType(objmor.getType());
335369
spec.getPropSet().add(pSpec);
336370

337-
ManagedObjectReference filterSpecRef = vimPort.createFilter(propCollectorRef, spec, true);
371+
ManagedObjectReference propertyCollector = this.getPropCol();
372+
ManagedObjectReference filterSpecRef = vimPort.createFilter(propertyCollector, spec, true);
338373

339374
boolean reached = false;
340375

@@ -343,7 +378,7 @@ private Object[] waitForValues(ManagedObjectReference objmor, String[] filterPro
343378
List<ObjectUpdate> objupary = null;
344379
List<PropertyChange> propchgary = null;
345380
while (!reached) {
346-
updateset = vimPort.waitForUpdates(propCollectorRef, version);
381+
updateset = vimPort.waitForUpdates(propertyCollector, version);
347382
if (updateset == null || updateset.getFilterSet() == null) {
348383
continue;
349384
}
@@ -546,7 +581,7 @@ public ManagedObjectReference getDecendentMoRef(ManagedObjectReference root, Str
546581
List<PropertyFilterSpec> specArr = new ArrayList<PropertyFilterSpec>();
547582
specArr.add(spec);
548583

549-
List<ObjectContent> ocary = vimPort.retrieveProperties(propCollectorRef, specArr);
584+
List<ObjectContent> ocary = vimPort.retrieveProperties(getPropCol(), specArr);
550585

551586
if (ocary == null || ocary.size() == 0) {
552587
return null;

vmware-base/src/com/cloud/hypervisor/vmware/util/VmwareContext.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ public VmwareContext(VmwareClient client, String address) {
102102
if(s_logger.isInfoEnabled())
103103
s_logger.info("New VmwareContext object, current outstanding count: " + getOutstandingContextCount());
104104
}
105+
106+
public boolean validate() {
107+
return _vimClient.validate();
108+
}
105109

106110
public void registerStockObject(String name, Object obj) {
107111
synchronized(_stockMap) {

0 commit comments

Comments
 (0)