Search in sources :

Example 1 with KafkaClusterState

use of com.linkedin.kafka.cruisecontrol.KafkaClusterState in project cruise-control by linkedin.

the class KafkaCruiseControlServlet method getKafkaClusterState.

private void getKafkaClusterState(HttpServletRequest request, HttpServletResponse response) throws Exception {
    boolean verbose;
    boolean json = wantJSON(request);
    try {
        String verboseString = request.getParameter(VERBOSE_PARAM);
        verbose = verboseString != null && Boolean.parseBoolean(verboseString);
    } catch (Exception e) {
        StringWriter sw = new StringWriter();
        e.printStackTrace(new PrintWriter(sw));
        setErrorResponse(response, sw.toString(), e.getMessage(), SC_BAD_REQUEST, json);
        return;
    }
    KafkaClusterState state = _asyncKafkaCruiseControl.kafkaClusterState();
    OutputStream out = response.getOutputStream();
    if (json) {
        String stateString = state.getJSONString(JSON_VERSION, verbose);
        setJSONResponseCode(response, SC_OK);
        response.setContentLength(stateString.length());
        out.write(stateString.getBytes(StandardCharsets.UTF_8));
    } else {
        setResponseCode(response, SC_OK);
        Cluster clusterState = state.kafkaCluster();
        // Brokers summary.
        SortedMap<Integer, Integer> leaderCountByBrokerId = new TreeMap<>();
        SortedMap<Integer, Integer> outOfSyncCountByBrokerId = new TreeMap<>();
        SortedMap<Integer, Integer> replicaCountByBrokerId = new TreeMap<>();
        state.populateKafkaBrokerState(leaderCountByBrokerId, outOfSyncCountByBrokerId, replicaCountByBrokerId);
        String initMessage = "Brokers with replicas:";
        out.write(String.format("%s%n%20s%20s%20s%20s%n", initMessage, "BROKER", "LEADER(S)", "REPLICAS", "OUT-OF-SYNC").getBytes(StandardCharsets.UTF_8));
        for (Integer brokerId : replicaCountByBrokerId.keySet()) {
            out.write(String.format("%20d%20d%20d%20d%n", brokerId, leaderCountByBrokerId.getOrDefault(brokerId, 0), replicaCountByBrokerId.getOrDefault(brokerId, 0), outOfSyncCountByBrokerId.getOrDefault(brokerId, 0)).getBytes(StandardCharsets.UTF_8));
        }
        // Partitions summary.
        int topicNameLength = clusterState.topics().stream().mapToInt(String::length).max().orElse(20) + 5;
        initMessage = verbose ? "All Partitions in the Cluster (verbose):" : "Under Replicated and Offline Partitions in the Cluster:";
        out.write(String.format("%n%s%n%" + topicNameLength + "s%10s%10s%40s%40s%30s%n", initMessage, "TOPIC", "PARTITION", "LEADER", "REPLICAS", "IN-SYNC", "OUT-OF-SYNC").getBytes(StandardCharsets.UTF_8));
        // Gather the cluster state.
        Comparator<PartitionInfo> comparator = Comparator.comparing(PartitionInfo::topic).thenComparingInt(PartitionInfo::partition);
        SortedSet<PartitionInfo> underReplicatedPartitions = new TreeSet<>(comparator);
        SortedSet<PartitionInfo> offlinePartitions = new TreeSet<>(comparator);
        SortedSet<PartitionInfo> otherPartitions = new TreeSet<>(comparator);
        state.populateKafkaPartitionState(underReplicatedPartitions, offlinePartitions, otherPartitions, verbose);
        // Write the cluster state.
        out.write(String.format("Offline Partitions:%n").getBytes(StandardCharsets.UTF_8));
        writeKafkaClusterState(out, offlinePartitions, topicNameLength);
        out.write(String.format("Under Replicated Partitions:%n").getBytes(StandardCharsets.UTF_8));
        writeKafkaClusterState(out, underReplicatedPartitions, topicNameLength);
        if (verbose) {
            out.write(String.format("Other Partitions:%n").getBytes(StandardCharsets.UTF_8));
            writeKafkaClusterState(out, otherPartitions, topicNameLength);
        }
    }
    response.getOutputStream().flush();
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) OutputStream(java.io.OutputStream) Cluster(org.apache.kafka.common.Cluster) TreeMap(java.util.TreeMap) TimeoutException(java.util.concurrent.TimeoutException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) EndPoint(com.linkedin.kafka.cruisecontrol.servlet.KafkaCruiseControlServlet.EndPoint) StringWriter(java.io.StringWriter) TreeSet(java.util.TreeSet) KafkaClusterState(com.linkedin.kafka.cruisecontrol.KafkaClusterState) PartitionInfo(org.apache.kafka.common.PartitionInfo) PrintWriter(java.io.PrintWriter)

Aggregations

KafkaClusterState (com.linkedin.kafka.cruisecontrol.KafkaClusterState)1 EndPoint (com.linkedin.kafka.cruisecontrol.servlet.KafkaCruiseControlServlet.EndPoint)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 TreeMap (java.util.TreeMap)1 TreeSet (java.util.TreeSet)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 ServletOutputStream (javax.servlet.ServletOutputStream)1 Cluster (org.apache.kafka.common.Cluster)1 PartitionInfo (org.apache.kafka.common.PartitionInfo)1