Skip to content

Commit d8230c9

Browse files
authored
Usage: Heartbeat should not schedule usage job when a job is already running (#12616)
1 parent e22f842 commit d8230c9

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.util.concurrent.Future;
3232
import java.util.concurrent.ScheduledExecutorService;
3333
import java.util.concurrent.TimeUnit;
34+
import java.util.concurrent.atomic.AtomicBoolean;
3435

3536
import com.cloud.network.Network;
3637
import com.cloud.usage.dao.UsageNetworksDao;
@@ -192,6 +193,7 @@ public class UsageManagerImpl extends ManagerBase implements UsageManager, Runna
192193
private final List<UsageVmDiskVO> usageVmDisks = new ArrayList<UsageVmDiskVO>();
193194

194195
private final ScheduledExecutorService _executor = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("Usage-Job"));
196+
private final AtomicBoolean isParsingJobRunning = new AtomicBoolean(false);
195197
private final ScheduledExecutorService _heartbeatExecutor = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("Usage-HB"));
196198
private final ScheduledExecutorService _sanityExecutor = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("Usage-Sanity"));
197199
private Future _scheduledFuture = null;
@@ -367,7 +369,12 @@ public void run() {
367369
(new ManagedContextRunnable() {
368370
@Override
369371
protected void runInContext() {
370-
runInContextInternal();
372+
isParsingJobRunning.set(true);
373+
try {
374+
runInContextInternal();
375+
} finally {
376+
isParsingJobRunning.set(false);
377+
}
371378
}
372379
}).run();
373380
}
@@ -2267,9 +2274,14 @@ protected void runInContext() {
22672274

22682275
if ((timeSinceLastSuccessJob > 0) && (timeSinceLastSuccessJob > (aggregationDurationMillis - 100))) {
22692276
if (timeToJob > (aggregationDurationMillis / 2)) {
2270-
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)"
2271-
, timeSinceLastSuccessJob, timeToJob, _aggregationDuration);
2272-
scheduleParse();
2277+
logger.debug("Heartbeat: it's been {} ms since last finished usage job and {} ms until next job (aggregation duration is {} minutes)",
2278+
timeSinceLastSuccessJob, timeToJob, _aggregationDuration);
2279+
if (isParsingJobRunning.get()) {
2280+
logger.debug("Heartbeat: A parsing job is already running");
2281+
} else {
2282+
logger.debug("Heartbeat: Scheduling an immediate job to catch up");
2283+
scheduleParse();
2284+
}
22732285
}
22742286
}
22752287

0 commit comments

Comments
 (0)