You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by GitBox <gi...@apache.org> on 2022/09/27 13:10:58 UTC

[GitHub] [cloudstack] DaanHoogland commented on a diff in pull request #6755: Feature: Safely shutdown cloudstack

DaanHoogland commented on code in PR #6755:
URL: https://github.com/apache/cloudstack/pull/6755#discussion_r981207221


##########
plugins/shutdown/src/main/java/org/apache/cloudstack/api/command/PrepareForShutdownCmd.java:
##########
@@ -0,0 +1,45 @@
+package org.apache.cloudstack.api.command;

Review Comment:
   license



##########
framework/jobs/src/main/java/org/apache/cloudstack/framework/jobs/impl/AsyncJobManagerImpl.java:
##########
@@ -116,6 +116,8 @@ public class AsyncJobManagerImpl extends ManagerBase implements AsyncJobManager,
     private static final int HEARTBEAT_INTERVAL = 2000;
     private static final int GC_INTERVAL = 10000;                // 10 seconds
 
+    private boolean allowAsyncJobs = true ;

Review Comment:
   seems like this should be called `shutdownTriggered`, or else the messages of the exceptions are to specific and we donĀ“t know the reason the async jobs are disallowed is a shutdown instead of some other maintenance task.



##########
plugins/shutdown/src/test/java/org/apache/cloudstack/shutdown/ShutdownManagerImplTest.java:
##########
@@ -0,0 +1,98 @@
+package org.apache.cloudstack.shutdown;

Review Comment:
   license



##########
plugins/shutdown/src/main/java/org/apache/cloudstack/shutdown/ShutdownManagerImpl.java:
##########
@@ -0,0 +1,118 @@
+package org.apache.cloudstack.shutdown;

Review Comment:
   license



##########
plugins/shutdown/src/main/java/org/apache/cloudstack/api/command/TriggerShutdownCmd.java:
##########
@@ -0,0 +1,58 @@
+package org.apache.cloudstack.api.command;

Review Comment:
   license



##########
plugins/shutdown/src/main/java/org/apache/cloudstack/shutdown/ShutdownManagerImpl.java:
##########
@@ -0,0 +1,118 @@
+package org.apache.cloudstack.shutdown;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.inject.Inject;
+
+import org.apache.cloudstack.api.command.CancelShutdownCmd;
+import org.apache.cloudstack.api.command.PrepareForShutdownCmd;
+import org.apache.cloudstack.api.command.ReadyForShutdownCmd;
+import org.apache.cloudstack.api.command.TriggerShutdownCmd;
+import org.apache.cloudstack.api.response.ReadyForShutdownResponse;
+import org.apache.cloudstack.framework.jobs.AsyncJobManager;
+import org.apache.log4j.Logger;
+
+import com.cloud.utils.component.ManagerBase;
+import com.cloud.utils.component.PluggableService;
+import com.cloud.utils.exception.CloudRuntimeException;
+
+public class ShutdownManagerImpl extends ManagerBase implements ShutdownManager, PluggableService{
+
+    private static Logger s_logger = Logger.getLogger(ShutdownManagerImpl.class);

Review Comment:
   ```suggestion
       private static Logger LOG = Logger.getLogger(ShutdownManagerImpl.class);
   ```



##########
plugins/shutdown/src/main/java/org/apache/cloudstack/api/command/CancelShutdownCmd.java:
##########
@@ -0,0 +1,47 @@
+package org.apache.cloudstack.api.command;

Review Comment:
   license



##########
plugins/shutdown/src/main/java/org/apache/cloudstack/shutdown/ShutdownManager.java:
##########
@@ -0,0 +1,17 @@
+package org.apache.cloudstack.shutdown;

Review Comment:
   license



##########
plugins/shutdown/src/main/java/org/apache/cloudstack/shutdown/ShutdownManagerImpl.java:
##########
@@ -0,0 +1,118 @@
+package org.apache.cloudstack.shutdown;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.inject.Inject;
+
+import org.apache.cloudstack.api.command.CancelShutdownCmd;
+import org.apache.cloudstack.api.command.PrepareForShutdownCmd;
+import org.apache.cloudstack.api.command.ReadyForShutdownCmd;
+import org.apache.cloudstack.api.command.TriggerShutdownCmd;
+import org.apache.cloudstack.api.response.ReadyForShutdownResponse;
+import org.apache.cloudstack.framework.jobs.AsyncJobManager;
+import org.apache.log4j.Logger;
+
+import com.cloud.utils.component.ManagerBase;
+import com.cloud.utils.component.PluggableService;
+import com.cloud.utils.exception.CloudRuntimeException;
+
+public class ShutdownManagerImpl extends ManagerBase implements ShutdownManager, PluggableService{
+
+    private static Logger s_logger = Logger.getLogger(ShutdownManagerImpl.class);
+
+    @Inject
+    private AsyncJobManager _jobMgr;
+
+    private boolean shutdownTriggered = false;
+
+    @Override
+    public boolean isShutdownTriggered() {
+        return shutdownTriggered;
+    }
+
+    protected ShutdownManagerImpl() {
+        super();
+    }
+
+    @Override
+    public long countPendingJobs() {
+        return _jobMgr.countPendingNonPseudoJobs();
+    }
+
+    @Override
+    public ReadyForShutdownResponse readyForShutdown() {
+        long pendingJobCount = countPendingJobs();
+        return new ReadyForShutdownResponse(!_jobMgr.isAllowAsyncJobs(), pendingJobCount == 0, pendingJobCount);
+    }
+
+    @Override
+    public ReadyForShutdownResponse prepareForShutdown() {
+        _jobMgr.disableAllowAsyncJobs();
+       return readyForShutdown();
+    }
+
+    @Override
+    public ReadyForShutdownResponse triggerShutdown(Boolean forced) {
+        this.shutdownTriggered = true;
+        if (forced != null && forced) {
+            System.exit(0);
+        }
+        _jobMgr.disableAllowAsyncJobs();
+        Thread shutdownTask = new ShutdownTask(this);
+        shutdownTask.start();
+        return readyForShutdown();
+    }
+
+    @Override
+    public ReadyForShutdownResponse cancelShutdown() {
+        this.shutdownTriggered = false;
+        if (_jobMgr.isAllowAsyncJobs()) {
+            throw new CloudRuntimeException("A shutdown has not been triggered");
+        }
+
+        _jobMgr.enableAllowAsyncJobs();
+       return readyForShutdown();
+    }
+
+    @Override
+    public List<Class<?>> getCommands() {
+        final List<Class<?>> cmdList = new ArrayList<>();
+        cmdList.add(CancelShutdownCmd.class);
+        cmdList.add(PrepareForShutdownCmd.class);
+        cmdList.add(ReadyForShutdownCmd.class);
+        cmdList.add(TriggerShutdownCmd.class);
+        return cmdList;
+    }
+
+    private final class ShutdownTask extends Thread {
+
+        private ShutdownManager shutdownManager;
+
+        private ShutdownTask(final ShutdownManager shutdownManager) {
+            this.shutdownManager = shutdownManager;
+        }
+
+        @Override
+        public void run() {
+            while(true) {
+                try {
+                    String msg = String.format("Checking for triggered shutdown... shutdownTriggered [%b] AllowAsyncJobs [%b] PendingJobCount [%d]",
+                        shutdownManager.isShutdownTriggered(), _jobMgr.isAllowAsyncJobs(), shutdownManager.countPendingJobs());
+                    s_logger.info(msg);
+                    if (!shutdownManager.isShutdownTriggered()) {
+                        return;
+                    }
+                    if (shutdownManager.countPendingJobs() == 0) {
+                        s_logger.info("Shutting down now");
+                        System.exit(0);

Review Comment:
   are other threads allowed to shutdown as well (by maybe setting a flag on `ManagedContextRunnable` and adding a check for it there?  (<== genuine question)



##########
plugins/shutdown/pom.xml:
##########
@@ -0,0 +1,25 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0"

Review Comment:
   license?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudstack.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org