Skip to content

Commit 58066b3

Browse files
Updated pending jobs check timer task with ScheduledExecutorService
1 parent 2b7d18b commit 58066b3

File tree

2 files changed

+25
-25
lines changed

2 files changed

+25
-25
lines changed

plugins/maintenance/src/main/java/org/apache/cloudstack/maintenance/ManagementServerMaintenanceManager.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ public interface ManagementServerMaintenanceManager {
8686

8787
void cancelPreparingForMaintenance(ManagementServerHostVO msHost);
8888

89+
void cancelWaitForPendingJobs();
90+
8991
// Returns whether the any of the ms can be shut down and if a shutdown has been triggered on any running ms
9092
ManagementServerMaintenanceResponse readyForShutdown(ReadyForShutdownCmd cmd);
9193

plugins/maintenance/src/main/java/org/apache/cloudstack/maintenance/ManagementServerMaintenanceManagerImpl.java

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@
2020
import java.util.ArrayList;
2121
import java.util.Arrays;
2222
import java.util.List;
23-
import java.util.Timer;
24-
import java.util.TimerTask;
23+
import java.util.concurrent.Executors;
24+
import java.util.concurrent.ScheduledExecutorService;
25+
import java.util.concurrent.TimeUnit;
2526

2627
import javax.inject.Inject;
2728

@@ -37,6 +38,7 @@
3738
import org.apache.cloudstack.framework.config.ConfigKey;
3839
import org.apache.cloudstack.framework.config.Configurable;
3940
import org.apache.cloudstack.framework.jobs.AsyncJobManager;
41+
import org.apache.cloudstack.managed.context.ManagedContextRunnable;
4042
import org.apache.cloudstack.management.ManagementServerHost.State;
4143
import org.apache.cloudstack.maintenance.command.CancelMaintenanceManagementServerHostCommand;
4244
import org.apache.cloudstack.maintenance.command.CancelShutdownManagementServerHostCommand;
@@ -56,6 +58,7 @@
5658
import com.cloud.utils.StringUtils;
5759
import com.cloud.utils.component.ManagerBase;
5860
import com.cloud.utils.component.PluggableService;
61+
import com.cloud.utils.concurrency.NamedThreadFactory;
5962
import com.cloud.utils.exception.CloudRuntimeException;
6063
import com.google.gson.Gson;
6164

@@ -84,8 +87,7 @@ public class ManagementServerMaintenanceManagerImpl extends ManagerBase implemen
8487
private long maintenanceStartTime = 0;
8588
private String lbAlgorithm;
8689

87-
private Timer timer;
88-
private TimerTask pendingJobsTask;
90+
private ScheduledExecutorService pendingJobsCheckTask;
8991

9092
protected ManagementServerMaintenanceManagerImpl() {
9193
super();
@@ -262,21 +264,17 @@ public void cancelMaintenance() {
262264

263265
private void waitForPendingJobs() {
264266
cancelWaitForPendingJobs();
265-
this.timer = new Timer();
266-
this.pendingJobsTask = new CheckPendingJobsTask(this);
267-
long pendingJobsCheckDelayInMs = 1000L; // 1 sec
268-
long pendingJobsCheckPeriodInMs = 3L * 1000; // every 3 secs, check more frequently for pending jobs
269-
timer.scheduleAtFixedRate(pendingJobsTask, pendingJobsCheckDelayInMs, pendingJobsCheckPeriodInMs);
267+
pendingJobsCheckTask = Executors.newScheduledThreadPool(1, new NamedThreadFactory("PendingJobsCheck"));
268+
long pendingJobsCheckDelayInSecs = 1L; // 1 sec
269+
long pendingJobsCheckPeriodInSecs = 3L; // every 3 secs, check more frequently for pending jobs
270+
pendingJobsCheckTask.scheduleAtFixedRate(new CheckPendingJobsTask(this), pendingJobsCheckDelayInSecs, pendingJobsCheckPeriodInSecs, TimeUnit.SECONDS);
270271
}
271272

272-
private void cancelWaitForPendingJobs() {
273-
if (this.pendingJobsTask != null) {
274-
this.pendingJobsTask.cancel();
275-
this.pendingJobsTask = null;
276-
}
277-
if (this.timer != null) {
278-
this.timer.cancel();
279-
this.timer = null;
273+
@Override
274+
public void cancelWaitForPendingJobs() {
275+
if (pendingJobsCheckTask != null) {
276+
pendingJobsCheckTask.shutdown();
277+
pendingJobsCheckTask = null;
280278
}
281279
}
282280

@@ -497,7 +495,7 @@ public ConfigKey<?>[] getConfigKeys() {
497495
};
498496
}
499497

500-
private final class CheckPendingJobsTask extends TimerTask {
498+
private final class CheckPendingJobsTask extends ManagedContextRunnable {
501499

502500
private ManagementServerMaintenanceManager managementServerMaintenanceManager;
503501
private boolean agentsTransferTriggered = false;
@@ -507,19 +505,19 @@ public CheckPendingJobsTask(ManagementServerMaintenanceManager managementServerM
507505
}
508506

509507
@Override
510-
public void run() {
508+
protected void runInContext() {
511509
try {
512510
// If the maintenance or shutdown has been cancelled
513511
if (!(managementServerMaintenanceManager.isPreparingForMaintenance() || managementServerMaintenanceManager.isPreparingForShutdown())) {
514512
logger.info("Maintenance/Shutdown cancelled, terminating the pending jobs check timer task");
515-
this.cancel();
513+
managementServerMaintenanceManager.cancelWaitForPendingJobs();
516514
return;
517515
}
518516

519517
if (managementServerMaintenanceManager.isPreparingForMaintenance() && isMaintenanceWindowExpired()) {
520518
logger.debug("Maintenance window timeout, terminating the pending jobs check timer task");
521519
managementServerMaintenanceManager.cancelPreparingForMaintenance(null);
522-
this.cancel();
520+
managementServerMaintenanceManager.cancelWaitForPendingJobs();
523521
return;
524522
}
525523

@@ -546,7 +544,7 @@ public void run() {
546544
logger.info("MS is in Maintenance Mode");
547545
msHostDao.updateState(msHost.getId(), State.Maintenance);
548546
managementServerMaintenanceManager.onMaintenance();
549-
this.cancel();
547+
managementServerMaintenanceManager.cancelWaitForPendingJobs();
550548
return;
551549
}
552550

@@ -561,21 +559,21 @@ public void run() {
561559
if (!agentsMigrated) {
562560
logger.warn(String.format("Unable to prepare for maintenance, cannot migrate indirect agents on this management server node %d (id: %s)", ManagementServerNode.getManagementServerId(), msHost.getUuid()));
563561
managementServerMaintenanceManager.cancelPreparingForMaintenance(msHost);
564-
this.cancel();
562+
managementServerMaintenanceManager.cancelWaitForPendingJobs();
565563
return;
566564
}
567565

568566
if(!agentMgr.transferDirectAgentsFromMS(msHost.getUuid(), ManagementServerNode.getManagementServerId(), remainingMaintenanceWindowInMs())) {
569567
logger.warn(String.format("Unable to prepare for maintenance, cannot transfer direct agents on this management server node %d (id: %s)", ManagementServerNode.getManagementServerId(), msHost.getUuid()));
570568
managementServerMaintenanceManager.cancelPreparingForMaintenance(msHost);
571-
this.cancel();
569+
managementServerMaintenanceManager.cancelWaitForPendingJobs();
572570
return;
573571
}
574572
} else if (managementServerMaintenanceManager.isPreparingForShutdown()) {
575573
logger.info("MS is Ready To Shutdown");
576574
ManagementServerHostVO msHost = msHostDao.findByMsid(ManagementServerNode.getManagementServerId());
577575
msHostDao.updateState(msHost.getId(), State.ReadyToShutDown);
578-
this.cancel();
576+
managementServerMaintenanceManager.cancelWaitForPendingJobs();
579577
return;
580578
}
581579
} catch (final Exception e) {

0 commit comments

Comments
 (0)