Java Code Examples for org.apache.kafka.common.Cluster#topics()

The following examples show how to use org.apache.kafka.common.Cluster#topics() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TopicReplicationFactorAnomalyFinder.java    From cruise-control with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public Set<TopicAnomaly> topicAnomalies() {
  LOG.info("Start to detect topic replication factor anomaly.");
  Cluster cluster = _kafkaCruiseControl.kafkaCluster();
  Set<String> topicsToCheck = new HashSet<>();
  for (String topic : cluster.topics()) {
    if (_topicExcludedFromCheck.matcher(topic).matches()) {
      continue;
    }
    for (PartitionInfo partition : cluster.partitionsForTopic(topic)) {
      if (partition.replicas().length != _targetReplicationFactor) {
        topicsToCheck.add(topic);
        break;
      }
    }
  }
  refreshTopicMinISRCache();
  if (!topicsToCheck.isEmpty()) {
    maybeRetrieveAndCacheTopicMinISR(topicsToCheck);
    Map<Short, Set<TopicReplicationFactorAnomalyEntry>> badTopicsByReplicationFactor = populateBadTopicsByReplicationFactor(topicsToCheck, cluster);
    if (!badTopicsByReplicationFactor.isEmpty()) {
      return Collections.singleton(createTopicReplicationFactorAnomaly(badTopicsByReplicationFactor, _targetReplicationFactor));
    }
  }
  return Collections.emptySet();
}
 
Example 2
Source File: RunnableUtils.java    From cruise-control with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Check partitions in the given cluster for existence of offline replicas, and get the first such partition.
 * If there no such partitions are identified, return {@code null}.
 *
 * @param cluster The current cluster state.
 * @return The first identified partition with offline replicas, {@code null} if no such partition exists.
 */
public static PartitionInfo partitionWithOfflineReplicas(Cluster cluster) {
  for (String topic : cluster.topics()) {
    for (PartitionInfo partitionInfo : cluster.partitionsForTopic(topic)) {
      if (partitionInfo.offlineReplicas().length > 0) {
        return partitionInfo;
      }
    }
  }
  return null;
}
 
Example 3
Source File: MonitorUtils.java    From cruise-control with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * @return True if the metadata has changed, false otherwise.
 */
public static boolean metadataChanged(Cluster prev, Cluster curr) {
  // Broker has changed.
  Set<Node> prevNodeSet = new HashSet<>(prev.nodes());
  if (prevNodeSet.size() != curr.nodes().size()) {
    return true;
  }
  prevNodeSet.removeAll(curr.nodes());
  if (!prevNodeSet.isEmpty()) {
    return true;
  }
  // Topic has changed
  if (!prev.topics().equals(curr.topics())) {
    return true;
  }

  // partition has changed.
  for (String topic : prev.topics()) {
    if (!prev.partitionCountForTopic(topic).equals(curr.partitionCountForTopic(topic))) {
      return true;
    }
    for (PartitionInfo prevPartInfo : prev.partitionsForTopic(topic)) {
      PartitionInfo currPartInfo = curr.partition(new TopicPartition(prevPartInfo.topic(), prevPartInfo.partition()));
      if (leaderChanged(prevPartInfo, currPartInfo) || replicaListChanged(prevPartInfo, currPartInfo)) {
        return true;
      }
    }
  }
  return false;
}
 
Example 4
Source File: MonitorUtils.java    From cruise-control with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * @param cluster Kafka cluster.
 * @return All the brokers in the cluster that host at least one replica.
 */
static Set<Integer> brokersWithReplicas(Cluster cluster) {
  Set<Integer> allBrokers = new HashSet<>();
  for (String topic : cluster.topics()) {
    for (PartitionInfo partition : cluster.partitionsForTopic(topic)) {
      Arrays.stream(partition.replicas()).map(Node::id).forEach(allBrokers::add);
    }
  }
  return allBrokers;
}
 
Example 5
Source File: DefaultMetricSamplerPartitionAssignor.java    From cruise-control with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public Set<TopicPartition> assignPartitions(Cluster cluster) {
  // Create an array to host the assignment of the metric fetcher.
  Set<TopicPartition> assignment = new HashSet<>();
  for (String topic : cluster.topics()) {
    List<PartitionInfo> partitionsForTopic = cluster.partitionsForTopic(topic);
    for (PartitionInfo partitionInfo : partitionsForTopic) {
      assignment.add(new TopicPartition(partitionInfo.topic(), partitionInfo.partition()));
    }
  }
  LOG.trace("Partition assignment for metric fetcher: {}", assignment);
  return assignment;
}