Skip to content

Commit be0d688

Browse files
committed
CLOUDSTACK-5731: Use general instance type to categorize VM work jobs to correctly serialize VM operations
1 parent f1b1571 commit be0d688

8 files changed

Lines changed: 468 additions & 105 deletions

File tree

‎engine/components-api/src/com/cloud/vm/VmWorkConstants.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ public interface VmWorkConstants {
2020
public static final String VM_WORK_QUEUE = "VmWorkJobQueue";
2121
public static final String VM_WORK_JOB_DISPATCHER = "VmWorkJobDispatcher";
2222
public static final String VM_WORK_JOB_WAKEUP_DISPATCHER = "VmWorkJobWakeupDispatcher";
23+
public static final String VM_WORK_JOB_PLACEHOLDER = "VmWorkJobPlaceHolder";
2324
}

‎engine/orchestration/src/com/cloud/vm/VirtualMachineManagerImpl.java‎

Lines changed: 150 additions & 21 deletions
Large diffs are not rendered by default.

‎engine/orchestration/src/com/cloud/vm/VmWorkJobDispatcher.java‎

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,6 @@ public void runJob(AsyncJob job) {
5959
String cmd = job.getCmd();
6060
assert(cmd != null);
6161

62-
if(s_logger.isDebugEnabled())
63-
s_logger.debug("Run VM work job: " + cmd + ", job origin: " + job.getRelated());
64-
6562
Class<?> workClz = null;
6663
try {
6764
workClz = Class.forName(job.getCmd());
@@ -79,27 +76,33 @@ public void runJob(AsyncJob job) {
7976
return;
8077
}
8178

82-
if (_handlers == null || _handlers.isEmpty()) {
83-
s_logger.error("Invalid startup configuration, no work job handler is found. cmd: " + job.getCmd() + ", job info: " + job.getCmdInfo()
84-
+ ", job origin: " + job.getRelated());
85-
_asyncJobMgr.completeAsyncJob(job.getId(), JobInfo.Status.FAILED, 0, "Invalid startup configuration. no job handler is found");
86-
return;
87-
}
88-
89-
VmWorkJobHandler handler = _handlers.get(work.getHandlerName());
90-
91-
if (handler == null) {
92-
s_logger.error("Unable to find work job handler. handler name: " + work.getHandlerName() + ", job cmd: " + job.getCmd()
93-
+ ", job info: " + job.getCmdInfo() + ", job origin: " + job.getRelated());
94-
_asyncJobMgr.completeAsyncJob(job.getId(), JobInfo.Status.FAILED, 0, "Unable to find work job handler");
95-
return;
79+
if (s_logger.isDebugEnabled())
80+
s_logger.debug("Run VM work job: " + cmd + " for VM " + work.getVmId() + ", job origin: " + job.getRelated());
81+
try {
82+
if (_handlers == null || _handlers.isEmpty()) {
83+
s_logger.error("Invalid startup configuration, no work job handler is found. cmd: " + job.getCmd() + ", job info: " + job.getCmdInfo()
84+
+ ", job origin: " + job.getRelated());
85+
_asyncJobMgr.completeAsyncJob(job.getId(), JobInfo.Status.FAILED, 0, "Invalid startup configuration. no job handler is found");
86+
return;
87+
}
88+
89+
VmWorkJobHandler handler = _handlers.get(work.getHandlerName());
90+
91+
if (handler == null) {
92+
s_logger.error("Unable to find work job handler. handler name: " + work.getHandlerName() + ", job cmd: " + job.getCmd()
93+
+ ", job info: " + job.getCmdInfo() + ", job origin: " + job.getRelated());
94+
_asyncJobMgr.completeAsyncJob(job.getId(), JobInfo.Status.FAILED, 0, "Unable to find work job handler");
95+
return;
96+
}
97+
98+
CallContext.register(work.getUserId(), work.getAccountId(), job.getRelated());
99+
100+
Pair<JobInfo.Status, String> result = handler.handleVmWorkJob(work);
101+
_asyncJobMgr.completeAsyncJob(job.getId(), result.first(), 0, result.second());
102+
} finally {
103+
if (s_logger.isDebugEnabled())
104+
s_logger.debug("Done with run of VM work job: " + cmd + " for VM " + work.getVmId() + ", job origin: " + job.getRelated());
96105
}
97-
98-
CallContext.register(work.getUserId(), work.getAccountId(), job.getRelated());
99-
100-
Pair<JobInfo.Status, String> result = handler.handleVmWorkJob(work);
101-
_asyncJobMgr.completeAsyncJob(job.getId(), result.first(), 0, result.second());
102-
103106
} catch(Throwable e) {
104107
s_logger.error("Unable to complete " + job + ", job origin:" + job.getRelated(), e);
105108

‎framework/jobs/src/org/apache/cloudstack/framework/jobs/AsyncJobExecutionContext.java‎

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import com.cloud.exception.ResourceUnavailableException;
3232

3333
public class AsyncJobExecutionContext {
34+
private static final Logger s_logger = Logger.getLogger(AsyncJobExecutionContext.class);
35+
3436
private AsyncJob _job;
3537

3638
static private AsyncJobManager _jobMgr;
@@ -112,29 +114,43 @@ public void joinJob(long joinJobId, String wakeupHandler, String wakeupDispatche
112114
}
113115

114116
//
115-
// check failure exception before we disjoin the worker job
117+
// check failure exception before we disjoin the worker job, work job usually fails with exception
118+
// this will help propogate exception between jobs
116119
// TODO : it is ugly and this will become unnecessary after we switch to full-async mode
117120
//
118121
public void disjoinJob(long joinedJobId) throws InsufficientCapacityException,
119122
ConcurrentOperationException, ResourceUnavailableException {
120123
assert(_job != null);
121124

122125
AsyncJobJoinMapVO record = _joinMapDao.getJoinRecord(_job.getId(), joinedJobId);
123-
if(record.getJoinStatus() == JobInfo.Status.FAILED && record.getJoinResult() != null) {
124-
Object exception = JobSerializerHelper.fromObjectSerializedString(record.getJoinResult());
125-
if(exception != null && exception instanceof Exception) {
126-
if(exception instanceof InsufficientCapacityException)
127-
throw (InsufficientCapacityException)exception;
128-
else if(exception instanceof ConcurrentOperationException)
129-
throw (ConcurrentOperationException)exception;
130-
else if(exception instanceof ResourceUnavailableException)
131-
throw (ResourceUnavailableException)exception;
132-
else
133-
throw new RuntimeException((Exception)exception);
134-
}
126+
_jobMgr.disjoinJob(_job.getId(), joinedJobId);
127+
128+
if (record.getJoinStatus() == JobInfo.Status.FAILED) {
129+
if (record.getJoinResult() != null) {
130+
Object exception = JobSerializerHelper.fromObjectSerializedString(record.getJoinResult());
131+
if (exception != null && exception instanceof Exception) {
132+
if (exception instanceof InsufficientCapacityException) {
133+
s_logger.error("Job " + joinedJobId + " failed with InsufficientCapacityException");
134+
throw (InsufficientCapacityException)exception;
135+
}
136+
else if (exception instanceof ConcurrentOperationException) {
137+
s_logger.error("Job " + joinedJobId + " failed with ConcurrentOperationException");
138+
throw (ConcurrentOperationException)exception;
139+
}
140+
else if (exception instanceof ResourceUnavailableException) {
141+
s_logger.error("Job " + joinedJobId + " failed with ResourceUnavailableException");
142+
throw (ResourceUnavailableException)exception;
143+
}
144+
else {
145+
s_logger.error("Job " + joinedJobId + " failed with exception");
146+
throw new RuntimeException((Exception)exception);
147+
}
148+
}
149+
} else {
150+
s_logger.error("Job " + joinedJobId + " failed without providing an error object");
151+
throw new RuntimeException("Job " + joinedJobId + " failed without providing an error object");
152+
}
135153
}
136-
137-
_jobMgr.disjoinJob(_job.getId(), joinedJobId);
138154
}
139155

140156
public void completeJoin(JobInfo.Status joinStatus, String joinResult) {
@@ -151,6 +167,8 @@ public void completeJobAndJoin(JobInfo.Status joinStatus, String joinResult) {
151167
public static AsyncJobExecutionContext getCurrentExecutionContext() {
152168
AsyncJobExecutionContext context = s_currentExectionContext.get();
153169
if (context == null) {
170+
// TODO, this has security implicitions
171+
s_logger.warn("Job is executed without a context, setup psudo job for the executing thread");
154172
context = registerPseudoExecutionContext(CallContext.current().getCallingAccountId(),
155173
CallContext.current().getCallingUserId());
156174
}

‎framework/jobs/src/org/apache/cloudstack/framework/jobs/dao/VmWorkJobDao.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ public interface VmWorkJobDao extends GenericDao<VmWorkJobVO, Long> {
2929
VmWorkJobVO findPendingWorkJob(VirtualMachine.Type type, long instanceId);
3030
List<VmWorkJobVO> listPendingWorkJobs(VirtualMachine.Type type, long instanceId);
3131
List<VmWorkJobVO> listPendingWorkJobs(VirtualMachine.Type type, long instanceId, String jobCmd);
32-
32+
3333
void updateStep(long workJobId, Step step);
3434
void expungeCompletedWorkJobs(Date cutDate);
35+
36+
void expungeLeftoverWorkJobs(long msid);
3537
}

‎framework/jobs/src/org/apache/cloudstack/framework/jobs/dao/VmWorkJobDaoImpl.java‎

Lines changed: 95 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@
1616
// under the License.
1717
package org.apache.cloudstack.framework.jobs.dao;
1818

19+
import java.sql.PreparedStatement;
20+
import java.sql.SQLException;
1921
import java.util.Date;
2022
import java.util.List;
23+
import java.util.TimeZone;
2124

2225
import javax.annotation.PostConstruct;
2326

@@ -31,24 +34,26 @@
3134
import com.cloud.utils.db.SearchBuilder;
3235
import com.cloud.utils.db.SearchCriteria;
3336
import com.cloud.utils.db.SearchCriteria.Op;
37+
import com.cloud.utils.db.Transaction;
38+
import com.cloud.utils.db.TransactionCallbackNoReturn;
39+
import com.cloud.utils.db.TransactionLegacy;
40+
import com.cloud.utils.db.TransactionStatus;
3441
import com.cloud.vm.VirtualMachine;
3542

3643
public class VmWorkJobDaoImpl extends GenericDaoBase<VmWorkJobVO, Long> implements VmWorkJobDao {
3744

3845
protected SearchBuilder<VmWorkJobVO> PendingWorkJobSearch;
3946
protected SearchBuilder<VmWorkJobVO> PendingWorkJobByCommandSearch;
40-
protected SearchBuilder<VmWorkJobVO> ExpungeWorkJobSearch;
41-
47+
4248
public VmWorkJobDaoImpl() {
4349
}
44-
50+
4551
@PostConstruct
4652
public void init() {
4753
PendingWorkJobSearch = createSearchBuilder();
4854
PendingWorkJobSearch.and("jobStatus", PendingWorkJobSearch.entity().getStatus(), Op.EQ);
4955
PendingWorkJobSearch.and("vmType", PendingWorkJobSearch.entity().getVmType(), Op.EQ);
5056
PendingWorkJobSearch.and("vmInstanceId", PendingWorkJobSearch.entity().getVmInstanceId(), Op.EQ);
51-
PendingWorkJobSearch.and("step", PendingWorkJobSearch.entity().getStep(), Op.NEQ);
5257
PendingWorkJobSearch.done();
5358

5459
PendingWorkJobByCommandSearch = createSearchBuilder();
@@ -58,68 +63,132 @@ public void init() {
5863
PendingWorkJobByCommandSearch.and("step", PendingWorkJobByCommandSearch.entity().getStep(), Op.NEQ);
5964
PendingWorkJobByCommandSearch.and("cmd", PendingWorkJobByCommandSearch.entity().getCmd(), Op.EQ);
6065
PendingWorkJobByCommandSearch.done();
61-
62-
ExpungeWorkJobSearch = createSearchBuilder();
63-
ExpungeWorkJobSearch.and("lastUpdated", ExpungeWorkJobSearch.entity().getLastUpdated(), Op.LT);
64-
ExpungeWorkJobSearch.and("jobStatus", ExpungeWorkJobSearch.entity().getStatus(), Op.NEQ);
65-
ExpungeWorkJobSearch.done();
6666
}
67-
67+
6868
@Override
6969
public VmWorkJobVO findPendingWorkJob(VirtualMachine.Type type, long instanceId) {
70-
70+
7171
SearchCriteria<VmWorkJobVO> sc = PendingWorkJobSearch.create();
7272
sc.setParameters("jobStatus", JobInfo. Status.IN_PROGRESS);
7373
sc.setParameters("vmType", type);
7474
sc.setParameters("vmInstanceId", instanceId);
75-
75+
7676
Filter filter = new Filter(VmWorkJobVO.class, "created", true, null, null);
7777
List<VmWorkJobVO> result = this.listBy(sc, filter);
7878
if(result != null && result.size() > 0)
7979
return result.get(0);
80-
80+
8181
return null;
8282
}
83-
83+
8484
@Override
8585
public List<VmWorkJobVO> listPendingWorkJobs(VirtualMachine.Type type, long instanceId) {
86-
86+
8787
SearchCriteria<VmWorkJobVO> sc = PendingWorkJobSearch.create();
8888
sc.setParameters("jobStatus", JobInfo.Status.IN_PROGRESS);
8989
sc.setParameters("vmType", type);
9090
sc.setParameters("vmInstanceId", instanceId);
91-
91+
9292
Filter filter = new Filter(VmWorkJobVO.class, "created", true, null, null);
9393
return this.listBy(sc, filter);
9494
}
9595

9696
@Override
9797
public List<VmWorkJobVO> listPendingWorkJobs(VirtualMachine.Type type, long instanceId, String jobCmd) {
98-
98+
9999
SearchCriteria<VmWorkJobVO> sc = PendingWorkJobByCommandSearch.create();
100100
sc.setParameters("jobStatus", JobInfo.Status.IN_PROGRESS);
101101
sc.setParameters("vmType", type);
102102
sc.setParameters("vmInstanceId", instanceId);
103103
sc.setParameters("cmd", jobCmd);
104-
104+
105105
Filter filter = new Filter(VmWorkJobVO.class, "created", true, null, null);
106106
return this.listBy(sc, filter);
107107
}
108-
108+
109109
@Override
110110
public void updateStep(long workJobId, Step step) {
111111
VmWorkJobVO jobVo = findById(workJobId);
112112
jobVo.setStep(step);
113113
jobVo.setLastUpdated(DateUtil.currentGMTTime());
114114
update(workJobId, jobVo);
115115
}
116-
116+
117117
@Override
118-
public void expungeCompletedWorkJobs(Date cutDate) {
119-
SearchCriteria<VmWorkJobVO> sc = ExpungeWorkJobSearch.create();
120-
sc.setParameters("lastUpdated",cutDate);
121-
sc.setParameters("jobStatus", JobInfo.Status.IN_PROGRESS);
122-
123-
expunge(sc);
118+
public void expungeCompletedWorkJobs(final Date cutDate) {
119+
// current DAO machenism does not support following usage
120+
/*
121+
SearchCriteria<VmWorkJobVO> sc = ExpungeWorkJobSearch.create();
122+
sc.setParameters("lastUpdated",cutDate);
123+
sc.setParameters("jobStatus", JobInfo.Status.IN_PROGRESS);
124+
125+
expunge(sc);
126+
*/
127+
Transaction.execute(new TransactionCallbackNoReturn() {
128+
@Override
129+
public void doInTransactionWithoutResult(TransactionStatus status) {
130+
TransactionLegacy txn = TransactionLegacy.currentTxn();
131+
132+
PreparedStatement pstmt = null;
133+
try {
134+
pstmt = txn.prepareAutoCloseStatement(
135+
"DELETE FROM vm_work_job WHERE id IN (SELECT id FROM async_job WHERE job_dispatcher='VmWorkJobDispatcher' AND job_status != 0 AND last_updated < ?)");
136+
pstmt.setString(1, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), cutDate));
137+
138+
pstmt.execute();
139+
} catch (SQLException e) {
140+
} catch (Throwable e) {
141+
}
142+
143+
try {
144+
pstmt = txn.prepareAutoCloseStatement(
145+
"DELETE FROM async_job WHERE job_dispatcher='VmWorkJobDispatcher' AND job_status != 0 AND last_updated < ?");
146+
pstmt.setString(1, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), cutDate));
147+
148+
pstmt.execute();
149+
} catch (SQLException e) {
150+
} catch (Throwable e) {
151+
}
152+
}
153+
});
124154
}
155+
156+
@Override
157+
public void expungeLeftoverWorkJobs(final long msid) {
158+
// current DAO machenism does not support following usage
159+
/*
160+
SearchCriteria<VmWorkJobVO> sc = ExpungePlaceHolderWorkJobSearch.create();
161+
sc.setParameters("dispatcher", "VmWorkJobPlaceHolder");
162+
sc.setParameters("msid", msid);
163+
164+
expunge(sc);
165+
*/
166+
Transaction.execute(new TransactionCallbackNoReturn() {
167+
@Override
168+
public void doInTransactionWithoutResult(TransactionStatus status) {
169+
TransactionLegacy txn = TransactionLegacy.currentTxn();
170+
171+
PreparedStatement pstmt = null;
172+
try {
173+
pstmt = txn.prepareAutoCloseStatement(
174+
"DELETE FROM vm_work_job WHERE id IN (SELECT id FROM async_job WHERE (job_dispatcher='VmWorkJobPlaceHolder' OR job_dispatcher='VmWorkJobDispatcher') AND job_init_msid=?)");
175+
pstmt.setLong(1, msid);
176+
177+
pstmt.execute();
178+
} catch (SQLException e) {
179+
} catch (Throwable e) {
180+
}
181+
182+
try {
183+
pstmt = txn.prepareAutoCloseStatement(
184+
"DELETE FROM async_job WHERE (job_dispatcher='VmWorkJobPlaceHolder' OR job_dispatcher='VmWorkJobDispatcher') AND job_init_msid=?");
185+
pstmt.setLong(1, msid);
186+
187+
pstmt.execute();
188+
} catch (SQLException e) {
189+
} catch (Throwable e) {
190+
}
191+
}
192+
});
193+
}
125194
}

0 commit comments

Comments
 (0)