-
Notifications
You must be signed in to change notification settings - Fork 15.4k
KAFKA-20790: add AssignmentConfigs interface #23092
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
base: trunk
Are you sure you want to change the base?
Changes from 13 commits
7a09a49
bad3070
f5324e2
63259d4
74c97bc
2117499
5016b90
a1fae8b
0bf596c
fd18f5f
3a90c24
e9549a4
347c5a7
5407888
897f454
73c22c9
261257d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 |
|---|---|---|
| @@ -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)), | ||
| 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(",")); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't we need
nullcheck here and translate it to0?There was a problem hiding this comment.
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
There was a problem hiding this comment.
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>andAssignmentConfigsImplsomehow, 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.