Skip to content

Commit 4807bc8

Browse files
author
Alex Huang
committed
Added a config to enable checking whether a db transaction is wrapped around communications with the agent. If it is, an exception is thrown. This assert has actually been there because it is part of CloudStack's design principle to not use db transactions as a way to enforce atomicity in executing things on hardware resources. However, the assert has been ignored since the move to maven which is not good with enabling asserts. Since then, there's been a lot of commands added that actually runs within db transaction. This is a big no no as the problem is that the remote operation may take a long time and the db can actually close the connection, causing a rollback of the transaction. We should not depend on transactions to enforce the atomicity anyways.
1 parent 15aa618 commit 4807bc8

1 file changed

Lines changed: 18 additions & 2 deletions

File tree

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,13 @@ public class AgentManagerImpl extends ManagerBase implements AgentManager, Handl
187187
"Default size for DirectAgentPool", false);
188188
protected final ConfigKey<Float> DirectAgentThreadCap = new ConfigKey<Float>(Float.class, "direct.agent.thread.cap", "Advanced", "0.1",
189189
"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);
190+
protected final ConfigKey<Boolean> CheckTxnBeforeSending = new ConfigKey<Boolean>(
191+
"Developer",
192+
Boolean.class,
193+
"check.txn.before.sending.agent.commands",
194+
"false",
195+
"This parameter allows developers to enable a check to see if a transaction wraps commands that are sent to the resource. This is not to be enabled on production systems.",
196+
true);
190197

191198
@Override
192199
public boolean configure(final String name, final Map<String, Object> params) throws ConfigurationException {
@@ -378,7 +385,16 @@ public Answer[] send(Long hostId, Commands commands, int timeout) throws AgentUn
378385
if (timeout <= 0) {
379386
timeout = Wait.value();
380387
}
381-
assert noDbTxn() : "I know, I know. Why are we so strict as to not allow txn across an agent call? ... Why are we so cruel ... Why are we such a dictator .... Too bad... Sorry...but NO AGENT COMMANDS WRAPPED WITHIN DB TRANSACTIONS!";
388+
389+
if (CheckTxnBeforeSending.value()) {
390+
if (!noDbTxn()) {
391+
throw new CloudRuntimeException("We do not allow transactions to be wrapped around commands sent to be executed on remote agents. "
392+
+ "We cannot predict how long it takes a command to complete. "
393+
+ "The transaction may be rolled back because the connection took too long.");
394+
}
395+
} else {
396+
assert noDbTxn() : "I know, I know. Why are we so strict as to not allow txn across an agent call? ... Why are we so cruel ... Why are we such a dictator .... Too bad... Sorry...but NO AGENT COMMANDS WRAPPED WITHIN DB TRANSACTIONS!";
397+
}
382398

383399
Command[] cmds = commands.toCommands();
384400

@@ -1582,7 +1598,7 @@ public String getConfigComponentName() {
15821598

15831599
@Override
15841600
public ConfigKey<?>[] getConfigKeys() {
1585-
return new ConfigKey<?>[] {Workers, Port, PingInterval, PingTimeout, Wait, AlertWait, DirectAgentLoadSize, DirectAgentPoolSize, DirectAgentThreadCap};
1601+
return new ConfigKey<?>[] {CheckTxnBeforeSending, Workers, Port, PingInterval, PingTimeout, Wait, AlertWait, DirectAgentLoadSize, DirectAgentPoolSize, DirectAgentThreadCap};
15861602
}
15871603

15881604
}

0 commit comments

Comments
 (0)