Skip to content

Commit 248e4fb

Browse files
committed
CLOUDSTACK-7749: AsyncJob GC thread cannot purge queue items that have been blocking for too long if exception is thrown in expunging some unfinished or completed old jobs, this will make some future jobs stuck.
1 parent a1b913d commit 248e4fb

2 files changed

Lines changed: 35 additions & 24 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public List<SyncQueueItemVO> getBlockedQueueItems(long thresholdMs, boolean excl
147147
SearchBuilder<SyncQueueItemVO> sbItem = createSearchBuilder();
148148
sbItem.and("lastProcessMsid", sbItem.entity().getLastProcessMsid(), SearchCriteria.Op.NNULL);
149149
sbItem.and("lastProcessNumber", sbItem.entity().getLastProcessNumber(), SearchCriteria.Op.NNULL);
150-
sbItem.and("lastProcessNumber", sbItem.entity().getLastProcessTime(), SearchCriteria.Op.NNULL);
150+
sbItem.and("lastProcessTime", sbItem.entity().getLastProcessTime(), SearchCriteria.Op.NNULL);
151151
sbItem.and("lastProcessTime2", sbItem.entity().getLastProcessTime(), SearchCriteria.Op.LT);
152152

153153
sbItem.done();

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

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -769,46 +769,57 @@ protected void runInContext() {
769769

770770
public void reallyRun() {
771771
try {
772-
s_logger.trace("Begin cleanup expired async-jobs");
772+
s_logger.info("Begin cleanup expired async-jobs");
773773

774-
Date cutTime = new Date(DateUtil.currentGMTTime().getTime() - JobExpireMinutes.value() * 60000);
774+
// forcefully cancel blocking queue items if they've been staying there for too long
775+
List<SyncQueueItemVO> blockItems = _queueMgr.getBlockedQueueItems(JobCancelThresholdMinutes.value() * 60000, false);
776+
if (blockItems != null && blockItems.size() > 0) {
777+
for (SyncQueueItemVO item : blockItems) {
778+
try {
779+
if (item.getContentType().equalsIgnoreCase(SyncQueueItem.AsyncJobContentType)) {
780+
s_logger.info("Remove Job-" + item.getContentId() + " from Queue-" + item.getId() + " since it has been blocked for too long");
781+
completeAsyncJob(item.getContentId(), JobInfo.Status.FAILED, 0, "Job is cancelled as it has been blocking others for too long");
782+
783+
_jobMonitor.unregisterByJobId(item.getContentId());
784+
}
785+
786+
// purge the item and resume queue processing
787+
_queueMgr.purgeItem(item.getId());
788+
} catch (Throwable e) {
789+
s_logger.error("Unexpected exception when trying to remove job from sync queue, ", e);
790+
}
791+
}
792+
}
775793

794+
Date cutTime = new Date(DateUtil.currentGMTTime().getTime() - JobExpireMinutes.value() * 60000);
776795
// limit to 100 jobs per turn, this gives cleanup throughput as 600 jobs per minute
777796
// hopefully this will be fast enough to balance potential growth of job table
778797
// 1) Expire unfinished jobs that weren't processed yet
779798
List<AsyncJobVO> unfinishedJobs = _jobDao.getExpiredUnfinishedJobs(cutTime, 100);
780799
for (AsyncJobVO job : unfinishedJobs) {
781-
s_logger.info("Expunging unfinished job " + job);
800+
try {
801+
s_logger.info("Expunging unfinished job-" + job.getId());
782802

783-
_jobMonitor.unregisterByJobId(job.getId());
784-
expungeAsyncJob(job);
803+
_jobMonitor.unregisterByJobId(job.getId());
804+
expungeAsyncJob(job);
805+
} catch (Throwable e) {
806+
s_logger.error("Unexpected exception when trying to expunge job-" + job.getId(), e);
807+
}
785808
}
786809

787810
// 2) Expunge finished jobs
788811
List<AsyncJobVO> completedJobs = _jobDao.getExpiredCompletedJobs(cutTime, 100);
789812
for (AsyncJobVO job : completedJobs) {
790-
s_logger.trace("Expunging completed job " + job);
791-
792-
expungeAsyncJob(job);
793-
}
794-
795-
// forcefully cancel blocking queue items if they've been staying there for too long
796-
List<SyncQueueItemVO> blockItems = _queueMgr.getBlockedQueueItems(JobCancelThresholdMinutes.value() * 60000, false);
797-
if (blockItems != null && blockItems.size() > 0) {
798-
for (SyncQueueItemVO item : blockItems) {
799-
if (item.getContentType().equalsIgnoreCase(SyncQueueItem.AsyncJobContentType)) {
800-
s_logger.info("Remove Job-" + item.getContentId() + " from Queue-" + item.getId() + " since it has been blocked for too long");
801-
completeAsyncJob(item.getContentId(), JobInfo.Status.FAILED, 0, "Job is cancelled as it has been blocking others for too long");
802-
803-
_jobMonitor.unregisterByJobId(item.getContentId());
804-
}
813+
try {
814+
s_logger.info("Expunging completed job-" + job.getId());
805815

806-
// purge the item and resume queue processing
807-
_queueMgr.purgeItem(item.getId());
816+
expungeAsyncJob(job);
817+
} catch (Throwable e) {
818+
s_logger.error("Unexpected exception when trying to expunge job-" + job.getId(), e);
808819
}
809820
}
810821

811-
s_logger.trace("End cleanup expired async-jobs");
822+
s_logger.info("End cleanup expired async-jobs");
812823
} catch (Throwable e) {
813824
s_logger.error("Unexpected exception when trying to execute queue item, ", e);
814825
}

0 commit comments

Comments
 (0)