3434import java .util .concurrent .Executors ;
3535import java .util .concurrent .LinkedBlockingQueue ;
3636import java .util .concurrent .ScheduledExecutorService ;
37+ import java .util .concurrent .ScheduledFuture ;
3738import java .util .concurrent .SynchronousQueue ;
3839import java .util .concurrent .ThreadPoolExecutor ;
3940import java .util .concurrent .TimeUnit ;
9091import com .cloud .utils .nio .NioClient ;
9192import com .cloud .utils .nio .NioConnection ;
9293import com .cloud .utils .nio .Task ;
93- import com .cloud .utils .script .OutputInterpreter ;
9494import com .cloud .utils .script .Script ;
9595
9696/**
@@ -115,7 +115,7 @@ public enum ExitStatus {
115115 Configuration (66 ), // Exiting due to configuration problems.
116116 Error (67 ); // Exiting because of error.
117117
118- int value ;
118+ final int value ;
119119
120120 ExitStatus (final int value ) {
121121 this .value = value ;
@@ -138,7 +138,7 @@ public int value() {
138138 ScheduledExecutorService certExecutor ;
139139 ScheduledExecutorService hostLbCheckExecutor ;
140140
141- CopyOnWriteArrayList <WatchTask > watchList = new CopyOnWriteArrayList <>();
141+ CopyOnWriteArrayList <ScheduledFuture <?> > watchList = new CopyOnWriteArrayList <>();
142142 AtomicLong sequence = new AtomicLong (0 );
143143 AtomicLong lastPingResponseTime = new AtomicLong (0L );
144144 long pingInterval = 0 ;
@@ -433,8 +433,8 @@ public void scheduleWatch(final Link link, final Request request, final long del
433433 s_logger .debug ("Adding a watch list" );
434434 }
435435 final WatchTask task = new WatchTask (link , request , this );
436- selfTaskExecutor .scheduleAtFixedRate (task , delay , period , TimeUnit .MILLISECONDS );
437- watchList .add (task );
436+ final ScheduledFuture <?> future = selfTaskExecutor .scheduleAtFixedRate (task , delay , period , TimeUnit .MILLISECONDS );
437+ watchList .add (future );
438438 }
439439
440440 public void triggerUpdate () {
@@ -451,8 +451,8 @@ public void triggerUpdate() {
451451 }
452452
453453 protected void cancelTasks () {
454- for (final WatchTask task : watchList ) {
455- task .cancel ();
454+ for (final ScheduledFuture <?> task : watchList ) {
455+ task .cancel (true );
456456 }
457457 if (s_logger .isDebugEnabled ()) {
458458 s_logger .debug ("Clearing watch list: " + watchList .size ());
@@ -475,19 +475,20 @@ protected void cleanupAgentZoneProperties() {
475475
476476 public void lockStartupTask (final Link link ) {
477477 if (s_logger .isDebugEnabled ()) {
478- s_logger .debug (String .format ("Creating startup task - %s" , getLinkLog (link )));
478+ s_logger .debug (String .format ("Creating startup task for link: %s" , getLinkLog (link )));
479479 }
480480 StartupTask currentTask = startupTask .get ();
481481 if (currentTask != null ) {
482- s_logger .warn ("A Startup task is already locked or in progress." );
482+ s_logger .warn (String .format ("A Startup task is already locked or in progress, cannot create for link %s" ,
483+ getLinkLog (link )));
483484 return ;
484485 }
485486 currentTask = new StartupTask (link );
486487 if (startupTask .compareAndSet (null , currentTask )) {
487488 selfTaskExecutor .schedule (currentTask , startupWait , TimeUnit .SECONDS );
488489 return ;
489490 }
490- s_logger .warn ("Failed to lock a StartupTask" );
491+ s_logger .warn (String . format ( "Failed to lock a StartupTask for link: %s" , getLinkLog ( link )) );
491492 }
492493
493494 protected boolean cancelStartupTask () {
@@ -533,15 +534,13 @@ protected String retrieveHostname() {
533534 if (s_logger .isTraceEnabled ()) {
534535 s_logger .trace (" Retrieving hostname " + serverResource .getClass ().getSimpleName ());
535536 }
536- final Script command = new Script ("hostname" , 500 , s_logger );
537- final OutputInterpreter .OneLineParser parser = new OutputInterpreter .OneLineParser ();
538- final String result = command .execute (parser );
539- if (result != null ) {
540- return parser .getLine ();
537+ final String result = Script .runSimpleBashScript (Script .getExecutableAbsolutePath ("hostname" ), 500 );
538+ if (StringUtils .isNotBlank (result )) {
539+ return result ;
541540 }
542541 try {
543- InetAddress addr = InetAddress .getLocalHost ();
544- return addr .toString ();
542+ InetAddress address = InetAddress .getLocalHost ();
543+ return address .toString ();
545544 } catch (final UnknownHostException e ) {
546545 s_logger .warn ("unknown host? " , e );
547546 throw new CloudRuntimeException ("Cannot get local IP address" );
@@ -1079,7 +1078,7 @@ public void run() {
10791078 }
10801079 }
10811080
1082- public class WatchTask extends ManagedContextTimerTask {
1081+ public class WatchTask implements Runnable {
10831082 protected Request _request ;
10841083 protected Agent _agent ;
10851084 protected Link _link ;
@@ -1092,7 +1091,7 @@ public WatchTask(final Link link, final Request request, final Agent agent) {
10921091 }
10931092
10941093 @ Override
1095- protected void runInContext () {
1094+ public void run () {
10961095 if (s_logger .isTraceEnabled ()) {
10971096 s_logger .trace ("Scheduling " + (_request instanceof Response ? "Ping" : "Watch Task" ));
10981097 }
@@ -1108,7 +1107,7 @@ protected void runInContext() {
11081107 }
11091108 }
11101109
1111- public class StartupTask extends ManagedContextTimerTask {
1110+ public class StartupTask implements Runnable {
11121111 protected Link _link ;
11131112 private final AtomicBoolean cancelled = new AtomicBoolean (false );
11141113
@@ -1117,19 +1116,17 @@ public StartupTask(final Link link) {
11171116 _link = link ;
11181117 }
11191118
1120- @ Override
11211119 public boolean cancel () {
11221120 // TimerTask.cancel may fail depends on the calling context
11231121 if (cancelled .compareAndSet (false , true )) {
11241122 startupWait = DEFAULT_STARTUP_WAIT ;
11251123 s_logger .debug ("Startup task cancelled" );
1126- return super .cancel ();
11271124 }
11281125 return true ;
11291126 }
11301127
11311128 @ Override
1132- protected void runInContext () {
1129+ public void run () {
11331130 if (cancelled .compareAndSet (false , true )) {
11341131 s_logger .info ("The running startup command is now invalid. Attempting reconnect" );
11351132 startupTask .set (null );
0 commit comments