You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by he...@apache.org on 2016/01/21 02:23:31 UTC

[4/6] incubator-brooklyn git commit: background the dev-mode web server shutdown, and Entities.destroyAll is more graceful on concurrent shutdown

background the dev-mode web server shutdown,
and Entities.destroyAll is more graceful on concurrent shutdown


Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/db44cd77
Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/db44cd77
Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/db44cd77

Branch: refs/heads/master
Commit: db44cd77670672efcb6c3b44f81a716b76b072af
Parents: 2354c48
Author: Alex Heneveld <al...@cloudsoftcorp.com>
Authored: Wed Jan 20 18:26:56 2016 +0000
Committer: Alex Heneveld <al...@cloudsoftcorp.com>
Committed: Wed Jan 20 21:07:13 2016 +0000

----------------------------------------------------------------------
 .../apache/brooklyn/core/entity/Entities.java   | 13 ++++---
 .../brooklyn/rest/resources/ServerResource.java |  2 +-
 .../util/ServerStoppingShutdownHandler.java     | 38 ++++++++++++--------
 3 files changed, 33 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/db44cd77/brooklyn-server/core/src/main/java/org/apache/brooklyn/core/entity/Entities.java
----------------------------------------------------------------------
diff --git a/brooklyn-server/core/src/main/java/org/apache/brooklyn/core/entity/Entities.java b/brooklyn-server/core/src/main/java/org/apache/brooklyn/core/entity/Entities.java
index 9297388..9c8ebc8 100644
--- a/brooklyn-server/core/src/main/java/org/apache/brooklyn/core/entity/Entities.java
+++ b/brooklyn-server/core/src/main/java/org/apache/brooklyn/core/entity/Entities.java
@@ -805,10 +805,15 @@ public class Entities {
                 ((ManagementContextInternal)mgmt).terminate();
             }
             if (error.get() != null) throw Exceptions.propagate(error.get());
-        } catch (InterruptedException e) {
-            throw Exceptions.propagate(e);
-        } catch (ExecutionException e) {
-            throw Exceptions.propagate(e);
+        } catch (Exception e) {
+            if (!mgmt.isRunning()) {
+                // we've checked this above so it would only happen if a different thread stopped it;
+                // this does happen sometimes e.g. in CliTest where the server shutdown occurs concurrently
+                log.debug("Destroying apps gave an error, but mgmt context was concurrently stopped so not really a problem; swallowing (unless fatal): "+e);
+                Exceptions.propagateIfFatal(e);
+            } else {
+                throw Exceptions.propagate(e);
+            }
         } finally {
             executor.shutdownNow();
         }

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/db44cd77/brooklyn-server/rest/rest-server/src/main/java/org/apache/brooklyn/rest/resources/ServerResource.java
----------------------------------------------------------------------
diff --git a/brooklyn-server/rest/rest-server/src/main/java/org/apache/brooklyn/rest/resources/ServerResource.java b/brooklyn-server/rest/rest-server/src/main/java/org/apache/brooklyn/rest/resources/ServerResource.java
index e624d89..c029bd3 100644
--- a/brooklyn-server/rest/rest-server/src/main/java/org/apache/brooklyn/rest/resources/ServerResource.java
+++ b/brooklyn-server/rest/rest-server/src/main/java/org/apache/brooklyn/rest/resources/ServerResource.java
@@ -218,7 +218,7 @@ public class ServerResource extends AbstractBrooklynRestResource implements Serv
                         if (shutdownHandler != null) {
                             shutdownHandler.onShutdownRequest();
                         } else {
-                            // should always be set as it is required by jersey injection?
+                            // should normally be set, as @Context is required by jersey injection
                             log.warn("ShutdownHandler not set, exiting process");
                             System.exit(0);
                         }

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/db44cd77/brooklyn-server/rest/rest-server/src/test/java/org/apache/brooklyn/rest/util/ServerStoppingShutdownHandler.java
----------------------------------------------------------------------
diff --git a/brooklyn-server/rest/rest-server/src/test/java/org/apache/brooklyn/rest/util/ServerStoppingShutdownHandler.java b/brooklyn-server/rest/rest-server/src/test/java/org/apache/brooklyn/rest/util/ServerStoppingShutdownHandler.java
index 7244378..78dde57 100644
--- a/brooklyn-server/rest/rest-server/src/test/java/org/apache/brooklyn/rest/util/ServerStoppingShutdownHandler.java
+++ b/brooklyn-server/rest/rest-server/src/test/java/org/apache/brooklyn/rest/util/ServerStoppingShutdownHandler.java
@@ -20,6 +20,8 @@ package org.apache.brooklyn.rest.util;
 
 import org.apache.brooklyn.api.mgmt.ManagementContext;
 import org.apache.brooklyn.core.mgmt.internal.ManagementContextInternal;
+import org.apache.brooklyn.util.time.Duration;
+import org.apache.brooklyn.util.time.Time;
 import org.eclipse.jetty.server.Server;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -40,23 +42,29 @@ public class ServerStoppingShutdownHandler implements ShutdownHandler {
 
     @Override
     public void onShutdownRequest() {
-        log.info("Shutting down (when running in rest-api dev mode)...");
+        log.info("Shutting down server (when running in rest-api dev mode, using background thread)");
 
-        // essentially same as BrooklynLauncher.terminate() but cut down as this is only used in dev mode
-        
-        if (server!=null) {
-            try {
-                server.stop();
-                server.join();
-            } catch (Exception e) {
-                log.debug("Stopping server gave an error (not usually a concern): "+e);
-                /* NPE may be thrown e.g. if threadpool not started */
-            }
-        }
+        // essentially same as BrooklynLauncher.terminate() but cut down ...
+        // NB: this is only used in dev mode use of BrooklynJavascriptGuiLauncher
+        new Thread(new Runnable() {
+            public void run() {
+                Time.sleep(Duration.millis(250));
+                log.debug("Shutting down server in background thread, closing "+server+" and "+mgmt);
+                if (server!=null) {
+                    try {
+                        server.stop();
+                        server.join();
+                    } catch (Exception e) {
+                        log.debug("Stopping server gave an error (not usually a concern): "+e);
+                        /* NPE may be thrown e.g. if threadpool not started */
+                    }
+                }
 
-        if (mgmt instanceof ManagementContextInternal) {
-            ((ManagementContextInternal)mgmt).terminate();
-        }
+                if (mgmt instanceof ManagementContextInternal) {
+                    ((ManagementContextInternal)mgmt).terminate();
+                }
+            }
+        }).start();
     }
 
     /** Expect this to be injeted; typically it is not known when this is created, but we need it to trigger shutdown. */