rajinisivaram commented on code in PR #12990: URL: https://github.com/apache/kafka/pull/12990#discussion_r1112027236
########## core/src/test/scala/integration/kafka/api/PlaintextConsumerTest.scala: ########## @@ -1951,20 +1954,47 @@ class PlaintextConsumerTest extends BaseConsumerTest { } @Test - def testConsumerRackIdPropagatedToPartitionAssignor(): Unit = { - consumerConfig.setProperty(ConsumerConfig.CLIENT_RACK_CONFIG, "rack-a") - consumerConfig.setProperty(ConsumerConfig.PARTITION_ASSIGNMENT_STRATEGY_CONFIG, classOf[RackAwareAssignor].getName) - val consumer = createConsumer() - consumer.subscribe(Set(topic).asJava) - awaitAssignment(consumer, Set(tp, tp2)) - } -} + def testRackAwareRangeAssignor(): Unit = { + val partitionList = servers.indices.toList + + val topicWithAllPartitionsOnAllRacks = "topicWithAllPartitionsOnAllRacks" + createTopic(topicWithAllPartitionsOnAllRacks, servers.size, servers.size) + + // Racks are in order of broker ids, assign leaders in reverse order + val topicWithSingleRackPartitions = "topicWithSingleRackPartitions" + createTopicWithAssignment(topicWithSingleRackPartitions, partitionList.map(i => (i, Seq(servers.size - i - 1))).toMap) + + // Create consumers with instance ids in ascending order, with racks in the same order. + consumerConfig.setProperty(ConsumerConfig.PARTITION_ASSIGNMENT_STRATEGY_CONFIG, classOf[RangeAssignor].getName) + val consumers = servers.map { server => + consumerConfig.setProperty(ConsumerConfig.CLIENT_RACK_CONFIG, server.config.rack.orNull) + consumerConfig.setProperty(ConsumerConfig.GROUP_INSTANCE_ID_CONFIG, s"instance-${server.config.brokerId}") + createConsumer() + } + + val executor = Executors.newFixedThreadPool(consumers.size) + def waitForAssignments(assignments: List[Set[TopicPartition]]): Unit = { + val futures = consumers.zipWithIndex.map { case (consumer, i) => + executor.submit(() => awaitAssignment(consumer, assignments(i)), 0) + } + futures.foreach(future => assertEquals(0, future.get(20, TimeUnit.SECONDS))) + } -class RackAwareAssignor extends RoundRobinAssignor { - override def assign(partitionsPerTopic: util.Map[String, Integer], subscriptions: util.Map[String, ConsumerPartitionAssignor.Subscription]): util.Map[String, util.List[TopicPartition]] = { - assertEquals(1, subscriptions.size()) - assertEquals(Optional.of("rack-a"), subscriptions.values.asScala.head.rackId) - super.assign(partitionsPerTopic, subscriptions) + try { + // Rack-based assignment results in partitions assigned in reverse order since partition racks are in the reverse order. + consumers.foreach(_.subscribe(Collections.singleton(topicWithSingleRackPartitions))) + waitForAssignments(partitionList.reverse.map(p => Set(new TopicPartition(topicWithSingleRackPartitions, p)))) + + // Non-rack-aware assignment results in ordered partitions. + consumers.foreach(_.subscribe(Collections.singleton(topicWithAllPartitionsOnAllRacks))) + waitForAssignments(partitionList.map(p => Set(new TopicPartition(topicWithAllPartitionsOnAllRacks, p)))) + + // Rack-aware assignment with co-partitioning results in reverse assignment for both topics. + consumers.foreach(_.subscribe(Set(topicWithSingleRackPartitions, topicWithAllPartitionsOnAllRacks).asJava)) + waitForAssignments(partitionList.reverse.map(p => Set(new TopicPartition(topicWithAllPartitionsOnAllRacks, p), new TopicPartition(topicWithSingleRackPartitions, p)))) Review Comment: No, we don't do any leader-specific assignment, we are assuming all replicas are equal. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: jira-unsubscr...@kafka.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org