Skip to content

Commit 2ecf9e3

Browse files
author
Alena Prokharchyk
committed
CLOUDSTACK-2680: Async job expunge thread - expunge only:
1) Unfinished jobs that are yet to be processed. 2) Completed jobs The jobs that are in process, will be skipped by the expunge thread Conflicts: server/src/com/cloud/async/dao/AsyncJobDao.java server/src/com/cloud/async/dao/AsyncJobDaoImpl.java server/src/com/cloud/storage/snapshot/SnapshotManagerImpl.java
1 parent 6f65a5b commit 2ecf9e3

3 files changed

Lines changed: 70 additions & 40 deletions

File tree

‎server/src/com/cloud/async/AsyncJobManagerImpl.java‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -621,11 +621,18 @@ public void reallyRun() {
621621

622622
// limit to 100 jobs per turn, this gives cleanup throughput as 600 jobs per minute
623623
// hopefully this will be fast enough to balance potential growth of job table
624-
List<AsyncJobVO> l = _jobDao.getExpiredJobs(cutTime, 100);
625-
if(l != null && l.size() > 0) {
626-
for(AsyncJobVO job : l) {
627-
expungeAsyncJob(job);
628-
}
624+
//1) Expire unfinished jobs that weren't processed yet
625+
List<AsyncJobVO> l = _jobDao.getExpiredUnfinishedJobs(cutTime, 100);
626+
for(AsyncJobVO job : l) {
627+
s_logger.trace("Expunging unfinished job " + job);
628+
expungeAsyncJob(job);
629+
}
630+
631+
//2) Expunge finished jobs
632+
List<AsyncJobVO> completedJobs = _jobDao.getExpiredCompletedJobs(cutTime, 100);
633+
for(AsyncJobVO job : completedJobs) {
634+
s_logger.trace("Expunging completed job " + job);
635+
expungeAsyncJob(job);
629636
}
630637

631638
// forcefully cancel blocking queue items if they've been staying there for too long

‎server/src/com/cloud/async/dao/AsyncJobDao.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
public interface AsyncJobDao extends GenericDao<AsyncJobVO, Long> {
2727
AsyncJobVO findInstancePendingAsyncJob(String instanceType, long instanceId);
2828
List<AsyncJobVO> findInstancePendingAsyncJobs(AsyncJob.Type instanceType, Long accountId);
29-
List<AsyncJobVO> getExpiredJobs(Date cutTime, int limit);
29+
List<AsyncJobVO> getExpiredUnfinishedJobs(Date cutTime, int limit);
3030
void resetJobProcess(long msid, int jobResultCode, String jobResultMessage);
31-
}
31+
List<AsyncJobVO> getExpiredCompletedJobs(Date cutTime, int limit);
32+
}

‎server/src/com/cloud/async/dao/AsyncJobDaoImpl.java‎

Lines changed: 55 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,19 @@ public class AsyncJobDaoImpl extends GenericDaoBase<AsyncJobVO, Long> implements
4242
private static final Logger s_logger = Logger.getLogger(AsyncJobDaoImpl.class.getName());
4343

4444
private final SearchBuilder<AsyncJobVO> pendingAsyncJobSearch;
45-
private final SearchBuilder<AsyncJobVO> pendingAsyncJobsSearch;
46-
private final SearchBuilder<AsyncJobVO> expiringAsyncJobSearch;
47-
48-
public AsyncJobDaoImpl() {
49-
pendingAsyncJobSearch = createSearchBuilder();
50-
pendingAsyncJobSearch.and("instanceType", pendingAsyncJobSearch.entity().getInstanceType(),
51-
SearchCriteria.Op.EQ);
52-
pendingAsyncJobSearch.and("instanceId", pendingAsyncJobSearch.entity().getInstanceId(),
53-
SearchCriteria.Op.EQ);
54-
pendingAsyncJobSearch.and("status", pendingAsyncJobSearch.entity().getStatus(),
55-
SearchCriteria.Op.EQ);
45+
private final SearchBuilder<AsyncJobVO> pendingAsyncJobsSearch;
46+
private final SearchBuilder<AsyncJobVO> expiringUnfinishedAsyncJobSearch;
47+
private final SearchBuilder<AsyncJobVO> expiringCompletedAsyncJobSearch;
48+
49+
50+
public AsyncJobDaoImpl() {
51+
pendingAsyncJobSearch = createSearchBuilder();
52+
pendingAsyncJobSearch.and("instanceType", pendingAsyncJobSearch.entity().getInstanceType(),
53+
SearchCriteria.Op.EQ);
54+
pendingAsyncJobSearch.and("instanceId", pendingAsyncJobSearch.entity().getInstanceId(),
55+
SearchCriteria.Op.EQ);
56+
pendingAsyncJobSearch.and("status", pendingAsyncJobSearch.entity().getStatus(),
57+
SearchCriteria.Op.EQ);
5658
pendingAsyncJobSearch.done();
5759

5860
pendingAsyncJobsSearch = createSearchBuilder();
@@ -64,27 +66,36 @@ public AsyncJobDaoImpl() {
6466
SearchCriteria.Op.EQ);
6567
pendingAsyncJobsSearch.done();
6668

67-
expiringAsyncJobSearch = createSearchBuilder();
68-
expiringAsyncJobSearch.and("created", expiringAsyncJobSearch.entity().getCreated(),
69+
expiringUnfinishedAsyncJobSearch = createSearchBuilder();
70+
expiringUnfinishedAsyncJobSearch.and("created", expiringUnfinishedAsyncJobSearch.entity().getCreated(),
6971
SearchCriteria.Op.LTEQ);
70-
expiringAsyncJobSearch.done();
71-
}
72-
73-
public AsyncJobVO findInstancePendingAsyncJob(String instanceType, long instanceId) {
74-
SearchCriteria<AsyncJobVO> sc = pendingAsyncJobSearch.create();
75-
sc.setParameters("instanceType", instanceType);
76-
sc.setParameters("instanceId", instanceId);
77-
sc.setParameters("status", AsyncJobResult.STATUS_IN_PROGRESS);
78-
79-
List<AsyncJobVO> l = listIncludingRemovedBy(sc);
80-
if(l != null && l.size() > 0) {
81-
if(l.size() > 1) {
82-
s_logger.warn("Instance " + instanceType + "-" + instanceId + " has multiple pending async-job");
83-
}
84-
85-
return l.get(0);
86-
}
87-
return null;
72+
expiringUnfinishedAsyncJobSearch.and("completeMsId", expiringUnfinishedAsyncJobSearch.entity().getCompleteMsid(), SearchCriteria.Op.NULL);
73+
expiringUnfinishedAsyncJobSearch.and("jobStatus", expiringUnfinishedAsyncJobSearch.entity().getStatus(), SearchCriteria.Op.EQ);
74+
expiringUnfinishedAsyncJobSearch.done();
75+
76+
expiringCompletedAsyncJobSearch = createSearchBuilder();
77+
expiringCompletedAsyncJobSearch.and("created", expiringCompletedAsyncJobSearch.entity().getCreated(),
78+
SearchCriteria.Op.LTEQ);
79+
expiringCompletedAsyncJobSearch.and("completeMsId", expiringCompletedAsyncJobSearch.entity().getCompleteMsid(), SearchCriteria.Op.NNULL);
80+
expiringCompletedAsyncJobSearch.and("jobStatus", expiringCompletedAsyncJobSearch.entity().getStatus(), SearchCriteria.Op.NEQ);
81+
expiringCompletedAsyncJobSearch.done();
82+
}
83+
84+
public AsyncJobVO findInstancePendingAsyncJob(String instanceType, long instanceId) {
85+
SearchCriteria<AsyncJobVO> sc = pendingAsyncJobSearch.create();
86+
sc.setParameters("instanceType", instanceType);
87+
sc.setParameters("instanceId", instanceId);
88+
sc.setParameters("status", AsyncJobResult.STATUS_IN_PROGRESS);
89+
90+
List<AsyncJobVO> l = listIncludingRemovedBy(sc);
91+
if(l != null && l.size() > 0) {
92+
if(l.size() > 1) {
93+
s_logger.warn("Instance " + instanceType + "-" + instanceId + " has multiple pending async-job");
94+
}
95+
96+
return l.get(0);
97+
}
98+
return null;
8899
}
89100

90101
public List<AsyncJobVO> findInstancePendingAsyncJobs(AsyncJob.Type instanceType, Long accountId) {
@@ -99,9 +110,20 @@ public List<AsyncJobVO> findInstancePendingAsyncJobs(AsyncJob.Type instanceType,
99110
return listBy(sc);
100111
}
101112

102-
public List<AsyncJobVO> getExpiredJobs(Date cutTime, int limit) {
103-
SearchCriteria<AsyncJobVO> sc = expiringAsyncJobSearch.create();
113+
@Override
114+
public List<AsyncJobVO> getExpiredUnfinishedJobs(Date cutTime, int limit) {
115+
SearchCriteria<AsyncJobVO> sc = expiringUnfinishedAsyncJobSearch.create();
116+
sc.setParameters("created", cutTime);
117+
sc.setParameters("jobStatus", 0);
118+
Filter filter = new Filter(AsyncJobVO.class, "created", true, 0L, (long)limit);
119+
return listIncludingRemovedBy(sc, filter);
120+
}
121+
122+
@Override
123+
public List<AsyncJobVO> getExpiredCompletedJobs(Date cutTime, int limit) {
124+
SearchCriteria<AsyncJobVO> sc = expiringCompletedAsyncJobSearch.create();
104125
sc.setParameters("created", cutTime);
126+
sc.setParameters("jobStatus", 0);
105127
Filter filter = new Filter(AsyncJobVO.class, "created", true, 0L, (long)limit);
106128
return listIncludingRemovedBy(sc, filter);
107129
}

0 commit comments

Comments
 (0)