Skip to content

Commit d5754d9

Browse files
committed
CLOUDSTACK-6740: Direct agent command throttling improvements
List of changes: 1. Created a separate thread pool for handling cron and ping tasks. The size of the pool is based on direct.agent.pool.size. The existing direct agent pool will run all commands other than cron and ping. 2. For normal tasks (generated as part of user/admin API calls), if throttle limit is reached then tasks get queued up for subsequent execution once threads are available. 3. For cron and ping tasks (internally generated by MS like ping, VM sync etc.), if throttle limit is reached then these gets rejected. Since these are internally generated these can be rejected without any issues.
1 parent cd62aec commit d5754d9

3 files changed

Lines changed: 100 additions & 18 deletions

File tree

‎engine/orchestration/src/com/cloud/agent/manager/AgentAttache.java‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import java.util.concurrent.Executors;
3131
import java.util.concurrent.ScheduledExecutorService;
3232
import java.util.concurrent.TimeUnit;
33-
import java.util.concurrent.atomic.AtomicInteger;
3433

3534
import org.apache.cloudstack.managed.context.ManagedContextRunnable;
3635
import org.apache.log4j.Logger;
@@ -106,7 +105,6 @@ public int compare(final Object o1, final Object o2) {
106105
protected Status _status = Status.Connecting;
107106
protected boolean _maintenance;
108107
protected long _nextSequence;
109-
protected AtomicInteger _outstandingTaskCount;
110108

111109
protected AgentManagerImpl _agentMgr;
112110

@@ -129,7 +127,6 @@ protected AgentAttache(final AgentManagerImpl agentMgr, final long id, final Str
129127
_requests = new LinkedList<Request>();
130128
_agentMgr = agentMgr;
131129
_nextSequence = new Long(s_rand.nextInt(Short.MAX_VALUE)) << 48;
132-
_outstandingTaskCount = new AtomicInteger(0);
133130
}
134131

135132
public synchronized long getNextSequence() {

‎engine/orchestration/src/com/cloud/agent/manager/AgentManagerImpl.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ public class AgentManagerImpl extends ManagerBase implements AgentManager, Handl
162162
protected ExecutorService _executor;
163163
protected ThreadPoolExecutor _connectExecutor;
164164
protected ScheduledExecutorService _directAgentExecutor;
165+
protected ScheduledExecutorService _cronJobExecutor;
165166
protected ScheduledExecutorService _monitorExecutor;
166167

167168
private int _directAgentThreadCap;
@@ -219,7 +220,10 @@ public boolean configure(final String name, final Map<String, Object> params) th
219220
_connection = new NioServer("AgentManager", Port.value(), Workers.value() + 10, this);
220221
s_logger.info("Listening on " + Port.value() + " with " + Workers.value() + " workers");
221222

223+
// executes all agent commands other than cron and ping
222224
_directAgentExecutor = new ScheduledThreadPoolExecutor(DirectAgentPoolSize.value(), new NamedThreadFactory("DirectAgent"));
225+
// executes cron and ping agent commands
226+
_cronJobExecutor = new ScheduledThreadPoolExecutor(DirectAgentPoolSize.value(), new NamedThreadFactory("DirectAgentCronJob"));
223227
s_logger.debug("Created DirectAgentAttache pool with size: " + DirectAgentPoolSize.value());
224228
_directAgentThreadCap = Math.round(DirectAgentPoolSize.value() * DirectAgentThreadCap.value()) + 1; // add 1 to always make the value > 0
225229

@@ -1452,6 +1456,10 @@ public ScheduledExecutorService getDirectAgentPool() {
14521456
return _directAgentExecutor;
14531457
}
14541458

1459+
public ScheduledExecutorService getCronJobPool() {
1460+
return _cronJobExecutor;
1461+
}
1462+
14551463
public int getDirectAgentThreadCap() {
14561464
return _directAgentThreadCap;
14571465
}

‎engine/orchestration/src/com/cloud/agent/manager/DirectAgentAttache.java‎

Lines changed: 92 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@
1717
package com.cloud.agent.manager;
1818

1919
import java.util.ArrayList;
20+
import java.util.LinkedList;
2021
import java.util.List;
2122
import java.util.concurrent.ScheduledFuture;
2223
import java.util.concurrent.TimeUnit;
24+
import java.util.concurrent.atomic.AtomicInteger;
2325

2426
import org.apache.log4j.Logger;
25-
2627
import org.apache.cloudstack.managed.context.ManagedContextRunnable;
2728

2829
import com.cloud.agent.api.Answer;
@@ -43,11 +44,16 @@ public class DirectAgentAttache extends AgentAttache {
4344
List<ScheduledFuture<?>> _futures = new ArrayList<ScheduledFuture<?>>();
4445
AgentManagerImpl _mgr;
4546
long _seq = 0;
47+
LinkedList<Task> tasks = new LinkedList<Task>();
48+
AtomicInteger _outstandingTaskCount;
49+
AtomicInteger _outstandingCronTaskCount;
4650

4751
public DirectAgentAttache(AgentManagerImpl agentMgr, long id, String name, ServerResource resource, boolean maintenance, AgentManagerImpl mgr) {
4852
super(agentMgr, id, name, maintenance);
4953
_resource = resource;
5054
_mgr = mgr;
55+
_outstandingTaskCount = new AtomicInteger(0);
56+
_outstandingCronTaskCount = new AtomicInteger(0);
5157
}
5258

5359
@Override
@@ -90,15 +96,16 @@ public void send(Request req) throws AgentUnavailableException {
9096
if (answers != null && answers[0] instanceof StartupAnswer) {
9197
StartupAnswer startup = (StartupAnswer)answers[0];
9298
int interval = startup.getPingInterval();
93-
_futures.add(_agentMgr.getDirectAgentPool().scheduleAtFixedRate(new PingTask(), interval, interval, TimeUnit.SECONDS));
99+
_futures.add(_agentMgr.getCronJobPool().scheduleAtFixedRate(new PingTask(), interval, interval, TimeUnit.SECONDS));
94100
}
95101
} else {
96102
Command[] cmds = req.getCommands();
97103
if (cmds.length > 0 && !(cmds[0] instanceof CronCommand)) {
98-
_agentMgr.getDirectAgentPool().execute(new Task(req));
104+
queueTask(new Task(req));
105+
scheduleFromQueue();
99106
} else {
100107
CronCommand cmd = (CronCommand)cmds[0];
101-
_futures.add(_agentMgr.getDirectAgentPool().scheduleAtFixedRate(new Task(req), cmd.getInterval(), cmd.getInterval(), TimeUnit.SECONDS));
108+
_futures.add(_agentMgr.getCronJobPool().scheduleAtFixedRate(new CronTask(req), cmd.getInterval(), cmd.getInterval(), TimeUnit.SECONDS));
102109
}
103110
}
104111
}
@@ -109,7 +116,7 @@ public void process(Answer[] answers) {
109116
StartupAnswer startup = (StartupAnswer)answers[0];
110117
int interval = startup.getPingInterval();
111118
s_logger.info("StartupAnswer received " + startup.getHostId() + " Interval = " + interval);
112-
_futures.add(_agentMgr.getDirectAgentPool().scheduleAtFixedRate(new PingTask(), interval, interval, TimeUnit.SECONDS));
119+
_futures.add(_agentMgr.getCronJobPool().scheduleAtFixedRate(new PingTask(), interval, interval, TimeUnit.SECONDS));
113120
}
114121
}
115122

@@ -128,13 +135,26 @@ protected void finalize() throws Throwable {
128135
}
129136
}
130137

138+
private synchronized void queueTask(Task task) {
139+
tasks.add(task);
140+
}
141+
142+
private synchronized void scheduleFromQueue() {
143+
if (s_logger.isTraceEnabled()) {
144+
s_logger.trace("Agent attache=" + _id + ", task queue size=" + tasks.size() + ", outstanding tasks=" + _outstandingTaskCount.get());
145+
}
146+
while (!tasks.isEmpty() && _outstandingTaskCount.get() < _agentMgr.getDirectAgentThreadCap()) {
147+
_outstandingTaskCount.incrementAndGet();
148+
_agentMgr.getDirectAgentPool().execute(tasks.remove());
149+
}
150+
}
151+
131152
protected class PingTask extends ManagedContextRunnable {
132153
@Override
133154
protected synchronized void runInContext() {
134155
try {
135-
if (_outstandingTaskCount.incrementAndGet() > _agentMgr.getDirectAgentThreadCap()) {
136-
s_logger.warn("Task execution for direct attache(" + _id + ") has reached maximum outstanding limit(" + _agentMgr.getDirectAgentThreadCap() +
137-
"), bailing out");
156+
if (_outstandingCronTaskCount.incrementAndGet() >= _agentMgr.getDirectAgentThreadCap()) {
157+
s_logger.warn("PingTask execution for direct attache(" + _id + ") has reached maximum outstanding limit(" + _agentMgr.getDirectAgentThreadCap() + "), bailing out");
138158
return;
139159
}
140160

@@ -162,15 +182,15 @@ protected synchronized void runInContext() {
162182
} catch (Exception e) {
163183
s_logger.warn("Unable to complete the ping task", e);
164184
} finally {
165-
_outstandingTaskCount.decrementAndGet();
185+
_outstandingCronTaskCount.decrementAndGet();
166186
}
167187
}
168188
}
169189

170-
protected class Task extends ManagedContextRunnable {
190+
protected class CronTask extends ManagedContextRunnable {
171191
Request _req;
172192

173-
public Task(Request req) {
193+
public CronTask(Request req) {
174194
_req = req;
175195
}
176196

@@ -194,9 +214,8 @@ private void bailout() {
194214
protected void runInContext() {
195215
long seq = _req.getSequence();
196216
try {
197-
if (_outstandingTaskCount.incrementAndGet() > _agentMgr.getDirectAgentThreadCap()) {
198-
s_logger.warn("Task execution for direct attache(" + _id + ") has reached maximum outstanding limit(" + _agentMgr.getDirectAgentThreadCap() +
199-
"), bailing out");
217+
if (_outstandingCronTaskCount.incrementAndGet() >= _agentMgr.getDirectAgentThreadCap()) {
218+
s_logger.warn("CronTask execution for direct attache(" + _id + ") has reached maximum outstanding limit(" + _agentMgr.getDirectAgentThreadCap() + "), bailing out");
200219
bailout();
201220
return;
202221
}
@@ -243,9 +262,67 @@ protected void runInContext() {
243262
} catch (Exception e) {
244263
s_logger.warn(log(seq, "Exception caught "), e);
245264
} finally {
246-
_outstandingTaskCount.decrementAndGet();
265+
_outstandingCronTaskCount.decrementAndGet();
247266
}
248267
}
249268
}
250269

270+
protected class Task extends ManagedContextRunnable {
271+
Request _req;
272+
273+
public Task(Request req) {
274+
_req = req;
275+
}
276+
277+
@Override
278+
protected void runInContext() {
279+
long seq = _req.getSequence();
280+
try {
281+
ServerResource resource = _resource;
282+
Command[] cmds = _req.getCommands();
283+
boolean stopOnError = _req.stopOnError();
284+
285+
if (s_logger.isDebugEnabled()) {
286+
s_logger.debug(log(seq, "Executing request"));
287+
}
288+
ArrayList<Answer> answers = new ArrayList<Answer>(cmds.length);
289+
for (int i = 0; i < cmds.length; i++) {
290+
Answer answer = null;
291+
try {
292+
if (resource != null) {
293+
answer = resource.executeRequest(cmds[i]);
294+
if (answer == null) {
295+
s_logger.warn("Resource returned null answer!");
296+
answer = new Answer(cmds[i], false, "Resource returned null answer");
297+
}
298+
} else {
299+
answer = new Answer(cmds[i], false, "Agent is disconnected");
300+
}
301+
} catch (Exception e) {
302+
s_logger.warn(log(seq, "Exception Caught while executing command"), e);
303+
answer = new Answer(cmds[i], false, e.toString());
304+
}
305+
answers.add(answer);
306+
if (!answer.getResult() && stopOnError) {
307+
if (i < cmds.length - 1 && s_logger.isDebugEnabled()) {
308+
s_logger.debug(log(seq, "Cancelling because one of the answers is false and it is stop on error."));
309+
}
310+
break;
311+
}
312+
}
313+
314+
Response resp = new Response(_req, answers.toArray(new Answer[answers.size()]));
315+
if (s_logger.isDebugEnabled()) {
316+
s_logger.debug(log(seq, "Response Received: "));
317+
}
318+
319+
processAnswers(seq, resp);
320+
} catch (Exception e) {
321+
s_logger.warn(log(seq, "Exception caught "), e);
322+
} finally {
323+
_outstandingTaskCount.decrementAndGet();
324+
scheduleFromQueue();
325+
}
326+
}
327+
}
251328
}

0 commit comments

Comments
 (0)