You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by pe...@apache.org on 2020/11/10 07:02:35 UTC

[pulsar] 02/04: [Pulsar Proxy] Add error log for pulsar proxy starter (#8451)

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

penghui pushed a commit to branch branch-2.6
in repository https://gitbox.apache.org/repos/asf/pulsar.git

commit bcb97d16adcca76c452e622e265b454b03213bbb
Author: ran <ga...@126.com>
AuthorDate: Thu Nov 5 14:24:29 2020 +0800

    [Pulsar Proxy] Add error log for pulsar proxy starter (#8451)
    
    ### Motivation
    
    Currently, the proxy service starter will throw all exceptions to the main method directly, it's hard to check the error log if there is an exception when proxy service start.
    
    ### Modifications
    
    Add try-catch for proxy start method.
    
    (cherry picked from commit 442a54704e4586f604050727cfc28ef17c3f7e02)
---
 .../pulsar/proxy/server/ProxyServiceStarter.java   | 160 +++++++++++----------
 1 file changed, 84 insertions(+), 76 deletions(-)

diff --git a/pulsar-proxy/src/main/java/org/apache/pulsar/proxy/server/ProxyServiceStarter.java b/pulsar-proxy/src/main/java/org/apache/pulsar/proxy/server/ProxyServiceStarter.java
index 695bd0c..3a30b47 100644
--- a/pulsar-proxy/src/main/java/org/apache/pulsar/proxy/server/ProxyServiceStarter.java
+++ b/pulsar-proxy/src/main/java/org/apache/pulsar/proxy/server/ProxyServiceStarter.java
@@ -25,6 +25,7 @@ import static org.apache.pulsar.common.stats.JvmMetrics.getJvmDirectMemoryUsed;
 import static org.slf4j.bridge.SLF4JBridgeHandler.install;
 import static org.slf4j.bridge.SLF4JBridgeHandler.removeHandlersForRootLogger;
 
+import org.apache.pulsar.broker.PulsarServerException;
 import org.apache.pulsar.broker.authentication.AuthenticationService;
 import org.apache.pulsar.common.configuration.PulsarConfigurationLoader;
 import org.apache.pulsar.proxy.server.plugin.servlet.ProxyAdditionalServletWithClassLoader;
@@ -76,97 +77,104 @@ public class ProxyServiceStarter {
     private boolean help = false;
 
     public ProxyServiceStarter(String[] args) throws Exception {
-        // setup handlers
-        removeHandlersForRootLogger();
-        install();
-
-        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss,SSS");
-        Thread.setDefaultUncaughtExceptionHandler((thread, exception) -> {
-            System.out.println(String.format("%s [%s] error Uncaught exception in thread %s: %s", dateFormat.format(new Date()), thread.getContextClassLoader(), thread.getName(), exception.getMessage()));
-        });
-
-        JCommander jcommander = new JCommander();
         try {
-            jcommander.addObject(this);
-            jcommander.parse(args);
-            if (help || isBlank(configFile)) {
-                jcommander.usage();
-                return;
-            }
-        } catch (Exception e) {
-            jcommander.usage();
-            System.exit(-1);
-        }
-
-        // load config file
-        final ProxyConfiguration config = PulsarConfigurationLoader.create(configFile, ProxyConfiguration.class);
-
-        if (!isBlank(zookeeperServers)) {
-            // Use zookeeperServers from command line
-            config.setZookeeperServers(zookeeperServers);
-        }
-
-        if (!isBlank(globalZookeeperServers)) {
-            // Use globalZookeeperServers from command line
-            config.setConfigurationStoreServers(globalZookeeperServers);
-        }
-        if (!isBlank(configurationStoreServers)) {
-            // Use configurationStoreServers from command line
-            config.setConfigurationStoreServers(configurationStoreServers);
-        }
-
-        if ((isBlank(config.getBrokerServiceURL()) && isBlank(config.getBrokerServiceURLTLS()))
-                || config.isAuthorizationEnabled()) {
-            checkArgument(!isEmpty(config.getZookeeperServers()), "zookeeperServers must be provided");
-            checkArgument(!isEmpty(config.getConfigurationStoreServers()),
-                    "configurationStoreServers must be provided");
-        }
 
-        if ((!config.isTlsEnabledWithBroker() && isBlank(config.getBrokerWebServiceURL()))
-                || (config.isTlsEnabledWithBroker() && isBlank(config.getBrokerWebServiceURLTLS()))) {
-            checkArgument(!isEmpty(config.getZookeeperServers()), "zookeeperServers must be provided");
-        }
+            // setup handlers
+            removeHandlersForRootLogger();
+            install();
 
-        AuthenticationService authenticationService = new AuthenticationService(
-                PulsarConfigurationLoader.convertFrom(config));
-        // create proxy service
-        ProxyService proxyService = new ProxyService(config, authenticationService);
-        // create a web-service
-        final WebServer server = new WebServer(config, authenticationService);
+            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss,SSS");
+            Thread.setDefaultUncaughtExceptionHandler((thread, exception) -> {
+                System.out.println(String.format("%s [%s] error Uncaught exception in thread %s: %s", dateFormat.format(new Date()), thread.getContextClassLoader(), thread.getName(), exception.getMessage()));
+            });
 
-        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
+            JCommander jcommander = new JCommander();
             try {
-                proxyService.close();
-                server.stop();
+                jcommander.addObject(this);
+                jcommander.parse(args);
+                if (help || isBlank(configFile)) {
+                    jcommander.usage();
+                    return;
+                }
             } catch (Exception e) {
-                log.warn("server couldn't stop gracefully {}", e.getMessage(), e);
+                jcommander.usage();
+                System.exit(-1);
             }
-        }));
 
-        proxyService.start();
+            // load config file
+            final ProxyConfiguration config = PulsarConfigurationLoader.create(configFile, ProxyConfiguration.class);
 
-        // Setup metrics
-        DefaultExports.initialize();
+            if (!isBlank(zookeeperServers)) {
+                // Use zookeeperServers from command line
+                config.setZookeeperServers(zookeeperServers);
+            }
 
-        // Report direct memory from Netty counters
-        Gauge.build("jvm_memory_direct_bytes_used", "-").create().setChild(new Child() {
-            @Override
-            public double get() {
-                return getJvmDirectMemoryUsed();
+            if (!isBlank(globalZookeeperServers)) {
+                // Use globalZookeeperServers from command line
+                config.setConfigurationStoreServers(globalZookeeperServers);
+            }
+            if (!isBlank(configurationStoreServers)) {
+                // Use configurationStoreServers from command line
+                config.setConfigurationStoreServers(configurationStoreServers);
+            }
+
+            if ((isBlank(config.getBrokerServiceURL()) && isBlank(config.getBrokerServiceURLTLS()))
+                    || config.isAuthorizationEnabled()) {
+                checkArgument(!isEmpty(config.getZookeeperServers()), "zookeeperServers must be provided");
+                checkArgument(!isEmpty(config.getConfigurationStoreServers()),
+                        "configurationStoreServers must be provided");
             }
-        }).register(CollectorRegistry.defaultRegistry);
 
-        Gauge.build("jvm_memory_direct_bytes_max", "-").create().setChild(new Child() {
-            @Override
-            public double get() {
-                return PlatformDependent.maxDirectMemory();
+            if ((!config.isTlsEnabledWithBroker() && isBlank(config.getBrokerWebServiceURL()))
+                    || (config.isTlsEnabledWithBroker() && isBlank(config.getBrokerWebServiceURLTLS()))) {
+                checkArgument(!isEmpty(config.getZookeeperServers()), "zookeeperServers must be provided");
             }
-        }).register(CollectorRegistry.defaultRegistry);
 
-        addWebServerHandlers(server, config, proxyService, proxyService.getDiscoveryProvider());
+            AuthenticationService authenticationService = new AuthenticationService(
+                    PulsarConfigurationLoader.convertFrom(config));
+            // create proxy service
+            ProxyService proxyService = new ProxyService(config, authenticationService);
+            // create a web-service
+            final WebServer server = new WebServer(config, authenticationService);
+
+            Runtime.getRuntime().addShutdownHook(new Thread(() -> {
+                try {
+                    proxyService.close();
+                    server.stop();
+                } catch (Exception e) {
+                    log.warn("server couldn't stop gracefully {}", e.getMessage(), e);
+                }
+            }));
+
+            proxyService.start();
+
+            // Setup metrics
+            DefaultExports.initialize();
+
+            // Report direct memory from Netty counters
+            Gauge.build("jvm_memory_direct_bytes_used", "-").create().setChild(new Child() {
+                @Override
+                public double get() {
+                    return getJvmDirectMemoryUsed();
+                }
+            }).register(CollectorRegistry.defaultRegistry);
+
+            Gauge.build("jvm_memory_direct_bytes_max", "-").create().setChild(new Child() {
+                @Override
+                public double get() {
+                    return PlatformDependent.maxDirectMemory();
+                }
+            }).register(CollectorRegistry.defaultRegistry);
+
+            addWebServerHandlers(server, config, proxyService, proxyService.getDiscoveryProvider());
+
+            // start web-service
+            server.start();
 
-        // start web-service
-        server.start();
+        } catch (Exception e) {
+            log.error("Failed to start pulsar proxy service. error msg " + e.getMessage(), e);
+            throw new PulsarServerException(e);
+        }
     }
 
     public static void main(String[] args) throws Exception {