org.apache.kafka.common.Node Java Examples

The following examples show how to use org.apache.kafka.common.Node. 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: ClusterTopicManipulationService.java    From kafka-monitor with Apache License 2.0 6 votes vote down vote up
/**
 * iterates through the broker's log directories and checks for the ongoing topic partitions and replica's existence.
 * @param logDirectoriesResponseMap map of log directories response in the broker
 * @param broker broker to process the log dirs in
 * @param topic ongoing kmf manipulation topic
 */
int processBroker(Map<Integer, Map<String, DescribeLogDirsResponse.LogDirInfo>> logDirectoriesResponseMap,
    Node broker, String topic) {
  int totalPartitionsInBroker = 0;
  LOGGER.trace("logDirectoriesResponseMap: {}", logDirectoriesResponseMap);
  Map<String, DescribeLogDirsResponse.LogDirInfo> logDirInfoMap = logDirectoriesResponseMap.get(broker.id());
  String logDirectoriesKey = logDirInfoMap.keySet().iterator().next();
  LOGGER.trace("logDirInfoMap: {}", logDirInfoMap.get(logDirectoriesKey));
  DescribeLogDirsResponse.LogDirInfo logDirInfo = logDirInfoMap.get(logDirectoriesKey);

  if (logDirInfo != null && !logDirectoriesResponseMap.isEmpty()) {
    Map<TopicPartition, DescribeLogDirsResponse.ReplicaInfo> topicPartitionReplicaInfoMap = logDirInfo.replicaInfos;
    totalPartitionsInBroker += this.processLogDirsWithinBroker(topicPartitionReplicaInfoMap, topic, broker);
  }

  return totalPartitionsInBroker;
}
 
Example #2
Source File: MultiClusterTopicManagementServiceTest.java    From kafka-monitor with Apache License 2.0 6 votes vote down vote up
@Test(invocationCount = 2)
protected void maybeAddPartitionsTest() {
  Set<BrokerMetadata> brokerMetadataSet = new LinkedHashSet<>();
  for (Node broker : nodeSet) {
    brokerMetadataSet.add(new BrokerMetadata(broker.id(), Option.apply(broker.rack())));
  }

  int minPartitionNum = 14;
  int partitionNum = 5;
  int rf = 4;

  List<List<Integer>> newPartitionAssignments =
      MultiClusterTopicManagementService.TopicManagementHelper.newPartitionAssignments(minPartitionNum, partitionNum, brokerMetadataSet, rf);
  Assert.assertNotNull(newPartitionAssignments);

  System.out.println(newPartitionAssignments);
  Assert.assertEquals(newPartitionAssignments.size(), minPartitionNum - partitionNum);
  Assert.assertEquals(newPartitionAssignments.get(0).size(), rf);
}
 
Example #3
Source File: KafkaPartitionMetricSampleAggregator.java    From cruise-control with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * This is a simple sanity check on the sample data. We only verify that
 * <p>
 * 1. the broker of the sampled data is from the broker who holds the leader replica. If it is not, we simply
 * discard the data because leader migration may have occurred so the metrics on the old data might not be
 * accurate anymore.
 * <p>
 * 2. The sample contains metric for all the resources.
 *
 * @param sample the sample to do the sanity check.
 * @param leaderValidation whether do the leader validation or not.
 * @return {@code true} if the sample is valid.
 */
private boolean isValidSample(PartitionMetricSample sample, boolean leaderValidation) {
  boolean validLeader = true;
  if (leaderValidation) {
    Node leader = _metadata.fetch().leaderFor(sample.entity().tp());
    validLeader = (leader != null) && (sample.brokerId() == leader.id());
    if (!validLeader) {
      LOG.warn("The metric sample is discarded due to invalid leader. Current leader {}, Sample: {}", leader, sample);
    }
  }

  // TODO: We do not have the replication bytes rate at this point. Use the default validation after they are available.
  boolean completeMetrics = sample.isValid(_metricDef) || (sample.allMetricValues().size() == _metricDef.size() - 2
                                                           && sample.allMetricValues().containsKey(_metricDef.metricInfo(
                                                               KafkaMetricDef.REPLICATION_BYTES_IN_RATE.name()).id())
                                                           && sample.allMetricValues().containsKey(_metricDef.metricInfo(
                                                               KafkaMetricDef.REPLICATION_BYTES_OUT_RATE.name()).id()));

  if (!completeMetrics) {
    LOG.warn("The metric sample is discarded due to missing metrics. Sample: {}", sample);
  }
  return validLeader && completeMetrics;
}
 
Example #4
Source File: KafkaCruiseControlUtils.java    From cruise-control with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private static MetadataResponseData.MetadataResponseTopic prepareMetadataResponseTopic(MetadataResponse.TopicMetadata topicMetadata) {
  MetadataResponseData.MetadataResponseTopic metadataResponseTopic = new MetadataResponseData.MetadataResponseTopic();
  metadataResponseTopic.setErrorCode(topicMetadata.error().code())
                       .setName(topicMetadata.topic())
                       .setIsInternal(topicMetadata.isInternal())
                       .setTopicAuthorizedOperations(topicMetadata.authorizedOperations());

  for (MetadataResponse.PartitionMetadata partitionMetadata : topicMetadata.partitionMetadata()) {
    metadataResponseTopic.partitions().add(
        new MetadataResponseData.MetadataResponsePartition()
            .setErrorCode(partitionMetadata.error().code())
            .setPartitionIndex(partitionMetadata.partition())
            .setLeaderId(partitionMetadata.leader() == null ? -1 : partitionMetadata.leader().id())
            .setLeaderEpoch(partitionMetadata.leaderEpoch().orElse(RecordBatch.NO_PARTITION_LEADER_EPOCH))
            .setReplicaNodes(partitionMetadata.replicas().stream().map(Node::id).collect(Collectors.toList()))
            .setIsrNodes(partitionMetadata.isr().stream().map(Node::id).collect(Collectors.toList()))
            .setOfflineReplicas(partitionMetadata.offlineReplicas().stream().map(Node::id).collect(Collectors.toList())));
  }

  return metadataResponseTopic;
}
 
Example #5
Source File: TopicPartitionsOffsetInfo.java    From kafka-utilities with Apache License 2.0 6 votes vote down vote up
private void RequestPerBrokerPartitionOffsetDetails(final long value) throws OffsetFetchException {
    CountDownLatch countDownLatch = new CountDownLatch(nodePartitionMap.size());
    int threadCount = 0;
    for(Map.Entry<Node,List<TopicPartition>>nodeListEntry: nodePartitionMap.entrySet()){
        Thread newThread = new Thread(new TopicPartitionsOffsetInfo(this.adminClient, nodeListEntry.getKey(),nodeListEntry.getValue(),countDownLatch, value));
        newThread.setName("ThreadNumber:" + threadCount);   //Need to think on Executor framework.
        newThread.start();
        threadCount ++;
    }

    try {
        //wait for all threads to complete
        countDownLatch.await();
    } catch (InterruptedException e) {
        throw new OffsetFetchException("TopicPartitionsOffsetInfo RequestPerBrokerPartitionOffsetDetails InterruptedException:" + e.getMessage());
    }
}
 
Example #6
Source File: CruiseControlMetricsProcessor.java    From cruise-control with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Update the cached number of cores by broker id. The cache is refreshed only for brokers with missing number of cores.
 * Note that if the broker capacity resolver is unable to resolve or can only estimate certain broker's capacity, the core
 * number for that broker will not be updated.
 * @param cluster Kafka cluster.
 */
private void updateCachedNumCoresByBroker(Cluster cluster) {
  for (int brokerId : _brokerLoad.keySet()) {
    // Compute cached number of cores by broker id if they have not been cached already.
    _cachedNumCoresByBroker.computeIfAbsent(brokerId, bid -> {
      Node node = cluster.nodeById(bid);
      if (node == null) {
        LOG.warn("Received metrics from unrecognized broker {}.", bid);
        return null;
      }
      try {
        BrokerCapacityInfo capacity =
            _brokerCapacityConfigResolver.capacityForBroker(getRackHandleNull(node), node.host(), bid, BROKER_CAPACITY_FETCH_TIMEOUT_MS,
                                                            _allowCpuCapacityEstimation);
        return capacity == null ? null : capacity.numCpuCores();
      } catch (TimeoutException | BrokerCapacityResolutionException e) {
        LOG.warn("Unable to get number of CPU cores for broker {}.", node.id(), e);
        return null;
      }
    });
  }
}
 
Example #7
Source File: KafkaClusterManager.java    From doctorkafka with Apache License 2.0 6 votes vote down vote up
/**
 *  Remove the under-replicated partitions that are in the middle of partition reassignment.
 */
public List<PartitionInfo> filterOutInReassignmentUrps(List<PartitionInfo> urps,
                                                       Map<String, Integer> replicationFactors) {
  List<PartitionInfo> result = new ArrayList<>();
  for (PartitionInfo urp : urps) {
    if (urp.replicas().length <= replicationFactors.get(urp.topic())) {
      // # of replicas <= replication factor
      result.add(urp);
    } else {
      // # of replicas > replication factor. this can happen after
      // a failed partition reassignment
      Set<Integer> liveReplicas = new HashSet<>();
      for (Node node : urp.replicas()) {
        if (node.host() != null && OperatorUtil.pingKafkaBroker(node.host(), 9092, 5000)) {
          liveReplicas.add(node.id());
        }
      }
      if (liveReplicas.size() < replicationFactors.get(urp.topic())) {
        result.add(urp);
      }
    }
  }
  return result;
}
 
Example #8
Source File: ClientKafkaMonitor.java    From Kafdrop with Apache License 2.0 6 votes vote down vote up
@Override
public List<BrokerVO> getBrokers()
{

   DescribeClusterResult cluster = admin(AdminClient::describeCluster);

   Collection<Node> nodeList = waitOnFuture(cluster.nodes());
   if (nodeList.isEmpty())
   {
      return Collections.emptyList();
   }
   else
   {
      return createBrokers(nodeList, waitOnFuture(cluster.controller()));
   }
}
 
Example #9
Source File: KafkaOpenMetadataTopicConnector.java    From egeria with Apache License 2.0 6 votes vote down vote up
boolean getRunningBrokers(Properties connectionProperties ) {

            boolean found = false;
            try {
                AdminClient adminClient = KafkaAdminClient.create(connectionProperties);
                DescribeClusterResult describeClusterResult = adminClient.describeCluster();
                Collection<Node> brokers = describeClusterResult.nodes().get();
                if (!brokers.isEmpty()) {
                    found = true;
                }
            } catch (Exception e) {
                //gulp down any exceptions, the waiting method will control any audit logging
                //but keep a copy for reference
                lastException = e;
            }

            return found;
        }
 
Example #10
Source File: KafkaPool.java    From feeyo-redisproxy with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private Collection<Node> discoverNodes() {
	Set<String> theHostList = availableHostList;
	if (theHostList.isEmpty()) {
		theHostList = backupHostList;
	}
	for (String availableHost : theHostList) {
		String[] hostAndPort = availableHost.split(":");
		
		KafkaAdmin kafkaAdmin = null;
		try {
			kafkaAdmin = KafkaAdmin.create(hostAndPort[0] + ":" + hostAndPort[1]);
			Collection<Node> nodes = kafkaAdmin.getClusterNodes();
			if (nodes == null || nodes.isEmpty()) {
				continue;
			}
			return nodes;
		} finally {
			if (kafkaAdmin != null)
				kafkaAdmin.close();
		}
	}
	return null;
}
 
Example #11
Source File: KafkaRequestHandler.java    From kop with Apache License 2.0 6 votes vote down vote up
static PartitionMetadata newPartitionMetadata(TopicName topicName, Node node) {
    int pulsarPartitionIndex = topicName.getPartitionIndex();
    int kafkaPartitionIndex = pulsarPartitionIndex == -1 ? 0 : pulsarPartitionIndex;

    if (log.isDebugEnabled()) {
        log.debug("Return PartitionMetadata node: {}, topicName: {}", node, topicName);
    }

    return new PartitionMetadata(
        Errors.NONE,
        kafkaPartitionIndex,
        node,                      // leader
        Lists.newArrayList(node),  // replicas
        Lists.newArrayList(node),  // isr
        Collections.emptyList()     // offline replicas
    );
}
 
Example #12
Source File: KafkaRequestHandler.java    From kop with Apache License 2.0 6 votes vote down vote up
static PartitionMetadata newFailedPartitionMetadata(TopicName topicName) {
    int pulsarPartitionIndex = topicName.getPartitionIndex();
    int kafkaPartitionIndex = pulsarPartitionIndex == -1 ? 0 : pulsarPartitionIndex;

    log.warn("Failed find Broker metadata, create PartitionMetadata with NOT_LEADER_FOR_PARTITION");

    // most of this error happens when topic is in loading/unloading status,
    return new PartitionMetadata(
        Errors.NOT_LEADER_FOR_PARTITION,
        kafkaPartitionIndex,
        Node.noNode(),                      // leader
        Lists.newArrayList(Node.noNode()),  // replicas
        Lists.newArrayList(Node.noNode()),  // isr
        Collections.emptyList()             // offline replicas
    );
}
 
Example #13
Source File: KafkaClusterManagerTest.java    From doctorkafka with Apache License 2.0 6 votes vote down vote up
/**
 *  Partition(topic = brokerstats, partition = 7, leader = 7016,
 *          replicas = [7017,7011,7018,7016], isr = [7017,7011,7016])
 */
@Test
public void getOutOfSyncBrokersTest() throws Exception {
  DoctorKafkaConfig config = new DoctorKafkaConfig("./config/doctorkafka.properties");

  Node leader = new Node(7016, "datakafka07016", 9092);
  Node[] replicas = new Node[]{new Node(7017, "datakafka07017", 9092),
                               new Node(7011, "datakafka07011", 9092),
                               new Node(7018, "datakafka07018", 9092),
                               new Node(7016, "datakafka07016", 9092)};

  Node[] isrs = new Node[]{new Node(7017, "datakafka07017", 9092),
                           new Node(7011, "datakafka07011", 9092),
                           new Node(7016, "datakafka07016", 9092)};

  PartitionInfo partitionInfo = new PartitionInfo("brokerstats", 7, leader, replicas, isrs);
  KafkaClusterManager clusterManager = new KafkaClusterManager("datazk001:2181/testk10", null,
      config.getClusterConfigByName("cluster1"), null, null, null, null);
  Set<Node> nodes = KafkaUtils.getNotInSyncBrokers(partitionInfo);
  assertEquals(1, nodes.size());
  assertEquals(replicas[2], nodes.iterator().next());
}
 
Example #14
Source File: KafkaProducerUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
void givenKeyValue_whenSendWithPartitioning_thenVerifyPartitionNumber() throws ExecutionException, InterruptedException {

    PartitionInfo partitionInfo0 = new PartitionInfo(TOPIC_NAME, 0, null, null, null);
    PartitionInfo partitionInfo1 = new PartitionInfo(TOPIC_NAME, 1, null, null, null);
    List<PartitionInfo> list = new ArrayList<>();
    list.add(partitionInfo0);
    list.add(partitionInfo1);
    Cluster cluster = new Cluster("kafkab", new ArrayList<Node>(), list, emptySet(), emptySet());
    this.mockProducer = new MockProducer<>(cluster, true, new EvenOddPartitioner(), new StringSerializer(), new StringSerializer());
    //when
    kafkaProducer = new KafkaProducer(mockProducer);
    Future<RecordMetadata> recordMetadataFuture = kafkaProducer.send("partition", "{\"site\" : \"baeldung\"}");

    //then
    assertTrue(recordMetadataFuture.get().partition() == 1);

}
 
Example #15
Source File: Utils.java    From strimzi-kafka-operator with Apache License 2.0 6 votes vote down vote up
public static TopicMetadata getTopicMetadata(Topic kubeTopic) {
    List<Node> nodes = new ArrayList<>();
    for (int nodeId = 0; nodeId < kubeTopic.getNumReplicas(); nodeId++) {
        nodes.add(new Node(nodeId, "localhost", 9092 + nodeId));
    }
    List<TopicPartitionInfo> partitions = new ArrayList<>();
    for (int partitionId = 0; partitionId < kubeTopic.getNumPartitions(); partitionId++) {
        partitions.add(new TopicPartitionInfo(partitionId, nodes.get(0), nodes, nodes));
    }
    List<ConfigEntry> configs = new ArrayList<>();
    for (Map.Entry<String, String> entry: kubeTopic.getConfig().entrySet()) {
        configs.add(new ConfigEntry(entry.getKey(), entry.getValue()));
    }

    return new TopicMetadata(new TopicDescription(kubeTopic.getTopicName().toString(), false,
            partitions), new Config(configs));
}
 
Example #16
Source File: KafkaBinderHealthIndicatorTest.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 6 votes vote down vote up
@Test
public void consumerCreationFailsFirstTime() {
	final List<PartitionInfo> partitions = partitions(new Node(0, null, 0));
	topicsInUse.put(TEST_TOPIC, new KafkaMessageChannelBinder.TopicInformation(
			"foo-healthIndicator", partitions, false));

	org.mockito.BDDMockito.given(consumerFactory.createConsumer())
			.willThrow(KafkaException.class).willReturn(consumer);

	Health health = indicator.health();
	assertThat(health.getStatus()).isEqualTo(Status.DOWN);

	health = indicator.health();
	assertThat(health.getStatus()).isEqualTo(Status.UP);

	org.mockito.Mockito.verify(this.consumerFactory, Mockito.times(2))
			.createConsumer();
}
 
Example #17
Source File: FairPartitionerTest.java    From common-kafka with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() throws InterruptedException {
    partitioner = new FairPartitioner();
    topic = testName.getMethodName();
    key = new Object();
    keyBytes = new byte[0];
    value = new Object();
    valueBytes = new byte[0];

    node = new Node(1, "example.com", 6667);

    allPartitions =
            IntStream.range(0, 8).mapToObj(i -> {
                //null leader means not available
                Node leader = null;
                if(i % 2 == 0){
                    //a non-null leader means it is available
                    leader = node;
                }
                return new PartitionInfo(topic, i, leader, null, null);
            }).collect(Collectors.toList());
    notAvailablePartitions = allPartitions.stream().filter(p -> p.leader() == null).collect(Collectors.toList());

    cluster = new Cluster("clusterId", Collections.singleton(node), allPartitions,
            Collections.emptySet(), Collections.emptySet());

    // Wait until next clock window tick.
    long millis = System.currentTimeMillis() / FairPartitioner.ROTATE_MILLIS;
    while (System.currentTimeMillis() / FairPartitioner.ROTATE_MILLIS == millis) {
        Thread.sleep(1);
    }
}
 
Example #18
Source File: PartitionState.java    From cruise-control with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public PartitionState(PartitionInfo partitionInfo) {
  _topic = partitionInfo.topic();
  _partition = partitionInfo.partition();
  _leader = partitionInfo.leader() == null ? -1 : partitionInfo.leader().id();
  _replicas = Arrays.stream(partitionInfo.replicas()).map(Node::id).collect(Collectors.toList());
  _inSyncReplicas = Arrays.stream(partitionInfo.inSyncReplicas()).map(Node::id).collect(Collectors.toList());
  _outOfSyncReplicas = new HashSet<>(_replicas);
  _outOfSyncReplicas.removeAll(_inSyncReplicas);
  _offlineReplicas = Arrays.stream(partitionInfo.offlineReplicas()).map(Node::id).collect(Collectors.toSet());
}
 
Example #19
Source File: MockKafkaClientFactory.java    From kafka-pubsub-emulator with Apache License 2.0 5 votes vote down vote up
private MockConsumerConfiguration(
    String topic, int partitions, long startingOffset, long endOffset) {
  this.topic = topic;
  partitionInfoList = new ArrayList<>();
  topicPartitionList = new ArrayList<>();
  startOffsets = new HashMap<>();
  endOffsets = new HashMap<>();
  for (int i = 0; i < partitions; i++) {
    Node node = new Node(i, "localhost", 9092 + i);
    partitionInfoList.add(new PartitionInfo(topic, i, node, null, null));
    topicPartitionList.add(new TopicPartition(topic, i));
    startOffsets.put(topicPartitionList.get(i), startingOffset);
    endOffsets.put(topicPartitionList.get(i), endOffset);
  }
}
 
Example #20
Source File: SamplingUtils.java    From cruise-control with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Get the number of leader partitions for each topic by each broker. It is useful to derive the partition level IO
 * from the topic level IO on a broker.
 * TODO: Create open source KIP to provide per partition IO metrics.
 *
 * @param cluster Kafka cluster
 * @return The number of leader partitions for each topic by each broker.
 */
static Map<Integer, Map<String, Integer>> leaderDistribution(Cluster cluster) {
  List<Node> clusterNodes = cluster.nodes();
  Map<Integer, Map<String, Integer>> stats = new HashMap<>(clusterNodes.size());
  for (Node node : clusterNodes) {
    Map<String, Integer> numLeadersByTopic = new HashMap<>();
    stats.put(node.id(), numLeadersByTopic);
    cluster.partitionsForNode(node.id()).forEach(partitionInfo -> numLeadersByTopic.merge(partitionInfo.topic(), 1, Integer::sum));
  }
  return stats;
}
 
Example #21
Source File: KafkaBinderMetricsTest.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldNotCalculateLagForProducerTopics() {
	List<PartitionInfo> partitions = partitions(new Node(0, null, 0));
	topicsInUse.put(TEST_TOPIC, new TopicInformation(null, partitions, false));
	metrics.bindTo(meterRegistry);
	assertThat(meterRegistry.getMeters()).isEmpty();
}
 
Example #22
Source File: GroupCoordinator.java    From DataLink with Apache License 2.0 5 votes vote down vote up
public List<Node> getAllGroupCoordinators() {
    List<Node> result = Lists.newArrayList();
    int nodeId = 1;
    for (ManagerMetaData mi : serverStatusMonitor.getAllAliveManagers()) {
        result.add(new Node(nodeId++, mi.getAddress(), mi.getPort()));
    }
    return result;
}
 
Example #23
Source File: KafkaTopicsListTest.java    From ksql-fork-with-deep-learning-function with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldBuildValidTopicList() {

  Collection<KsqlTopic> ksqlTopics = Collections.emptyList();
  // represent the full list of topics
  Map<String, TopicDescription> topicDescriptions = new HashMap<>();
  TopicPartitionInfo topicPartitionInfo = new TopicPartitionInfo(1, new Node(1, "", 8088),
                                                                 Collections.emptyList(), Collections.emptyList());
  topicDescriptions.put("test-topic", new TopicDescription("test-topic", false, Collections.singletonList(topicPartitionInfo)));


  /**
   * Return POJO for consumerGroupClient
   */
  TopicPartition topicPartition = new TopicPartition("test-topic", 1);
  KafkaConsumerGroupClientImpl.ConsumerSummary consumerSummary = new KafkaConsumerGroupClientImpl.ConsumerSummary("consumer-id");
  consumerSummary.addPartition(topicPartition);
  KafkaConsumerGroupClientImpl.ConsumerGroupSummary consumerGroupSummary = new KafkaConsumerGroupClientImpl.ConsumerGroupSummary();
  consumerGroupSummary.addConsumerSummary(consumerSummary);



  KafkaConsumerGroupClient consumerGroupClient = mock(KafkaConsumerGroupClient.class);
  expect(consumerGroupClient.listGroups()).andReturn(Collections.singletonList("test-topic"));
  expect(consumerGroupClient.describeConsumerGroup("test-topic")).andReturn(consumerGroupSummary);
  replay(consumerGroupClient);

  /**
   * Test
   */

  KafkaTopicsList topicsList = KafkaTopicsList.build("statement test", ksqlTopics, topicDescriptions, new KsqlConfig(Collections.EMPTY_MAP), consumerGroupClient);

  assertThat(topicsList.getTopics().size(), equalTo(1));
  KafkaTopicInfo first = topicsList.getTopics().iterator().next();
  assertThat(first.getConsumerGroupCount(), equalTo(1));
  assertThat(first.getConsumerCount(), equalTo(1));
  assertThat(first.getReplicaInfo().size(), equalTo(1));

}
 
Example #24
Source File: FakeKafkaTopicClient.java    From ksql-fork-with-deep-learning-function with Apache License 2.0 5 votes vote down vote up
public TopicDescription getDescription() {
  Node node = new Node(0, "localhost", 9091);
  List<TopicPartitionInfo> partitionInfoList =
      IntStream.range(0, numPartitions)
          .mapToObj(
              p -> new TopicPartitionInfo(p, node, Collections.emptyList(), Collections.emptyList()))
          .collect(Collectors.toList());
  return new TopicDescription(topicName, false, partitionInfoList);
}
 
Example #25
Source File: KafkaIT.java    From uavstack with Apache License 2.0 5 votes vote down vote up
/**
 * @param metadata
 * @return
 */
private static void getPollHost(Metadata metadata) {
    if ("".equals(pollHost)) {
        List<Node> nodesList = metadata.fetch().nodes();
        List<String> nList = new ArrayList<String>();
        for (int i = 0; i < nodesList.size(); i++) {
            nList.add(nodesList.get(i).host() + ":" + nodesList.get(i).port());
        }
        Collections.sort(nList);
        pollHost = "mq:kafka://" + StringHelper.join(nList, ",");
    }
}
 
Example #26
Source File: KafkaTopicPartitionLeader.java    From flink with Apache License 2.0 5 votes vote down vote up
public Node getLeader() {
	if (this.leaderId == -1) {
		return null;
	} else {
		return new Node(leaderId, leaderHost, leaderPort);
	}
}
 
Example #27
Source File: MultiClusterTopicManagementService.java    From kafka-monitor with Apache License 2.0 5 votes vote down vote up
private static void reassignPartitions(KafkaZkClient zkClient, Collection<Node> brokers, String topic, int partitionCount, int replicationFactor) {
  scala.collection.mutable.ArrayBuffer<BrokerMetadata> brokersMetadata = new scala.collection.mutable.ArrayBuffer<>(brokers.size());
  for (Node broker : brokers) {
    brokersMetadata.$plus$eq(new BrokerMetadata(broker.id(), Option$.MODULE$.apply(broker.rack())));
  }
  scala.collection.Map<Object, Seq<Object>> assignedReplicas =
      AdminUtils.assignReplicasToBrokers(brokersMetadata, partitionCount, replicationFactor, 0, 0);

  scala.collection.immutable.Map<TopicPartition, Seq<Object>> newAssignment = new scala.collection.immutable.HashMap<>();
  scala.collection.Iterator<scala.Tuple2<Object, scala.collection.Seq<Object>>> it = assignedReplicas.iterator();
  while (it.hasNext()) {
    scala.Tuple2<Object, scala.collection.Seq<Object>> scalaTuple = it.next();
    TopicPartition tp = new TopicPartition(topic, (Integer) scalaTuple._1);
    newAssignment = newAssignment.$plus(new scala.Tuple2<>(tp, scalaTuple._2));
  }

  scala.collection.immutable.Set<String> topicList = new scala.collection.immutable.Set.Set1<>(topic);
  scala.collection.Map<Object, scala.collection.Seq<Object>> currentAssignment =
      zkClient.getPartitionAssignmentForTopics(topicList).apply(topic);
  String currentAssignmentJson = formatAsNewReassignmentJson(topic, currentAssignment);
  String newAssignmentJson = formatAsNewReassignmentJson(topic, assignedReplicas);

  LOGGER.info("Reassign partitions for topic " + topic);
  LOGGER.info("Current partition replica assignment " + currentAssignmentJson);
  LOGGER.info("New partition replica assignment " + newAssignmentJson);
  zkClient.createPartitionReassignment(newAssignment);
}
 
Example #28
Source File: BrokerNodeFunction.java    From data-highway with Apache License 2.0 5 votes vote down vote up
public BrokerNode apply(Predicate<String> hostNamePredicate) {
  Collection<Node> nodes = KafkaFutures.join(client.describeCluster().nodes());

  try {
    Node node = find(nodes, n -> hostNamePredicate.test(n.host()));
    log.debug("Using broker {}", node);
    return new BrokerNode(node.id(), ofNullable(node.rack()).orElse("none"), node.host());
  } catch (NoSuchElementException e) {
    throw new RuntimeException("No broker found on localhost!");
  }
}
 
Example #29
Source File: JoinNodeTest.java    From ksql-fork-with-deep-learning-function with Apache License 2.0 5 votes vote down vote up
private void
setupTopicClientExpectations(int streamPartitions, int tablePartitions) {
  Node node = new Node(0, "localhost", 9091);

  List<TopicPartitionInfo> streamPartitionInfoList =
      IntStream.range(0, streamPartitions)
          .mapToObj(
              p -> new TopicPartitionInfo(p, node, Collections.emptyList(), Collections.emptyList()))
          .collect(Collectors.toList());
  EasyMock.expect(topicClient.describeTopics(Arrays.asList("test1")))
      .andReturn(
          Collections.singletonMap(
              "test1",
              new TopicDescription("test1", false, streamPartitionInfoList)));

  List<TopicPartitionInfo> tablePartitionInfoList =
      IntStream.range(0, tablePartitions)
      .mapToObj(
          p -> new TopicPartitionInfo(p, node, Collections.emptyList(), Collections.emptyList()))
      .collect(Collectors.toList());
  EasyMock.expect(topicClient.describeTopics(Arrays.asList("test2")))
      .andReturn(
          Collections.singletonMap(
              "test2",
              new TopicDescription("test2", false, tablePartitionInfoList)));
  EasyMock.replay(topicClient);
}
 
Example #30
Source File: MonitorUnitTestUtils.java    From cruise-control with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Get cluster that consists of {@link #NODE_0} and {@link #NODE_1} to be used in tests.
 * Partitions to be queried all have leader replica at {@link #NODE_0} and replicas/ISR at {@link #NODES}.
 *
 * @param partitions Partitions to include in the cluster.
 * @return Cluster that consists of {@link #NODE_0} and {@link #NODE_1} to be used in tests.
 */
public static Cluster getCluster(Collection<TopicPartition> partitions) {
  Set<Node> allNodes = new HashSet<>(2);
  allNodes.add(NODE_0);
  allNodes.add(NODE_1);
  Set<PartitionInfo> partitionInfo = new HashSet<>(partitions.size());
  for (TopicPartition tp : partitions) {
    partitionInfo.add(new PartitionInfo(tp.topic(), tp.partition(), NODE_0, NODES, NODES));
  }
  return new Cluster("cluster_id", allNodes, partitionInfo, Collections.emptySet(), Collections.emptySet());
}