Skip to content
Open
Show file tree
Hide file tree
Changes from 13 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
@@ -0,0 +1,43 @@
/*
* 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.api.streams.assignor;

import org.apache.kafka.common.annotation.InterfaceAudience;
import org.apache.kafka.common.annotation.InterfaceStability;

import java.util.List;

/**
* The assignment configurations that the group coordinator passes to the task assignor.
*
* <p>This interface is not intended to be implemented by task assignors: new configurations may be added to it.
*/
@InterfaceAudience.Public
@InterfaceStability.Evolving
public interface AssignmentConfigs {

/**
* @return The number of standby replicas for each task.
*/
int numStandbyReplicas();

/**
* @return The client tags used to distribute standby tasks across racks. The list is unmodifiable.
*/
List<String> rackAwareAssignmentTags();

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.apache.kafka.common.annotation.InterfaceStability;

import java.util.Collection;
import java.util.Map;

/**
* The group metadata specifications required to compute the target assignment.
Expand Down Expand Up @@ -51,8 +50,8 @@ public interface GroupSpec {
MemberAssignmentState memberAssignmentState(String memberId);

/**
* @return Any configurations passed to the assignor. The map is unmodifiable.
* @return The assignment configurations passed to the assignor.
*/
Map<String, String> configs();
AssignmentConfigs configs();

}
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,8 @@
import static org.apache.kafka.coordinator.group.streams.StreamsCoordinatorRecordHelpers.newStreamsGroupTargetAssignmentTombstoneRecord;
import static org.apache.kafka.coordinator.group.streams.StreamsCoordinatorRecordHelpers.newStreamsGroupTopologyRecord;
import static org.apache.kafka.coordinator.group.streams.StreamsGroupMember.hasAssignedTasksChanged;
import static org.apache.kafka.coordinator.group.streams.assignor.AssignmentConfigsImpl.NUM_STANDBY_REPLICAS_CONFIG;
import static org.apache.kafka.coordinator.group.streams.assignor.AssignmentConfigsImpl.RACK_AWARE_ASSIGNMENT_TAGS_CONFIG;


/**
Expand Down Expand Up @@ -2397,7 +2399,7 @@ private CoordinatorResult<StreamsGroupHeartbeatResult, CoordinatorRecord> stream
)
));

String rackAwareTagsValue = currentAssignmentConfigs.getOrDefault("rack.aware.assignment.tags", "").trim();
String rackAwareTagsValue = currentAssignmentConfigs.getOrDefault(RACK_AWARE_ASSIGNMENT_TAGS_CONFIG, "").trim();
// The MISSING_CLIENT_TAGS status (code 6) requires version 1 of the RPC: version 0 clients
// throw on unknown status codes, so it must not be sent to them.
if (requestApiVersion >= 1 && !rackAwareTagsValue.isEmpty()) {
Expand Down Expand Up @@ -9914,9 +9916,9 @@ private Map<String, String> streamsGroupAssignmentConfigs(String groupId) {
final List<String> rackAwareAssignmentTags = groupConfig.flatMap(GroupConfig::streamsRackAwareAssignmentTags)
.orElse(config.streamsGroupRackAwareAssignmentTags());
Map<String, String> configs = new TreeMap<>();
configs.put("num.standby.replicas", numStandbyReplicas.toString());
configs.put(NUM_STANDBY_REPLICAS_CONFIG, numStandbyReplicas.toString());
if (!rackAwareAssignmentTags.isEmpty()) {
configs.put("rack.aware.assignment.tags", String.join(",", rackAwareAssignmentTags));
configs.put(RACK_AWARE_ASSIGNMENT_TAGS_CONFIG, String.join(",", rackAwareAssignmentTags));
}
return configs;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
import org.apache.kafka.common.utils.Time;
import org.apache.kafka.coordinator.common.runtime.CoordinatorMetadataImage;
import org.apache.kafka.coordinator.common.runtime.CoordinatorRecord;
import org.apache.kafka.coordinator.group.api.streams.assignor.AssignmentConfigs;
import org.apache.kafka.coordinator.group.api.streams.assignor.GroupAssignment;
import org.apache.kafka.coordinator.group.api.streams.assignor.MemberAssignment;
import org.apache.kafka.coordinator.group.api.streams.assignor.TaskAssignor;
import org.apache.kafka.coordinator.group.api.streams.assignor.TaskAssignorException;
import org.apache.kafka.coordinator.group.streams.assignor.AssignmentConfigsImpl;
import org.apache.kafka.coordinator.group.streams.assignor.GroupSpecImpl;
import org.apache.kafka.coordinator.group.streams.assignor.MemberMetadataAndStateImpl;
import org.apache.kafka.coordinator.group.streams.topics.ConfiguredTopology;
Expand Down Expand Up @@ -70,7 +72,7 @@ public class TargetAssignmentBuilder {
/**
* The assignment configs.
*/
private final Map<String, String> assignmentConfigs;
private final AssignmentConfigs assignmentConfigs;

/**
* The members in the group.
Expand Down Expand Up @@ -114,7 +116,7 @@ public TargetAssignmentBuilder(
this.groupId = Objects.requireNonNull(groupId);
this.groupEpoch = groupEpoch;
this.assignor = Objects.requireNonNull(assignor);
this.assignmentConfigs = Objects.requireNonNull(assignmentConfigs);
this.assignmentConfigs = AssignmentConfigsImpl.fromMap(Objects.requireNonNull(assignmentConfigs));
}

static MemberMetadataAndStateImpl createMemberMetadataAndState(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* 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 org.apache.kafka.coordinator.group.GroupCoordinatorConfig;
import org.apache.kafka.coordinator.group.api.streams.assignor.AssignmentConfigs;

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

/**
* The assignment configurations for a streams group.
*
* @param numStandbyReplicas The number of standby replicas for each task.
* @param rackAwareAssignmentTags The client tags used to distribute standby tasks across racks.
*/
public record AssignmentConfigsImpl(
int numStandbyReplicas,
List<String> rackAwareAssignmentTags
) implements AssignmentConfigs {

// The names under which the configurations are passed to the assignor and recorded for the group.
public static final String NUM_STANDBY_REPLICAS_CONFIG = "num.standby.replicas";
public static final String RACK_AWARE_ASSIGNMENT_TAGS_CONFIG = "rack.aware.assignment.tags";

/**
* The configs of a group that has none of them set, holding the default value of every configuration.
*/
public static final AssignmentConfigsImpl DEFAULT = new AssignmentConfigsImpl(
GroupCoordinatorConfig.STREAMS_GROUP_NUM_STANDBY_REPLICAS_DEFAULT,
parseRackAwareAssignmentTags(GroupCoordinatorConfig.STREAMS_GROUP_RACK_AWARE_ASSIGNMENT_TAGS_DEFAULT)
);

public AssignmentConfigsImpl {
// The list is exposed to a custom assignor through the public AssignmentConfigs interface.
rackAwareAssignmentTags = List.copyOf(Objects.requireNonNull(rackAwareAssignmentTags));
}

/**
* Converts the raw assignment configs computed for the group into the typed configs passed to the assignor.
*/
public static AssignmentConfigsImpl fromMap(Map<String, String> configs) {
// The map is empty when it was replayed from a group metadata record written before the last assignment
// configs were persisted.
if (configs.isEmpty()) {
return DEFAULT;
}
// The rack-aware assignment tags are only recorded when any are configured, so an absent value means the
// configuration is at its default.
String rackAwareAssignmentTags = configs.getOrDefault(
RACK_AWARE_ASSIGNMENT_TAGS_CONFIG, GroupCoordinatorConfig.STREAMS_GROUP_RACK_AWARE_ASSIGNMENT_TAGS_DEFAULT);
return new AssignmentConfigsImpl(
Integer.parseInt(configs.get(NUM_STANDBY_REPLICAS_CONFIG)),

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.

Don't we need null check here and translate it to 0 ?

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.

In the original code, for replicas, there are two path, one is the whole map is empty, then it's ok to not having replicas set and here we return DEFAULT, other than that, the replicas should always be in the config

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.

Ok. Seems we are scattered logic, making it hard to reason about this... No critical for 4.4 release, but wondering if we could do a follow up cleanup PR, streamlining this a little bit better to make it easier as a (human 🤣) reviewer to follow...

Maybe we should "unify" the raw Map<String, String> and AssignmentConfigsImpl somehow, to have a single object we use and pass around, an avoid converting the one into the other multiple times along the way, at different places.

parseRackAwareAssignmentTags(rackAwareAssignmentTags)
);
}

/**
* Parses a recorded rack-aware assignment tags value, the way {@link org.apache.kafka.common.config.ConfigDef}
* parses a {@code LIST} configuration: an empty value is an empty list, not a list holding an empty string.
*/
private static List<String> parseRackAwareAssignmentTags(String rackAwareAssignmentTags) {
return rackAwareAssignmentTags.isEmpty() ? List.of() : List.of(rackAwareAssignmentTags.split(","));

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.

isEmpty() checks for length() == 0, right? What about " " (or similar). Do we need to to do rackAwareAssignmentTags.trim().isEmpty()` ?

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.

the input of this will be the output of String.join(",", tags), in this case, there won't be " ". But I do feel like here the type has been a bit confusing, I will submit a follow up pr to clean up those things

}

/**
* Returns these configs with the number of standby replicas replaced.
*/
public AssignmentConfigsImpl withNumStandbyReplicas(int numStandbyReplicas) {
return new AssignmentConfigsImpl(numStandbyReplicas, rackAwareAssignmentTags);
}

/**
* Returns these configs with the rack-aware assignment tags replaced.
*/
public AssignmentConfigsImpl withRackAwareAssignmentTags(List<String> rackAwareAssignmentTags) {
return new AssignmentConfigsImpl(numStandbyReplicas, rackAwareAssignmentTags);
}
}
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 org.apache.kafka.coordinator.group.api.streams.assignor.AssignmentConfigs;
import org.apache.kafka.coordinator.group.api.streams.assignor.GroupSpec;
import org.apache.kafka.coordinator.group.api.streams.assignor.MemberAssignmentMetadata;
import org.apache.kafka.coordinator.group.api.streams.assignor.MemberAssignmentState;
Expand All @@ -30,17 +31,17 @@
*
* @param members The member metadata keyed by member Id. Each value provides both the
* {@link MemberAssignmentMetadata} and the {@link MemberAssignmentState} for the member.
* @param configs Any configurations passed to the assignor.
* @param configs The assignment configurations passed to the assignor.
*/
public record GroupSpecImpl(
Map<String, MemberMetadataAndStateImpl> members,
Map<String, String> configs
AssignmentConfigs configs
) implements GroupSpec {

public GroupSpecImpl {
// Both maps are exposed to a custom assignor through the public GroupSpec interface.
// The map is exposed to a custom assignor through the public GroupSpec interface.
members = Collections.unmodifiableMap(Objects.requireNonNull(members));
configs = Collections.unmodifiableMap(Objects.requireNonNull(configs));
Objects.requireNonNull(configs);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,7 @@ private static LinkedList<TaskId> taskIds(final TopologyDescriber topologyDescri

private static LocalState initialize(final GroupSpec groupSpec, final TopologyDescriber topologyDescriber) {
final LocalState localState = new LocalState();
localState.numStandbyReplicas =
groupSpec.configs().isEmpty() ? 0
: Integer.parseInt(groupSpec.configs().get("num.standby.replicas"));
localState.numStandbyReplicas = groupSpec.configs().numStandbyReplicas();

// Helpers for computing active tasks per member, and tasks per member
localState.totalActiveTasks = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@
import org.apache.kafka.coordinator.group.streams.TaskAssignmentTestUtil.TaskRole;
import org.apache.kafka.coordinator.group.streams.TasksTuple;
import org.apache.kafka.coordinator.group.streams.TasksTupleWithEpochs;
import org.apache.kafka.coordinator.group.streams.assignor.AssignmentConfigsImpl;
import org.apache.kafka.image.MetadataDelta;
import org.apache.kafka.image.MetadataImage;
import org.apache.kafka.image.MetadataProvenance;
Expand Down Expand Up @@ -186,7 +187,6 @@
import java.util.Optional;
import java.util.Properties;
import java.util.Set;
import java.util.TreeMap;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.function.BiFunction;
Expand Down Expand Up @@ -227,6 +227,7 @@
import static org.apache.kafka.coordinator.group.GroupMetadataManagerTestContext.DEFAULT_CLIENT_ADDRESS;
import static org.apache.kafka.coordinator.group.GroupMetadataManagerTestContext.DEFAULT_CLIENT_ID;
import static org.apache.kafka.coordinator.group.GroupMetadataManagerTestContext.DEFAULT_PROCESS_ID;
import static org.apache.kafka.coordinator.group.StreamsGroupTestUtil.getDefaultAssignmentConfigs;
import static org.apache.kafka.coordinator.group.Utils.computeGroupHash;
import static org.apache.kafka.coordinator.group.Utils.computeTopicHash;
import static org.apache.kafka.coordinator.group.Utils.toAssignmentWithEpochs;
Expand Down Expand Up @@ -23654,7 +23655,7 @@ public void testStreamsGroupDynamicConfigs() {
.setWarmupTasks(List.of()));
assertEquals(2, result.response().data().memberEpoch());
assertEquals(
getDefaultAssignmentConfigs(),
AssignmentConfigsImpl.DEFAULT,
assignor.lastPassedAssignmentConfigs()
);

Expand Down Expand Up @@ -23693,7 +23694,7 @@ public void testStreamsGroupDynamicConfigs() {

// Verify that the new number of standby replicas is used
assertEquals(
Map.of("num.standby.replicas", "2"),
AssignmentConfigsImpl.DEFAULT.withNumStandbyReplicas(2),
assignor.lastPassedAssignmentConfigs()
);

Expand Down Expand Up @@ -24041,7 +24042,7 @@ public void testStreamsGroupEvaluatedConfigs() {
context.assertSessionTimeout(groupId, memberId,
GroupCoordinatorConfig.STREAMS_GROUP_SESSION_TIMEOUT_MS_DEFAULT);
assertEquals(
getDefaultAssignmentConfigs(),
AssignmentConfigsImpl.DEFAULT,
assignor.lastPassedAssignmentConfigs());
assertEquals(GroupCoordinatorConfig.STREAMS_GROUP_TASK_OFFSET_INTERVAL_MS_DEFAULT,
result.response().data().taskOffsetIntervalMs());
Expand Down Expand Up @@ -24081,7 +24082,7 @@ public void testStreamsGroupEvaluatedConfigs() {
// Verify that the number of standby replicas is evaluated to max,
// and task offset interval is evaluated to min
assertEquals(
Map.of("num.standby.replicas", String.valueOf(GroupCoordinatorConfig.STREAMS_GROUP_MAX_STANDBY_REPLICAS_DEFAULT)),
AssignmentConfigsImpl.DEFAULT.withNumStandbyReplicas(GroupCoordinatorConfig.STREAMS_GROUP_MAX_STANDBY_REPLICAS_DEFAULT),
assignor.lastPassedAssignmentConfigs());
assertEquals(GroupCoordinatorConfig.STREAMS_GROUP_MIN_TASK_OFFSET_INTERVAL_MS_DEFAULT,
result.response().data().taskOffsetIntervalMs());
Expand Down Expand Up @@ -30350,15 +30351,4 @@ private Map<Uuid, DeleteShareGroupOffsetsResponseData.DeleteShareGroupOffsetsRes
return responseTopics.stream()
.collect(Collectors.toMap(DeleteShareGroupOffsetsResponseData.DeleteShareGroupOffsetsResponseTopic::topicId, Function.identity()));
}

/**
* Returns the default assignment configurations that would be used by the system.
* This matches what streamsGroupAssignmentConfigs() would return.
*/
private Map<String, String> getDefaultAssignmentConfigs() {
// Use the same default value as GroupCoordinatorConfig.STREAMS_GROUP_NUM_STANDBY_REPLICAS_DEFAULT
return new TreeMap<>(Map.of(
"num.standby.replicas", String.valueOf(GroupCoordinatorConfig.STREAMS_GROUP_NUM_STANDBY_REPLICAS_DEFAULT)
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.kafka.coordinator.group.streams.TaskAssignmentTestUtil;
import org.apache.kafka.coordinator.group.streams.TasksTuple;
import org.apache.kafka.coordinator.group.streams.TasksTupleWithEpochs;
import org.apache.kafka.coordinator.group.streams.assignor.AssignmentConfigsImpl;

import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -71,9 +72,9 @@ static StreamsGroupMember.Builder streamsGroupMemberBuilderWithDefaults(String m
* This matches what streamsGroupAssignmentConfigs() would return.
*/
static Map<String, String> getDefaultAssignmentConfigs() {
// Use the same default value as GroupCoordinatorConfig.STREAMS_GROUP_NUM_STANDBY_REPLICAS_DEFAULT
return new TreeMap<>(Map.of(
"num.standby.replicas", String.valueOf(GroupCoordinatorConfig.STREAMS_GROUP_NUM_STANDBY_REPLICAS_DEFAULT)
AssignmentConfigsImpl.NUM_STANDBY_REPLICAS_CONFIG,
String.valueOf(AssignmentConfigsImpl.DEFAULT.numStandbyReplicas())
));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
*/
package org.apache.kafka.coordinator.group.streams;

import org.apache.kafka.coordinator.group.api.streams.assignor.AssignmentConfigs;
import org.apache.kafka.coordinator.group.api.streams.assignor.GroupAssignment;
import org.apache.kafka.coordinator.group.api.streams.assignor.GroupSpec;
import org.apache.kafka.coordinator.group.api.streams.assignor.MemberAssignment;
import org.apache.kafka.coordinator.group.api.streams.assignor.TaskAssignor;
import org.apache.kafka.coordinator.group.api.streams.assignor.TaskAssignorException;
import org.apache.kafka.coordinator.group.api.streams.assignor.TopologyDescriber;
import org.apache.kafka.coordinator.group.streams.assignor.AssignmentConfigsImpl;

import java.util.Map;
import java.util.Map.Entry;
Expand All @@ -31,7 +33,7 @@ public class MockTaskAssignor implements TaskAssignor {

private final String name;
private GroupAssignment preparedGroupAssignment = null;
private Map<String, String> assignmentConfigs = Map.of();
private AssignmentConfigs assignmentConfigs = AssignmentConfigsImpl.DEFAULT;

public MockTaskAssignor(String name) {
this.name = name;
Expand All @@ -53,7 +55,7 @@ public void prepareGroupAssignment(Map<String, TasksTuple> memberAssignments) {
})));
}

public Map<String, String> lastPassedAssignmentConfigs() {
public AssignmentConfigs lastPassedAssignmentConfigs() {
return assignmentConfigs;
}

Expand Down
Loading
Loading