Skip to content

Commit 94c8e28

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

File tree

5 files changed

+71
-11
lines changed

5 files changed

+71
-11
lines changed

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 edu.emory.mathcs.backport.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
@@ -109,7 +109,6 @@ private static void trustAllHttpsCertificates() throws Exception {
109109
}
110110

111111
private ManagedObjectReference SVC_INST_REF = new ManagedObjectReference();
112-
private ManagedObjectReference propCollectorRef;
113112
private static VimService vimService;
114113
private VimPortType vimPort;
115114
private String serviceCookie;
@@ -153,8 +152,6 @@ public void connect(String url, String userName, String password) throws Excepti
153152

154153
vimPort.login(serviceContent.getSessionManager(), userName, password, null);
155154
isConnected = true;
156-
157-
propCollectorRef = serviceContent.getPropertyCollector();
158155
}
159156

160157
/**
@@ -199,7 +196,7 @@ public String getServiceCookie() {
199196
* @return Service property collector
200197
*/
201198
public ManagedObjectReference getPropCol() {
202-
return propCollectorRef;
199+
return getServiceContent().getPropertyCollector();
203200
}
204201

205202
/**
@@ -209,6 +206,43 @@ public ManagedObjectReference getRootFolder() {
209206
return getServiceContent().getRootFolder();
210207
}
211208

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

271-
return vimPort.retrieveProperties(propCollectorRef, specArr);
305+
return vimPort.retrieveProperties(getPropCol(), specArr);
272306
}
273307

274308
public boolean waitForTask2(ManagedObjectReference task) throws RuntimeFaultFaultMsg, RemoteException, InterruptedException {
@@ -416,7 +450,8 @@ private Object[] waitForValues(ManagedObjectReference objmor, String[] filterPro
416450
pSpec.setType(objmor.getType());
417451
spec.getPropSet().add(pSpec);
418452

419-
ManagedObjectReference filterSpecRef = vimPort.createFilter(propCollectorRef, spec, true);
453+
ManagedObjectReference propertyCollector = this.getPropCol();
454+
ManagedObjectReference filterSpecRef = vimPort.createFilter(propertyCollector, spec, true);
420455

421456
boolean reached = false;
422457

@@ -425,7 +460,7 @@ private Object[] waitForValues(ManagedObjectReference objmor, String[] filterPro
425460
List<ObjectUpdate> objupary = null;
426461
List<PropertyChange> propchgary = null;
427462
while (!reached) {
428-
updateset = vimPort.waitForUpdates(propCollectorRef, version);
463+
updateset = vimPort.waitForUpdates(propertyCollector, version);
429464
if (updateset == null || updateset.getFilterSet() == null) {
430465
continue;
431466
}
@@ -628,7 +663,7 @@ public ManagedObjectReference getDecendentMoRef(ManagedObjectReference root, Str
628663
List<PropertyFilterSpec> specArr = new ArrayList<PropertyFilterSpec>();
629664
specArr.add(spec);
630665

631-
List<ObjectContent> ocary = vimPort.retrieveProperties(propCollectorRef, specArr);
666+
List<ObjectContent> ocary = vimPort.retrieveProperties(getPropCol(), specArr);
632667

633668
if (ocary == null || ocary.size() == 0) {
634669
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)