Skip to content

MINOR: add test for kafka-consumer-groups.sh should not fail when partition offline#20235

Merged
AndrewJSchofield merged 19 commits into
apache:trunkfrom
TaiJuWu:fix_leaderNotFind
Jul 31, 2025
Merged

MINOR: add test for kafka-consumer-groups.sh should not fail when partition offline#20235
AndrewJSchofield merged 19 commits into
apache:trunkfrom
TaiJuWu:fix_leaderNotFind

Conversation

@TaiJuWu

@TaiJuWu TaiJuWu commented Jul 24, 2025

Copy link
Copy Markdown
Collaborator

See: #20168 (comment)

add follow test case:

Given a topic with three partitions, where partition t-2 is offline,
if partitionsToReset contains only t-1, the method
filterNoneLeaderPartitions incorrectly includes t-2 in the result,
leading to a failure in the tool.

Reviewers: Chia-Ping Tsai chia7712@gmail.com, Jhen-Yung Hsu
jhenyunghsu@gmail.com, Ken Huang s7133700@gmail.com, Andrew
Schofield aschofield@confluent.io

@AndrewJSchofield AndrewJSchofield left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR. A couple of small comments.

cluster.createTopic(topic, 3, (short) 2);

try (ConsumerGroupCommand.ConsumerGroupService service = getConsumerGroupService(args);
Admin admin = cluster.admin()) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: The indentation here is a bit strange not being a multiple of 4 spaces. I wonder whether just concatenating this on the end of the previous line would be simplest.

@TaiJuWu TaiJuWu Jul 24, 2025

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change to
try (Admin admin = cluster.admin(); ConsumerGroupCommand.ConsumerGroupService service = getConsumerGroupService(args))

}
}

@ClusterTest(brokers = 5)

@Yunyung Yunyung Jul 24, 2025

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could just use 2 brokers for this test? 5 is overkill.

@TaiJuWu TaiJuWu Jul 24, 2025

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for review.
I use 3 broker as default since we need to ensure __consumere_offset always have two brokers in any condition and we need to shutdown a broker.

If the replica of __consumer_offset is 1, we can't sure the leader is not shutdown broker.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can two brokers with two replicas fix the issue you mentioned?

@TaiJuWu TaiJuWu Jul 25, 2025

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change to two brokers and explicitly define min_insyn_replica equals one.

Yunyung

This comment was marked as duplicate.

}

@ClusterTest(brokers = 5)
public void testResetOffsetsWithOfflinePartition(ClusterInstance cluster) throws Exception {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To better distinguish this test from testResetOffsetsWithPartitionNoneLeader, maybe rename it to testResetOffsetsWithOfflinePartitionNotInResetTarget or something similar.

@Yunyung Yunyung left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the updates. Left two comments, one here: https://github.com/apache/kafka/pull/20168/files#r2228628205

.stream()
.flatMap(entry -> entry.getValue().partitions().stream()
.filter(partitionInfo -> partitionInfo.leader() == null)
.filter(partitionInfo -> topicPartitions.contains(new TopicPartition(entry.getKey(), partitionInfo.partition())) && partitionInfo.leader() == null)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: check partitionInfo.leader() == null before creating the TopicPartition for performance.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, thanks.

cluster.waitTopicCreation(topic, 2);

cluster.shutdownBroker(1);
TestUtils.waitForCondition(() -> !cluster.aliveBrokers().containsKey(1),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to https://github.com/apache/kafka/pull/20168/files#r2228677312, I understand that shutdown() already waits until the shutdown is complete. So this can be removed as well.

@chia7712 chia7712 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TaiJuWu thanks for this patch

.flatMap(entry -> entry.getValue().partitions().stream()
.filter(partitionInfo -> partitionInfo.leader() == null)
.filter(partitionInfo -> partitionInfo.leader() == null && topicPartitions.contains(new TopicPartition(entry.getKey(), partitionInfo.partition())))
.map(partitionInfo -> new TopicPartition(entry.getKey(), partitionInfo.partition())))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add the filter after map to avoid creating extra TopicPartition object.

                                .filter(partitionInfo -> partitionInfo.leader() == null)
                                .map(partitionInfo -> new TopicPartition(entry.getKey(), partitionInfo.partition()))
                                .filter(topicPartitions::contains))

}

@ClusterTest(
brokers = 3,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems two brokers could work well for this test, right?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is the reason I selected three brokers instead of two.
#20235 (comment)

@github-actions github-actions Bot removed the triage PRs from the community label Jul 25, 2025
Comment on lines +161 to +162
@ClusterConfigProperty(key = OFFSETS_TOPIC_REPLICATION_FACTOR_CONFIG, value = "2"),
@ClusterConfigProperty(key = TopicConfig.MIN_IN_SYNC_REPLICAS_CONFIG, value = "1")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need to set these properties? We can reproduce the failure without setting them.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These settings can avoid flaky.
In my mind, if __consumer_offset is at broker 1 and we shutdown it, we don't have any __consumer_offset.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The min.insync.replicas default value is 1, so we don't need to configure it explicitly?

@github-actions github-actions Bot added the tests Test fixes (including flaky tests) label Jul 30, 2025
@TaiJuWu TaiJuWu changed the title MINOR: kafka-consumer-groups.sh should not fail when partition offline MINOR: add test for kafka-consumer-groups.sh should not fail when partition offline Jul 30, 2025
@chia7712

Copy link
Copy Markdown
Member

@TaiJuWu please fix the build error

* What went wrong:
Execution failed for task ':tools:spotlessJavaCheck'.
> The following files had format violations:
      src/test/java/org/apache/kafka/tools/consumer/group/ResetConsumerGroupOffsetTest.java
          @@ -27,7 +27,6 @@
           import·org.apache.kafka.clients.producer.ProducerRecord;
           import·org.apache.kafka.common.GroupState;
           import·org.apache.kafka.common.TopicPartition;
          -import·org.apache.kafka.common.config.TopicConfig;
           import·org.apache.kafka.common.errors.LeaderNotAvailableException;
           import·org.apache.kafka.common.errors.UnknownTopicOrPartitionException;
           import·org.apache.kafka.common.serialization.ByteArraySerializer;
  Run './gradlew :tools:spotlessApply' to fix these violations.

@TaiJuWu

TaiJuWu commented Jul 30, 2025

Copy link
Copy Markdown
Collaborator Author

@TaiJuWu please fix the build error

* What went wrong:
Execution failed for task ':tools:spotlessJavaCheck'.
> The following files had format violations:
      src/test/java/org/apache/kafka/tools/consumer/group/ResetConsumerGroupOffsetTest.java
          @@ -27,7 +27,6 @@
           import·org.apache.kafka.clients.producer.ProducerRecord;
           import·org.apache.kafka.common.GroupState;
           import·org.apache.kafka.common.TopicPartition;
          -import·org.apache.kafka.common.config.TopicConfig;
           import·org.apache.kafka.common.errors.LeaderNotAvailableException;
           import·org.apache.kafka.common.errors.UnknownTopicOrPartitionException;
           import·org.apache.kafka.common.serialization.ByteArraySerializer;
  Run './gradlew :tools:spotlessApply' to fix these violations.

Thanks for remaining me!

JimmyWang6 added a commit to JimmyWang6/jimmy-KAFKA-14048 that referenced this pull request Jul 31, 2025
JimmyWang6 added a commit to JimmyWang6/jimmy-KAFKA-14048 that referenced this pull request Jul 31, 2025
JimmyWang6 added a commit to JimmyWang6/jimmy-KAFKA-14048 that referenced this pull request Jul 31, 2025

@AndrewJSchofield AndrewJSchofield left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR. Looks good to me.

@AndrewJSchofield
AndrewJSchofield merged commit dfaf9f9 into apache:trunk Jul 31, 2025
23 checks passed
@TaiJuWu
TaiJuWu deleted the fix_leaderNotFind branch July 31, 2025 15:17
airlock-confluentinc Bot pushed a commit to confluentinc/kafka that referenced this pull request Aug 6, 2025
…artition offline (apache#20235)

See: apache#20168 (comment)

add follow test case:

Given a topic with three partitions, where partition `t-2` is offline,
if partitionsToReset contains only `t-1`, the method
filterNoneLeaderPartitions incorrectly includes `t-2` in the result,
leading to a failure in the tool.

Reviewers: Chia-Ping Tsai <chia7712@gmail.com>, Jhen-Yung Hsu
 <jhenyunghsu@gmail.com>, Ken Huang <s7133700@gmail.com>, Andrew
 Schofield <aschofield@confluent.io>
k-apol pushed a commit to k-apol/kafka that referenced this pull request Aug 8, 2025
…artition offline (apache#20235)

See: apache#20168 (comment)

add follow test case:

Given a topic with three partitions, where partition `t-2` is offline,
if partitionsToReset contains only `t-1`, the method
filterNoneLeaderPartitions incorrectly includes `t-2` in the result,
leading to a failure in the tool.

Reviewers: Chia-Ping Tsai <chia7712@gmail.com>, Jhen-Yung Hsu
 <jhenyunghsu@gmail.com>, Ken Huang <s7133700@gmail.com>, Andrew
 Schofield <aschofield@confluent.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

small Small PRs tests Test fixes (including flaky tests) tools

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants