Skip to content

Commit 0d65c8c

Browse files
author
Daan Hoogland
committed
Merge branch '4.19' into 4.20
2 parents c94f75c + 06c80cd commit 0d65c8c

File tree

3 files changed

+48
-18
lines changed

3 files changed

+48
-18
lines changed

engine/schema/src/main/java/com/cloud/usage/dao/UsageJobDao.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,6 @@ public interface UsageJobDao extends GenericDao<UsageJobVO, Long> {
3737
UsageJobVO isOwner(String hostname, int pid);
3838

3939
void updateJobSuccess(Long jobId, long startMillis, long endMillis, long execTime, boolean success);
40+
41+
void removeLastOpenJobsOwned(String hostname, int pid);
4042
}

engine/schema/src/main/java/com/cloud/usage/dao/UsageJobDaoImpl.java

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.List;
2323

2424

25+
import org.apache.commons.collections.CollectionUtils;
2526
import org.springframework.stereotype.Component;
2627

2728
import com.cloud.usage.UsageJobVO;
@@ -114,7 +115,7 @@ public Long checkHeartbeat(String hostname, int pid, int aggregationDuration) {
114115
public UsageJobVO isOwner(String hostname, int pid) {
115116
TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB);
116117
try {
117-
if ((hostname == null) || (pid <= 0)) {
118+
if (hostname == null || pid <= 0) {
118119
return null;
119120
}
120121

@@ -174,7 +175,7 @@ public UsageJobVO getNextImmediateJob() {
174175
SearchCriteria<UsageJobVO> sc = createSearchCriteria();
175176
sc.addAnd("endMillis", SearchCriteria.Op.EQ, Long.valueOf(0));
176177
sc.addAnd("jobType", SearchCriteria.Op.EQ, Integer.valueOf(UsageJobVO.JOB_TYPE_SINGLE));
177-
sc.addAnd("scheduled", SearchCriteria.Op.EQ, Integer.valueOf(0));
178+
sc.addAnd("scheduled", SearchCriteria.Op.EQ, Integer.valueOf(UsageJobVO.JOB_NOT_SCHEDULED));
178179
List<UsageJobVO> jobs = search(sc, filter);
179180

180181
if ((jobs == null) || jobs.isEmpty()) {
@@ -194,4 +195,36 @@ public Date getLastHeartbeat() {
194195
}
195196
return jobs.get(0).getHeartbeat();
196197
}
198+
199+
private List<UsageJobVO> getLastOpenJobsOwned(String hostname, int pid) {
200+
SearchCriteria<UsageJobVO> sc = createSearchCriteria();
201+
sc.addAnd("endMillis", SearchCriteria.Op.EQ, Long.valueOf(0));
202+
sc.addAnd("host", SearchCriteria.Op.EQ, hostname);
203+
if (pid > 0) {
204+
sc.addAnd("pid", SearchCriteria.Op.EQ, Integer.valueOf(pid));
205+
}
206+
return listBy(sc);
207+
}
208+
209+
@Override
210+
public void removeLastOpenJobsOwned(String hostname, int pid) {
211+
if (hostname == null) {
212+
return;
213+
}
214+
215+
TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB);
216+
try {
217+
List<UsageJobVO> jobs = getLastOpenJobsOwned(hostname, pid);
218+
if (CollectionUtils.isNotEmpty(jobs)) {
219+
logger.info("Found {} opens job, to remove", jobs.size());
220+
for (UsageJobVO job : jobs) {
221+
logger.debug("Removing job - id: {}, pid: {}, job type: {}, scheduled: {}, heartbeat: {}",
222+
job.getId(), job.getPid(), job.getJobType(), job.getScheduled(), job.getHeartbeat());
223+
remove(job.getId());
224+
}
225+
}
226+
} finally {
227+
txn.close();
228+
}
229+
}
197230
}

usage/src/main/java/com/cloud/usage/UsageManagerImpl.java

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,9 @@ public boolean start() {
324324
logger.info("Starting Usage Manager");
325325
}
326326

327+
_usageJobDao.removeLastOpenJobsOwned(_hostname, 0);
328+
Runtime.getRuntime().addShutdownHook(new AbandonJob());
329+
327330
// use the configured exec time and aggregation duration for scheduling the job
328331
_scheduledFuture =
329332
_executor.scheduleAtFixedRate(this, _jobExecTime.getTimeInMillis() - System.currentTimeMillis(), _aggregationDuration * 60 * 1000, TimeUnit.MILLISECONDS);
@@ -336,7 +339,6 @@ public boolean start() {
336339
_sanity = _sanityExecutor.scheduleAtFixedRate(new SanityCheck(), 1, _sanityCheckInterval, TimeUnit.DAYS);
337340
}
338341

339-
Runtime.getRuntime().addShutdownHook(new AbandonJob());
340342
TransactionLegacy usageTxn = TransactionLegacy.open(TransactionLegacy.USAGE_DB);
341343
try {
342344
if (_heartbeatLock.lock(3)) { // 3 second timeout
@@ -2262,19 +2264,17 @@ protected void runInContext() {
22622264
// the aggregation range away from executing the next job
22632265
long now = System.currentTimeMillis();
22642266
long timeToJob = _jobExecTime.getTimeInMillis() - now;
2265-
long timeSinceJob = 0;
2267+
long timeSinceLastSuccessJob = 0;
22662268
long aggregationDurationMillis = _aggregationDuration * 60L * 1000L;
22672269
long lastSuccess = _usageJobDao.getLastJobSuccessDateMillis();
22682270
if (lastSuccess > 0) {
2269-
timeSinceJob = now - lastSuccess;
2271+
timeSinceLastSuccessJob = now - lastSuccess;
22702272
}
22712273

2272-
if ((timeSinceJob > 0) && (timeSinceJob > (aggregationDurationMillis - 100))) {
2274+
if ((timeSinceLastSuccessJob > 0) && (timeSinceLastSuccessJob > (aggregationDurationMillis - 100))) {
22732275
if (timeToJob > (aggregationDurationMillis / 2)) {
2274-
if (logger.isDebugEnabled()) {
2275-
logger.debug("it's been " + timeSinceJob + " ms since last usage job and " + timeToJob +
2276-
" ms until next job, scheduling an immediate job to catch up (aggregation duration is " + _aggregationDuration + " minutes)");
2277-
}
2276+
logger.debug("it's been {} ms since last usage job and {} ms until next job, scheduling an immediate job to catch up (aggregation duration is {} minutes)"
2277+
, timeSinceLastSuccessJob, timeToJob, _aggregationDuration);
22782278
scheduleParse();
22792279
}
22802280
}
@@ -2359,17 +2359,12 @@ protected void runInContext() {
23592359
}
23602360
}
23612361
}
2362+
23622363
private class AbandonJob extends Thread {
23632364
@Override
23642365
public void run() {
2365-
logger.info("exitting Usage Manager");
2366-
deleteOpenjob();
2367-
}
2368-
private void deleteOpenjob() {
2369-
UsageJobVO job = _usageJobDao.isOwner(_hostname, _pid);
2370-
if (job != null) {
2371-
_usageJobDao.remove(job.getId());
2372-
}
2366+
logger.info("exiting Usage Manager");
2367+
_usageJobDao.removeLastOpenJobsOwned(_hostname, _pid);
23732368
}
23742369
}
23752370
}

0 commit comments

Comments
 (0)