@@ -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" ));
0 commit comments