sijie commented on a change in pull request #1726: (WIP) Introduce lifecycle 
components for managing components in AutoRecovery
URL: https://github.com/apache/bookkeeper/pull/1726#discussion_r222105266
 
 

 ##########
 File path: 
bookkeeper-server/src/main/java/org/apache/bookkeeper/replication/AutoRecoveryMain.java
 ##########
 @@ -266,45 +302,93 @@ private static ServerConfiguration parseArgs(String[] 
args)
     }
 
     public static void main(String[] args) {
-        ServerConfiguration conf = null;
+        int retCode = doMain(args);
+        Runtime.getRuntime().exit(retCode);
+    }
+
+    static int doMain(String[] args) {
+
+        ServerConfiguration conf;
+
+        // 0. parse command line
         try {
             conf = parseArgs(args);
         } catch (IllegalArgumentException iae) {
-            LOG.error("Error parsing command line arguments : ", iae);
-            System.err.println(iae.getMessage());
-            printUsage();
-            System.exit(ExitCode.INVALID_CONF);
+            return ExitCode.INVALID_CONF;
         }
 
+        // 1. building the component stack:
+        LifecycleComponent server;
         try {
-            final AutoRecoveryMain autoRecoveryMain = new 
AutoRecoveryMain(conf);
-            autoRecoveryMain.start();
-            HttpServerLoader.loadHttpServer(conf);
-            final HttpServer httpServer = HttpServerLoader.get();
-            if (conf.isHttpServerEnabled() && httpServer != null) {
-                BKHttpServiceProvider serviceProvider = new 
BKHttpServiceProvider.Builder()
-                    .setAutoRecovery(autoRecoveryMain)
-                    .setServerConfiguration(conf)
-                    .build();
-                httpServer.initialize(serviceProvider);
-                httpServer.startServer(conf.getHttpServerPort());
-            }
-            Runtime.getRuntime().addShutdownHook(new Thread() {
-                @Override
-                public void run() {
-                    autoRecoveryMain.shutdown();
-                    if (httpServer != null && httpServer.isRunning()) {
-                        httpServer.stopServer();
-                    }
-                    LOG.info("Shutdown AutoRecoveryMain successfully");
-                }
-            });
-            LOG.info("Register shutdown hook successfully");
-            autoRecoveryMain.join();
-            System.exit(autoRecoveryMain.getExitCode());
+            server = buildAutoRecoveryServer(new BookieConfiguration(conf));
         } catch (Exception e) {
-            LOG.error("Exception running AutoRecoveryMain : ", e);
-            System.exit(ExitCode.SERVER_EXCEPTION);
+            LOG.error("Failed to build AutoRecovery Server", e);
+            return ExitCode.SERVER_EXCEPTION;
+        }
+
+        // 2. start the server
+        try {
+            ComponentStarter.startComponent(server).get();
+        } catch (InterruptedException ie) {
+            Thread.currentThread().interrupt();
+            // the server is interrupted
+            LOG.info("AutoRecovery server is interrupted. Exiting ...");
+        } catch (ExecutionException ee) {
+            LOG.error("Error in bookie shutdown", ee.getCause());
+            return ExitCode.SERVER_EXCEPTION;
+        }
+        return ExitCode.OK;
+    }
+
+    static LifecycleComponentStack buildAutoRecoveryServer(BookieConfiguration 
conf) throws Exception {
+        LifecycleComponentStack.Builder serverBuilder = 
LifecycleComponentStack.newBuilder()
+                .withName("autorecovery-server");
+
+        // 1. build stats provider
+        StatsProviderService statsProviderService = new 
StatsProviderService(conf);
+        StatsLogger rootStatsLogger = 
statsProviderService.getStatsProvider().getStatsLogger("");
+
+        serverBuilder.addComponent(statsProviderService);
+        LOG.info("Load lifecycle component : {}", 
StatsProviderService.class.getName());
+
+        // 2. build AutoRecovery server
+        AutoRecoveryService autoRecoveryService = new 
AutoRecoveryService(conf, rootStatsLogger);
+
+        serverBuilder.addComponent(autoRecoveryService);
+        LOG.info("Load lifecycle component : {}", 
AutoRecoveryService.class.getName());
+
+        // 4. build http service
+        if (conf.getServerConf().isHttpServerEnabled()) {
+            BKHttpServiceProvider provider = new 
BKHttpServiceProvider.Builder()
+                    
.setAutoRecovery(autoRecoveryService.getAutoRecoveryServer())
+                    .setServerConfiguration(conf.getServerConf())
+                    
.setStatsProvider(statsProviderService.getStatsProvider()).build();
+            HttpService httpService = new HttpService(provider, conf, 
rootStatsLogger);
+
+            serverBuilder.addComponent(httpService);
+            LOG.info("Load lifecycle component : {}", 
HttpService.class.getName());
         }
+
+        // 5. build extra services
+        String[] extraComponents = 
conf.getServerConf().getExtraServerComponents();
 
 Review comment:
   that probably needs to be dropped. since that argument is supposed to be 
used on bookies only.

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to