sijie commented on a change in pull request #2883: Add healthcheck for broker URL: https://github.com/apache/pulsar/pull/2883#discussion_r229085858
########## 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) Review comment: > If we don't check that we have a working connection to ZK, we maybe report a unhealthy broker as healthy. well. I have a different opinion here. if we report broker as unhealthy when it fails to talk to zookeeper, the health check system can potentially kill the whole cluster of brokers when zookeeper has problems. ideally we should not put zookeeper in any critical checking path. the reason is pulsar is able to continue publish and consume even zookeeper is unavailable until pulsar decide to roll a new ledger. if you are adding this check into health check, you are basically making broker's healthy tied to zookeeper availability, which can have pretty bad side effects when running this check on production. from this perspective, I would also suggest leaving out subscribing and unsubscribing from the health check. ---------------------------------------------------------------- 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
