Retry 3 times on deadlock: if not updated, then throw a runtime exception to roll back the vm start

This commit is contained in:
Chiradeep Vittal 2011-08-30 21:49:22 -07:00
parent 82fcfe8edc
commit 925f5f2f09
2 changed files with 16 additions and 4 deletions

View File

@ -31,6 +31,7 @@ import com.cloud.exception.AgentUnavailableException;
import com.cloud.network.security.SecurityGroupWork.Step;
import com.cloud.uservm.UserVm;
import com.cloud.utils.Profiler;
import com.cloud.utils.exception.CloudRuntimeException;
import com.cloud.vm.VirtualMachine.State;
/**
@ -98,6 +99,9 @@ public class SecurityGroupManagerImpl2 extends SecurityGroupManagerImpl {
int updated = 0;
if (updateSeqno) {
updated = _rulesetLogDao.createOrUpdate(workItems);
if (updated < workItems.size()) {
throw new CloudRuntimeException("Failed to create ruleset log entries");
}
}
int newJobs = _workQueue.submitWorkForVms(workItems);
p.stop();

View File

@ -88,7 +88,7 @@ public class VmRulesetLogDaoImpl extends GenericDaoBase<VmRulesetLogVO, Long> im
private int executeWithRetryOnDeadlock(Transaction txn, String pstmt, List<Long> vmIds) throws SQLException {
int numUpdated = 0;
final int maxTries = 2;
final int maxTries = 3;
for (int i=0; i < maxTries; i++) {
try {
PreparedStatement stmtInsert = txn.prepareAutoCloseStatement(pstmt);
@ -99,8 +99,15 @@ public class VmRulesetLogDaoImpl extends GenericDaoBase<VmRulesetLogVO, Long> im
numUpdated = stmtInsert.executeUpdate();
i = maxTries;
} catch (SQLTransactionRollbackException e1) {
if (i < maxTries-1)
s_logger.debug("Caught a deadlock exception while inserting security group rule log, retrying");
if (i < maxTries-1) {
int delayMs = (i+1)*1000;
s_logger.debug("Caught a deadlock exception while inserting security group rule log, retrying in " + delayMs);
try {
Thread.sleep(delayMs);
} catch(InterruptedException ie) {
}
}
else
s_logger.warn("Caught another deadlock exception while retrying inserting security group rule log, giving up");
@ -134,7 +141,8 @@ public class VmRulesetLogDaoImpl extends GenericDaoBase<VmRulesetLogVO, Long> im
if (s_logger.isTraceEnabled()) {
s_logger.trace("Inserted or updated " + numUpdated + " rows");
}
count += stmtSize;
if (numUpdated > 0)
count += stmtSize;
}
remaining = remaining - numStmts * stmtSize;
}