Skip to content
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

KAFKA-17125: Streams Sticky Task Assignor #18652

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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 @@ -16,6 +16,7 @@
*/
package org.apache.kafka.coordinator.group.streams.assignor;

import java.util.List;
import java.util.Map;

/**
Expand All @@ -28,6 +29,11 @@ public interface GroupSpec {
*/
Map<String, AssignmentMemberSpec> members();

/**
* @return The list of subtopologies.
*/
List<String> subtopologies();

/**
* @return Any configurations passed to the assignor.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.kafka.coordinator.group.streams.assignor;

import java.util.List;
import java.util.Map;
import java.util.Objects;

Expand All @@ -26,10 +27,12 @@
* @param assignmentConfigs Any configurations passed to the assignor.
*/
public record GroupSpecImpl(Map<String, AssignmentMemberSpec> members,
List<String> subtopologies,
Map<String, String> assignmentConfigs) implements GroupSpec {

public GroupSpecImpl {
Objects.requireNonNull(members);
Objects.requireNonNull(subtopologies);
Objects.requireNonNull(assignmentConfigs);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.kafka.coordinator.group.streams.assignor;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import static java.util.Collections.unmodifiableSet;
import static org.apache.kafka.common.utils.Utils.union;

public class ProcessState {
private final String processId;
// number of members
private int capacity;
Copy link
Member

Choose a reason for hiding this comment

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

nit: why not just make it numMembersCapacity although it's fine as is.

private double load;
private final Map<String, Integer> memberToTaskCounts;
private final Map<String, Set<TaskId>> assignedActiveTasks;
private final Map<String, Set<TaskId>> assignedStandbyTasks;


ProcessState(final String processId) {
this.processId = processId;
this.capacity = 0;
this.load = Double.MAX_VALUE;
this.assignedActiveTasks = new HashMap<>();
this.assignedStandbyTasks = new HashMap<>();
Copy link
Member

Choose a reason for hiding this comment

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

So we're not going to consider warm-up tasks as part of the load?

this.memberToTaskCounts = new HashMap<>();
}


public String processId() {
return processId;
}

public int capacity() {
return capacity;
}

public int totalTaskCount() {
return assignedStandbyTasks().size() + assignedActiveTasks().size();
}

public double load() {
return load;
}

public Map<String, Integer> memberToTaskCounts() {
return memberToTaskCounts;
}

public Set<TaskId> assignedActiveTasks() {
return assignedActiveTasks.values().stream()
.flatMap(Set::stream)
.collect(Collectors.toSet());
}

public Map<String, Set<TaskId>> assignedActiveTasksByMember() {
return assignedActiveTasks;
}

public Set<TaskId> assignedStandbyTasks() {
return assignedStandbyTasks.values().stream()
.flatMap(Set::stream)
.collect(Collectors.toSet());
}

public Map<String, Set<TaskId>> assignedStandbyTasksByMember() {
return assignedStandbyTasks;
}

public void addTask(final String memberId, final TaskId taskId, final boolean isActive) {
if (isActive) {
assignedActiveTasks.putIfAbsent(memberId, new HashSet<>());
assignedActiveTasks.get(memberId).add(taskId);
} else {
assignedStandbyTasks.putIfAbsent(memberId, new HashSet<>());
assignedStandbyTasks.get(memberId).add(taskId);
}
memberToTaskCounts.put(memberId, memberToTaskCounts.get(memberId) + 1);
computeLoad();
}

private void incrementCapacity() {
capacity++;
computeLoad();
}
public void computeLoad() {
if (capacity <= 0) {
this.load = -1;
} else {
this.load = (double) totalTaskCount() / capacity;
}
}

public void addMember(final String member) {
this.memberToTaskCounts.put(member, 0);
incrementCapacity();
}

public boolean hasCapacity() {
return totalTaskCount() < capacity;
}

public int compareTo(final ProcessState other) {
int loadCompare = Double.compare(this.load, other.load());
if (loadCompare == 0) {
return Integer.compare(other.capacity, this.capacity);
}
return loadCompare;
}

public boolean hasTask(final TaskId taskId) {
return assignedActiveTasks().contains(taskId) || assignedStandbyTasks().contains(taskId); }


Set<TaskId> assignedTasks() {
final Set<TaskId> assignedActiveTaskIds = assignedActiveTasks();
final Set<TaskId> assignedStandbyTaskIds = assignedStandbyTasks();
return unmodifiableSet(
union(
() -> new HashSet<>(assignedActiveTaskIds.size() + assignedStandbyTaskIds.size()),
assignedActiveTaskIds,
assignedStandbyTaskIds
)
);
}
}
Loading
Loading