sijie closed pull request #1186: Propogate exception when bookie shutdown fails
URL: https://github.com/apache/bookkeeper/pull/1186
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

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 8ee6f77ff..fa9e9b1d4 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 @@
     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 void run() {
      *
      * @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 eadbbd945..e99c30124 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.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 @@
   @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 5ee8bb549..2fa40faef 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.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 @@ static int doMain(String[] args) {
         }
 
         // 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;
     }


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to