Skip to content

Commit 3c3cdbe

Browse files
authored
Merge pull request #1872 from sateesh-chodapuneedi/pr-cloudstack-3223
CLOUDSTACK-3223 Exception observed while creating CPVM in VMware Setup with DVS
2 parents 7803c12 + 9bed11d commit 3c3cdbe

1 file changed

Lines changed: 47 additions & 3 deletions

File tree

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

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.HashMap;
2121
import java.util.List;
2222
import java.util.Map;
23+
import java.util.concurrent.ConcurrentHashMap;
2324

2425
import org.apache.log4j.Logger;
2526

@@ -36,24 +37,67 @@
3637
public class DistributedVirtualSwitchMO extends BaseMO {
3738
@SuppressWarnings("unused")
3839
private static final Logger s_logger = Logger.getLogger(DistributedVirtualSwitchMO.class);
40+
private static ConcurrentHashMap<String, List<String>> s_dvPortGroupCacheMap = null;
3941

4042
public DistributedVirtualSwitchMO(VmwareContext context, ManagedObjectReference morDvs) {
4143
super(context, morDvs);
44+
s_dvPortGroupCacheMap = new ConcurrentHashMap<String, List<String>>();
4245
}
4346

4447
public DistributedVirtualSwitchMO(VmwareContext context, String morType, String morValue) {
4548
super(context, morType, morValue);
49+
s_dvPortGroupCacheMap = new ConcurrentHashMap<String, List<String>>();
4650
}
4751

4852
public void createDVPortGroup(DVPortgroupConfigSpec dvPortGroupSpec) throws Exception {
4953
List<DVPortgroupConfigSpec> dvPortGroupSpecArray = new ArrayList<DVPortgroupConfigSpec>();
5054
dvPortGroupSpecArray.add(dvPortGroupSpec);
51-
_context.getService().addDVPortgroupTask(_mor, dvPortGroupSpecArray);
55+
boolean dvPortGroupExists = false;
56+
String dvSwitchInstance = _mor.getValue();
57+
String dvPortGroupName = dvPortGroupSpec.getName();
58+
String uniquedvPortGroupPerDvs = dvSwitchInstance + dvPortGroupName;
59+
List<String> dvPortGroupList = null;
60+
synchronized (uniquedvPortGroupPerDvs.intern()) {
61+
// Looking up local cache rather than firing another API call to see if dvPortGroup exists already.
62+
if (s_dvPortGroupCacheMap.containsKey(dvSwitchInstance)) {
63+
dvPortGroupList = s_dvPortGroupCacheMap.get(dvSwitchInstance);
64+
if (dvPortGroupList != null && dvPortGroupList.contains(dvPortGroupName)) {
65+
dvPortGroupExists = true;
66+
}
67+
}
68+
if (!dvPortGroupExists) {
69+
ManagedObjectReference task = _context.getService().addDVPortgroupTask(_mor, dvPortGroupSpecArray);
70+
if (!_context.getVimClient().waitForTask(task)) {
71+
throw new Exception("Failed to create dvPortGroup " + dvPortGroupSpec.getName());
72+
} else {
73+
if (s_dvPortGroupCacheMap.containsKey(dvSwitchInstance)) {
74+
dvPortGroupList = s_dvPortGroupCacheMap.get(dvSwitchInstance);
75+
if (dvPortGroupList == null) {
76+
dvPortGroupList = new ArrayList<String>();
77+
}
78+
dvPortGroupList.add(dvPortGroupName); //does this update map?
79+
} else {
80+
dvPortGroupList = new ArrayList<String>();
81+
dvPortGroupList.add(dvPortGroupName);
82+
s_dvPortGroupCacheMap.put(dvSwitchInstance, dvPortGroupList);
83+
}
84+
}
85+
if (s_logger.isTraceEnabled()) {
86+
s_logger.trace("Created dvPortGroup. dvPortGroup cache is :" + s_dvPortGroupCacheMap);
87+
}
88+
} else if (s_logger.isDebugEnabled()) {
89+
s_logger.debug("Detected dvPortGroup [" + dvPortGroupName + "] already present. Not attempting to create again.");
90+
}
91+
}
5292
}
5393

5494
public void updateDvPortGroup(ManagedObjectReference dvPortGroupMor, DVPortgroupConfigSpec dvPortGroupSpec) throws Exception {
55-
// TODO(sateesh): Update numPorts
56-
_context.getService().reconfigureDVPortgroupTask(dvPortGroupMor, dvPortGroupSpec);
95+
synchronized (dvPortGroupMor.getValue().intern()) {
96+
ManagedObjectReference task = _context.getService().reconfigureDVPortgroupTask(dvPortGroupMor, dvPortGroupSpec);
97+
if (!_context.getVimClient().waitForTask(task)) {
98+
throw new Exception("Failed to update dvPortGroup " + dvPortGroupMor.getValue());
99+
}
100+
}
57101
}
58102

59103
public void updateVMWareDVSwitch(ManagedObjectReference dvSwitchMor, VMwareDVSConfigSpec dvsSpec) throws Exception {

0 commit comments

Comments
 (0)