Skip to content

Commit a507796

Browse files
author
Alena Prokharchyk
committed
CS-16592: process handleConnectedAgent in a separate thread pool
1 parent 3948d7d commit a507796

1 file changed

Lines changed: 43 additions & 14 deletions

File tree

‎server/src/com/cloud/agent/manager/AgentManagerImpl.java‎

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ public class AgentManagerImpl implements AgentManager, HandlerFactory, Manager {
219219
protected AgentMonitor _monitor = null;
220220

221221
protected ExecutorService _executor;
222+
protected ThreadPoolExecutor _connectExecutor;
222223

223224
protected StateMachine2<Status, Status.Event, Host> _statusStateMachine = Status.getStateMachine();
224225

@@ -274,7 +275,12 @@ public boolean configure(final String name, final Map<String, Object> params) th
274275
registerForHostEvents(_monitor, true, true, false);
275276

276277
_executor = new ThreadPoolExecutor(threads, threads, 60l, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), new NamedThreadFactory("AgentTaskPool"));
277-
278+
279+
_connectExecutor = new ThreadPoolExecutor(100, 500, 60l, TimeUnit.SECONDS,
280+
new LinkedBlockingQueue<Runnable>(), new NamedThreadFactory("AgentConnectTaskPool"));
281+
//allow core threads to time out even when there are no items in the queue
282+
_connectExecutor.allowCoreThreadTimeOut(true);
283+
278284
_connection = new NioServer("AgentManager", _port, workers + 10, this);
279285

280286
s_logger.info("Listening on " + _port + " with " + workers + " workers");
@@ -828,6 +834,8 @@ public boolean stop() {
828834
}
829835
}
830836
}
837+
838+
_connectExecutor.shutdownNow();
831839
return true;
832840
}
833841

@@ -1206,6 +1214,35 @@ public void run() {
12061214
}
12071215
}
12081216
}
1217+
1218+
protected class HandleAgentConnectTask implements Runnable {
1219+
Link _link;
1220+
Command[] _cmds;
1221+
Request _request;
1222+
1223+
HandleAgentConnectTask(Link link, final Command[] cmds, final Request request) {
1224+
_link = link;
1225+
_cmds = cmds;
1226+
_request = request;
1227+
}
1228+
1229+
@Override
1230+
public void run() {
1231+
_request.logD("Processing the first command ");
1232+
StartupCommand[] startups = new StartupCommand[_cmds.length];
1233+
for (int i = 0; i < _cmds.length; i++) {
1234+
startups[i] = (StartupCommand) _cmds[i];
1235+
}
1236+
AgentAttache attache = handleConnectedAgent(_link, startups, _request);
1237+
if (attache == null) {
1238+
s_logger.warn("Unable to create attache for agent: " + _request);
1239+
}
1240+
}
1241+
}
1242+
1243+
protected void connectAgent(Link link, final Command[] cmds, final Request request) {
1244+
_connectExecutor.execute(new HandleAgentConnectTask(link, cmds, request));
1245+
}
12091246

12101247
public class AgentHandler extends Task {
12111248
public AgentHandler(Task.Type type, Link link, byte[] data) {
@@ -1218,21 +1255,13 @@ protected void processRequest(final Link link, final Request request) {
12181255
Command cmd = cmds[0];
12191256
boolean logD = true;
12201257

1221-
Response response = null;
12221258
if (attache == null) {
1223-
request.logD("Processing the first command ");
12241259
if (!(cmd instanceof StartupCommand)) {
12251260
s_logger.warn("Throwing away a request because it came through as the first command on a connect: " + request);
1226-
return;
1227-
}
1228-
1229-
StartupCommand[] startups = new StartupCommand[cmds.length];
1230-
for (int i = 0; i < cmds.length; i++) {
1231-
startups[i] = (StartupCommand) cmds[i];
1232-
}
1233-
attache = handleConnectedAgent(link, startups, request);
1234-
if (attache == null) {
1235-
s_logger.warn("Unable to create attache for agent: " + request);
1261+
} else {
1262+
//submit the task for execution
1263+
request.logD("Scheduling the first command ");
1264+
connectAgent(link, cmds, request);
12361265
}
12371266
return;
12381267
}
@@ -1331,7 +1360,7 @@ protected void processRequest(final Link link, final Request request) {
13311360
answers[i] = answer;
13321361
}
13331362

1334-
response = new Response(request, answers, _nodeId, attache.getId());
1363+
Response response = new Response(request, answers, _nodeId, attache.getId());
13351364
if (s_logger.isDebugEnabled()) {
13361365
if (logD) {
13371366
s_logger.debug("SeqA " + attache.getId() + "-" + response.getSequence() + ": Sending " + response);

0 commit comments

Comments
 (0)