4343import org .apache .log4j .Logger ;
4444
4545import com .cloud .agent .AgentManager ;
46- import com .cloud .agent .StartupCommandProcessor ;
4746import com .cloud .agent .Listener ;
47+ import com .cloud .agent .StartupCommandProcessor ;
4848import com .cloud .agent .api .AgentControlAnswer ;
4949import com .cloud .agent .api .AgentControlCommand ;
5050import com .cloud .agent .api .Answer ;
@@ -481,7 +481,11 @@ public void notifyAnswersToMonitors(long agentId, long seq, Answer[] answers) {
481481 }
482482
483483 public AgentAttache findAttache (long hostId ) {
484- return _agents .get (hostId );
484+ AgentAttache attache = null ;
485+ synchronized (_agents ) {
486+ attache = _agents .get (hostId );
487+ }
488+ return attache ;
485489 }
486490
487491 @ Override
@@ -587,8 +591,9 @@ public List<? extends Cluster> discoverCluster(AddClusterCmd cmd) throws Illegal
587591 String username = cmd .getUsername ();
588592 String password = cmd .getPassword ();
589593
590- if (url != null )
591- url = URLDecoder .decode (url );
594+ if (url != null ) {
595+ url = URLDecoder .decode (url );
596+ }
592597
593598 URI uri = null ;
594599
@@ -716,7 +721,7 @@ public List<? extends Cluster> discoverCluster(AddClusterCmd cmd) throws Illegal
716721 break ;
717722 }
718723
719- AgentAttache attache = simulateStart (resource , entry .getValue (), true , null , null );
724+ AgentAttache attache = simulateStart (null , resource , entry .getValue (), true , null , null );
720725 if (attache != null ) {
721726 hosts .add (_hostDao .findById (attache .getId ()));
722727 }
@@ -950,7 +955,7 @@ private List<HostVO> discoverHostsFull(Long dcId, Long podId, Long clusterId, St
950955 }
951956 return null ;
952957 }
953- AgentAttache attache = simulateStart (resource , entry .getValue (), true , hostTags , allocationState );
958+ AgentAttache attache = simulateStart (null , resource , entry .getValue (), true , hostTags , allocationState );
954959 if (attache != null ) {
955960 hosts .add (_hostDao .findById (attache .getId ()));
956961 }
@@ -1694,11 +1699,9 @@ protected boolean handleDisconnect(AgentAttache attache, Status.Event event, boo
16941699 if (s_logger .isDebugEnabled ()) {
16951700 s_logger .debug ("Deregistering link for " + hostId + " with state " + nextState );
16961701 }
1697-
1698- _hostDao .disconnect (host , event , _nodeId );
1699-
17001702 removeAgent (attache , nextState );
1701-
1703+ _hostDao .disconnect (host , event , _nodeId );
1704+
17021705 host = _hostDao .findById (host .getId ());
17031706 if (host .getStatus () == Status .Alert || host .getStatus () == Status .Down ) {
17041707 _haMgr .scheduleRestartForVmsOnHost (host , investigate );
@@ -1873,11 +1876,19 @@ protected void loadDirectlyConnectedHost(HostVO host) {
18731876 _executor .execute (new SimulateStartTask (host .getId (), resource , host .getDetails (), null ));
18741877 }
18751878
1876- protected AgentAttache simulateStart (ServerResource resource , Map <String , String > details , boolean old , List <String > hostTags , String allocationState ) throws IllegalArgumentException {
1879+ protected AgentAttache simulateStart (Long id , ServerResource resource , Map <String , String > details , boolean old , List <String > hostTags , String allocationState ) throws IllegalArgumentException {
18771880 StartupCommand [] cmds = resource .initialize ();
18781881 if (cmds == null ) {
18791882 return null ;
18801883 }
1884+
1885+ if (id != null ) {
1886+ HostVO host = _hostDao .findById (id );
1887+ if (!_hostDao .directConnect (host , _nodeId )) {
1888+ s_logger .info ("Someone else is loading " + host );
1889+ return null ;
1890+ }
1891+ }
18811892
18821893 AgentAttache attache = null ;
18831894 if (s_logger .isDebugEnabled ()) {
@@ -2386,7 +2397,7 @@ public Host addHost(long zoneId, ServerResource resource, Type hostType, Map<Str
23862397 }
23872398 }
23882399
2389- AgentAttache attache = simulateStart (resource , hostDetails , true , null , null );
2400+ AgentAttache attache = simulateStart (null , resource , hostDetails , true , null , null );
23902401 return _hostDao .findById (attache .getId ());
23912402 }
23922403
@@ -2559,8 +2570,7 @@ protected AgentAttache createAttache(long id, HostVO server, Link link) {
25592570 link .attach (attache );
25602571 AgentAttache old = null ;
25612572 synchronized (_agents ) {
2562- old = _agents .get (id );
2563- _agents .put (id , attache );
2573+ old = _agents .put (id , attache );
25642574 }
25652575 if (old != null ) {
25662576 old .disconnect (Status .Removed );
@@ -2577,8 +2587,7 @@ protected AgentAttache createAttache(long id, HostVO server, ServerResource reso
25772587 || server .getStatus () == Status .PrepareForMaintenance , this );
25782588 AgentAttache old = null ;
25792589 synchronized (_agents ) {
2580- old = _agents .get (id );
2581- _agents .put (id , attache );
2590+ old = _agents .put (id , attache );
25822591 }
25832592 if (old != null ) {
25842593 old .disconnect (Status .Removed );
@@ -2959,23 +2968,17 @@ public SimulateStartTask(long id, ServerResource resource, Map<String, String> d
29592968
29602969 @ Override
29612970 public void run () {
2962- AgentAttache at = null ;
29632971 try {
29642972 if (s_logger .isDebugEnabled ()) {
29652973 s_logger .debug ("Simulating start for resource " + resource .getName () + " id " + id );
29662974 }
2967- simulateStart (resource , details , false , null , null );
2975+ simulateStart (id , resource , details , false , null , null );
29682976 } catch (Exception e ) {
29692977 s_logger .warn ("Unable to simulate start on resource " + id + " name " + resource .getName (), e );
29702978 } finally {
29712979 if (actionDelegate != null ) {
29722980 actionDelegate .action (new Long (id ));
29732981 }
2974- if (at == null ) {
2975- HostVO host = _hostDao .findById (id );
2976- host .setManagementServerId (null );
2977- _hostDao .update (id , host );
2978- }
29792982 StackMaid .current ().exitCleanup ();
29802983 }
29812984 }
0 commit comments