clolov commented on code in PR #13538: URL: https://github.com/apache/kafka/pull/13538#discussion_r1175040697
########## group-coordinator/src/main/java/org/apache/kafka/coordinator/group/consumer/ConsumerGroupMember.java: ########## @@ -0,0 +1,613 @@ +/* + * 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.consumer; + +import org.apache.kafka.common.Uuid; +import org.apache.kafka.coordinator.group.generated.ConsumerGroupCurrentMemberAssignmentValue; +import org.apache.kafka.coordinator.group.generated.ConsumerGroupMemberMetadataValue; + +import java.util.Collections; +import java.util.Comparator; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.OptionalInt; +import java.util.Set; +import java.util.stream.Collectors; + +/** + * ConsumerGroupMember contains all the information related to a member + * within a consumer group. This class is immutable and is fully backed + * by records stored in the __consumer_offsets topic. + */ +public class ConsumerGroupMember { + /** + * A builder allowing to create a new member or update an + * existing one. + * + * Please refer to the javadoc of {{@link ConsumerGroupMember}} for the + * definition of the fields. + */ + public static class Builder { + private final String memberId; + private int memberEpoch = 0; + private int previousMemberEpoch = -1; + private int nextMemberEpoch = 0; + private String instanceId = null; + private String rackId = null; + private int rebalanceTimeoutMs = -1; + private String clientId = ""; + private String clientHost = ""; + private List<String> subscribedTopicNames = Collections.emptyList(); + private String subscribedTopicRegex = ""; + private String serverAssignorName = null; + private List<ClientAssignor> clientAssignors = Collections.emptyList(); + private Map<Uuid, Set<Integer>> assignedPartitions = Collections.emptyMap(); + private Map<Uuid, Set<Integer>> partitionsPendingRevocation = Collections.emptyMap(); + private Map<Uuid, Set<Integer>> partitionsPendingAssignment = Collections.emptyMap(); + + public Builder(String memberId) { + this.memberId = Objects.requireNonNull(memberId); + } + + public Builder(ConsumerGroupMember member) { + Objects.requireNonNull(member); + + this.memberId = member.memberId; + this.memberEpoch = member.memberEpoch; + this.previousMemberEpoch = member.previousMemberEpoch; + this.nextMemberEpoch = member.nextMemberEpoch; + this.instanceId = member.instanceId; + this.rackId = member.rackId; + this.rebalanceTimeoutMs = member.rebalanceTimeoutMs; + this.clientId = member.clientId; + this.clientHost = member.clientHost; + this.subscribedTopicNames = member.subscribedTopicNames; + this.subscribedTopicRegex = member.subscribedTopicRegex; + this.serverAssignorName = member.serverAssignorName; + this.clientAssignors = member.clientAssignors; + this.assignedPartitions = member.assignedPartitions; + this.partitionsPendingRevocation = member.partitionsPendingRevocation; + this.partitionsPendingAssignment = member.partitionsPendingAssignment; + } + + public Builder setMemberEpoch(int memberEpoch) { + this.memberEpoch = memberEpoch; + return this; + } + + public Builder setPreviousMemberEpoch(int previousMemberEpoch) { + this.previousMemberEpoch = previousMemberEpoch; + return this; + } + + public Builder setNextMemberEpoch(int nextMemberEpoch) { + this.nextMemberEpoch = nextMemberEpoch; + return this; + } + + public Builder setInstanceId(String instanceId) { + this.instanceId = instanceId; + return this; + } + + public Builder maybeUpdateInstanceId(Optional<String> instanceId) { + this.instanceId = instanceId.orElse(this.instanceId); + return this; + } + + public Builder setRackId(String rackId) { + this.rackId = rackId; + return this; + } + + public Builder maybeUpdateRackId(Optional<String> rackId) { + this.rackId = rackId.orElse(this.rackId); + return this; + } + + public Builder setRebalanceTimeoutMs(int rebalanceTimeoutMs) { + this.rebalanceTimeoutMs = rebalanceTimeoutMs; + return this; + } + + public Builder maybeUpdateRebalanceTimeoutMs(OptionalInt rebalanceTimeoutMs) { + this.rebalanceTimeoutMs = rebalanceTimeoutMs.orElse(this.rebalanceTimeoutMs); + return this; + } + + public Builder setClientId(String clientId) { + this.clientId = clientId; + return this; + } + + public Builder setClientHost(String clientHost) { + this.clientHost = clientHost; + return this; + } + + public Builder setSubscribedTopicNames(List<String> subscribedTopicNames) { + this.subscribedTopicNames = subscribedTopicNames; + this.subscribedTopicNames.sort(Comparator.naturalOrder()); + return this; + } + + public Builder maybeUpdateSubscribedTopicNames(Optional<List<String>> subscribedTopicNames) { + this.subscribedTopicNames = subscribedTopicNames.orElse(this.subscribedTopicNames); + this.subscribedTopicNames.sort(Comparator.naturalOrder()); + return this; + } + + public Builder setSubscribedTopicRegex(String subscribedTopicRegex) { + this.subscribedTopicRegex = subscribedTopicRegex; + return this; + } + + public Builder maybeUpdateSubscribedTopicRegex(Optional<String> subscribedTopicRegex) { + this.subscribedTopicRegex = subscribedTopicRegex.orElse(this.subscribedTopicRegex); + return this; + } + + public Builder setServerAssignorName(String serverAssignorName) { + this.serverAssignorName = serverAssignorName; + return this; + } + + public Builder maybeUpdateServerAssignorName(Optional<String> serverAssignorName) { + this.serverAssignorName = serverAssignorName.orElse(this.serverAssignorName); + return this; + } + + public Builder setClientAssignors(List<ClientAssignor> clientAssignors) { + this.clientAssignors = clientAssignors; + return this; + } + + public Builder maybeUpdateClientAssignors(Optional<List<ClientAssignor>> clientAssignors) { + this.clientAssignors = clientAssignors.orElse(this.clientAssignors); + return this; + } + + public Builder setAssignedPartitions(Map<Uuid, Set<Integer>> assignedPartitions) { + this.assignedPartitions = assignedPartitions; + return this; + } + + public Builder setPartitionsPendingRevocation(Map<Uuid, Set<Integer>> partitionsPendingRevocation) { + this.partitionsPendingRevocation = partitionsPendingRevocation; + return this; + } + + public Builder setPartitionsPendingAssignment(Map<Uuid, Set<Integer>> partitionsPendingAssignment) { + this.partitionsPendingAssignment = partitionsPendingAssignment; + return this; + } + + public Builder mergeWith(ConsumerGroupMemberMetadataValue record) { Review Comment: Sure, `updateWith` makes sense to me! -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: jira-unsubscr...@kafka.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org