This is an automated email from the ASF dual-hosted git repository.

sijie pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/bookkeeper.git


The following commit(s) were added to refs/heads/master by this push:
     new 13dc923  Propogate exception when bookie shutdown fails
13dc923 is described below

commit 13dc9230e79c0ecbc30b8b804e506b978c3f9cb1
Author: Ivan Kelly <[email protected]>
AuthorDate: Tue Feb 20 10:42:21 2018 -0800

    Propogate exception when bookie shutdown fails
    
    When a bookie fails to shutdown cleanly, we should propogate the
    exception that caused the problem to the main thread.
    
    Author: Ivan Kelly <[email protected]>
    
    Reviewers: Enrico Olivelli <[email protected]>, Sijie Guo 
<[email protected]>
    
    This closes #1186 from ivankelly/prop-ex
---
 .../bookkeeper/common/component/ComponentStarter.java | 19 +++++++++++--------
 .../common/component/TestComponentStarter.java        | 11 +++++------
 .../main/java/org/apache/bookkeeper/server/Main.java  |  9 +++++----
 3 files changed, 21 insertions(+), 18 deletions(-)

diff --git 
a/bookkeeper-common/src/main/java/org/apache/bookkeeper/common/component/ComponentStarter.java
 
b/bookkeeper-common/src/main/java/org/apache/bookkeeper/common/component/ComponentStarter.java
index 8ee6f77..fa9e9b1 100644
--- 
a/bookkeeper-common/src/main/java/org/apache/bookkeeper/common/component/ComponentStarter.java
+++ 
b/bookkeeper-common/src/main/java/org/apache/bookkeeper/common/component/ComponentStarter.java
@@ -18,8 +18,9 @@
 
 package org.apache.bookkeeper.common.component;
 
-import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.CompletableFuture;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.bookkeeper.common.concurrent.FutureUtils;
 
 /**
  * Utils to start components.
@@ -30,24 +31,25 @@ public class ComponentStarter {
     static class ComponentShutdownHook implements Runnable {
 
         private final LifecycleComponent component;
-        private final CountDownLatch aliveLatch;
+        private final CompletableFuture<Void> future;
 
         ComponentShutdownHook(LifecycleComponent component,
-                              CountDownLatch aliveLatch) {
+                              CompletableFuture<Void> future) {
             this.component = component;
-            this.aliveLatch = aliveLatch;
+            this.future = future;
         }
 
         @Override
         public void run() {
-            aliveLatch.countDown();
             log.info("Closing component {} in shutdown hook.", 
component.getName());
             try {
                 component.close();
                 log.info("Closed component {} in shutdown hook successfully. 
Exiting.", component.getName());
+                FutureUtils.complete(future, null);
             } catch (Exception e) {
                 log.error("Failed to close component {} in shutdown hook 
gracefully, Exiting anyway",
                     component.getName(), e);
+                future.completeExceptionally(e);
             }
         }
 
@@ -58,14 +60,15 @@ public class ComponentStarter {
      *
      * @param component component to start.
      */
-    public static void startComponent(LifecycleComponent component,
-                                      CountDownLatch aliveLatch) {
+    public static CompletableFuture<Void> startComponent(LifecycleComponent 
component) {
+        CompletableFuture<Void> future = new CompletableFuture<>();
         Runtime.getRuntime().addShutdownHook(new Thread(
-            new ComponentShutdownHook(component, aliveLatch), 
"component-shutdown-thread"));
+            new ComponentShutdownHook(component, future), 
"component-shutdown-thread"));
 
         log.info("Starting component {}.", component.getName());
         component.start();
         log.info("Started component {}.", component.getName());
+        return future;
     }
 
 }
diff --git 
a/bookkeeper-common/src/test/java/org/apache/bookkeeper/common/component/TestComponentStarter.java
 
b/bookkeeper-common/src/test/java/org/apache/bookkeeper/common/component/TestComponentStarter.java
index eadbbd9..e99c301 100644
--- 
a/bookkeeper-common/src/test/java/org/apache/bookkeeper/common/component/TestComponentStarter.java
+++ 
b/bookkeeper-common/src/test/java/org/apache/bookkeeper/common/component/TestComponentStarter.java
@@ -22,7 +22,7 @@ import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
-import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.CompletableFuture;
 import 
org.apache.bookkeeper.common.component.ComponentStarter.ComponentShutdownHook;
 import org.junit.Test;
 
@@ -34,21 +34,20 @@ public class TestComponentStarter {
   @Test
   public void testStartComponent() {
     LifecycleComponent component = mock(LifecycleComponent.class);
-    CountDownLatch latch = new CountDownLatch(1);
     when(component.getName()).thenReturn("test-start-component");
-    ComponentStarter.startComponent(component, latch);
+    ComponentStarter.startComponent(component);
     verify(component).start();
   }
 
   @Test
   public void testComponentShutdownHook() throws Exception {
     LifecycleComponent component = mock(LifecycleComponent.class);
-    CountDownLatch latch = new CountDownLatch(1);
     when(component.getName()).thenReturn("test-shutdown-hook");
-    ComponentShutdownHook shutdownHook = new ComponentShutdownHook(component, 
latch);
+    CompletableFuture<Void> future = new CompletableFuture<>();
+    ComponentShutdownHook shutdownHook = new ComponentShutdownHook(component, 
future);
     shutdownHook.run();
     verify(component).close();
-    latch.await();
+    future.get();
   }
 
 }
diff --git 
a/bookkeeper-server/src/main/java/org/apache/bookkeeper/server/Main.java 
b/bookkeeper-server/src/main/java/org/apache/bookkeeper/server/Main.java
index 5ee8bb5..2fa40fa 100644
--- a/bookkeeper-server/src/main/java/org/apache/bookkeeper/server/Main.java
+++ b/bookkeeper-server/src/main/java/org/apache/bookkeeper/server/Main.java
@@ -25,7 +25,7 @@ import java.io.File;
 import java.net.MalformedURLException;
 import java.util.Arrays;
 import java.util.List;
-import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutionException;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.bookkeeper.bookie.ExitCode;
 import org.apache.bookkeeper.common.component.ComponentStarter;
@@ -211,13 +211,14 @@ public class Main {
         }
 
         // 2. start the server
-        CountDownLatch aliveLatch = new CountDownLatch(1);
-        ComponentStarter.startComponent(server, aliveLatch);
         try {
-            aliveLatch.await();
+            ComponentStarter.startComponent(server).get();
         } catch (InterruptedException ie) {
             // the server is interrupted
             log.info("Bookie server is interrupted. Exiting ...");
+        } catch (ExecutionException ee) {
+            log.error("Error in bookie shutdown", ee.getCause());
+            return ExitCode.SERVER_EXCEPTION;
         }
         return ExitCode.OK;
     }

-- 
To stop receiving notification emails like this one, please contact
[email protected].

Reply via email to