Skip to content

Commit 0becde9

Browse files
committed
CLOUDSTACK-3568: Refactore CloudStack/vCenter API session management facility to avoid the situation that multiple API requests are sharing with the same vCenter API session
1 parent c6dfb33 commit 0becde9

7 files changed

Lines changed: 354 additions & 151 deletions

File tree

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,23 @@
2525
import com.cloud.hypervisor.vmware.manager.VmwareManager;
2626
import com.cloud.hypervisor.vmware.util.VmwareClient;
2727
import com.cloud.hypervisor.vmware.util.VmwareContext;
28+
import com.cloud.hypervisor.vmware.util.VmwareContextPool;
2829
import com.cloud.utils.StringUtils;
2930

3031
@Component
3132
public class VmwareContextFactory {
32-
3333
private static final Logger s_logger = Logger.getLogger(VmwareContextFactory.class);
3434

3535
private static volatile int s_seq = 1;
3636
private static VmwareManager s_vmwareMgr;
37+
private static VmwareContextPool s_pool;
3738

3839
@Inject VmwareManager _vmwareMgr;
3940

4041
static {
4142
// skip certificate check
4243
System.setProperty("axis.socketSecureFactory", "org.apache.axis.components.net.SunFakeTrustSocketFactory");
43-
//s_vmwareMgr = ComponentContext.inject(VmwareManagerImpl.class);
44+
s_pool = new VmwareContextPool();
4445
}
4546

4647
@PostConstruct
@@ -54,8 +55,6 @@ public static VmwareContext create(String vCenterAddress, String vCenterUserName
5455
assert(vCenterPassword != null);
5556

5657
String serviceUrl = "https://" + vCenterAddress + "/sdk/vimService";
57-
//String[] params = new String[] {"--url", serviceUrl, "--username", vCenterUserName, "--password", vCenterPassword };
58-
5958
if(s_logger.isDebugEnabled())
6059
s_logger.debug("initialize VmwareContext. url: " + serviceUrl + ", username: " + vCenterUserName + ", password: " + StringUtils.getMaskedPasswordForDisplay(vCenterPassword));
6160

@@ -70,4 +69,21 @@ public static VmwareContext create(String vCenterAddress, String vCenterUserName
7069

7170
return context;
7271
}
72+
73+
public static VmwareContext getContext(String vCenterAddress, String vCenterUserName, String vCenterPassword) throws Exception {
74+
VmwareContext context = s_pool.getContext(vCenterAddress, vCenterUserName);
75+
if(context == null)
76+
context = create(vCenterAddress, vCenterUserName, vCenterPassword);
77+
78+
if(context != null) {
79+
context.setPoolInfo(s_pool, VmwareContextPool.composePoolKey(vCenterAddress, vCenterUserName));
80+
81+
context.registerStockObject(VmwareManager.CONTEXT_STOCK_NAME, s_vmwareMgr);
82+
83+
context.registerStockObject("serviceconsole", s_vmwareMgr.getServiceConsolePortGroupName());
84+
context.registerStockObject("manageportgroup", s_vmwareMgr.getManagementPortGroupName());
85+
}
86+
87+
return context;
88+
}
7389
}

‎plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/resource/VmwareResource.java‎

Lines changed: 47 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa
374374
protected DiskControllerType _rootDiskController = DiskControllerType.ide;
375375

376376
protected ManagedObjectReference _morHyperHost;
377-
protected VmwareContext _serviceContext;
377+
protected ThreadLocal<VmwareContext> _serviceContext = new ThreadLocal<VmwareContext>();
378378
protected String _hostName;
379379

380380
protected HashMap<String, State> _vms = new HashMap<String, State>(71);
@@ -602,10 +602,10 @@ public Answer executeRequest(Command cmd) {
602602
}
603603

604604
} finally {
605+
recycleServiceContext();
605606
NDC.pop();
606607
}
607608

608-
609609
if(s_logger.isTraceEnabled())
610610
s_logger.trace("End executeRequest(), cmd: " + cmd.getClass().getSimpleName());
611611

@@ -3324,7 +3324,7 @@ private String getNetworkNamePrefix(NicTO nicTo) throws Exception {
33243324
}
33253325
}
33263326

3327-
protected synchronized Answer execute(final RemoteAccessVpnCfgCommand cmd) {
3327+
protected Answer execute(final RemoteAccessVpnCfgCommand cmd) {
33283328
String controlIp = getRouterSshControlIp(cmd);
33293329
StringBuffer argsBuf = new StringBuffer();
33303330
if (cmd.isCreate()) {
@@ -3368,7 +3368,7 @@ protected synchronized Answer execute(final RemoteAccessVpnCfgCommand cmd) {
33683368
return new Answer(cmd);
33693369
}
33703370

3371-
protected synchronized Answer execute(final VpnUsersCfgCommand cmd) {
3371+
protected Answer execute(final VpnUsersCfgCommand cmd) {
33723372
VmwareManager mgr = getServiceContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME);
33733373

33743374
String controlIp = getRouterSshControlIp(cmd);
@@ -5363,7 +5363,7 @@ private boolean createVMLinkedClone(VirtualMachineMO vmTemplate, DatacenterMO dc
53635363

53645364

53655365
@Override
5366-
public synchronized CreateAnswer execute(CreateCommand cmd) {
5366+
public CreateAnswer execute(CreateCommand cmd) {
53675367
if (s_logger.isInfoEnabled()) {
53685368
s_logger.info("Executing resource CreateCommand: " + _gson.toJson(cmd));
53695369
}
@@ -5397,12 +5397,11 @@ public synchronized CreateAnswer execute(CreateCommand cmd) {
53975397
}
53985398

53995399
String volumeDatastorePath = String.format("[%s] %s.vmdk", dsMo.getName(), vmdkName);
5400-
synchronized (this) {
5401-
s_logger.info("Delete file if exists in datastore to clear the way for creating the volume. file: " + volumeDatastorePath);
5402-
VmwareStorageLayoutHelper.deleteVolumeVmdkFiles(dsMo, vmdkName, dcMo);
5403-
vmMo.createDisk(volumeDatastorePath, getMBsFromBytes(dskch.getSize()), morDatastore, -1);
5404-
vmMo.detachDisk(volumeDatastorePath, false);
5405-
}
5400+
5401+
s_logger.info("Delete file if exists in datastore to clear the way for creating the volume. file: " + volumeDatastorePath);
5402+
VmwareStorageLayoutHelper.deleteVolumeVmdkFiles(dsMo, vmdkName, dcMo);
5403+
vmMo.createDisk(volumeDatastorePath, getMBsFromBytes(dskch.getSize()), morDatastore, -1);
5404+
vmMo.detachDisk(volumeDatastorePath, false);
54065405

54075406
VolumeTO vol = new VolumeTO(cmd.getVolumeId(), dskch.getType(), pool.getType(), pool.getUuid(), dskch.getName(), pool.getPath(), vmdkName, dskch.getSize(), null);
54085407
return new CreateAnswer(cmd, vol);
@@ -5454,13 +5453,11 @@ public synchronized CreateAnswer execute(CreateCommand cmd) {
54545453
throw new Exception("Unable to create a dummy VM for volume creation");
54555454
}
54565455

5457-
synchronized (this) {
5458-
// s_logger.info("Delete file if exists in datastore to clear the way for creating the volume. file: " + volumeDatastorePath);
5459-
VmwareStorageLayoutHelper.deleteVolumeVmdkFiles(dsMo, volumeUuid.toString(), dcMo);
5456+
// s_logger.info("Delete file if exists in datastore to clear the way for creating the volume. file: " + volumeDatastorePath);
5457+
VmwareStorageLayoutHelper.deleteVolumeVmdkFiles(dsMo, volumeUuid.toString(), dcMo);
54605458

5461-
vmMo.createDisk(volumeDatastorePath, getMBsFromBytes(dskch.getSize()), morDatastore, vmMo.getScsiDeviceControllerKey());
5462-
vmMo.detachDisk(volumeDatastorePath, false);
5463-
}
5459+
vmMo.createDisk(volumeDatastorePath, getMBsFromBytes(dskch.getSize()), morDatastore, vmMo.getScsiDeviceControllerKey());
5460+
vmMo.detachDisk(volumeDatastorePath, false);
54645461

54655462
VolumeTO vol = new VolumeTO(cmd.getVolumeId(), dskch.getType(), pool.getType(), pool.getUuid(), dskch.getName(), pool.getPath(), volumeUuid, dskch.getSize(), null);
54665463
return new CreateAnswer(cmd, vol);
@@ -5627,6 +5624,8 @@ public StartupCommand[] initialize() {
56275624
s_logger.info("Host " + hyperHost.getHyperHostName() + " is not in connected state");
56285625
return null;
56295626
}
5627+
5628+
((HostMO)hyperHost).enableVncOnHostFirewall();
56305629

56315630
AboutInfo aboutInfo = ((HostMO)hyperHost).getHostAboutInfo();
56325631
hostApiVersion = aboutInfo.getApiVersion();
@@ -6451,48 +6450,40 @@ public VmwareHypervisorHost getHyperHost(VmwareContext context) {
64516450
}
64526451

64536452
@Override
6454-
public synchronized VmwareContext getServiceContext(Command cmd) {
6455-
if (_serviceContext == null) {
6456-
try {
6457-
_serviceContext = VmwareContextFactory.create(_vCenterAddress, _username, _password);
6458-
VmwareHypervisorHost hyperHost = getHyperHost(_serviceContext, cmd);
6459-
assert(hyperHost instanceof HostMO);
6460-
6461-
HostFirewallSystemMO firewallMo = ((HostMO)hyperHost).getHostFirewallSystemMO();
6462-
boolean bRefresh = false;
6463-
if(firewallMo != null) {
6464-
HostFirewallInfo firewallInfo = firewallMo.getFirewallInfo();
6465-
if(firewallInfo != null && firewallInfo.getRuleset() != null) {
6466-
for(HostFirewallRuleset rule : firewallInfo.getRuleset()) {
6467-
if("vncServer".equalsIgnoreCase(rule.getKey())) {
6468-
bRefresh = true;
6469-
firewallMo.enableRuleset("vncServer");
6470-
} else if("gdbserver".equalsIgnoreCase(rule.getKey())) {
6471-
bRefresh = true;
6472-
firewallMo.enableRuleset("gdbserver");
6473-
}
6474-
}
6475-
}
6476-
6477-
if(bRefresh)
6478-
firewallMo.refreshFirewall();
6479-
}
6480-
} catch (Exception e) {
6481-
s_logger.error("Unable to connect to vSphere server: " + _vCenterAddress, e);
6482-
throw new CloudRuntimeException("Unable to connect to vSphere server: " + _vCenterAddress);
6483-
}
6453+
public VmwareContext getServiceContext(Command cmd) {
6454+
if(_serviceContext.get() != null)
6455+
return _serviceContext.get();
6456+
6457+
VmwareContext context = null;
6458+
try {
6459+
context = VmwareContextFactory.getContext(_vCenterAddress, _username, _password);
6460+
_serviceContext.set(context);
6461+
} catch (Exception e) {
6462+
s_logger.error("Unable to connect to vSphere server: " + _vCenterAddress, e);
6463+
throw new CloudRuntimeException("Unable to connect to vSphere server: " + _vCenterAddress);
64846464
}
6485-
return _serviceContext;
6465+
return context;
64866466
}
64876467

64886468
@Override
6489-
public synchronized void invalidateServiceContext(VmwareContext context) {
6490-
if (_serviceContext != null) {
6491-
_serviceContext.close();
6492-
}
6493-
_serviceContext = null;
6494-
}
6495-
6469+
public void invalidateServiceContext(VmwareContext context) {
6470+
assert(_serviceContext.get() == context);
6471+
6472+
_serviceContext.set(null);
6473+
if(context != null)
6474+
context.close();
6475+
}
6476+
6477+
private void recycleServiceContext() {
6478+
VmwareContext context = _serviceContext.get();
6479+
_serviceContext.set(null);
6480+
6481+
if(context != null) {
6482+
assert(context.getPool() != null);
6483+
context.getPool().returnContext(context);
6484+
}
6485+
}
6486+
64966487
@Override
64976488
public VmwareHypervisorHost getHyperHost(VmwareContext context, Command cmd) {
64986489
if (_morHyperHost.getType().equalsIgnoreCase("HostSystem")) {
@@ -6517,7 +6508,6 @@ public String getWorkerName(VmwareContext context, Command cmd, int workerSequen
65176508
@Override
65186509
public void setName(String name) {
65196510
// TODO Auto-generated method stub
6520-
65216511
}
65226512

65236513
@Override
@@ -6541,7 +6531,6 @@ public int getRunLevel() {
65416531
@Override
65426532
public void setRunLevel(int level) {
65436533
// TODO Auto-generated method stub
6544-
65456534
}
65466535

65476536
@Override
@@ -6590,6 +6579,7 @@ public Answer execute(DestroyCommand cmd) {
65906579
return new Answer(cmd, false, msg);
65916580
}
65926581
}
6582+
65936583
private boolean isVMWareToolsInstalled(VirtualMachineMO vmMo) throws Exception{
65946584
GuestInfo guestInfo = vmMo.getVmGuestInfo();
65956585
return (guestInfo != null && guestInfo.getGuestState() != null && guestInfo.getGuestState().equalsIgnoreCase("running"));

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

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,52 +22,48 @@
2222

2323
import com.cloud.hypervisor.vmware.util.VmwareClient;
2424
import com.cloud.hypervisor.vmware.util.VmwareContext;
25+
import com.cloud.hypervisor.vmware.util.VmwareContextPool;
2526

2627
public class VmwareSecondaryStorageContextFactory {
2728
private static volatile int s_seq = 1;
2829

29-
private static Map<String, VmwareContext> s_contextMap = new HashMap<String, VmwareContext>();
30+
private static VmwareContextPool s_pool;
3031

3132
public static void initFactoryEnvironment() {
3233
System.setProperty("axis.socketSecureFactory", "org.apache.axis.components.net.SunFakeTrustSocketFactory");
34+
s_pool = new VmwareContextPool();
3335
}
3436

3537
public static VmwareContext create(String vCenterAddress, String vCenterUserName, String vCenterPassword) throws Exception {
3638
assert(vCenterAddress != null);
3739
assert(vCenterUserName != null);
3840
assert(vCenterPassword != null);
3941

40-
VmwareContext context = null;
41-
42-
synchronized(s_contextMap) {
43-
context = s_contextMap.get(vCenterAddress);
44-
if(context == null) {
45-
String serviceUrl = "https://" + vCenterAddress + "/sdk/vimService";
46-
//String[] params = new String[] {"--url", serviceUrl, "--username", vCenterUserName, "--password", vCenterPassword };
47-
VmwareClient vimClient = new VmwareClient(vCenterAddress + "-" + s_seq++);
48-
vimClient.connect(serviceUrl, vCenterUserName, vCenterPassword);
49-
context = new VmwareContext(vimClient, vCenterAddress);
50-
context.registerStockObject("username", vCenterUserName);
51-
context.registerStockObject("password", vCenterPassword);
52-
53-
s_contextMap.put(vCenterAddress, context);
54-
}
55-
}
56-
42+
String serviceUrl = "https://" + vCenterAddress + "/sdk/vimService";
43+
VmwareClient vimClient = new VmwareClient(vCenterAddress + "-" + s_seq++);
44+
vimClient.connect(serviceUrl, vCenterUserName, vCenterPassword);
45+
VmwareContext context = new VmwareContext(vimClient, vCenterAddress);
5746
assert(context != null);
5847
return context;
5948
}
60-
61-
public static void invalidate(VmwareContext context) {
62-
synchronized(s_contextMap) {
63-
for(Iterator<Map.Entry<String, VmwareContext>> entryIter = s_contextMap.entrySet().iterator(); entryIter.hasNext();) {
64-
Map.Entry<String, VmwareContext> entry = entryIter.next();
65-
if(entry.getValue() == context) {
66-
entryIter.remove();
67-
}
68-
}
49+
50+
public static VmwareContext getContext(String vCenterAddress, String vCenterUserName, String vCenterPassword) throws Exception {
51+
VmwareContext context = s_pool.getContext(vCenterAddress, vCenterUserName);
52+
if(context == null) {
53+
context = create(vCenterAddress, vCenterUserName, vCenterPassword);
6954
}
70-
55+
56+
if(context != null) {
57+
context.setPoolInfo(s_pool, VmwareContextPool.composePoolKey(vCenterAddress, vCenterUserName));
58+
59+
context.registerStockObject("username", vCenterUserName);
60+
context.registerStockObject("password", vCenterPassword);
61+
}
62+
63+
return context;
64+
}
65+
66+
public static void invalidate(VmwareContext context) {
7167
context.close();
7268
}
7369
}

0 commit comments

Comments
 (0)