Skip to content

KAFKA-19362: Finalize homogeneous simple share assignor #19977

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jun 20, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,10 @@ public GroupAssignment build() {

revokeUnassignablePartitions();

revokeOversharedPartitions();

revokeOverfilledMembers();

revokeOversharedPartitions();

// Add in any partitions which are currently not in the assignment.
targetPartitions.forEach(topicPartition -> finalAssignmentByPartition.computeIfAbsent(topicPartition, k -> new HashSet<>()));

Expand Down Expand Up @@ -274,7 +274,32 @@ private void revokeUnassignablePartitions() {
}

/**
* Revoke any over-shared partitions. Prefer to revoke from overfilled members first.
* Revoke partitions from members which are overfilled.
*/
private void revokeOverfilledMembers() {
if (overfilledMembers.isEmpty())
return;

overfilledMembers.forEach(memberIndex -> {
int memberDesiredAssignmentCount = desiredAssignmentCount[memberIndex];
Set<TopicIdPartition> memberFinalAssignment = finalAssignmentByMember.get(memberIndex);
if (memberFinalAssignment.size() > memberDesiredAssignmentCount) {
Iterator<TopicIdPartition> iterator = memberFinalAssignment.iterator();
while (iterator.hasNext()) {
TopicIdPartition topicPartition = iterator.next();
newGroupAssignment.get(memberIndex).get(topicPartition.topicId()).remove(topicPartition.partitionId());
finalAssignmentByPartition.get(topicPartition).remove(memberIndex);
iterator.remove();
if (memberFinalAssignment.size() == memberDesiredAssignmentCount) {
break;
}
}
}
});
}

/**
* Revoke any over-shared partitions.
*/
private void revokeOversharedPartitions() {
finalAssignmentByPartition.forEach((topicPartition, assignedMembers) -> {
Expand All @@ -283,8 +308,13 @@ private void revokeOversharedPartitions() {
Iterator<Integer> assignedMemberIterator = assignedMembers.iterator();
while (assignedMemberIterator.hasNext()) {
Integer memberIndex = assignedMemberIterator.next();
if (overfilledMembers.contains(memberIndex)) {
Set<Integer> partitions = newGroupAssignment.get(memberIndex).get(topicPartition.topicId());
Map<Uuid, Set<Integer>> newMemberAssignment = newGroupAssignment.get(memberIndex);
if (newMemberAssignment == null) {
newMemberAssignment = AssignorHelpers.deepCopyAssignment(oldGroupAssignment.get(memberIndex));
newGroupAssignment.put(memberIndex, newMemberAssignment);
}
Set<Integer> partitions = newMemberAssignment.get(topicPartition.topicId());
if (partitions != null) {
if (partitions.remove(topicPartition.partitionId())) {
assignedMemberCount--;
assignedMemberIterator.remove();
Expand All @@ -297,55 +327,6 @@ private void revokeOversharedPartitions() {
}
}
}
if (assignedMemberCount > desiredSharing) {
Iterator<Integer> assignedMemberIterator = assignedMembers.iterator();
while (assignedMemberIterator.hasNext()) {
Integer memberIndex = assignedMemberIterator.next();
if (!overfilledMembers.contains(memberIndex)) {
Map<Uuid, Set<Integer>> newMemberAssignment = newGroupAssignment.get(memberIndex);
if (newMemberAssignment == null) {
newMemberAssignment = AssignorHelpers.deepCopyAssignment(oldGroupAssignment.get(memberIndex));
newGroupAssignment.put(memberIndex, newMemberAssignment);
}
Set<Integer> partitions = newMemberAssignment.get(topicPartition.topicId());
if (partitions != null) {
if (partitions.remove(topicPartition.partitionId())) {
assignedMemberCount--;
assignedMemberIterator.remove();
finalAssignmentByMember.get(memberIndex).remove(topicPartition);
unfilledMembers.add(memberIndex);
}
}
}
if (assignedMemberCount <= desiredSharing) {
break;
}
}
}
});
}

/**
* Revoke partitions from members which are still overfilled.
*/
private void revokeOverfilledMembers() {
if (overfilledMembers.isEmpty())
return;

overfilledMembers.forEach(memberIndex -> {
int memberDesiredAssignmentCount = desiredAssignmentCount[memberIndex];
Set<TopicIdPartition> memberFinalAssignment = finalAssignmentByMember.get(memberIndex);
if (memberFinalAssignment.size() > memberDesiredAssignmentCount) {
Iterator<TopicIdPartition> iterator = memberFinalAssignment.iterator();
while (iterator.hasNext()) {
TopicIdPartition topicPartition = iterator.next();
finalAssignmentByPartition.get(topicPartition).remove(memberIndex);
iterator.remove();
if (memberFinalAssignment.size() == memberDesiredAssignmentCount) {
break;
}
}
}
});
}

Expand Down