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();
}
Aggregations