Skip to content

Commit a275bd7

Browse files
committed
CLOUDSTACK-7864: CPVM continues to be in Stopped state after a failure to start because of a management server restart.
1 parent 392eaf3 commit a275bd7

3 files changed

Lines changed: 34 additions & 2 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,6 @@ public interface AsyncJobDao extends GenericDao<AsyncJobVO, Long> {
3939
void resetJobProcess(long msid, int jobResultCode, String jobResultMessage);
4040

4141
List<AsyncJobVO> getExpiredCompletedJobs(Date cutTime, int limit);
42+
43+
List<AsyncJobVO> getResetJobs(long msid);
4244
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,24 @@ public void resetJobProcess(long msid, int jobResultCode, String jobResultMessag
186186
s_logger.warn("Unable to reset job status for management server " + msid, e);
187187
}
188188
}
189+
190+
@Override
191+
public List<AsyncJobVO> getResetJobs(long msid) {
192+
SearchCriteria<AsyncJobVO> sc = pendingAsyncJobSearch.create();
193+
sc.setParameters("status", JobInfo.Status.IN_PROGRESS);
194+
195+
// construct query: (job_executing_msid=msid OR (job_executing_msid IS NULL AND job_init_msid=msid))
196+
SearchCriteria<AsyncJobVO> msQuery = createSearchCriteria();
197+
msQuery.addOr("executingMsid", SearchCriteria.Op.EQ, msid);
198+
SearchCriteria<AsyncJobVO> initMsQuery = createSearchCriteria();
199+
initMsQuery.addAnd("executingMsid", SearchCriteria.Op.NULL);
200+
initMsQuery.addAnd("initMsid", SearchCriteria.Op.EQ, msid);
201+
msQuery.addOr("initMsId", SearchCriteria.Op.SC, initMsQuery);
202+
203+
sc.addAnd("executingMsid", SearchCriteria.Op.SC, msQuery);
204+
205+
Filter filter = new Filter(AsyncJobVO.class, "created", true, null, null);
206+
return listIncludingRemovedBy(sc, filter);
207+
208+
}
189209
}

‎framework/jobs/src/org/apache/cloudstack/framework/jobs/impl/AsyncJobManagerImpl.java‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,15 +232,17 @@ public void completeAsyncJob(final long jobId, final Status jobStatus, final int
232232
s_logger.debug("job-" + jobId + " no longer exists, we just log completion info here. " + jobStatus + ", resultCode: " + resultCode + ", result: " +
233233
resultObject);
234234
}
235-
235+
// still purge item from queue to avoid any blocking
236+
_queueMgr.purgeAsyncJobQueueItemId(jobId);
236237
return;
237238
}
238239

239240
if (job.getStatus() != JobInfo.Status.IN_PROGRESS) {
240241
if (s_logger.isDebugEnabled()) {
241242
s_logger.debug("job-" + jobId + " is already completed.");
242243
}
243-
244+
// still purge item from queue to avoid any blocking
245+
_queueMgr.purgeAsyncJobQueueItemId(jobId);
244246
return;
245247
}
246248

@@ -547,6 +549,8 @@ protected void runInContext() {
547549
// guard final clause as well
548550
try {
549551
if (job.getSyncSource() != null) {
552+
// here check queue item one more time to double make sure that queue item is removed in case of any uncaught exception
553+
_queueMgr.purgeItem(job.getSyncSource().getId());
550554
checkQueue(job.getSyncSource().getQueueId());
551555
}
552556

@@ -976,6 +980,12 @@ public void doInTransactionWithoutResult(TransactionStatus status) {
976980
_queueMgr.cleanupActiveQueueItems(msid, true);
977981
// reset job status for all jobs running on this ms node
978982
_jobDao.resetJobProcess(msid, ApiErrorCode.INTERNAL_ERROR.getHttpCode(), "job cancelled because of management server restart or shutdown");
983+
// purge those queue items for those cancelled jobs above, which may not be picked up by any MS node yet
984+
List<AsyncJobVO> cancelJobs = _jobDao.getResetJobs(msid);
985+
for (AsyncJobVO job : cancelJobs){
986+
_queueMgr.purgeAsyncJobQueueItemId(job.getId());
987+
}
988+
979989
}
980990
});
981991
} catch (Throwable e) {

0 commit comments

Comments
 (0)