Skip to content

Commit 516ef2c

Browse files
committed
put pingtask is in a seperate thread pool
1 parent 501c46b commit 516ef2c

1 file changed

Lines changed: 25 additions & 1 deletion

File tree

‎agent/src/com/cloud/agent/Agent.java‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
import java.util.Map;
2929
import java.util.Timer;
3030
import java.util.TimerTask;
31+
32+
import java.util.concurrent.SynchronousQueue;
33+
import java.util.concurrent.ThreadPoolExecutor;
34+
import java.util.concurrent.TimeUnit;
3135
import java.util.concurrent.atomic.AtomicInteger;
3236

3337
import javax.naming.ConfigurationException;
@@ -52,6 +56,7 @@
5256
import com.cloud.resource.ServerResource;
5357
import com.cloud.utils.PropertiesUtil;
5458
import com.cloud.utils.backoff.BackoffAlgorithm;
59+
import com.cloud.utils.concurrency.NamedThreadFactory;
5560
import com.cloud.utils.exception.CloudRuntimeException;
5661
import com.cloud.utils.nio.HandlerFactory;
5762
import 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

Comments
 (0)