Skip to content

Commit 269a4ef

Browse files
committed
CLOUDSTACK-4855: Throttle based on the # of outstanding requests to the directly managed HV host (direct agents)
Cloudstack sends requests to directly managed HV hosts (direct agents) using the direct agent thread pool. The size of the pool is determined by global config direct.agent.pool.size defaulted to 500. Currently there is no restriction on the number of threads a direct agent can use from this shared thread pool to send requests to the host. This is fine as long as the host is responding to requests in a reasonable amount of time. But if there is a considerable delay in getting response, the thread remain blocked for that much time. As more commands are send to the slow host threads keep getting blocked. This can eventually lead to a situation where requests to healthy hosts cannot be processed as there are not enough free threads. The problem being addressed here is to localize the impact of few bad hosts, so that entire management server is not affected. One such way is to throttle based on the # of outstanding requests on per host basis. The outstanding requests to a host will be a % of direct agent pool size. This is configurable based on direct.agent.thread.cap. The default value is 0.1 or 10%, a value of 1 would mean the old behavior where there is no upper cap. This will ensure that the impacted host will be bound by a upper cap on the number of threads it can use to process requests and not the entire pool.
1 parent 99ead34 commit 269a4ef

3 files changed

Lines changed: 54 additions & 12 deletions

File tree

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
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;
3334

3435
import org.apache.cloudstack.managed.context.ManagedContextRunnable;
3536
import org.apache.log4j.Logger;
@@ -107,7 +108,8 @@ public int compare(Object o1, Object o2) {
107108
protected Long _currentSequence;
108109
protected Status _status = Status.Connecting;
109110
protected boolean _maintenance;
110-
protected long _nextSequence;
111+
protected long _nextSequence;
112+
protected AtomicInteger _outstandingTaskCount;
111113

112114
protected AgentManagerImpl _agentMgr;
113115

@@ -131,6 +133,7 @@ protected AgentAttache(AgentManagerImpl agentMgr, final long id, final String na
131133
_requests = new LinkedList<Request>();
132134
_agentMgr = agentMgr;
133135
_nextSequence = s_rand.nextInt(Short.MAX_VALUE) << 48;
136+
_outstandingTaskCount = new AtomicInteger(0);
134137
}
135138

136139
public synchronized long getNextSequence() {

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

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -159,24 +159,28 @@ public class AgentManagerImpl extends ManagerBase implements AgentManager, Handl
159159
protected ScheduledExecutorService _directAgentExecutor;
160160
protected ScheduledExecutorService _monitorExecutor;
161161

162+
private int _directAgentThreadCap;
163+
162164
protected StateMachine2<Status, Status.Event, Host> _statusStateMachine = Status.getStateMachine();
163165
private final Map<Long, Long> _pingMap = new ConcurrentHashMap<Long, Long>(10007);
164166

165167
@Inject ResourceManager _resourceMgr;
166168

167-
protected final ConfigKey<Integer> Workers = new ConfigKey<Integer>(Integer.class, "workers", "Advance", "5",
169+
protected final ConfigKey<Integer> Workers = new ConfigKey<Integer>(Integer.class, "workers", "Advanced", "5",
168170
"Number of worker threads handling remote agent connections.", false);
169-
protected final ConfigKey<Integer> Port = new ConfigKey<Integer>(Integer.class, "port", "Advance", "8250", "Port to listen on for remote agent connections.", false);
170-
protected final ConfigKey<Integer> PingInterval = new ConfigKey<Integer>(Integer.class, "ping.interval", "Advance", "60",
171+
protected final ConfigKey<Integer> Port = new ConfigKey<Integer>(Integer.class, "port", "Advanced", "8250", "Port to listen on for remote agent connections.", false);
172+
protected final ConfigKey<Integer> PingInterval = new ConfigKey<Integer>(Integer.class, "ping.interval", "Advanced", "60",
171173
"Interval to send application level pings to make sure the connection is still working", false);
172-
protected final ConfigKey<Float> PingTimeout = new ConfigKey<Float>(Float.class, "ping.timeout", "Advance", "2.5",
174+
protected final ConfigKey<Float> PingTimeout = new ConfigKey<Float>(Float.class, "ping.timeout", "Advanced", "2.5",
173175
"Multiplier to ping.interval before announcing an agent has timed out", true);
174-
protected final ConfigKey<Integer> AlertWait = new ConfigKey<Integer>(Integer.class, "alert.wait", "Advance", "1800",
176+
protected final ConfigKey<Integer> AlertWait = new ConfigKey<Integer>(Integer.class, "alert.wait", "Advanced", "1800",
175177
"Seconds to wait before alerting on a disconnected agent", true);
176-
protected final ConfigKey<Integer> DirectAgentLoadSize = new ConfigKey<Integer>(Integer.class, "direct.agent.load.size", "Advance", "16",
178+
protected final ConfigKey<Integer> DirectAgentLoadSize = new ConfigKey<Integer>(Integer.class, "direct.agent.load.size", "Advanced", "16",
177179
"The number of direct agents to load each time", false);
178-
protected final ConfigKey<Integer> DirectAgentPoolSize = new ConfigKey<Integer>(Integer.class, "direct.agent.pool.size", "Advance", "500",
180+
protected final ConfigKey<Integer> DirectAgentPoolSize = new ConfigKey<Integer>(Integer.class, "direct.agent.pool.size", "Advanced", "500",
179181
"Default size for DirectAgentPool", false);
182+
protected final ConfigKey<Float> DirectAgentThreadCap = new ConfigKey<Float>(Float.class, "direct.agent.thread.cap", "Advanced", "0.1",
183+
"Percentage (as a value between 0 and 1) of direct.agent.pool.size to be used as upper thread cap for a single direct agent to process requests", false);
180184

181185
@Override
182186
public boolean configure(final String name, final Map<String, Object> params) throws ConfigurationException {
@@ -202,10 +206,10 @@ public boolean configure(final String name, final Map<String, Object> params) th
202206
_connection = new NioServer("AgentManager", Port.value(), Workers.value() + 10, this);
203207
s_logger.info("Listening on " + Port.value() + " with " + Workers.value() + " workers");
204208

205-
206209
_directAgentExecutor = new ScheduledThreadPoolExecutor(DirectAgentPoolSize.value(), new NamedThreadFactory("DirectAgent"));
207210
s_logger.debug("Created DirectAgentAttache pool with size: " + DirectAgentPoolSize.value());
208-
211+
_directAgentThreadCap = Math.round(DirectAgentPoolSize.value() * DirectAgentThreadCap.value()) + 1; // add 1 to always make the value > 0
212+
209213
_monitorExecutor = new ScheduledThreadPoolExecutor(1, new NamedThreadFactory("AgentMonitor"));
210214

211215
return true;
@@ -1422,6 +1426,10 @@ public ScheduledExecutorService getDirectAgentPool() {
14221426
return _directAgentExecutor;
14231427
}
14241428

1429+
public int getDirectAgentThreadCap() {
1430+
return _directAgentThreadCap;
1431+
}
1432+
14251433
public Long getAgentPingTime(long agentId) {
14261434
return _pingMap.get(agentId);
14271435
}
@@ -1568,7 +1576,7 @@ public String getConfigComponentName() {
15681576

15691577
@Override
15701578
public ConfigKey<?>[] getConfigKeys() {
1571-
return new ConfigKey<?>[] {Workers, Port, PingInterval, PingTimeout, Wait, AlertWait, DirectAgentLoadSize, DirectAgentPoolSize};
1579+
return new ConfigKey<?>[] {Workers, Port, PingInterval, PingTimeout, Wait, AlertWait, DirectAgentLoadSize, DirectAgentPoolSize, DirectAgentThreadCap};
15721580
}
15731581

15741582
}

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ protected class PingTask extends ManagedContextRunnable {
132132
@Override
133133
protected synchronized void runInContext() {
134134
try {
135+
if (_outstandingTaskCount.incrementAndGet() > _agentMgr.getDirectAgentThreadCap()) {
136+
s_logger.warn("Task execution for direct attache(" + _id + ") has reached maximum outstanding limit(" + _agentMgr.getDirectAgentThreadCap() + "), bailing out");
137+
return;
138+
}
139+
135140
ServerResource resource = _resource;
136141

137142
if (resource != null) {
@@ -156,6 +161,8 @@ protected synchronized void runInContext() {
156161
}
157162
} catch (Exception e) {
158163
s_logger.warn("Unable to complete the ping task", e);
164+
} finally {
165+
_outstandingTaskCount.decrementAndGet();
159166
}
160167
}
161168
}
@@ -168,10 +175,32 @@ public Task(Request req) {
168175
_req = req;
169176
}
170177

178+
private void bailout() {
179+
long seq = _req.getSequence();
180+
try {
181+
Command[] cmds = _req.getCommands();
182+
ArrayList<Answer> answers = new ArrayList<Answer>(cmds.length);
183+
for (Command cmd : cmds) {
184+
Answer answer = new Answer(cmd, false, "Bailed out as maximum oustanding task limit reached");
185+
answers.add(answer);
186+
}
187+
Response resp = new Response(_req, answers.toArray(new Answer[answers.size()]));
188+
processAnswers(seq, resp);
189+
} catch (Exception e) {
190+
s_logger.warn(log(seq, "Exception caught in bailout "), e);
191+
}
192+
}
193+
171194
@Override
172195
protected void runInContext() {
173196
long seq = _req.getSequence();
174197
try {
198+
if (_outstandingTaskCount.incrementAndGet() > _agentMgr.getDirectAgentThreadCap()) {
199+
s_logger.warn("Task execution for direct attache(" + _id + ") has reached maximum outstanding limit(" + _agentMgr.getDirectAgentThreadCap() + "), bailing out");
200+
bailout();
201+
return;
202+
}
203+
175204
ServerResource resource = _resource;
176205
Command[] cmds = _req.getCommands();
177206
boolean stopOnError = _req.stopOnError();
@@ -186,7 +215,7 @@ protected void runInContext() {
186215
if (resource != null) {
187216
answer = resource.executeRequest(cmds[i]);
188217
if(answer == null) {
189-
s_logger.warn("Resource returned null answer!");
218+
s_logger.warn("Resource returned null answer!");
190219
answer = new Answer(cmds[i], false, "Resource returned null answer");
191220
}
192221
} else {
@@ -213,6 +242,8 @@ protected void runInContext() {
213242
processAnswers(seq, resp);
214243
} catch (Exception e) {
215244
s_logger.warn(log(seq, "Exception caught "), e);
245+
} finally {
246+
_outstandingTaskCount.decrementAndGet();
216247
}
217248
}
218249
}

0 commit comments

Comments
 (0)