pulsar-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] ivankelly commented on a change in pull request #2883: Add healthcheck for broker
Date Mon, 29 Oct 2018 20:04:53 GMT
ivankelly commented on a change in pull request #2883: Add healthcheck for broker
URL: https://github.com/apache/pulsar/pull/2883#discussion_r229078299
 
 

 ##########
 File path: pulsar-broker/src/main/java/org/apache/pulsar/PulsarHealthcheck.java
 ##########
 @@ -0,0 +1,204 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pulsar;
+
+import com.beust.jcommander.JCommander;
+import com.beust.jcommander.Parameter;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.nio.file.Paths;
+
+import java.util.Map;
+import java.util.HashMap;
+
+import java.util.Properties;
+import java.util.UUID;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
+import org.apache.commons.lang3.StringUtils;
+
+import org.apache.pulsar.client.admin.PulsarAdmin;
+import org.apache.pulsar.client.admin.PulsarAdminBuilder;
+import org.apache.pulsar.client.api.ClientBuilder;
+import org.apache.pulsar.client.api.Consumer;
+import org.apache.pulsar.client.api.Message;
+import org.apache.pulsar.client.api.Producer;
+import org.apache.pulsar.client.api.PulsarClient;
+import org.apache.pulsar.client.api.Schema;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+public class PulsarHealthcheck {
+    private static final Logger log = LoggerFactory.getLogger(PulsarHealthcheck.class);
+
+    private static class HealthcheckArguments {
+        @Parameter(names = {"-c", "--clientConf"}, description = "Configuration file for
healthcheck")
+        private String healthcheckConf = Paths.get("").toAbsolutePath().normalize().toString()
+ "/conf/client.conf";
+
+        @Parameter(names = {"-t", "--timeout"}, description = "Max number of seconds to wait
for the healthcheck")
+        private int timeoutSeconds = 10;
+
+        @Parameter(names = {"-h", "--help"}, description = "Show this help message")
+        private boolean help = false;
+    }
+
+    private static class Healthcheck implements Callable<Void> {
+        private final Properties properties;
+
+        Healthcheck(Properties properties) {
+            this.properties = properties;
+        }
+
+        @Override
+        public Void call() throws Exception {
+            log.info("Running healthcheck");
+            try (PulsarAdmin admin = propsToAdminBuilder(properties).build()) {
+                String topic = String.format("persistent://%s/healthcheck",
+                                             admin.brokers().getInternalConfigurationData().getHeartbeatNamespace());
+                log.info("Subscribing to topic {}", topic);
+                try (PulsarClient client = propsToClientBuilder(properties).build();
+                     Producer<String> producer = client.newProducer(Schema.STRING).topic(topic).create();
+                     Consumer<String> consumer = client.newConsumer(Schema.STRING)
+                     .subscriptionName("healthcheck-" + UUID.randomUUID().toString()).topic(topic).subscribe())
{
+
+                    String messageStr = UUID.randomUUID().toString();
+                    log.info("Sending message '{}' to {}", messageStr, topic);
+                    producer.send(messageStr);
+                    Message m = consumer.receive(10, TimeUnit.SECONDS);
+                    log.info("Received message '{}' from {}, unsubscribing", m != null ?
m.getValue() : "null", topic);
+                    consumer.unsubscribe();
+
+                    if (m == null) {
+                        throw new IllegalStateException("Healthcheck didn't receive message
back");
+                    }
+                    if (!m.getValue().equals(messageStr)) {
+                        throw new IllegalStateException("Healthcheck didn't receive back
the message it sent");
+                    }
+                }
+            }
+            log.info("Everything looks good!");
+            return null;
+        }
+    }
+
+    private static PulsarAdminBuilder propsToAdminBuilder(Properties properties) throws Exception
{
+        PulsarAdminBuilder builder = PulsarAdmin.builder();
+        builder.serviceHttpUrl(properties.getProperty("webServiceUrl"));
+
+        if (StringUtils.isNotBlank(properties.getProperty("authPlugin"))) {
+            builder.authentication(properties.getProperty("authPlugin"), properties.getProperty("authParams"));
+        }
+        builder.tlsTrustCertsFilePath(properties.getProperty("tlsTrustCertsFilePath"));
+        builder.allowTlsInsecureConnection(
+                Boolean.parseBoolean(properties.getProperty("tlsAllowInsecureConnection",
"false")));
+        builder.enableTlsHostnameVerification(
+                Boolean.parseBoolean(properties.getProperty("tlsEnableHostnameVerification",
"false")));
+        return builder;
+    }
+
+    private static ClientBuilder propsToClientBuilder(Properties properties) throws Exception
{
+        ClientBuilder builder = PulsarClient.builder();
+        if (StringUtils.isNotBlank(properties.getProperty("brokerServiceUrl"))) {
+            builder.serviceUrl(properties.getProperty("brokerServiceUrl"));
+        } else {
+            builder.serviceUrl(properties.getProperty("webServiceUrl"));
+        }
+
+        if (StringUtils.isNotBlank(properties.getProperty("authPlugin"))) {
+            builder.authentication(properties.getProperty("authPlugin"), properties.getProperty("authParams"));
+        }
+        builder.tlsTrustCertsFilePath(properties.getProperty("tlsTrustCertsFilePath"));
+        builder.allowTlsInsecureConnection(
+                Boolean.parseBoolean(properties.getProperty("tlsAllowInsecureConnection",
"false")));
+        builder.enableTlsHostnameVerification(
+                Boolean.parseBoolean(properties.getProperty("tlsEnableHostnameVerification",
"false")));
+        return builder;
+    }
+
+    private static void successExit() {
+        ObjectMapper mapper = new ObjectMapper();
+        Map<String, String> output = new HashMap<>();
+        output.put("result", "ok");
+        try {
+            mapper.writeValue(System.out, output);
+        } catch (IOException ioe) {
+            log.error("Error writing JSON", ioe);
+        }
+        System.exit(0);
+    }
+
+    private static void errorExit(int code, String reason) {
+        ObjectMapper mapper = new ObjectMapper();
+        Map<String, String> output = new HashMap<>();
+        output.put("result", "error");
+        output.put("cause", reason);
+        try {
+            mapper.writeValue(System.out, output);
+        } catch (IOException ioe) {
+            log.error("Error writing JSON", ioe);
+        }
+        System.exit(code);
+    }
+
+    public static void main(String[] args) throws Exception {
+        HealthcheckArguments healthcheckArguments = new HealthcheckArguments();
+        JCommander jcommander = new JCommander(healthcheckArguments);
+        jcommander.setProgramName("PulsarHealthcheck");
+
+        jcommander.parse(args);
+        if (healthcheckArguments.help) {
+            jcommander.usage();
+            System.exit(-1);
+        }
+
+        String configFile = healthcheckArguments.healthcheckConf;
+        Properties properties = new Properties();
+        try (FileInputStream fis = new FileInputStream(configFile)) {
+            properties.load(fis);
+        }
+
+        // no need to clean this executor up, as we exit straight after
+        ExecutorService executor = Executors.newSingleThreadExecutor((r) -> {
 
 Review comment:
   Admin does not. Admin hangs forever. 

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


With regards,
Apache Git Services

Mime
View raw message