2828import java .util .Map ;
2929import java .util .Timer ;
3030import java .util .TimerTask ;
31+
32+ import java .util .concurrent .SynchronousQueue ;
33+ import java .util .concurrent .ThreadPoolExecutor ;
34+ import java .util .concurrent .TimeUnit ;
3135import java .util .concurrent .atomic .AtomicInteger ;
3236
3337import javax .naming .ConfigurationException ;
5256import com .cloud .resource .ServerResource ;
5357import com .cloud .utils .PropertiesUtil ;
5458import com .cloud .utils .backoff .BackoffAlgorithm ;
59+ import com .cloud .utils .concurrency .NamedThreadFactory ;
5560import com .cloud .utils .exception .CloudRuntimeException ;
5661import com .cloud .utils .nio .HandlerFactory ;
5762import com .cloud .utils .nio .Link ;
@@ -112,6 +117,8 @@ public int value() {
112117
113118 StartupTask _startup = null ;
114119 boolean _reconnectAllowed = true ;
120+ //For time sentitive task, e.g. PingTask
121+ private ThreadPoolExecutor _ugentTaskPool ;
115122
116123 // for simulator use only
117124 public Agent (IAgentShell shell ) {
@@ -121,6 +128,10 @@ public Agent(IAgentShell shell) {
121128 _connection = new NioClient ("Agent" , _shell .getHost (), _shell .getPort (), _shell .getWorkers (), this );
122129
123130 Runtime .getRuntime ().addShutdownHook (new ShutdownThread (this ));
131+
132+ _ugentTaskPool = new ThreadPoolExecutor (shell .getPingRetries (), 2 * shell .getPingRetries (), 10 , TimeUnit .MINUTES ,
133+ new SynchronousQueue <Runnable >(), new NamedThreadFactory ("UgentTask" )
134+ );
124135 }
125136
126137 public Agent (IAgentShell shell , int localAgentId , ServerResource resource ) throws ConfigurationException {
@@ -152,6 +163,10 @@ public Agent(IAgentShell shell, int localAgentId, ServerResource resource) throw
152163 s_logger .debug ("Adding shutdown hook" );
153164 Runtime .getRuntime ().addShutdownHook (new ShutdownThread (this ));
154165
166+ _ugentTaskPool = new ThreadPoolExecutor (shell .getPingRetries (), 2 * shell .getPingRetries (), 10 , TimeUnit .MINUTES ,
167+ new SynchronousQueue <Runnable >(), new NamedThreadFactory ("UgentTask" )
168+ );
169+
155170 s_logger .info ("Agent [id = " + (_id != null ? _id : "new" ) + " : type = " + getResourceName () + " : zone = " + _shell .getZone () + " : pod = " + _shell .getPod () + " : workers = "
156171 + _shell .getWorkers () + " : host = " + _shell .getHost () + " : port = " + _shell .getPort ());
157172 }
@@ -246,6 +261,8 @@ public void stop(final String reason, final String detail) {
246261 _resource .stop ();
247262 _resource = null ;
248263 }
264+
265+ _ugentTaskPool .shutdownNow ();
249266 }
250267
251268 public Long getId () {
@@ -414,6 +431,9 @@ public void processStartupAnswer(Answer answer, Response response, Link link) {
414431
415432 setLastPingResponseTime ();
416433 scheduleWatch (link , response , _pingInterval , _pingInterval );
434+
435+ _ugentTaskPool .setKeepAliveTime (2 * _pingInterval , TimeUnit .MILLISECONDS );
436+
417437 s_logger .info ("Startup Response Received: agent id = " + getId ());
418438 }
419439
@@ -728,7 +748,11 @@ public void run() {
728748 s_logger .trace ("Scheduling " + (_request instanceof Response ? "Ping" : "Watch Task" ));
729749 }
730750 try {
731- _link .schedule (new ServerHandler (Task .Type .OTHER , _link , _request ));
751+ if (_request instanceof Response ) {
752+ _ugentTaskPool .submit (new ServerHandler (Task .Type .OTHER , _link , _request ));
753+ } else {
754+ _link .schedule (new ServerHandler (Task .Type .OTHER , _link , _request ));
755+ }
732756 } catch (final ClosedChannelException e ) {
733757 s_logger .warn ("Unable to schedule task because channel is closed" );
734758 }
0 commit comments