Skip to content

KAFKA-19193 Support rack-aware partitioning for Kafka producer#19850

Merged
viktorsomogyi merged 1 commit intoapache:trunkfrom
ivanyu:kip-1123-rack-aware-partitioning
Apr 22, 2026
Merged

KAFKA-19193 Support rack-aware partitioning for Kafka producer#19850
viktorsomogyi merged 1 commit intoapache:trunkfrom
ivanyu:kip-1123-rack-aware-partitioning

Conversation

@ivanyu
Copy link
Copy Markdown
Contributor

@ivanyu ivanyu commented May 29, 2025

According to
KIP-1123,
this commit adds the support for rack-aware partitioning to
BuiltInPartitioner. It comes with two new configs for the producer:
partitioner.rack.aware and client.rack, which allows enabling the
new behavior.

Apart from the added unit tests, the desired behavior was tested by
kafka-producer-perf-test.sh with an existing and a non-existing rack
against a 4 node cluster with two racks and 12-partition topic:

./kafka_2.13-4.1.0-SNAPSHOT/bin/kafka-producer-perf-test.sh \
  --topic test-topic --num-records 100000 --throughput -1 --record-size
1 \
  --producer-props bootstrap.servers=127.0.0.1:9092 \
  client.rack=rack0 partitioner.rack.aware=true

Reviewers: Viktor Somogyi-Vass viktorsomogyi@gmail.com, Liam
Clarke-Hutchinson liam@steelsky.co.nz

@github-actions github-actions Bot added triage PRs from the community producer clients labels May 29, 2025
@ivanyu ivanyu force-pushed the kip-1123-rack-aware-partitioning branch 11 times, most recently from 1af9189 to 564e60f Compare May 31, 2025 06:14
@ivanyu ivanyu marked this pull request as ready for review May 31, 2025 06:20
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jun 6, 2025

A label of 'needs-attention' was automatically added to this PR in order to raise the
attention of the committers. Once this issue has been triaged, the triage label
should be removed to prevent this automation from happening again.

@marcospassos
Copy link
Copy Markdown

Is there any chance this feature will be included in the next release?

@ivanyu
Copy link
Copy Markdown
Contributor Author

ivanyu commented Aug 22, 2025

@marcospassos I'm waiting for 4.1 release to be cut, after that I'll ping the mailing list for review.

@github-actions
Copy link
Copy Markdown

A label of 'needs-attention' was automatically added to this PR in order to raise the
attention of the committers. Once this issue has been triaged, the triage label
should be removed to prevent this automation from happening again.

@github-actions
Copy link
Copy Markdown

A label of 'needs-attention' was automatically added to this PR in order to raise the
attention of the committers. Once this issue has been triaged, the triage label
should be removed to prevent this automation from happening again.

Copy link
Copy Markdown
Contributor

@LiamClarkeNZ LiamClarkeNZ left a comment

Choose a reason for hiding this comment

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

Love the tests.

private final String rack;

private volatile PartitionLoadStats partitionLoadStats = null;
private volatile PartitionLoadStatsHolder partitionLoadStats = null;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
private volatile PartitionLoadStatsHolder partitionLoadStats = null;
private volatile PartitionLoadStatsHolder partitionLoadStatsHolder = null;

As it got a bit confusing when later the code is retrieving partitionLoadStats.inThisRack which is a member of PartitionLoadStatsHolder of type PartitionLoadStats. :D

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks, renamed


// Create partitions with "sticky" batch size to accommodate 3 records.
BuiltInPartitioner builtInPartitionerA = new SequentialPartitioner(logContext, TOPIC_A, 3);
BuiltInPartitioner builtInPartitionerA = new SequentialPartitioner(logContext, TOPIC_A, 3, false, "");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
BuiltInPartitioner builtInPartitionerA = new SequentialPartitioner(logContext, TOPIC_A, 3, false, "");
final boolean rackAware = false;
final String clientRackId = "";
BuiltInPartitioner builtInPartitionerA = new SequentialPartitioner(logContext, TOPIC_A, 3, rackAware, clientRackId);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done


// Check that switching works even when there is one partition.
BuiltInPartitioner builtInPartitionerB = new SequentialPartitioner(logContext, TOPIC_B, 1);
BuiltInPartitioner builtInPartitionerB = new SequentialPartitioner(logContext, TOPIC_B, 1, false, "");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
BuiltInPartitioner builtInPartitionerB = new SequentialPartitioner(logContext, TOPIC_B, 1, false, "");
BuiltInPartitioner builtInPartitionerB = new SequentialPartitioner(logContext, TOPIC_B, 1, rackAware, clientRackId);

Just to make the meaning of false and "" obvious at a glance :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done

partitionRacks[i] = leader.rack();
}
allPartitions.add(new PartitionInfo(TOPIC_A, i, leader, NODES, NODES));
expectedFrequencies[i] = 6 - queueSizes[i]; // 6 is max(queueSizes) + 1
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Love the comment :)

@github-actions github-actions Bot removed needs-attention triage PRs from the community labels Oct 5, 2025
@github-actions github-actions Bot added the stale Stale PRs label Jan 3, 2026
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Feb 2, 2026

This PR has been closed since it has not had any activity in 120 days. If you feel like this
was a mistake, or you would like to continue working on it, please feel free to re-open the
PR and ask for a review.

@github-actions github-actions Bot added the closed-stale PRs that were closed due to inactivity label Feb 2, 2026
@github-actions github-actions Bot closed this Feb 2, 2026
@SoerenHenning
Copy link
Copy Markdown

Hi, unfortunately this was closed. Are there any plan to continue this effort or anything we could do to support it?

@marcospassos
Copy link
Copy Markdown

This issue would definitely benefit the community and should not be closed.

@kirktrue
Copy link
Copy Markdown
Contributor

@ivanyu—are you still able to work on this? Or do you need the community to follow up? LMK. Thanks!

@ivanyu
Copy link
Copy Markdown
Contributor Author

ivanyu commented Mar 19, 2026

@kirktrue sure, I will review the PR soon!

@ivanyu
Copy link
Copy Markdown
Contributor Author

ivanyu commented Mar 28, 2026

For some reason, I cannot reopen the PR (the button isn't even there). Maybe someone else can?

@viktorsomogyi viktorsomogyi reopened this Mar 30, 2026
@github-actions github-actions Bot added the triage PRs from the community label Mar 30, 2026
@viktorsomogyi viktorsomogyi removed stale Stale PRs closed-stale PRs that were closed due to inactivity labels Mar 30, 2026
@ivanyu ivanyu force-pushed the kip-1123-rack-aware-partitioning branch from 564e60f to 6816a21 Compare March 30, 2026 08:45
@ivanyu
Copy link
Copy Markdown
Contributor Author

ivanyu commented Mar 30, 2026

Thank you @LiamClarkeNZ, I addressed your comments

@viktorsomogyi viktorsomogyi added ci-approved and removed triage PRs from the community labels Mar 30, 2026
@ivanyu ivanyu force-pushed the kip-1123-rack-aware-partitioning branch 3 times, most recently from 1c6c786 to 520eeed Compare March 30, 2026 13:49
@ivanyu ivanyu force-pushed the kip-1123-rack-aware-partitioning branch from 520eeed to 1a47d94 Compare April 2, 2026 06:03
this.rack = rack;

if (rackAware && Utils.isBlank(rack)) {
throw new IllegalArgumentException("client.rack must be provided if partitioner.rack.aware is enabled");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The KIP states:

Select the next partition from all partitions following the current algorithm in the following cases:

  • if the client rack is not specified;
  • if there are no partitions with leaders in the same rack as the client;
  • if all the “nearby” partitions are unavailable.
  • if partitioner.rack.aware is false.

Based on this, is it correct to throw an exception here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think this is OK, for the sake of the user. This exception will be thrown at the producer constructor time, preventing inconsistent config from silently sneaking in.

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.

Should we use ConfigException instead for consistency?

@ivanyu ivanyu force-pushed the kip-1123-rack-aware-partitioning branch from 1a47d94 to 7404aa9 Compare April 21, 2026 07:25
According to KIP-1123, this commit adds the support for rack-aware partitioning to `BuiltInPartitioner`. It comes with two new configs for the producer: `partitioner.rack.aware` and `client.rack`, which allows enabling the new behavior.

Apart from the added unit tests, the desired behavior was validated by `kafka-producer-perf-test.sh` with an existing and a non-existing rack against a 4 node cluster with two racks and 12-partition topic:
```shell
./kafka_2.13-4.1.0-SNAPSHOT/bin/kafka-producer-perf-test.sh \
  --topic test-topic --num-records 100000 --throughput -1 --record-size 1 \
  --producer-props bootstrap.servers=127.0.0.1:9092 \
  client.rack=rack0 partitioner.rack.aware=true
```
@ivanyu ivanyu force-pushed the kip-1123-rack-aware-partitioning branch from 7404aa9 to e8965db Compare April 21, 2026 09:15
Copy link
Copy Markdown
Contributor

@viktorsomogyi viktorsomogyi left a comment

Choose a reason for hiding this comment

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

LGTM, let's merge it when the CI is green.

@ivanyu
Copy link
Copy Markdown
Contributor Author

ivanyu commented Apr 21, 2026

The failed tests are marked as flaky and pass locally

@viktorsomogyi viktorsomogyi merged commit a3f1732 into apache:trunk Apr 22, 2026
34 of 36 checks passed
Copy link
Copy Markdown
Member

@chia7712 chia7712 left a comment

Choose a reason for hiding this comment

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

@ivanyu thanks for driving this patch! I left a few minor suggestions for you. On a related note, have you considered adding a metric like rack-aware-fallback-rate? If it's out of scope for your KIP, I'd gladly draft a follow-up KIP to add it. A silent fallback might lead to an unpleasant surprise for users when they see their cloud networking bills!

this.rack = rack;

if (rackAware && Utils.isBlank(rack)) {
throw new IllegalArgumentException("client.rack must be provided if partitioner.rack.aware is enabled");
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.

Should we use ConfigException instead for consistency?

// Invert and fold the queue size, so that they become separator values in the CFT.
invertAndFoldQueueSizeArray(queueSizes, maxSizePlus1, length);

log.trace("Partition load stats for topic {}: CFT={}, IDs={}, length={}",
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.

Should we include the rack-specific load stats (partitionLoadStatsInThisRack) in this trace log when rackAware is true?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants