mirror of https://github.com/apache/kafka.git
KAFKA-14462; [12/N] Add GroupMetadataManager and ConsumerGroup (#13639)
This patch adds the GroupMetadataManager to the group-coordinator module. This manager is responsible for handling the groups management, the members management and the entire reconciliation process. At this point, only the new consumer group type/protocol is implemented. The new manager is based on an architecture inspired from the quorum controller. A request can access/read the state but can't mutate it directly. Instead, a list of records is generated together with the response and those records are applied to the state by the runtime framework. We use timeline data structures. Note that the runtime framework is not part of this patch. It will come in a following one. Reviewers: Jeff Kim <jeff.kim@confluent.io>, Justine Olshan <jolshan@confluent.io>
This commit is contained in:
parent
9edf2ec5cc
commit
49d9c6775d
|
@ -224,12 +224,15 @@
|
||||||
<allow pkg="org.apache.kafka.clients.consumer" />
|
<allow pkg="org.apache.kafka.clients.consumer" />
|
||||||
<allow pkg="org.apache.kafka.common.annotation" />
|
<allow pkg="org.apache.kafka.common.annotation" />
|
||||||
<allow pkg="org.apache.kafka.common.message" />
|
<allow pkg="org.apache.kafka.common.message" />
|
||||||
|
<allow pkg="org.apache.kafka.common.metadata" />
|
||||||
|
<allow pkg="org.apache.kafka.common.network" />
|
||||||
<allow pkg="org.apache.kafka.common.protocol" />
|
<allow pkg="org.apache.kafka.common.protocol" />
|
||||||
<allow pkg="org.apache.kafka.common.requests" />
|
<allow pkg="org.apache.kafka.common.requests" />
|
||||||
<allow pkg="org.apache.kafka.coordinator.group" />
|
<allow pkg="org.apache.kafka.coordinator.group" />
|
||||||
<allow pkg="org.apache.kafka.image"/>
|
<allow pkg="org.apache.kafka.image"/>
|
||||||
<allow pkg="org.apache.kafka.server.common"/>
|
<allow pkg="org.apache.kafka.server.common"/>
|
||||||
<allow pkg="org.apache.kafka.server.util"/>
|
<allow pkg="org.apache.kafka.server.util"/>
|
||||||
|
<allow pkg="org.apache.kafka.timeline"/>
|
||||||
</subpackage>
|
</subpackage>
|
||||||
</subpackage>
|
</subpackage>
|
||||||
|
|
||||||
|
|
|
@ -321,7 +321,11 @@
|
||||||
|
|
||||||
<!-- group coordinator -->
|
<!-- group coordinator -->
|
||||||
<suppress checks="CyclomaticComplexity"
|
<suppress checks="CyclomaticComplexity"
|
||||||
files="(ConsumerGroupMember).java"/>
|
files="(ConsumerGroupMember|GroupMetadataManager).java"/>
|
||||||
|
<suppress checks="MethodLength"
|
||||||
|
files="(ConsumerGroupTest|GroupMetadataManagerTest).java"/>
|
||||||
|
<suppress checks="NPathComplexity"
|
||||||
|
files="(GroupMetadataManager).java"/>
|
||||||
<suppress checks="ParameterNumber"
|
<suppress checks="ParameterNumber"
|
||||||
files="(ConsumerGroupMember).java"/>
|
files="(ConsumerGroupMember).java"/>
|
||||||
<suppress checks="ClassDataAbstractionCouplingCheck"
|
<suppress checks="ClassDataAbstractionCouplingCheck"
|
||||||
|
|
|
@ -0,0 +1,876 @@
|
||||||
|
/*
|
||||||
|
* 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;
|
||||||
|
|
||||||
|
import org.apache.kafka.common.Uuid;
|
||||||
|
import org.apache.kafka.common.errors.ApiException;
|
||||||
|
import org.apache.kafka.common.errors.FencedMemberEpochException;
|
||||||
|
import org.apache.kafka.common.errors.GroupIdNotFoundException;
|
||||||
|
import org.apache.kafka.common.errors.GroupMaxSizeReachedException;
|
||||||
|
import org.apache.kafka.common.errors.InvalidRequestException;
|
||||||
|
import org.apache.kafka.common.errors.NotCoordinatorException;
|
||||||
|
import org.apache.kafka.common.errors.UnknownServerException;
|
||||||
|
import org.apache.kafka.common.errors.UnsupportedAssignorException;
|
||||||
|
import org.apache.kafka.common.message.ConsumerGroupHeartbeatRequestData;
|
||||||
|
import org.apache.kafka.common.message.ConsumerGroupHeartbeatResponseData;
|
||||||
|
import org.apache.kafka.common.requests.RequestContext;
|
||||||
|
import org.apache.kafka.common.utils.LogContext;
|
||||||
|
import org.apache.kafka.coordinator.group.assignor.PartitionAssignor;
|
||||||
|
import org.apache.kafka.coordinator.group.assignor.PartitionAssignorException;
|
||||||
|
import org.apache.kafka.coordinator.group.consumer.Assignment;
|
||||||
|
import org.apache.kafka.coordinator.group.consumer.ConsumerGroup;
|
||||||
|
import org.apache.kafka.coordinator.group.consumer.ConsumerGroupMember;
|
||||||
|
import org.apache.kafka.coordinator.group.consumer.CurrentAssignmentBuilder;
|
||||||
|
import org.apache.kafka.coordinator.group.consumer.TargetAssignmentBuilder;
|
||||||
|
import org.apache.kafka.coordinator.group.consumer.TopicMetadata;
|
||||||
|
import org.apache.kafka.coordinator.group.generated.ConsumerGroupCurrentMemberAssignmentKey;
|
||||||
|
import org.apache.kafka.coordinator.group.generated.ConsumerGroupCurrentMemberAssignmentValue;
|
||||||
|
import org.apache.kafka.coordinator.group.generated.ConsumerGroupMemberMetadataKey;
|
||||||
|
import org.apache.kafka.coordinator.group.generated.ConsumerGroupMemberMetadataValue;
|
||||||
|
import org.apache.kafka.coordinator.group.generated.ConsumerGroupMetadataKey;
|
||||||
|
import org.apache.kafka.coordinator.group.generated.ConsumerGroupMetadataValue;
|
||||||
|
import org.apache.kafka.coordinator.group.generated.ConsumerGroupPartitionMetadataKey;
|
||||||
|
import org.apache.kafka.coordinator.group.generated.ConsumerGroupPartitionMetadataValue;
|
||||||
|
import org.apache.kafka.coordinator.group.generated.ConsumerGroupTargetAssignmentMemberKey;
|
||||||
|
import org.apache.kafka.coordinator.group.generated.ConsumerGroupTargetAssignmentMemberValue;
|
||||||
|
import org.apache.kafka.coordinator.group.generated.ConsumerGroupTargetAssignmentMetadataKey;
|
||||||
|
import org.apache.kafka.coordinator.group.generated.ConsumerGroupTargetAssignmentMetadataValue;
|
||||||
|
import org.apache.kafka.image.TopicsImage;
|
||||||
|
import org.apache.kafka.timeline.SnapshotRegistry;
|
||||||
|
import org.apache.kafka.timeline.TimelineHashMap;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.OptionalInt;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.function.Function;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import static org.apache.kafka.coordinator.group.RecordHelpers.newCurrentAssignmentRecord;
|
||||||
|
import static org.apache.kafka.coordinator.group.RecordHelpers.newCurrentAssignmentTombstoneRecord;
|
||||||
|
import static org.apache.kafka.coordinator.group.RecordHelpers.newGroupEpochRecord;
|
||||||
|
import static org.apache.kafka.coordinator.group.RecordHelpers.newGroupSubscriptionMetadataRecord;
|
||||||
|
import static org.apache.kafka.coordinator.group.RecordHelpers.newMemberSubscriptionRecord;
|
||||||
|
import static org.apache.kafka.coordinator.group.RecordHelpers.newMemberSubscriptionTombstoneRecord;
|
||||||
|
import static org.apache.kafka.coordinator.group.RecordHelpers.newTargetAssignmentTombstoneRecord;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The GroupMetadataManager manages the metadata of all generic and consumer groups. It holds
|
||||||
|
* the hard and the soft state of the groups. This class has two kinds of methods:
|
||||||
|
* 1) The request handlers which handle the requests and generate a response and records to
|
||||||
|
* mutate the hard state. Those records will be written by the runtime and applied to the
|
||||||
|
* hard state via the replay methods.
|
||||||
|
* 2) The replay methods which apply records to the hard state. Those are used in the request
|
||||||
|
* handling as well as during the initial loading of the records from the partitions.
|
||||||
|
*/
|
||||||
|
public class GroupMetadataManager {
|
||||||
|
|
||||||
|
public static class Builder {
|
||||||
|
private LogContext logContext = null;
|
||||||
|
private SnapshotRegistry snapshotRegistry = null;
|
||||||
|
private List<PartitionAssignor> assignors = null;
|
||||||
|
private TopicsImage topicsImage = null;
|
||||||
|
private int consumerGroupMaxSize = Integer.MAX_VALUE;
|
||||||
|
private int consumerGroupHeartbeatIntervalMs = 5000;
|
||||||
|
|
||||||
|
Builder withLogContext(LogContext logContext) {
|
||||||
|
this.logContext = logContext;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
Builder withSnapshotRegistry(SnapshotRegistry snapshotRegistry) {
|
||||||
|
this.snapshotRegistry = snapshotRegistry;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
Builder withAssignors(List<PartitionAssignor> assignors) {
|
||||||
|
this.assignors = assignors;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
Builder withConsumerGroupMaxSize(int consumerGroupMaxSize) {
|
||||||
|
this.consumerGroupMaxSize = consumerGroupMaxSize;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
Builder withConsumerGroupHeartbeatInterval(int consumerGroupHeartbeatIntervalMs) {
|
||||||
|
this.consumerGroupHeartbeatIntervalMs = consumerGroupHeartbeatIntervalMs;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
Builder withTopicsImage(TopicsImage topicsImage) {
|
||||||
|
this.topicsImage = topicsImage;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
GroupMetadataManager build() {
|
||||||
|
if (logContext == null) logContext = new LogContext();
|
||||||
|
if (snapshotRegistry == null) snapshotRegistry = new SnapshotRegistry(logContext);
|
||||||
|
if (topicsImage == null) topicsImage = TopicsImage.EMPTY;
|
||||||
|
|
||||||
|
if (assignors == null || assignors.isEmpty()) {
|
||||||
|
throw new IllegalStateException("Assignors must be set before building.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return new GroupMetadataManager(
|
||||||
|
snapshotRegistry,
|
||||||
|
logContext,
|
||||||
|
assignors,
|
||||||
|
topicsImage,
|
||||||
|
consumerGroupMaxSize,
|
||||||
|
consumerGroupHeartbeatIntervalMs
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The logger.
|
||||||
|
*/
|
||||||
|
private final Logger log;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The snapshot registry.
|
||||||
|
*/
|
||||||
|
private final SnapshotRegistry snapshotRegistry;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The supported partition assignors keyed by their name.
|
||||||
|
*/
|
||||||
|
private final Map<String, PartitionAssignor> assignors;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The default assignor used.
|
||||||
|
*/
|
||||||
|
private final PartitionAssignor defaultAssignor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The generic and consumer groups keyed by their name.
|
||||||
|
*/
|
||||||
|
private final TimelineHashMap<String, Group> groups;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The maximum number of members allowed in a single consumer group.
|
||||||
|
*/
|
||||||
|
private final int consumerGroupMaxSize;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The heartbeat interval for consumer groups.
|
||||||
|
*/
|
||||||
|
private final int consumerGroupHeartbeatIntervalMs;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The topics metadata (or image).
|
||||||
|
*/
|
||||||
|
private TopicsImage topicsImage;
|
||||||
|
|
||||||
|
private GroupMetadataManager(
|
||||||
|
SnapshotRegistry snapshotRegistry,
|
||||||
|
LogContext logContext,
|
||||||
|
List<PartitionAssignor> assignors,
|
||||||
|
TopicsImage topicsImage,
|
||||||
|
int consumerGroupMaxSize,
|
||||||
|
int consumerGroupHeartbeatIntervalMs
|
||||||
|
) {
|
||||||
|
this.log = logContext.logger(GroupMetadataManager.class);
|
||||||
|
this.snapshotRegistry = snapshotRegistry;
|
||||||
|
this.topicsImage = topicsImage;
|
||||||
|
this.assignors = assignors.stream().collect(Collectors.toMap(PartitionAssignor::name, Function.identity()));
|
||||||
|
this.defaultAssignor = assignors.get(0);
|
||||||
|
this.groups = new TimelineHashMap<>(snapshotRegistry, 0);
|
||||||
|
this.consumerGroupMaxSize = consumerGroupMaxSize;
|
||||||
|
this.consumerGroupHeartbeatIntervalMs = consumerGroupHeartbeatIntervalMs;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets or maybe creates a consumer group.
|
||||||
|
*
|
||||||
|
* @param groupId The group id.
|
||||||
|
* @param createIfNotExists A boolean indicating whether the group should be
|
||||||
|
* created if it does not exist.
|
||||||
|
*
|
||||||
|
* @return A ConsumerGroup.
|
||||||
|
* @throws GroupIdNotFoundException if the group does not exist and createIfNotExists is false or
|
||||||
|
* if the group is not a consumer group.
|
||||||
|
*
|
||||||
|
* Package private for testing.
|
||||||
|
*/
|
||||||
|
ConsumerGroup getOrMaybeCreateConsumerGroup(
|
||||||
|
String groupId,
|
||||||
|
boolean createIfNotExists
|
||||||
|
) throws GroupIdNotFoundException {
|
||||||
|
Group group = groups.get(groupId);
|
||||||
|
|
||||||
|
if (group == null && !createIfNotExists) {
|
||||||
|
throw new GroupIdNotFoundException(String.format("Consumer group %s not found.", groupId));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (group == null) {
|
||||||
|
ConsumerGroup consumerGroup = new ConsumerGroup(snapshotRegistry, groupId);
|
||||||
|
groups.put(groupId, consumerGroup);
|
||||||
|
return consumerGroup;
|
||||||
|
} else {
|
||||||
|
if (group.type() == Group.GroupType.CONSUMER) {
|
||||||
|
return (ConsumerGroup) group;
|
||||||
|
} else {
|
||||||
|
// We don't support upgrading/downgrading between protocols at the moment so
|
||||||
|
// we throw an exception if a group exists with the wrong type.
|
||||||
|
throw new GroupIdNotFoundException(String.format("Group %s is not a consumer group.", groupId));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes the group.
|
||||||
|
*
|
||||||
|
* @param groupId The group id.
|
||||||
|
*/
|
||||||
|
private void removeGroup(
|
||||||
|
String groupId
|
||||||
|
) {
|
||||||
|
groups.remove(groupId);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Throws an InvalidRequestException if the value is non-null and empty.
|
||||||
|
*
|
||||||
|
* @param value The value.
|
||||||
|
* @param error The error message.
|
||||||
|
* @throws InvalidRequestException
|
||||||
|
*/
|
||||||
|
private void throwIfEmptyString(
|
||||||
|
String value,
|
||||||
|
String error
|
||||||
|
) throws InvalidRequestException {
|
||||||
|
if (value != null && value.isEmpty()) {
|
||||||
|
throw new InvalidRequestException(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Throws an InvalidRequestException if the value is non-null.
|
||||||
|
*
|
||||||
|
* @param value The value.
|
||||||
|
* @param error The error message.
|
||||||
|
* @throws InvalidRequestException
|
||||||
|
*/
|
||||||
|
private void throwIfNotNull(
|
||||||
|
Object value,
|
||||||
|
String error
|
||||||
|
) throws InvalidRequestException {
|
||||||
|
if (value != null) {
|
||||||
|
throw new InvalidRequestException(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validates the request.
|
||||||
|
*
|
||||||
|
* @param request The request to validate.
|
||||||
|
*
|
||||||
|
* @throws InvalidRequestException if the request is not valid.
|
||||||
|
* @throws UnsupportedAssignorException if the assignor is not supported.
|
||||||
|
*/
|
||||||
|
private void throwIfConsumerGroupHeartbeatRequestIsInvalid(
|
||||||
|
ConsumerGroupHeartbeatRequestData request
|
||||||
|
) throws InvalidRequestException, UnsupportedAssignorException {
|
||||||
|
throwIfEmptyString(request.groupId(), "GroupId can't be empty.");
|
||||||
|
throwIfEmptyString(request.instanceId(), "InstanceId can't be empty.");
|
||||||
|
throwIfEmptyString(request.rackId(), "RackId can't be empty.");
|
||||||
|
throwIfNotNull(request.subscribedTopicRegex(), "SubscribedTopicRegex is not supported yet.");
|
||||||
|
throwIfNotNull(request.clientAssignors(), "Client side assignors are not supported yet.");
|
||||||
|
|
||||||
|
if (request.memberEpoch() > 0 || request.memberEpoch() == -1) {
|
||||||
|
throwIfEmptyString(request.memberId(), "MemberId can't be empty.");
|
||||||
|
} else if (request.memberEpoch() == 0) {
|
||||||
|
if (request.rebalanceTimeoutMs() == -1) {
|
||||||
|
throw new InvalidRequestException("RebalanceTimeoutMs must be provided in first request.");
|
||||||
|
}
|
||||||
|
if (request.topicPartitions() == null || !request.topicPartitions().isEmpty()) {
|
||||||
|
throw new InvalidRequestException("TopicPartitions must be empty when (re-)joining.");
|
||||||
|
}
|
||||||
|
if (request.subscribedTopicNames() == null || request.subscribedTopicNames().isEmpty()) {
|
||||||
|
throw new InvalidRequestException("SubscribedTopicNames must be set in first request.");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw new InvalidRequestException("MemberEpoch is invalid.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (request.serverAssignor() != null && !assignors.containsKey(request.serverAssignor())) {
|
||||||
|
throw new UnsupportedAssignorException("ServerAssignor " + request.serverAssignor()
|
||||||
|
+ " is not supported. Supported assignors: " + String.join(", ", assignors.keySet())
|
||||||
|
+ ".");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Verifies that the partitions currently owned by the member (the ones set in the
|
||||||
|
* request) matches the ones that the member should own. It matches if the consumer
|
||||||
|
* only owns partitions which are in the assigned partitions. It does not match if
|
||||||
|
* it owns any other partitions.
|
||||||
|
*
|
||||||
|
* @param ownedTopicPartitions The partitions provided by the consumer in the request.
|
||||||
|
* @param target The partitions that the member should have.
|
||||||
|
*
|
||||||
|
* @return A boolean indicating whether the owned partitions are a subset or not.
|
||||||
|
*/
|
||||||
|
private boolean isSubset(
|
||||||
|
List<ConsumerGroupHeartbeatRequestData.TopicPartitions> ownedTopicPartitions,
|
||||||
|
Map<Uuid, Set<Integer>> target
|
||||||
|
) {
|
||||||
|
if (ownedTopicPartitions == null) return false;
|
||||||
|
|
||||||
|
for (ConsumerGroupHeartbeatRequestData.TopicPartitions topicPartitions : ownedTopicPartitions) {
|
||||||
|
Set<Integer> partitions = target.get(topicPartitions.topicId());
|
||||||
|
if (partitions == null) return false;
|
||||||
|
for (Integer partitionId : topicPartitions.partitions()) {
|
||||||
|
if (!partitions.contains(partitionId)) return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks whether the consumer group can accept a new member or not based on the
|
||||||
|
* max group size defined.
|
||||||
|
*
|
||||||
|
* @param group The consumer group.
|
||||||
|
* @param memberId The member id.
|
||||||
|
*
|
||||||
|
* @throws GroupMaxSizeReachedException if the maximum capacity has been reached.
|
||||||
|
*/
|
||||||
|
private void throwIfConsumerGroupIsFull(
|
||||||
|
ConsumerGroup group,
|
||||||
|
String memberId
|
||||||
|
) throws GroupMaxSizeReachedException {
|
||||||
|
// If the consumer group has reached its maximum capacity, the member is rejected if it is not
|
||||||
|
// already a member of the consumer group.
|
||||||
|
if (group.numMembers() >= consumerGroupMaxSize && (memberId.isEmpty() || !group.hasMember(memberId))) {
|
||||||
|
throw new GroupMaxSizeReachedException("The consumer group has reached its maximum capacity of "
|
||||||
|
+ consumerGroupMaxSize + " members.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validates the member epoch provided in the heartbeat request.
|
||||||
|
*
|
||||||
|
* @param member The consumer group member.
|
||||||
|
* @param receivedMemberEpoch The member epoch.
|
||||||
|
* @param ownedTopicPartitions The owned partitions.
|
||||||
|
*
|
||||||
|
* @throws NotCoordinatorException if the provided epoch is ahead of the epoch known
|
||||||
|
* by this coordinator. This suggests that the member
|
||||||
|
* got a higher epoch from another coordinator.
|
||||||
|
* @throws FencedMemberEpochException if the provided epoch is behind the epoch known
|
||||||
|
* by this coordinator.
|
||||||
|
*/
|
||||||
|
private void throwIfMemberEpochIsInvalid(
|
||||||
|
ConsumerGroupMember member,
|
||||||
|
int receivedMemberEpoch,
|
||||||
|
List<ConsumerGroupHeartbeatRequestData.TopicPartitions> ownedTopicPartitions
|
||||||
|
) {
|
||||||
|
if (receivedMemberEpoch > member.memberEpoch()) {
|
||||||
|
throw new FencedMemberEpochException("The consumer group member has a greater member "
|
||||||
|
+ "epoch (" + receivedMemberEpoch + ") than the one known by the group coordinator ("
|
||||||
|
+ member.memberEpoch() + "). The member must abandon all its partitions and rejoin.");
|
||||||
|
} else if (receivedMemberEpoch < member.memberEpoch()) {
|
||||||
|
// If the member comes with the previous epoch and has a subset of the current assignment partitions,
|
||||||
|
// we accept it because the response with the bumped epoch may have been lost.
|
||||||
|
if (receivedMemberEpoch != member.previousMemberEpoch() || !isSubset(ownedTopicPartitions, member.assignedPartitions())) {
|
||||||
|
throw new FencedMemberEpochException("The consumer group member has a smaller member "
|
||||||
|
+ "epoch (" + receivedMemberEpoch + ") than the one known by the group coordinator ("
|
||||||
|
+ member.memberEpoch() + "). The member must abandon all its partitions and rejoin.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private ConsumerGroupHeartbeatResponseData.Assignment createResponseAssignment(
|
||||||
|
ConsumerGroupMember member
|
||||||
|
) {
|
||||||
|
ConsumerGroupHeartbeatResponseData.Assignment assignment = new ConsumerGroupHeartbeatResponseData.Assignment()
|
||||||
|
.setAssignedTopicPartitions(fromAssignmentMap(member.assignedPartitions()));
|
||||||
|
|
||||||
|
if (member.state() == ConsumerGroupMember.MemberState.ASSIGNING) {
|
||||||
|
assignment.setPendingTopicPartitions(fromAssignmentMap(member.partitionsPendingAssignment()));
|
||||||
|
}
|
||||||
|
|
||||||
|
return assignment;
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<ConsumerGroupHeartbeatResponseData.TopicPartitions> fromAssignmentMap(
|
||||||
|
Map<Uuid, Set<Integer>> assignment
|
||||||
|
) {
|
||||||
|
return assignment.entrySet().stream()
|
||||||
|
.map(keyValue -> new ConsumerGroupHeartbeatResponseData.TopicPartitions()
|
||||||
|
.setTopicId(keyValue.getKey())
|
||||||
|
.setPartitions(new ArrayList<>(keyValue.getValue())))
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
private OptionalInt ofSentinel(int value) {
|
||||||
|
return value != -1 ? OptionalInt.of(value) : OptionalInt.empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles a regular heartbeat from a consumer group member. It mainly consists of
|
||||||
|
* three parts:
|
||||||
|
* 1) The member is created or updated. The group epoch is bumped if the member
|
||||||
|
* has been created or updated.
|
||||||
|
* 2) The target assignment for the consumer group is updated if the group epoch
|
||||||
|
* is larger than the current target assignment epoch.
|
||||||
|
* 3) The member's assignment is reconciled with the target assignment.
|
||||||
|
*
|
||||||
|
* @param groupId The group id from the request.
|
||||||
|
* @param memberId The member id from the request.
|
||||||
|
* @param memberEpoch The member epoch from the request.
|
||||||
|
* @param instanceId The instance id from the request or null.
|
||||||
|
* @param rackId The rack id from the request or null.
|
||||||
|
* @param rebalanceTimeoutMs The rebalance timeout from the request or -1.
|
||||||
|
* @param clientId The client id.
|
||||||
|
* @param clientHost The client host.
|
||||||
|
* @param subscribedTopicNames The list of subscribed topic names from the request
|
||||||
|
* of null.
|
||||||
|
* @param subscribedTopicRegex The regular expression based subscription from the
|
||||||
|
* request or null.
|
||||||
|
* @param assignorName The assignor name from the request or null.
|
||||||
|
* @param ownedTopicPartitions The list of owned partitions from the request or null.
|
||||||
|
*
|
||||||
|
* @return A Result containing the ConsumerGroupHeartbeat response and
|
||||||
|
* a list of records to update the state machine.
|
||||||
|
*/
|
||||||
|
private Result<ConsumerGroupHeartbeatResponseData> consumerGroupHeartbeat(
|
||||||
|
String groupId,
|
||||||
|
String memberId,
|
||||||
|
int memberEpoch,
|
||||||
|
String instanceId,
|
||||||
|
String rackId,
|
||||||
|
int rebalanceTimeoutMs,
|
||||||
|
String clientId,
|
||||||
|
String clientHost,
|
||||||
|
List<String> subscribedTopicNames,
|
||||||
|
String subscribedTopicRegex,
|
||||||
|
String assignorName,
|
||||||
|
List<ConsumerGroupHeartbeatRequestData.TopicPartitions> ownedTopicPartitions
|
||||||
|
) throws ApiException {
|
||||||
|
List<Record> records = new ArrayList<>();
|
||||||
|
|
||||||
|
// Get or create the consumer group.
|
||||||
|
boolean createIfNotExists = memberEpoch == 0;
|
||||||
|
final ConsumerGroup group = getOrMaybeCreateConsumerGroup(groupId, createIfNotExists);
|
||||||
|
throwIfConsumerGroupIsFull(group, memberId);
|
||||||
|
|
||||||
|
// Get or create the member.
|
||||||
|
if (memberId.isEmpty()) memberId = Uuid.randomUuid().toString();
|
||||||
|
final ConsumerGroupMember member = group.getOrMaybeCreateMember(memberId, createIfNotExists);
|
||||||
|
throwIfMemberEpochIsInvalid(member, memberEpoch, ownedTopicPartitions);
|
||||||
|
|
||||||
|
if (memberEpoch == 0) {
|
||||||
|
log.info("[GroupId " + groupId + "] Member " + memberId + " joins the consumer group.");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 1. Create or update the member. If the member is new or has changed, a ConsumerGroupMemberMetadataValue
|
||||||
|
// record is written to the __consumer_offsets partition to persist the change. If the subscriptions have
|
||||||
|
// changed, the subscription metadata is updated and persisted by writing a ConsumerGroupPartitionMetadataValue
|
||||||
|
// record to the __consumer_offsets partition. Finally, the group epoch is bumped if the subscriptions have
|
||||||
|
// changed, and persisted by writing a ConsumerGroupMetadataValue record to the partition.
|
||||||
|
int groupEpoch = group.groupEpoch();
|
||||||
|
Map<String, TopicMetadata> subscriptionMetadata = group.subscriptionMetadata();
|
||||||
|
ConsumerGroupMember updatedMember = new ConsumerGroupMember.Builder(member)
|
||||||
|
.maybeUpdateInstanceId(Optional.ofNullable(instanceId))
|
||||||
|
.maybeUpdateRackId(Optional.ofNullable(rackId))
|
||||||
|
.maybeUpdateRebalanceTimeoutMs(ofSentinel(rebalanceTimeoutMs))
|
||||||
|
.maybeUpdateServerAssignorName(Optional.ofNullable(assignorName))
|
||||||
|
.maybeUpdateSubscribedTopicNames(Optional.ofNullable(subscribedTopicNames))
|
||||||
|
.maybeUpdateSubscribedTopicRegex(Optional.ofNullable(subscribedTopicRegex))
|
||||||
|
.setClientId(clientId)
|
||||||
|
.setClientHost(clientHost)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
if (!updatedMember.equals(member)) {
|
||||||
|
records.add(newMemberSubscriptionRecord(groupId, updatedMember));
|
||||||
|
|
||||||
|
if (!updatedMember.subscribedTopicNames().equals(member.subscribedTopicNames())) {
|
||||||
|
log.info("[GroupId " + groupId + "] Member " + memberId + " updated its subscribed topics to: " +
|
||||||
|
updatedMember.subscribedTopicNames());
|
||||||
|
|
||||||
|
subscriptionMetadata = group.computeSubscriptionMetadata(
|
||||||
|
member,
|
||||||
|
updatedMember,
|
||||||
|
topicsImage
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!subscriptionMetadata.equals(group.subscriptionMetadata())) {
|
||||||
|
log.info("[GroupId " + groupId + "] Computed new subscription metadata: "
|
||||||
|
+ subscriptionMetadata + ".");
|
||||||
|
records.add(newGroupSubscriptionMetadataRecord(groupId, subscriptionMetadata));
|
||||||
|
}
|
||||||
|
|
||||||
|
groupEpoch += 1;
|
||||||
|
records.add(newGroupEpochRecord(groupId, groupEpoch));
|
||||||
|
|
||||||
|
log.info("[GroupId " + groupId + "] Bumped group epoch to " + groupEpoch + ".");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Update the target assignment if the group epoch is larger than the target assignment epoch. The
|
||||||
|
// delta between the existing and the new target assignment is persisted to the partition.
|
||||||
|
int targetAssignmentEpoch = group.assignmentEpoch();
|
||||||
|
Assignment targetAssignment = group.targetAssignment(memberId);
|
||||||
|
if (groupEpoch > targetAssignmentEpoch) {
|
||||||
|
String preferredServerAssignor = group.computePreferredServerAssignor(
|
||||||
|
member,
|
||||||
|
updatedMember
|
||||||
|
).orElse(defaultAssignor.name());
|
||||||
|
|
||||||
|
try {
|
||||||
|
TargetAssignmentBuilder.TargetAssignmentResult assignmentResult =
|
||||||
|
new TargetAssignmentBuilder(groupId, groupEpoch, assignors.get(preferredServerAssignor))
|
||||||
|
.withMembers(group.members())
|
||||||
|
.withSubscriptionMetadata(subscriptionMetadata)
|
||||||
|
.withTargetAssignment(group.targetAssignment())
|
||||||
|
.addOrUpdateMember(memberId, updatedMember)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
log.info("[GroupId " + groupId + "] Computed a new target assignment for epoch " + groupEpoch + ": "
|
||||||
|
+ assignmentResult.targetAssignment() + ".");
|
||||||
|
|
||||||
|
records.addAll(assignmentResult.records());
|
||||||
|
targetAssignment = assignmentResult.targetAssignment().get(memberId);
|
||||||
|
targetAssignmentEpoch = groupEpoch;
|
||||||
|
} catch (PartitionAssignorException ex) {
|
||||||
|
String msg = "Failed to compute a new target assignment for epoch " + groupEpoch + ": " + ex + ".";
|
||||||
|
log.error("[GroupId " + groupId + "] " + msg);
|
||||||
|
throw new UnknownServerException(msg, ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. Reconcile the member's assignment with the target assignment. This is only required if
|
||||||
|
// the member is not stable or if a new target assignment has been installed.
|
||||||
|
boolean assignmentUpdated = false;
|
||||||
|
if (updatedMember.state() != ConsumerGroupMember.MemberState.STABLE || updatedMember.targetMemberEpoch() != targetAssignmentEpoch) {
|
||||||
|
ConsumerGroupMember prevMember = updatedMember;
|
||||||
|
updatedMember = new CurrentAssignmentBuilder(updatedMember)
|
||||||
|
.withTargetAssignment(targetAssignmentEpoch, targetAssignment)
|
||||||
|
.withCurrentPartitionEpoch(group::currentPartitionEpoch)
|
||||||
|
.withOwnedTopicPartitions(ownedTopicPartitions)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
// Checking the reference is enough here because a new instance
|
||||||
|
// is created only when the state has changed.
|
||||||
|
if (updatedMember != prevMember) {
|
||||||
|
assignmentUpdated = true;
|
||||||
|
records.add(newCurrentAssignmentRecord(groupId, updatedMember));
|
||||||
|
|
||||||
|
log.info("[GroupId " + groupId + "] Member " + memberId + " transitioned from " +
|
||||||
|
member.currentAssignmentSummary() + " to " + updatedMember.currentAssignmentSummary() + ".");
|
||||||
|
|
||||||
|
// TODO(dajac) Starts or restarts the timer for the revocation timeout.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO(dajac) Starts or restarts the timer for the session timeout.
|
||||||
|
|
||||||
|
// Prepare the response.
|
||||||
|
ConsumerGroupHeartbeatResponseData response = new ConsumerGroupHeartbeatResponseData()
|
||||||
|
.setMemberId(updatedMember.memberId())
|
||||||
|
.setMemberEpoch(updatedMember.memberEpoch())
|
||||||
|
.setHeartbeatIntervalMs(consumerGroupHeartbeatIntervalMs);
|
||||||
|
|
||||||
|
// The assignment is only provided in the following cases:
|
||||||
|
// 1. The member reported its owned partitions;
|
||||||
|
// 2. The member just joined or rejoined to group (epoch equals to zero);
|
||||||
|
// 3. The member's assignment has been updated.
|
||||||
|
if (ownedTopicPartitions != null || memberEpoch == 0 || assignmentUpdated) {
|
||||||
|
response.setAssignment(createResponseAssignment(updatedMember));
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Result<>(records, response);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles leave request from a consumer group member.
|
||||||
|
* @param groupId The group id from the request.
|
||||||
|
* @param memberId The member id from the request.
|
||||||
|
*
|
||||||
|
* @return A Result containing the ConsumerGroupHeartbeat response and
|
||||||
|
* a list of records to update the state machine.
|
||||||
|
*/
|
||||||
|
private Result<ConsumerGroupHeartbeatResponseData> consumerGroupLeave(
|
||||||
|
String groupId,
|
||||||
|
String memberId
|
||||||
|
) throws ApiException {
|
||||||
|
List<Record> records = new ArrayList<>();
|
||||||
|
|
||||||
|
ConsumerGroup group = getOrMaybeCreateConsumerGroup(groupId, false);
|
||||||
|
ConsumerGroupMember member = group.getOrMaybeCreateMember(memberId, false);
|
||||||
|
|
||||||
|
log.info("[GroupId " + groupId + "] Member " + memberId + " left the consumer group.");
|
||||||
|
|
||||||
|
// Write tombstones for the member. The order matters here.
|
||||||
|
records.add(newCurrentAssignmentTombstoneRecord(groupId, memberId));
|
||||||
|
records.add(newTargetAssignmentTombstoneRecord(groupId, memberId));
|
||||||
|
records.add(newMemberSubscriptionTombstoneRecord(groupId, memberId));
|
||||||
|
|
||||||
|
// We update the subscription metadata without the leaving member.
|
||||||
|
Map<String, TopicMetadata> subscriptionMetadata = group.computeSubscriptionMetadata(
|
||||||
|
member,
|
||||||
|
null,
|
||||||
|
topicsImage
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!subscriptionMetadata.equals(group.subscriptionMetadata())) {
|
||||||
|
log.info("[GroupId " + groupId + "] Computed new subscription metadata: "
|
||||||
|
+ subscriptionMetadata + ".");
|
||||||
|
records.add(newGroupSubscriptionMetadataRecord(groupId, subscriptionMetadata));
|
||||||
|
}
|
||||||
|
|
||||||
|
// We bump the group epoch.
|
||||||
|
int groupEpoch = group.groupEpoch() + 1;
|
||||||
|
records.add(newGroupEpochRecord(groupId, groupEpoch));
|
||||||
|
|
||||||
|
return new Result<>(records, new ConsumerGroupHeartbeatResponseData()
|
||||||
|
.setMemberId(memberId)
|
||||||
|
.setMemberEpoch(-1)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles a ConsumerGroupHeartbeat request.
|
||||||
|
*
|
||||||
|
* @param context The request context.
|
||||||
|
* @param request The actual ConsumerGroupHeartbeat request.
|
||||||
|
*
|
||||||
|
* @return A Result containing the ConsumerGroupHeartbeat response and
|
||||||
|
* a list of records to update the state machine.
|
||||||
|
*/
|
||||||
|
public Result<ConsumerGroupHeartbeatResponseData> consumerGroupHeartbeat(
|
||||||
|
RequestContext context,
|
||||||
|
ConsumerGroupHeartbeatRequestData request
|
||||||
|
) throws ApiException {
|
||||||
|
throwIfConsumerGroupHeartbeatRequestIsInvalid(request);
|
||||||
|
|
||||||
|
if (request.memberEpoch() == -1) {
|
||||||
|
// -1 means that the member wants to leave the group.
|
||||||
|
return consumerGroupLeave(
|
||||||
|
request.groupId(),
|
||||||
|
request.memberId()
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
// Otherwise, it is a regular heartbeat.
|
||||||
|
return consumerGroupHeartbeat(
|
||||||
|
request.groupId(),
|
||||||
|
request.memberId(),
|
||||||
|
request.memberEpoch(),
|
||||||
|
request.instanceId(),
|
||||||
|
request.rackId(),
|
||||||
|
request.rebalanceTimeoutMs(),
|
||||||
|
context.clientId(),
|
||||||
|
context.clientAddress.toString(),
|
||||||
|
request.subscribedTopicNames(),
|
||||||
|
request.subscribedTopicRegex(),
|
||||||
|
request.serverAssignor(),
|
||||||
|
request.topicPartitions()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Replays ConsumerGroupMemberMetadataKey/Value to update the hard state of
|
||||||
|
* the consumer group. It updates the subscription part of the member or
|
||||||
|
* delete the member.
|
||||||
|
*
|
||||||
|
* @param key A ConsumerGroupMemberMetadataKey key.
|
||||||
|
* @param value A ConsumerGroupMemberMetadataValue record.
|
||||||
|
*/
|
||||||
|
public void replay(
|
||||||
|
ConsumerGroupMemberMetadataKey key,
|
||||||
|
ConsumerGroupMemberMetadataValue value
|
||||||
|
) {
|
||||||
|
String groupId = key.groupId();
|
||||||
|
String memberId = key.memberId();
|
||||||
|
|
||||||
|
if (value != null) {
|
||||||
|
ConsumerGroup consumerGroup = getOrMaybeCreateConsumerGroup(groupId, true);
|
||||||
|
ConsumerGroupMember oldMember = consumerGroup.getOrMaybeCreateMember(memberId, true);
|
||||||
|
consumerGroup.updateMember(new ConsumerGroupMember.Builder(oldMember)
|
||||||
|
.updateWith(value)
|
||||||
|
.build());
|
||||||
|
} else {
|
||||||
|
ConsumerGroup consumerGroup = getOrMaybeCreateConsumerGroup(groupId, false);
|
||||||
|
ConsumerGroupMember oldMember = consumerGroup.getOrMaybeCreateMember(memberId, false);
|
||||||
|
if (oldMember.memberEpoch() != -1) {
|
||||||
|
throw new IllegalStateException("Received a tombstone record to delete member " + memberId
|
||||||
|
+ " but did not receive ConsumerGroupCurrentMemberAssignmentValue tombstone.");
|
||||||
|
}
|
||||||
|
if (consumerGroup.targetAssignment().containsKey(memberId)) {
|
||||||
|
throw new IllegalStateException("Received a tombstone record to delete member " + memberId
|
||||||
|
+ " but did not receive ConsumerGroupTargetAssignmentMetadataValue tombstone.");
|
||||||
|
}
|
||||||
|
consumerGroup.removeMember(memberId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Replays ConsumerGroupMetadataKey/Value to update the hard state of
|
||||||
|
* the consumer group. It updates the group epoch of the consumer
|
||||||
|
* group or deletes the consumer group.
|
||||||
|
*
|
||||||
|
* @param key A ConsumerGroupMetadataKey key.
|
||||||
|
* @param value A ConsumerGroupMetadataValue record.
|
||||||
|
*/
|
||||||
|
public void replay(
|
||||||
|
ConsumerGroupMetadataKey key,
|
||||||
|
ConsumerGroupMetadataValue value
|
||||||
|
) {
|
||||||
|
String groupId = key.groupId();
|
||||||
|
|
||||||
|
if (value != null) {
|
||||||
|
ConsumerGroup consumerGroup = getOrMaybeCreateConsumerGroup(groupId, true);
|
||||||
|
consumerGroup.setGroupEpoch(value.epoch());
|
||||||
|
} else {
|
||||||
|
ConsumerGroup consumerGroup = getOrMaybeCreateConsumerGroup(groupId, false);
|
||||||
|
if (!consumerGroup.members().isEmpty()) {
|
||||||
|
throw new IllegalStateException("Received a tombstone record to delete group " + groupId
|
||||||
|
+ " but the group still has " + consumerGroup.members().size() + " members.");
|
||||||
|
}
|
||||||
|
if (!consumerGroup.targetAssignment().isEmpty()) {
|
||||||
|
throw new IllegalStateException("Received a tombstone record to delete group " + groupId
|
||||||
|
+ " but the target assignment still has " + consumerGroup.targetAssignment().size()
|
||||||
|
+ " members.");
|
||||||
|
}
|
||||||
|
if (consumerGroup.assignmentEpoch() != -1) {
|
||||||
|
throw new IllegalStateException("Received a tombstone record to delete group " + groupId
|
||||||
|
+ " but did not receive ConsumerGroupTargetAssignmentMetadataValue tombstone.");
|
||||||
|
}
|
||||||
|
removeGroup(groupId);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Replays ConsumerGroupPartitionMetadataKey/Value to update the hard state of
|
||||||
|
* the consumer group. It updates the subscription metadata of the consumer
|
||||||
|
* group.
|
||||||
|
*
|
||||||
|
* @param key A ConsumerGroupPartitionMetadataKey key.
|
||||||
|
* @param value A ConsumerGroupPartitionMetadataValue record.
|
||||||
|
*/
|
||||||
|
public void replay(
|
||||||
|
ConsumerGroupPartitionMetadataKey key,
|
||||||
|
ConsumerGroupPartitionMetadataValue value
|
||||||
|
) {
|
||||||
|
String groupId = key.groupId();
|
||||||
|
ConsumerGroup consumerGroup = getOrMaybeCreateConsumerGroup(groupId, false);
|
||||||
|
|
||||||
|
if (value != null) {
|
||||||
|
Map<String, TopicMetadata> subscriptionMetadata = new HashMap<>();
|
||||||
|
value.topics().forEach(topicMetadata -> {
|
||||||
|
subscriptionMetadata.put(topicMetadata.topicName(), TopicMetadata.fromRecord(topicMetadata));
|
||||||
|
});
|
||||||
|
consumerGroup.setSubscriptionMetadata(subscriptionMetadata);
|
||||||
|
} else {
|
||||||
|
consumerGroup.setSubscriptionMetadata(Collections.emptyMap());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Replays ConsumerGroupTargetAssignmentMemberKey/Value to update the hard state of
|
||||||
|
* the consumer group. It updates the target assignment of the member or deletes it.
|
||||||
|
*
|
||||||
|
* @param key A ConsumerGroupTargetAssignmentMemberKey key.
|
||||||
|
* @param value A ConsumerGroupTargetAssignmentMemberValue record.
|
||||||
|
*/
|
||||||
|
public void replay(
|
||||||
|
ConsumerGroupTargetAssignmentMemberKey key,
|
||||||
|
ConsumerGroupTargetAssignmentMemberValue value
|
||||||
|
) {
|
||||||
|
String groupId = key.groupId();
|
||||||
|
String memberId = key.memberId();
|
||||||
|
ConsumerGroup consumerGroup = getOrMaybeCreateConsumerGroup(groupId, false);
|
||||||
|
|
||||||
|
if (value != null) {
|
||||||
|
consumerGroup.updateTargetAssignment(memberId, Assignment.fromRecord(value));
|
||||||
|
} else {
|
||||||
|
consumerGroup.removeTargetAssignment(memberId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Replays ConsumerGroupTargetAssignmentMetadataKey/Value to update the hard state of
|
||||||
|
* the consumer group. It updates the target assignment epoch or set it to -1 to signal
|
||||||
|
* that it has been deleted.
|
||||||
|
*
|
||||||
|
* @param key A ConsumerGroupTargetAssignmentMetadataKey key.
|
||||||
|
* @param value A ConsumerGroupTargetAssignmentMetadataValue record.
|
||||||
|
*/
|
||||||
|
public void replay(
|
||||||
|
ConsumerGroupTargetAssignmentMetadataKey key,
|
||||||
|
ConsumerGroupTargetAssignmentMetadataValue value
|
||||||
|
) {
|
||||||
|
String groupId = key.groupId();
|
||||||
|
ConsumerGroup consumerGroup = getOrMaybeCreateConsumerGroup(groupId, false);
|
||||||
|
|
||||||
|
if (value != null) {
|
||||||
|
consumerGroup.setTargetAssignmentEpoch(value.assignmentEpoch());
|
||||||
|
} else {
|
||||||
|
if (!consumerGroup.targetAssignment().isEmpty()) {
|
||||||
|
throw new IllegalStateException("Received a tombstone record to delete target assignment of " + groupId
|
||||||
|
+ " but the assignment still has " + consumerGroup.targetAssignment().size() + " members.");
|
||||||
|
}
|
||||||
|
consumerGroup.setTargetAssignmentEpoch(-1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Replays ConsumerGroupCurrentMemberAssignmentKey/Value to update the hard state of
|
||||||
|
* the consumer group. It updates the assignment of a member or deletes it.
|
||||||
|
*
|
||||||
|
* @param key A ConsumerGroupCurrentMemberAssignmentKey key.
|
||||||
|
* @param value A ConsumerGroupCurrentMemberAssignmentValue record.
|
||||||
|
*/
|
||||||
|
public void replay(
|
||||||
|
ConsumerGroupCurrentMemberAssignmentKey key,
|
||||||
|
ConsumerGroupCurrentMemberAssignmentValue value
|
||||||
|
) {
|
||||||
|
String groupId = key.groupId();
|
||||||
|
String memberId = key.memberId();
|
||||||
|
ConsumerGroup consumerGroup = getOrMaybeCreateConsumerGroup(groupId, false);
|
||||||
|
ConsumerGroupMember oldMember = consumerGroup.getOrMaybeCreateMember(memberId, false);
|
||||||
|
|
||||||
|
if (value != null) {
|
||||||
|
ConsumerGroupMember newMember = new ConsumerGroupMember.Builder(oldMember)
|
||||||
|
.updateWith(value)
|
||||||
|
.build();
|
||||||
|
consumerGroup.updateMember(newMember);
|
||||||
|
} else {
|
||||||
|
ConsumerGroupMember newMember = new ConsumerGroupMember.Builder(oldMember)
|
||||||
|
.setMemberEpoch(-1)
|
||||||
|
.setPreviousMemberEpoch(-1)
|
||||||
|
.setTargetMemberEpoch(-1)
|
||||||
|
.setAssignedPartitions(Collections.emptyMap())
|
||||||
|
.setPartitionsPendingRevocation(Collections.emptyMap())
|
||||||
|
.setPartitionsPendingAssignment(Collections.emptyMap())
|
||||||
|
.build();
|
||||||
|
consumerGroup.updateMember(newMember);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -330,7 +330,7 @@ public class RecordHelpers {
|
||||||
new ConsumerGroupCurrentMemberAssignmentValue()
|
new ConsumerGroupCurrentMemberAssignmentValue()
|
||||||
.setMemberEpoch(member.memberEpoch())
|
.setMemberEpoch(member.memberEpoch())
|
||||||
.setPreviousMemberEpoch(member.previousMemberEpoch())
|
.setPreviousMemberEpoch(member.previousMemberEpoch())
|
||||||
.setTargetMemberEpoch(member.nextMemberEpoch())
|
.setTargetMemberEpoch(member.targetMemberEpoch())
|
||||||
.setAssignedPartitions(toTopicPartitions(member.assignedPartitions()))
|
.setAssignedPartitions(toTopicPartitions(member.assignedPartitions()))
|
||||||
.setPartitionsPendingRevocation(toTopicPartitions(member.partitionsPendingRevocation()))
|
.setPartitionsPendingRevocation(toTopicPartitions(member.partitionsPendingRevocation()))
|
||||||
.setPartitionsPendingAssignment(toTopicPartitions(member.partitionsPendingAssignment())),
|
.setPartitionsPendingAssignment(toTopicPartitions(member.partitionsPendingAssignment())),
|
||||||
|
|
|
@ -0,0 +1,622 @@
|
||||||
|
/*
|
||||||
|
* 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.common.errors.UnknownMemberIdException;
|
||||||
|
import org.apache.kafka.coordinator.group.Group;
|
||||||
|
import org.apache.kafka.image.TopicImage;
|
||||||
|
import org.apache.kafka.image.TopicsImage;
|
||||||
|
import org.apache.kafka.timeline.SnapshotRegistry;
|
||||||
|
import org.apache.kafka.timeline.TimelineHashMap;
|
||||||
|
import org.apache.kafka.timeline.TimelineInteger;
|
||||||
|
import org.apache.kafka.timeline.TimelineObject;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Consumer Group. All the metadata in this class are backed by
|
||||||
|
* records in the __consumer_offsets partitions.
|
||||||
|
*/
|
||||||
|
public class ConsumerGroup implements Group {
|
||||||
|
|
||||||
|
public enum ConsumerGroupState {
|
||||||
|
EMPTY("empty"),
|
||||||
|
ASSIGNING("assigning"),
|
||||||
|
RECONCILING("reconciling"),
|
||||||
|
STABLE("stable"),
|
||||||
|
DEAD("dead");
|
||||||
|
|
||||||
|
private final String name;
|
||||||
|
|
||||||
|
ConsumerGroupState(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The snapshot registry.
|
||||||
|
*/
|
||||||
|
private final SnapshotRegistry snapshotRegistry;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The group id.
|
||||||
|
*/
|
||||||
|
private final String groupId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The group state.
|
||||||
|
*/
|
||||||
|
private final TimelineObject<ConsumerGroupState> state;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The group epoch. The epoch is incremented whenever the subscriptions
|
||||||
|
* are updated and it will trigger the computation of a new assignment
|
||||||
|
* for the group.
|
||||||
|
*/
|
||||||
|
private final TimelineInteger groupEpoch;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The group members.
|
||||||
|
*/
|
||||||
|
private final TimelineHashMap<String, ConsumerGroupMember> members;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The number of members supporting each server assignor name.
|
||||||
|
*/
|
||||||
|
private final TimelineHashMap<String, Integer> serverAssignors;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The number of subscribers per topic.
|
||||||
|
*/
|
||||||
|
private final TimelineHashMap<String, Integer> subscribedTopicNames;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The metadata associated with each subscribed topic name.
|
||||||
|
*/
|
||||||
|
private final TimelineHashMap<String, TopicMetadata> subscribedTopicMetadata;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The target assignment epoch. An assignment epoch smaller than the group epoch
|
||||||
|
* means that a new assignment is required. The assignment epoch is updated when
|
||||||
|
* a new assignment is installed.
|
||||||
|
*/
|
||||||
|
private final TimelineInteger targetAssignmentEpoch;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The target assignment per member id.
|
||||||
|
*/
|
||||||
|
private final TimelineHashMap<String, Assignment> targetAssignment;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The current partition epoch maps each topic-partitions to their current epoch where
|
||||||
|
* the epoch is the epoch of their owners. When a member revokes a partition, it removes
|
||||||
|
* its epochs from this map. When a member gets a partition, it adds its epochs to this map.
|
||||||
|
*/
|
||||||
|
private final TimelineHashMap<Uuid, TimelineHashMap<Integer, Integer>> currentPartitionEpoch;
|
||||||
|
|
||||||
|
public ConsumerGroup(
|
||||||
|
SnapshotRegistry snapshotRegistry,
|
||||||
|
String groupId
|
||||||
|
) {
|
||||||
|
this.snapshotRegistry = Objects.requireNonNull(snapshotRegistry);
|
||||||
|
this.groupId = Objects.requireNonNull(groupId);
|
||||||
|
this.state = new TimelineObject<>(snapshotRegistry, ConsumerGroupState.EMPTY);
|
||||||
|
this.groupEpoch = new TimelineInteger(snapshotRegistry);
|
||||||
|
this.members = new TimelineHashMap<>(snapshotRegistry, 0);
|
||||||
|
this.serverAssignors = new TimelineHashMap<>(snapshotRegistry, 0);
|
||||||
|
this.subscribedTopicNames = new TimelineHashMap<>(snapshotRegistry, 0);
|
||||||
|
this.subscribedTopicMetadata = new TimelineHashMap<>(snapshotRegistry, 0);
|
||||||
|
this.targetAssignmentEpoch = new TimelineInteger(snapshotRegistry);
|
||||||
|
this.targetAssignment = new TimelineHashMap<>(snapshotRegistry, 0);
|
||||||
|
this.currentPartitionEpoch = new TimelineHashMap<>(snapshotRegistry, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return The group type (Consumer).
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public GroupType type() {
|
||||||
|
return GroupType.CONSUMER;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return The current state as a String.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public String stateAsString() {
|
||||||
|
return state.get().toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return The group id.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public String groupId() {
|
||||||
|
return groupId;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return The current state.
|
||||||
|
*/
|
||||||
|
public ConsumerGroupState state() {
|
||||||
|
return state.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return The group epoch.
|
||||||
|
*/
|
||||||
|
public int groupEpoch() {
|
||||||
|
return groupEpoch.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the group epoch.
|
||||||
|
*
|
||||||
|
* @param groupEpoch The new group epoch.
|
||||||
|
*/
|
||||||
|
public void setGroupEpoch(int groupEpoch) {
|
||||||
|
this.groupEpoch.set(groupEpoch);
|
||||||
|
maybeUpdateGroupState();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return The target assignment epoch.
|
||||||
|
*/
|
||||||
|
public int assignmentEpoch() {
|
||||||
|
return targetAssignmentEpoch.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the assignment epoch.
|
||||||
|
*
|
||||||
|
* @param targetAssignmentEpoch The new assignment epoch.
|
||||||
|
*/
|
||||||
|
public void setTargetAssignmentEpoch(int targetAssignmentEpoch) {
|
||||||
|
this.targetAssignmentEpoch.set(targetAssignmentEpoch);
|
||||||
|
maybeUpdateGroupState();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets or creates a member.
|
||||||
|
*
|
||||||
|
* @param memberId The member id.
|
||||||
|
* @param createIfNotExists Booleans indicating whether the member must be
|
||||||
|
* created if it does not exist.
|
||||||
|
*
|
||||||
|
* @return A ConsumerGroupMember.
|
||||||
|
*/
|
||||||
|
public ConsumerGroupMember getOrMaybeCreateMember(
|
||||||
|
String memberId,
|
||||||
|
boolean createIfNotExists
|
||||||
|
) {
|
||||||
|
ConsumerGroupMember member = members.get(memberId);
|
||||||
|
if (member == null) {
|
||||||
|
if (!createIfNotExists) {
|
||||||
|
throw new UnknownMemberIdException(String.format("Member %s is not a member of group %s.",
|
||||||
|
memberId, groupId));
|
||||||
|
}
|
||||||
|
member = new ConsumerGroupMember.Builder(memberId).build();
|
||||||
|
members.put(memberId, member);
|
||||||
|
}
|
||||||
|
|
||||||
|
return member;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates the member.
|
||||||
|
*
|
||||||
|
* @param newMember The new member state.
|
||||||
|
*/
|
||||||
|
public void updateMember(ConsumerGroupMember newMember) {
|
||||||
|
if (newMember == null) {
|
||||||
|
throw new IllegalArgumentException("newMember cannot be null.");
|
||||||
|
}
|
||||||
|
ConsumerGroupMember oldMember = members.put(newMember.memberId(), newMember);
|
||||||
|
maybeUpdateSubscribedTopicNames(oldMember, newMember);
|
||||||
|
maybeUpdateServerAssignors(oldMember, newMember);
|
||||||
|
maybeUpdatePartitionEpoch(oldMember, newMember);
|
||||||
|
maybeUpdateGroupState();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove the member from the group.
|
||||||
|
*
|
||||||
|
* @param memberId The member id to remove.
|
||||||
|
*/
|
||||||
|
public void removeMember(String memberId) {
|
||||||
|
ConsumerGroupMember member = members.remove(memberId);
|
||||||
|
maybeRemovePartitionEpoch(member);
|
||||||
|
maybeUpdateGroupState();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if the member exists.
|
||||||
|
*
|
||||||
|
* @param memberId The member id.
|
||||||
|
*
|
||||||
|
* @return A boolean indicating whether the member exists or not.
|
||||||
|
*/
|
||||||
|
public boolean hasMember(String memberId) {
|
||||||
|
return members.containsKey(memberId);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return The number of members.
|
||||||
|
*/
|
||||||
|
public int numMembers() {
|
||||||
|
return members.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return An immutable Map containing all the members keyed by their id.
|
||||||
|
*/
|
||||||
|
public Map<String, ConsumerGroupMember> members() {
|
||||||
|
return Collections.unmodifiableMap(members);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the target assignment of the member.
|
||||||
|
*
|
||||||
|
* @return The ConsumerGroupMemberAssignment or an EMPTY one if it does not
|
||||||
|
* exist.
|
||||||
|
*/
|
||||||
|
public Assignment targetAssignment(String memberId) {
|
||||||
|
return targetAssignment.getOrDefault(memberId, Assignment.EMPTY);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates target assignment of a member.
|
||||||
|
*
|
||||||
|
* @param memberId The member id.
|
||||||
|
* @param newTargetAssignment The new target assignment.
|
||||||
|
*/
|
||||||
|
public void updateTargetAssignment(String memberId, Assignment newTargetAssignment) {
|
||||||
|
targetAssignment.put(memberId, newTargetAssignment);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes the target assignment of a member.
|
||||||
|
*
|
||||||
|
* @param memberId The member id.
|
||||||
|
*/
|
||||||
|
public void removeTargetAssignment(String memberId) {
|
||||||
|
targetAssignment.remove(memberId);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return An immutable Map containing all the target assignment keyed by member id.
|
||||||
|
*/
|
||||||
|
public Map<String, Assignment> targetAssignment() {
|
||||||
|
return Collections.unmodifiableMap(targetAssignment);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the current epoch of a partition or -1 if the partition
|
||||||
|
* does not have one.
|
||||||
|
*
|
||||||
|
* @param topicId The topic id.
|
||||||
|
* @param partitionId The partition id.
|
||||||
|
*
|
||||||
|
* @return The epoch or -1.
|
||||||
|
*/
|
||||||
|
public int currentPartitionEpoch(
|
||||||
|
Uuid topicId, int partitionId
|
||||||
|
) {
|
||||||
|
Map<Integer, Integer> partitions = currentPartitionEpoch.get(topicId);
|
||||||
|
if (partitions == null) {
|
||||||
|
return -1;
|
||||||
|
} else {
|
||||||
|
return partitions.getOrDefault(partitionId, -1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compute the preferred (server side) assignor for the group while
|
||||||
|
* taking into account the updated member. The computation relies
|
||||||
|
* on {{@link ConsumerGroup#serverAssignors}} persisted structure
|
||||||
|
* but it does not update it.
|
||||||
|
*
|
||||||
|
* @param oldMember The old member.
|
||||||
|
* @param newMember The new member.
|
||||||
|
*
|
||||||
|
* @return An Optional containing the preferred assignor.
|
||||||
|
*/
|
||||||
|
public Optional<String> computePreferredServerAssignor(
|
||||||
|
ConsumerGroupMember oldMember,
|
||||||
|
ConsumerGroupMember newMember
|
||||||
|
) {
|
||||||
|
// Copy the current count and update it.
|
||||||
|
Map<String, Integer> counts = new HashMap<>(this.serverAssignors);
|
||||||
|
maybeUpdateServerAssignors(counts, oldMember, newMember);
|
||||||
|
|
||||||
|
return counts.entrySet().stream()
|
||||||
|
.max(Map.Entry.comparingByValue())
|
||||||
|
.map(Map.Entry::getKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return The preferred assignor for the group.
|
||||||
|
*/
|
||||||
|
public Optional<String> preferredServerAssignor() {
|
||||||
|
return serverAssignors.entrySet().stream()
|
||||||
|
.max(Map.Entry.comparingByValue())
|
||||||
|
.map(Map.Entry::getKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return An immutable Map containing the subscription metadata for all the topics whose
|
||||||
|
* members are subscribed to.
|
||||||
|
*/
|
||||||
|
public Map<String, TopicMetadata> subscriptionMetadata() {
|
||||||
|
return Collections.unmodifiableMap(subscribedTopicMetadata);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates the subscription metadata. This replaces the previous one.
|
||||||
|
*
|
||||||
|
* @param subscriptionMetadata The new subscription metadata.
|
||||||
|
*/
|
||||||
|
public void setSubscriptionMetadata(
|
||||||
|
Map<String, TopicMetadata> subscriptionMetadata
|
||||||
|
) {
|
||||||
|
this.subscribedTopicMetadata.clear();
|
||||||
|
this.subscribedTopicMetadata.putAll(subscriptionMetadata);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Computes the subscription metadata based on the current subscription and
|
||||||
|
* an updated member.
|
||||||
|
*
|
||||||
|
* @param oldMember The old member.
|
||||||
|
* @param newMember The new member.
|
||||||
|
* @param topicsImage The topic metadata.
|
||||||
|
*
|
||||||
|
* @return The new subscription metadata as an immutable Map.
|
||||||
|
*/
|
||||||
|
public Map<String, TopicMetadata> computeSubscriptionMetadata(
|
||||||
|
ConsumerGroupMember oldMember,
|
||||||
|
ConsumerGroupMember newMember,
|
||||||
|
TopicsImage topicsImage
|
||||||
|
) {
|
||||||
|
// Copy and update the current subscriptions.
|
||||||
|
Map<String, Integer> subscribedTopicNames = new HashMap<>(this.subscribedTopicNames);
|
||||||
|
maybeUpdateSubscribedTopicNames(subscribedTopicNames, oldMember, newMember);
|
||||||
|
|
||||||
|
// Create the topic metadata for each subscribed topic.
|
||||||
|
Map<String, TopicMetadata> newSubscriptionMetadata = new HashMap<>(subscribedTopicNames.size());
|
||||||
|
subscribedTopicNames.forEach((topicName, count) -> {
|
||||||
|
TopicImage topicImage = topicsImage.getTopic(topicName);
|
||||||
|
if (topicImage != null) {
|
||||||
|
newSubscriptionMetadata.put(topicName, new TopicMetadata(
|
||||||
|
topicImage.id(),
|
||||||
|
topicImage.name(),
|
||||||
|
topicImage.partitions().size()
|
||||||
|
));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return Collections.unmodifiableMap(newSubscriptionMetadata);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates the current state of the group.
|
||||||
|
*/
|
||||||
|
private void maybeUpdateGroupState() {
|
||||||
|
if (members.isEmpty()) {
|
||||||
|
state.set(ConsumerGroupState.EMPTY);
|
||||||
|
} else if (groupEpoch.get() > targetAssignmentEpoch.get()) {
|
||||||
|
state.set(ConsumerGroupState.ASSIGNING);
|
||||||
|
} else {
|
||||||
|
for (ConsumerGroupMember member : members.values()) {
|
||||||
|
if (member.targetMemberEpoch() != targetAssignmentEpoch.get() || member.state() != ConsumerGroupMember.MemberState.STABLE) {
|
||||||
|
state.set(ConsumerGroupState.RECONCILING);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
state.set(ConsumerGroupState.STABLE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates the server assignors count.
|
||||||
|
*
|
||||||
|
* @param oldMember The old member.
|
||||||
|
* @param newMember The new member.
|
||||||
|
*/
|
||||||
|
private void maybeUpdateServerAssignors(
|
||||||
|
ConsumerGroupMember oldMember,
|
||||||
|
ConsumerGroupMember newMember
|
||||||
|
) {
|
||||||
|
maybeUpdateServerAssignors(serverAssignors, oldMember, newMember);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates the server assignors count.
|
||||||
|
*
|
||||||
|
* @param serverAssignorCount The count to update.
|
||||||
|
* @param oldMember The old member.
|
||||||
|
* @param newMember The new member.
|
||||||
|
*/
|
||||||
|
private static void maybeUpdateServerAssignors(
|
||||||
|
Map<String, Integer> serverAssignorCount,
|
||||||
|
ConsumerGroupMember oldMember,
|
||||||
|
ConsumerGroupMember newMember
|
||||||
|
) {
|
||||||
|
if (oldMember != null) {
|
||||||
|
oldMember.serverAssignorName().ifPresent(name ->
|
||||||
|
serverAssignorCount.compute(name, ConsumerGroup::decValue)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (newMember != null) {
|
||||||
|
newMember.serverAssignorName().ifPresent(name ->
|
||||||
|
serverAssignorCount.compute(name, ConsumerGroup::incValue)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates the subscribed topic names count.
|
||||||
|
*
|
||||||
|
* @param oldMember The old member.
|
||||||
|
* @param newMember The new member.
|
||||||
|
*/
|
||||||
|
private void maybeUpdateSubscribedTopicNames(
|
||||||
|
ConsumerGroupMember oldMember,
|
||||||
|
ConsumerGroupMember newMember
|
||||||
|
) {
|
||||||
|
maybeUpdateSubscribedTopicNames(subscribedTopicNames, oldMember, newMember);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates the subscription count.
|
||||||
|
*
|
||||||
|
* @param subscribedTopicCount The map to update.
|
||||||
|
* @param oldMember The old member.
|
||||||
|
* @param newMember The new member.
|
||||||
|
*/
|
||||||
|
private static void maybeUpdateSubscribedTopicNames(
|
||||||
|
Map<String, Integer> subscribedTopicCount,
|
||||||
|
ConsumerGroupMember oldMember,
|
||||||
|
ConsumerGroupMember newMember
|
||||||
|
) {
|
||||||
|
if (oldMember != null) {
|
||||||
|
oldMember.subscribedTopicNames().forEach(topicName ->
|
||||||
|
subscribedTopicCount.compute(topicName, ConsumerGroup::decValue)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (newMember != null) {
|
||||||
|
newMember.subscribedTopicNames().forEach(topicName ->
|
||||||
|
subscribedTopicCount.compute(topicName, ConsumerGroup::incValue)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates the partition epochs based on the old and the new member.
|
||||||
|
*
|
||||||
|
* @param oldMember The old member.
|
||||||
|
* @param newMember The new member.
|
||||||
|
*/
|
||||||
|
private void maybeUpdatePartitionEpoch(
|
||||||
|
ConsumerGroupMember oldMember,
|
||||||
|
ConsumerGroupMember newMember
|
||||||
|
) {
|
||||||
|
if (oldMember == null) {
|
||||||
|
addPartitionEpochs(newMember.assignedPartitions(), newMember.memberEpoch());
|
||||||
|
addPartitionEpochs(newMember.partitionsPendingRevocation(), newMember.memberEpoch());
|
||||||
|
} else {
|
||||||
|
if (!oldMember.assignedPartitions().equals(newMember.assignedPartitions())) {
|
||||||
|
removePartitionEpochs(oldMember.assignedPartitions());
|
||||||
|
addPartitionEpochs(newMember.assignedPartitions(), newMember.memberEpoch());
|
||||||
|
}
|
||||||
|
if (!oldMember.partitionsPendingRevocation().equals(newMember.partitionsPendingRevocation())) {
|
||||||
|
removePartitionEpochs(oldMember.partitionsPendingRevocation());
|
||||||
|
addPartitionEpochs(newMember.partitionsPendingRevocation(), newMember.memberEpoch());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes the partition epochs for the provided member.
|
||||||
|
*
|
||||||
|
* @param oldMember The old member.
|
||||||
|
*/
|
||||||
|
private void maybeRemovePartitionEpoch(
|
||||||
|
ConsumerGroupMember oldMember
|
||||||
|
) {
|
||||||
|
if (oldMember != null) {
|
||||||
|
removePartitionEpochs(oldMember.assignedPartitions());
|
||||||
|
removePartitionEpochs(oldMember.partitionsPendingRevocation());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes the partition epochs based on the provided assignment.
|
||||||
|
*
|
||||||
|
* @param assignment The assignment.
|
||||||
|
*/
|
||||||
|
private void removePartitionEpochs(
|
||||||
|
Map<Uuid, Set<Integer>> assignment
|
||||||
|
) {
|
||||||
|
assignment.forEach((topicId, assignedPartitions) -> {
|
||||||
|
currentPartitionEpoch.compute(topicId, (__, partitionsOrNull) -> {
|
||||||
|
if (partitionsOrNull != null) {
|
||||||
|
assignedPartitions.forEach(partitionsOrNull::remove);
|
||||||
|
if (partitionsOrNull.isEmpty()) {
|
||||||
|
return null;
|
||||||
|
} else {
|
||||||
|
return partitionsOrNull;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds the partitions epoch based on the provided assignment.
|
||||||
|
*
|
||||||
|
* @param assignment The assignment.
|
||||||
|
* @param epoch The new epoch.
|
||||||
|
*/
|
||||||
|
private void addPartitionEpochs(
|
||||||
|
Map<Uuid, Set<Integer>> assignment,
|
||||||
|
int epoch
|
||||||
|
) {
|
||||||
|
assignment.forEach((topicId, assignedPartitions) -> {
|
||||||
|
currentPartitionEpoch.compute(topicId, (__, partitionsOrNull) -> {
|
||||||
|
if (partitionsOrNull == null) {
|
||||||
|
partitionsOrNull = new TimelineHashMap<>(snapshotRegistry, assignedPartitions.size());
|
||||||
|
}
|
||||||
|
for (Integer partitionId : assignedPartitions) {
|
||||||
|
partitionsOrNull.put(partitionId, epoch);
|
||||||
|
}
|
||||||
|
return partitionsOrNull;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decrements value by 1; returns null when reaching zero. This helper is
|
||||||
|
* meant to be used with Map#compute.
|
||||||
|
*/
|
||||||
|
private static Integer decValue(String key, Integer value) {
|
||||||
|
if (value == null) return null;
|
||||||
|
return value == 1 ? null : value - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Increments value by 1; This helper is meant to be used with Map#compute.
|
||||||
|
*/
|
||||||
|
private static Integer incValue(String key, Integer value) {
|
||||||
|
return value == null ? 1 : value + 1;
|
||||||
|
}
|
||||||
|
}
|
|
@ -48,7 +48,7 @@ public class ConsumerGroupMember {
|
||||||
private final String memberId;
|
private final String memberId;
|
||||||
private int memberEpoch = 0;
|
private int memberEpoch = 0;
|
||||||
private int previousMemberEpoch = -1;
|
private int previousMemberEpoch = -1;
|
||||||
private int nextMemberEpoch = 0;
|
private int targetMemberEpoch = 0;
|
||||||
private String instanceId = null;
|
private String instanceId = null;
|
||||||
private String rackId = null;
|
private String rackId = null;
|
||||||
private int rebalanceTimeoutMs = -1;
|
private int rebalanceTimeoutMs = -1;
|
||||||
|
@ -72,7 +72,7 @@ public class ConsumerGroupMember {
|
||||||
this.memberId = member.memberId;
|
this.memberId = member.memberId;
|
||||||
this.memberEpoch = member.memberEpoch;
|
this.memberEpoch = member.memberEpoch;
|
||||||
this.previousMemberEpoch = member.previousMemberEpoch;
|
this.previousMemberEpoch = member.previousMemberEpoch;
|
||||||
this.nextMemberEpoch = member.nextMemberEpoch;
|
this.targetMemberEpoch = member.targetMemberEpoch;
|
||||||
this.instanceId = member.instanceId;
|
this.instanceId = member.instanceId;
|
||||||
this.rackId = member.rackId;
|
this.rackId = member.rackId;
|
||||||
this.rebalanceTimeoutMs = member.rebalanceTimeoutMs;
|
this.rebalanceTimeoutMs = member.rebalanceTimeoutMs;
|
||||||
|
@ -97,8 +97,8 @@ public class ConsumerGroupMember {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Builder setNextMemberEpoch(int nextMemberEpoch) {
|
public Builder setTargetMemberEpoch(int targetMemberEpoch) {
|
||||||
this.nextMemberEpoch = nextMemberEpoch;
|
this.targetMemberEpoch = targetMemberEpoch;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -217,7 +217,7 @@ public class ConsumerGroupMember {
|
||||||
public Builder updateWith(ConsumerGroupCurrentMemberAssignmentValue record) {
|
public Builder updateWith(ConsumerGroupCurrentMemberAssignmentValue record) {
|
||||||
setMemberEpoch(record.memberEpoch());
|
setMemberEpoch(record.memberEpoch());
|
||||||
setPreviousMemberEpoch(record.previousMemberEpoch());
|
setPreviousMemberEpoch(record.previousMemberEpoch());
|
||||||
setNextMemberEpoch(record.targetMemberEpoch());
|
setTargetMemberEpoch(record.targetMemberEpoch());
|
||||||
setAssignedPartitions(assignmentFromTopicPartitions(record.assignedPartitions()));
|
setAssignedPartitions(assignmentFromTopicPartitions(record.assignedPartitions()));
|
||||||
setPartitionsPendingRevocation(assignmentFromTopicPartitions(record.partitionsPendingRevocation()));
|
setPartitionsPendingRevocation(assignmentFromTopicPartitions(record.partitionsPendingRevocation()));
|
||||||
setPartitionsPendingAssignment(assignmentFromTopicPartitions(record.partitionsPendingAssignment()));
|
setPartitionsPendingAssignment(assignmentFromTopicPartitions(record.partitionsPendingAssignment()));
|
||||||
|
@ -246,7 +246,7 @@ public class ConsumerGroupMember {
|
||||||
memberId,
|
memberId,
|
||||||
memberEpoch,
|
memberEpoch,
|
||||||
previousMemberEpoch,
|
previousMemberEpoch,
|
||||||
nextMemberEpoch,
|
targetMemberEpoch,
|
||||||
instanceId,
|
instanceId,
|
||||||
rackId,
|
rackId,
|
||||||
rebalanceTimeoutMs,
|
rebalanceTimeoutMs,
|
||||||
|
@ -305,7 +305,7 @@ public class ConsumerGroupMember {
|
||||||
* assignment epoch used to compute the current assigned,
|
* assignment epoch used to compute the current assigned,
|
||||||
* revoking and assigning partitions.
|
* revoking and assigning partitions.
|
||||||
*/
|
*/
|
||||||
private final int nextMemberEpoch;
|
private final int targetMemberEpoch;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The instance id provided by the member.
|
* The instance id provided by the member.
|
||||||
|
@ -378,7 +378,7 @@ public class ConsumerGroupMember {
|
||||||
String memberId,
|
String memberId,
|
||||||
int memberEpoch,
|
int memberEpoch,
|
||||||
int previousMemberEpoch,
|
int previousMemberEpoch,
|
||||||
int nextMemberEpoch,
|
int targetMemberEpoch,
|
||||||
String instanceId,
|
String instanceId,
|
||||||
String rackId,
|
String rackId,
|
||||||
int rebalanceTimeoutMs,
|
int rebalanceTimeoutMs,
|
||||||
|
@ -396,7 +396,7 @@ public class ConsumerGroupMember {
|
||||||
this.memberId = memberId;
|
this.memberId = memberId;
|
||||||
this.memberEpoch = memberEpoch;
|
this.memberEpoch = memberEpoch;
|
||||||
this.previousMemberEpoch = previousMemberEpoch;
|
this.previousMemberEpoch = previousMemberEpoch;
|
||||||
this.nextMemberEpoch = nextMemberEpoch;
|
this.targetMemberEpoch = targetMemberEpoch;
|
||||||
this.instanceId = instanceId;
|
this.instanceId = instanceId;
|
||||||
this.rackId = rackId;
|
this.rackId = rackId;
|
||||||
this.rebalanceTimeoutMs = rebalanceTimeoutMs;
|
this.rebalanceTimeoutMs = rebalanceTimeoutMs;
|
||||||
|
@ -434,10 +434,10 @@ public class ConsumerGroupMember {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return The next member epoch.
|
* @return The target member epoch.
|
||||||
*/
|
*/
|
||||||
public int nextMemberEpoch() {
|
public int targetMemberEpoch() {
|
||||||
return nextMemberEpoch;
|
return targetMemberEpoch;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -535,10 +535,9 @@ public class ConsumerGroupMember {
|
||||||
* @return A string representation of the current assignment state.
|
* @return A string representation of the current assignment state.
|
||||||
*/
|
*/
|
||||||
public String currentAssignmentSummary() {
|
public String currentAssignmentSummary() {
|
||||||
return "CurrentAssignment(" +
|
return "CurrentAssignment(memberEpoch=" + memberEpoch +
|
||||||
", memberEpoch=" + memberEpoch +
|
|
||||||
", previousMemberEpoch=" + previousMemberEpoch +
|
", previousMemberEpoch=" + previousMemberEpoch +
|
||||||
", nextMemberEpoch=" + nextMemberEpoch +
|
", targetMemberEpoch=" + targetMemberEpoch +
|
||||||
", state=" + state +
|
", state=" + state +
|
||||||
", assignedPartitions=" + assignedPartitions +
|
", assignedPartitions=" + assignedPartitions +
|
||||||
", partitionsPendingRevocation=" + partitionsPendingRevocation +
|
", partitionsPendingRevocation=" + partitionsPendingRevocation +
|
||||||
|
@ -553,7 +552,7 @@ public class ConsumerGroupMember {
|
||||||
ConsumerGroupMember that = (ConsumerGroupMember) o;
|
ConsumerGroupMember that = (ConsumerGroupMember) o;
|
||||||
return memberEpoch == that.memberEpoch
|
return memberEpoch == that.memberEpoch
|
||||||
&& previousMemberEpoch == that.previousMemberEpoch
|
&& previousMemberEpoch == that.previousMemberEpoch
|
||||||
&& nextMemberEpoch == that.nextMemberEpoch
|
&& targetMemberEpoch == that.targetMemberEpoch
|
||||||
&& rebalanceTimeoutMs == that.rebalanceTimeoutMs
|
&& rebalanceTimeoutMs == that.rebalanceTimeoutMs
|
||||||
&& Objects.equals(memberId, that.memberId)
|
&& Objects.equals(memberId, that.memberId)
|
||||||
&& Objects.equals(instanceId, that.instanceId)
|
&& Objects.equals(instanceId, that.instanceId)
|
||||||
|
@ -574,7 +573,7 @@ public class ConsumerGroupMember {
|
||||||
int result = memberId != null ? memberId.hashCode() : 0;
|
int result = memberId != null ? memberId.hashCode() : 0;
|
||||||
result = 31 * result + memberEpoch;
|
result = 31 * result + memberEpoch;
|
||||||
result = 31 * result + previousMemberEpoch;
|
result = 31 * result + previousMemberEpoch;
|
||||||
result = 31 * result + nextMemberEpoch;
|
result = 31 * result + targetMemberEpoch;
|
||||||
result = 31 * result + Objects.hashCode(instanceId);
|
result = 31 * result + Objects.hashCode(instanceId);
|
||||||
result = 31 * result + Objects.hashCode(rackId);
|
result = 31 * result + Objects.hashCode(rackId);
|
||||||
result = 31 * result + rebalanceTimeoutMs;
|
result = 31 * result + rebalanceTimeoutMs;
|
||||||
|
@ -596,7 +595,7 @@ public class ConsumerGroupMember {
|
||||||
"memberId='" + memberId + '\'' +
|
"memberId='" + memberId + '\'' +
|
||||||
", memberEpoch=" + memberEpoch +
|
", memberEpoch=" + memberEpoch +
|
||||||
", previousMemberEpoch=" + previousMemberEpoch +
|
", previousMemberEpoch=" + previousMemberEpoch +
|
||||||
", nextMemberEpoch=" + nextMemberEpoch +
|
", targetMemberEpoch=" + targetMemberEpoch +
|
||||||
", instanceId='" + instanceId + '\'' +
|
", instanceId='" + instanceId + '\'' +
|
||||||
", rackId='" + rackId + '\'' +
|
", rackId='" + rackId + '\'' +
|
||||||
", rebalanceTimeoutMs=" + rebalanceTimeoutMs +
|
", rebalanceTimeoutMs=" + rebalanceTimeoutMs +
|
||||||
|
|
|
@ -172,7 +172,7 @@ public class CurrentAssignmentBuilder {
|
||||||
public ConsumerGroupMember build() {
|
public ConsumerGroupMember build() {
|
||||||
// A new target assignment has been installed, we need to restart
|
// A new target assignment has been installed, we need to restart
|
||||||
// the reconciliation loop from the beginning.
|
// the reconciliation loop from the beginning.
|
||||||
if (targetAssignmentEpoch != member.nextMemberEpoch()) {
|
if (targetAssignmentEpoch != member.targetMemberEpoch()) {
|
||||||
return transitionToNewTargetAssignmentState();
|
return transitionToNewTargetAssignmentState();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -258,7 +258,7 @@ public class CurrentAssignmentBuilder {
|
||||||
.setAssignedPartitions(newAssignedPartitions)
|
.setAssignedPartitions(newAssignedPartitions)
|
||||||
.setPartitionsPendingRevocation(newPartitionsPendingRevocation)
|
.setPartitionsPendingRevocation(newPartitionsPendingRevocation)
|
||||||
.setPartitionsPendingAssignment(newPartitionsPendingAssignment)
|
.setPartitionsPendingAssignment(newPartitionsPendingAssignment)
|
||||||
.setNextMemberEpoch(targetAssignmentEpoch)
|
.setTargetMemberEpoch(targetAssignmentEpoch)
|
||||||
.build();
|
.build();
|
||||||
} else {
|
} else {
|
||||||
if (!newPartitionsPendingAssignment.isEmpty()) {
|
if (!newPartitionsPendingAssignment.isEmpty()) {
|
||||||
|
@ -277,7 +277,7 @@ public class CurrentAssignmentBuilder {
|
||||||
.setPartitionsPendingAssignment(newPartitionsPendingAssignment)
|
.setPartitionsPendingAssignment(newPartitionsPendingAssignment)
|
||||||
.setPreviousMemberEpoch(member.memberEpoch())
|
.setPreviousMemberEpoch(member.memberEpoch())
|
||||||
.setMemberEpoch(targetAssignmentEpoch)
|
.setMemberEpoch(targetAssignmentEpoch)
|
||||||
.setNextMemberEpoch(targetAssignmentEpoch)
|
.setTargetMemberEpoch(targetAssignmentEpoch)
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -311,7 +311,7 @@ public class CurrentAssignmentBuilder {
|
||||||
.setPartitionsPendingAssignment(newPartitionsPendingAssignment)
|
.setPartitionsPendingAssignment(newPartitionsPendingAssignment)
|
||||||
.setPreviousMemberEpoch(member.memberEpoch())
|
.setPreviousMemberEpoch(member.memberEpoch())
|
||||||
.setMemberEpoch(targetAssignmentEpoch)
|
.setMemberEpoch(targetAssignmentEpoch)
|
||||||
.setNextMemberEpoch(targetAssignmentEpoch)
|
.setTargetMemberEpoch(targetAssignmentEpoch)
|
||||||
.build();
|
.build();
|
||||||
} else {
|
} else {
|
||||||
return member;
|
return member;
|
||||||
|
@ -340,7 +340,7 @@ public class CurrentAssignmentBuilder {
|
||||||
.setPartitionsPendingAssignment(newPartitionsPendingAssignment)
|
.setPartitionsPendingAssignment(newPartitionsPendingAssignment)
|
||||||
.setPreviousMemberEpoch(member.memberEpoch())
|
.setPreviousMemberEpoch(member.memberEpoch())
|
||||||
.setMemberEpoch(targetAssignmentEpoch)
|
.setMemberEpoch(targetAssignmentEpoch)
|
||||||
.setNextMemberEpoch(targetAssignmentEpoch)
|
.setTargetMemberEpoch(targetAssignmentEpoch)
|
||||||
.build();
|
.build();
|
||||||
} else {
|
} else {
|
||||||
return member;
|
return member;
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -378,7 +378,7 @@ public class RecordHelpersTest {
|
||||||
new ConsumerGroupMember.Builder("member-id")
|
new ConsumerGroupMember.Builder("member-id")
|
||||||
.setMemberEpoch(22)
|
.setMemberEpoch(22)
|
||||||
.setPreviousMemberEpoch(21)
|
.setPreviousMemberEpoch(21)
|
||||||
.setNextMemberEpoch(23)
|
.setTargetMemberEpoch(23)
|
||||||
.setAssignedPartitions(assigned)
|
.setAssignedPartitions(assigned)
|
||||||
.setPartitionsPendingRevocation(revoking)
|
.setPartitionsPendingRevocation(revoking)
|
||||||
.setPartitionsPendingAssignment(assigning)
|
.setPartitionsPendingAssignment(assigning)
|
||||||
|
|
|
@ -42,7 +42,7 @@ public class ConsumerGroupMemberTest {
|
||||||
ConsumerGroupMember member = new ConsumerGroupMember.Builder("member-id")
|
ConsumerGroupMember member = new ConsumerGroupMember.Builder("member-id")
|
||||||
.setMemberEpoch(10)
|
.setMemberEpoch(10)
|
||||||
.setPreviousMemberEpoch(9)
|
.setPreviousMemberEpoch(9)
|
||||||
.setNextMemberEpoch(11)
|
.setTargetMemberEpoch(11)
|
||||||
.setInstanceId("instance-id")
|
.setInstanceId("instance-id")
|
||||||
.setRackId("rack-id")
|
.setRackId("rack-id")
|
||||||
.setRebalanceTimeoutMs(5000)
|
.setRebalanceTimeoutMs(5000)
|
||||||
|
@ -71,7 +71,7 @@ public class ConsumerGroupMemberTest {
|
||||||
assertEquals("member-id", member.memberId());
|
assertEquals("member-id", member.memberId());
|
||||||
assertEquals(10, member.memberEpoch());
|
assertEquals(10, member.memberEpoch());
|
||||||
assertEquals(9, member.previousMemberEpoch());
|
assertEquals(9, member.previousMemberEpoch());
|
||||||
assertEquals(11, member.nextMemberEpoch());
|
assertEquals(11, member.targetMemberEpoch());
|
||||||
assertEquals("instance-id", member.instanceId());
|
assertEquals("instance-id", member.instanceId());
|
||||||
assertEquals("rack-id", member.rackId());
|
assertEquals("rack-id", member.rackId());
|
||||||
assertEquals("client-id", member.clientId());
|
assertEquals("client-id", member.clientId());
|
||||||
|
@ -105,7 +105,7 @@ public class ConsumerGroupMemberTest {
|
||||||
ConsumerGroupMember member1 = new ConsumerGroupMember.Builder("member-id")
|
ConsumerGroupMember member1 = new ConsumerGroupMember.Builder("member-id")
|
||||||
.setMemberEpoch(10)
|
.setMemberEpoch(10)
|
||||||
.setPreviousMemberEpoch(9)
|
.setPreviousMemberEpoch(9)
|
||||||
.setNextMemberEpoch(11)
|
.setTargetMemberEpoch(11)
|
||||||
.setInstanceId("instance-id")
|
.setInstanceId("instance-id")
|
||||||
.setRackId("rack-id")
|
.setRackId("rack-id")
|
||||||
.setRebalanceTimeoutMs(5000)
|
.setRebalanceTimeoutMs(5000)
|
||||||
|
@ -134,7 +134,7 @@ public class ConsumerGroupMemberTest {
|
||||||
ConsumerGroupMember member2 = new ConsumerGroupMember.Builder("member-id")
|
ConsumerGroupMember member2 = new ConsumerGroupMember.Builder("member-id")
|
||||||
.setMemberEpoch(10)
|
.setMemberEpoch(10)
|
||||||
.setPreviousMemberEpoch(9)
|
.setPreviousMemberEpoch(9)
|
||||||
.setNextMemberEpoch(11)
|
.setTargetMemberEpoch(11)
|
||||||
.setInstanceId("instance-id")
|
.setInstanceId("instance-id")
|
||||||
.setRackId("rack-id")
|
.setRackId("rack-id")
|
||||||
.setRebalanceTimeoutMs(5000)
|
.setRebalanceTimeoutMs(5000)
|
||||||
|
@ -172,7 +172,7 @@ public class ConsumerGroupMemberTest {
|
||||||
ConsumerGroupMember member = new ConsumerGroupMember.Builder("member-id")
|
ConsumerGroupMember member = new ConsumerGroupMember.Builder("member-id")
|
||||||
.setMemberEpoch(10)
|
.setMemberEpoch(10)
|
||||||
.setPreviousMemberEpoch(9)
|
.setPreviousMemberEpoch(9)
|
||||||
.setNextMemberEpoch(11)
|
.setTargetMemberEpoch(11)
|
||||||
.setInstanceId("instance-id")
|
.setInstanceId("instance-id")
|
||||||
.setRackId("rack-id")
|
.setRackId("rack-id")
|
||||||
.setRebalanceTimeoutMs(5000)
|
.setRebalanceTimeoutMs(5000)
|
||||||
|
@ -299,7 +299,7 @@ public class ConsumerGroupMemberTest {
|
||||||
|
|
||||||
assertEquals(10, member.memberEpoch());
|
assertEquals(10, member.memberEpoch());
|
||||||
assertEquals(9, member.previousMemberEpoch());
|
assertEquals(9, member.previousMemberEpoch());
|
||||||
assertEquals(11, member.nextMemberEpoch());
|
assertEquals(11, member.targetMemberEpoch());
|
||||||
assertEquals(mkAssignment(mkTopicAssignment(topicId1, 0, 1, 2)), member.assignedPartitions());
|
assertEquals(mkAssignment(mkTopicAssignment(topicId1, 0, 1, 2)), member.assignedPartitions());
|
||||||
assertEquals(mkAssignment(mkTopicAssignment(topicId2, 3, 4, 5)), member.partitionsPendingRevocation());
|
assertEquals(mkAssignment(mkTopicAssignment(topicId2, 3, 4, 5)), member.partitionsPendingRevocation());
|
||||||
assertEquals(mkAssignment(mkTopicAssignment(topicId3, 6, 7, 8)), member.partitionsPendingAssignment());
|
assertEquals(mkAssignment(mkTopicAssignment(topicId3, 6, 7, 8)), member.partitionsPendingAssignment());
|
||||||
|
|
|
@ -0,0 +1,544 @@
|
||||||
|
/*
|
||||||
|
* 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.common.errors.UnknownMemberIdException;
|
||||||
|
import org.apache.kafka.common.utils.LogContext;
|
||||||
|
import org.apache.kafka.coordinator.group.GroupMetadataManagerTest;
|
||||||
|
import org.apache.kafka.image.TopicsImage;
|
||||||
|
import org.apache.kafka.timeline.SnapshotRegistry;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import static org.apache.kafka.common.utils.Utils.mkEntry;
|
||||||
|
import static org.apache.kafka.common.utils.Utils.mkMap;
|
||||||
|
import static org.apache.kafka.coordinator.group.AssignmentTestUtil.mkAssignment;
|
||||||
|
import static org.apache.kafka.coordinator.group.AssignmentTestUtil.mkTopicAssignment;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
public class ConsumerGroupTest {
|
||||||
|
|
||||||
|
private ConsumerGroup createConsumerGroup(String groupId) {
|
||||||
|
SnapshotRegistry snapshotRegistry = new SnapshotRegistry(new LogContext());
|
||||||
|
return new ConsumerGroup(snapshotRegistry, groupId);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetOrCreateMember() {
|
||||||
|
ConsumerGroup consumerGroup = createConsumerGroup("foo");
|
||||||
|
ConsumerGroupMember member;
|
||||||
|
|
||||||
|
// Create a group.
|
||||||
|
member = consumerGroup.getOrMaybeCreateMember("member-id", true);
|
||||||
|
assertEquals("member-id", member.memberId());
|
||||||
|
|
||||||
|
// Get that group back.
|
||||||
|
member = consumerGroup.getOrMaybeCreateMember("member-id", false);
|
||||||
|
assertEquals("member-id", member.memberId());
|
||||||
|
|
||||||
|
assertThrows(UnknownMemberIdException.class, () ->
|
||||||
|
consumerGroup.getOrMaybeCreateMember("does-not-exist", false));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testUpdateMember() {
|
||||||
|
ConsumerGroup consumerGroup = createConsumerGroup("foo");
|
||||||
|
ConsumerGroupMember member;
|
||||||
|
|
||||||
|
member = consumerGroup.getOrMaybeCreateMember("member", true);
|
||||||
|
|
||||||
|
member = new ConsumerGroupMember.Builder(member)
|
||||||
|
.setSubscribedTopicNames(Arrays.asList("foo", "bar"))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
consumerGroup.updateMember(member);
|
||||||
|
|
||||||
|
assertEquals(member, consumerGroup.getOrMaybeCreateMember("member", false));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRemoveMember() {
|
||||||
|
ConsumerGroup consumerGroup = createConsumerGroup("foo");
|
||||||
|
|
||||||
|
consumerGroup.getOrMaybeCreateMember("member", true);
|
||||||
|
assertTrue(consumerGroup.hasMember("member"));
|
||||||
|
|
||||||
|
consumerGroup.removeMember("member");
|
||||||
|
assertFalse(consumerGroup.hasMember("member"));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testUpdatingMemberUpdatesPartitionEpoch() {
|
||||||
|
Uuid fooTopicId = Uuid.randomUuid();
|
||||||
|
Uuid barTopicId = Uuid.randomUuid();
|
||||||
|
Uuid zarTopicId = Uuid.randomUuid();
|
||||||
|
|
||||||
|
ConsumerGroup consumerGroup = createConsumerGroup("foo");
|
||||||
|
ConsumerGroupMember member;
|
||||||
|
|
||||||
|
member = new ConsumerGroupMember.Builder("member")
|
||||||
|
.setMemberEpoch(10)
|
||||||
|
.setAssignedPartitions(mkAssignment(
|
||||||
|
mkTopicAssignment(fooTopicId, 1, 2, 3)))
|
||||||
|
.setPartitionsPendingRevocation(mkAssignment(
|
||||||
|
mkTopicAssignment(barTopicId, 4, 5, 6)))
|
||||||
|
.setPartitionsPendingAssignment(mkAssignment(
|
||||||
|
mkTopicAssignment(zarTopicId, 7, 8, 9)))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
consumerGroup.updateMember(member);
|
||||||
|
|
||||||
|
assertEquals(10, consumerGroup.currentPartitionEpoch(fooTopicId, 1));
|
||||||
|
assertEquals(10, consumerGroup.currentPartitionEpoch(fooTopicId, 2));
|
||||||
|
assertEquals(10, consumerGroup.currentPartitionEpoch(fooTopicId, 3));
|
||||||
|
assertEquals(10, consumerGroup.currentPartitionEpoch(barTopicId, 4));
|
||||||
|
assertEquals(10, consumerGroup.currentPartitionEpoch(barTopicId, 5));
|
||||||
|
assertEquals(10, consumerGroup.currentPartitionEpoch(barTopicId, 6));
|
||||||
|
assertEquals(-1, consumerGroup.currentPartitionEpoch(zarTopicId, 7));
|
||||||
|
assertEquals(-1, consumerGroup.currentPartitionEpoch(zarTopicId, 8));
|
||||||
|
assertEquals(-1, consumerGroup.currentPartitionEpoch(zarTopicId, 9));
|
||||||
|
|
||||||
|
member = new ConsumerGroupMember.Builder(member)
|
||||||
|
.setMemberEpoch(11)
|
||||||
|
.setAssignedPartitions(mkAssignment(
|
||||||
|
mkTopicAssignment(barTopicId, 1, 2, 3)))
|
||||||
|
.setPartitionsPendingRevocation(mkAssignment(
|
||||||
|
mkTopicAssignment(zarTopicId, 4, 5, 6)))
|
||||||
|
.setPartitionsPendingAssignment(mkAssignment(
|
||||||
|
mkTopicAssignment(fooTopicId, 7, 8, 9)))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
consumerGroup.updateMember(member);
|
||||||
|
|
||||||
|
assertEquals(11, consumerGroup.currentPartitionEpoch(barTopicId, 1));
|
||||||
|
assertEquals(11, consumerGroup.currentPartitionEpoch(barTopicId, 2));
|
||||||
|
assertEquals(11, consumerGroup.currentPartitionEpoch(barTopicId, 3));
|
||||||
|
assertEquals(11, consumerGroup.currentPartitionEpoch(zarTopicId, 4));
|
||||||
|
assertEquals(11, consumerGroup.currentPartitionEpoch(zarTopicId, 5));
|
||||||
|
assertEquals(11, consumerGroup.currentPartitionEpoch(zarTopicId, 6));
|
||||||
|
assertEquals(-1, consumerGroup.currentPartitionEpoch(fooTopicId, 7));
|
||||||
|
assertEquals(-1, consumerGroup.currentPartitionEpoch(fooTopicId, 8));
|
||||||
|
assertEquals(-1, consumerGroup.currentPartitionEpoch(fooTopicId, 9));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDeletingMemberRemovesPartitionEpoch() {
|
||||||
|
Uuid fooTopicId = Uuid.randomUuid();
|
||||||
|
Uuid barTopicId = Uuid.randomUuid();
|
||||||
|
Uuid zarTopicId = Uuid.randomUuid();
|
||||||
|
|
||||||
|
ConsumerGroup consumerGroup = createConsumerGroup("foo");
|
||||||
|
ConsumerGroupMember member;
|
||||||
|
|
||||||
|
member = new ConsumerGroupMember.Builder("member")
|
||||||
|
.setMemberEpoch(10)
|
||||||
|
.setAssignedPartitions(mkAssignment(
|
||||||
|
mkTopicAssignment(fooTopicId, 1, 2, 3)))
|
||||||
|
.setPartitionsPendingRevocation(mkAssignment(
|
||||||
|
mkTopicAssignment(barTopicId, 4, 5, 6)))
|
||||||
|
.setPartitionsPendingAssignment(mkAssignment(
|
||||||
|
mkTopicAssignment(zarTopicId, 7, 8, 9)))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
consumerGroup.updateMember(member);
|
||||||
|
|
||||||
|
assertEquals(10, consumerGroup.currentPartitionEpoch(fooTopicId, 1));
|
||||||
|
assertEquals(10, consumerGroup.currentPartitionEpoch(fooTopicId, 2));
|
||||||
|
assertEquals(10, consumerGroup.currentPartitionEpoch(fooTopicId, 3));
|
||||||
|
assertEquals(10, consumerGroup.currentPartitionEpoch(barTopicId, 4));
|
||||||
|
assertEquals(10, consumerGroup.currentPartitionEpoch(barTopicId, 5));
|
||||||
|
assertEquals(10, consumerGroup.currentPartitionEpoch(barTopicId, 6));
|
||||||
|
assertEquals(-1, consumerGroup.currentPartitionEpoch(zarTopicId, 7));
|
||||||
|
assertEquals(-1, consumerGroup.currentPartitionEpoch(zarTopicId, 8));
|
||||||
|
assertEquals(-1, consumerGroup.currentPartitionEpoch(zarTopicId, 9));
|
||||||
|
|
||||||
|
consumerGroup.removeMember(member.memberId());
|
||||||
|
|
||||||
|
assertEquals(-1, consumerGroup.currentPartitionEpoch(barTopicId, 1));
|
||||||
|
assertEquals(-1, consumerGroup.currentPartitionEpoch(barTopicId, 2));
|
||||||
|
assertEquals(-1, consumerGroup.currentPartitionEpoch(barTopicId, 3));
|
||||||
|
assertEquals(-1, consumerGroup.currentPartitionEpoch(zarTopicId, 4));
|
||||||
|
assertEquals(-1, consumerGroup.currentPartitionEpoch(zarTopicId, 5));
|
||||||
|
assertEquals(-1, consumerGroup.currentPartitionEpoch(zarTopicId, 6));
|
||||||
|
assertEquals(-1, consumerGroup.currentPartitionEpoch(fooTopicId, 7));
|
||||||
|
assertEquals(-1, consumerGroup.currentPartitionEpoch(fooTopicId, 8));
|
||||||
|
assertEquals(-1, consumerGroup.currentPartitionEpoch(fooTopicId, 9));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGroupState() {
|
||||||
|
Uuid fooTopicId = Uuid.randomUuid();
|
||||||
|
ConsumerGroup consumerGroup = createConsumerGroup("foo");
|
||||||
|
assertEquals(ConsumerGroup.ConsumerGroupState.EMPTY, consumerGroup.state());
|
||||||
|
|
||||||
|
ConsumerGroupMember member1 = new ConsumerGroupMember.Builder("member1")
|
||||||
|
.setMemberEpoch(1)
|
||||||
|
.setPreviousMemberEpoch(0)
|
||||||
|
.setTargetMemberEpoch(1)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
consumerGroup.updateMember(member1);
|
||||||
|
consumerGroup.setGroupEpoch(1);
|
||||||
|
|
||||||
|
assertEquals(ConsumerGroupMember.MemberState.STABLE, member1.state());
|
||||||
|
assertEquals(ConsumerGroup.ConsumerGroupState.ASSIGNING, consumerGroup.state());
|
||||||
|
|
||||||
|
ConsumerGroupMember member2 = new ConsumerGroupMember.Builder("member2")
|
||||||
|
.setMemberEpoch(1)
|
||||||
|
.setPreviousMemberEpoch(0)
|
||||||
|
.setTargetMemberEpoch(1)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
consumerGroup.updateMember(member2);
|
||||||
|
consumerGroup.setGroupEpoch(2);
|
||||||
|
|
||||||
|
assertEquals(ConsumerGroupMember.MemberState.STABLE, member2.state());
|
||||||
|
assertEquals(ConsumerGroup.ConsumerGroupState.ASSIGNING, consumerGroup.state());
|
||||||
|
|
||||||
|
consumerGroup.setTargetAssignmentEpoch(2);
|
||||||
|
|
||||||
|
assertEquals(ConsumerGroup.ConsumerGroupState.RECONCILING, consumerGroup.state());
|
||||||
|
|
||||||
|
member1 = new ConsumerGroupMember.Builder(member1)
|
||||||
|
.setMemberEpoch(2)
|
||||||
|
.setPreviousMemberEpoch(1)
|
||||||
|
.setTargetMemberEpoch(2)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
consumerGroup.updateMember(member1);
|
||||||
|
|
||||||
|
assertEquals(ConsumerGroupMember.MemberState.STABLE, member1.state());
|
||||||
|
assertEquals(ConsumerGroup.ConsumerGroupState.RECONCILING, consumerGroup.state());
|
||||||
|
|
||||||
|
// Member 2 is not stable so the group stays in reconciling state.
|
||||||
|
member2 = new ConsumerGroupMember.Builder(member2)
|
||||||
|
.setMemberEpoch(2)
|
||||||
|
.setPreviousMemberEpoch(1)
|
||||||
|
.setTargetMemberEpoch(2)
|
||||||
|
.setPartitionsPendingAssignment(mkAssignment(
|
||||||
|
mkTopicAssignment(fooTopicId, 0)))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
consumerGroup.updateMember(member2);
|
||||||
|
|
||||||
|
assertEquals(ConsumerGroupMember.MemberState.ASSIGNING, member2.state());
|
||||||
|
assertEquals(ConsumerGroup.ConsumerGroupState.RECONCILING, consumerGroup.state());
|
||||||
|
|
||||||
|
member2 = new ConsumerGroupMember.Builder(member2)
|
||||||
|
.setMemberEpoch(2)
|
||||||
|
.setPreviousMemberEpoch(1)
|
||||||
|
.setTargetMemberEpoch(2)
|
||||||
|
.setAssignedPartitions(mkAssignment(
|
||||||
|
mkTopicAssignment(fooTopicId, 0)))
|
||||||
|
.setPartitionsPendingAssignment(Collections.emptyMap())
|
||||||
|
.build();
|
||||||
|
|
||||||
|
consumerGroup.updateMember(member2);
|
||||||
|
|
||||||
|
assertEquals(ConsumerGroupMember.MemberState.STABLE, member2.state());
|
||||||
|
assertEquals(ConsumerGroup.ConsumerGroupState.STABLE, consumerGroup.state());
|
||||||
|
|
||||||
|
consumerGroup.removeMember("member1");
|
||||||
|
consumerGroup.removeMember("member2");
|
||||||
|
|
||||||
|
assertEquals(ConsumerGroup.ConsumerGroupState.EMPTY, consumerGroup.state());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testPreferredServerAssignor() {
|
||||||
|
ConsumerGroup consumerGroup = createConsumerGroup("foo");
|
||||||
|
|
||||||
|
ConsumerGroupMember member1 = new ConsumerGroupMember.Builder("member1")
|
||||||
|
.setServerAssignorName("range")
|
||||||
|
.build();
|
||||||
|
ConsumerGroupMember member2 = new ConsumerGroupMember.Builder("member2")
|
||||||
|
.setServerAssignorName("range")
|
||||||
|
.build();
|
||||||
|
ConsumerGroupMember member3 = new ConsumerGroupMember.Builder("member3")
|
||||||
|
.setServerAssignorName("uniform")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
// The group is empty so the preferred assignor should be empty.
|
||||||
|
assertEquals(
|
||||||
|
Optional.empty(),
|
||||||
|
consumerGroup.preferredServerAssignor()
|
||||||
|
);
|
||||||
|
|
||||||
|
// Member 1 has got an updated assignor but this is not reflected in the group yet so
|
||||||
|
// we pass the updated member. The assignor should be range.
|
||||||
|
assertEquals(
|
||||||
|
Optional.of("range"),
|
||||||
|
consumerGroup.computePreferredServerAssignor(null, member1)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Update the group with member 1.
|
||||||
|
consumerGroup.updateMember(member1);
|
||||||
|
|
||||||
|
// Member 1 is in the group so the assignor should be range.
|
||||||
|
assertEquals(
|
||||||
|
Optional.of("range"),
|
||||||
|
consumerGroup.preferredServerAssignor()
|
||||||
|
);
|
||||||
|
|
||||||
|
// Member 1 has been removed but this is not reflected in the group yet so
|
||||||
|
// we pass the removed member. The assignor should be range.
|
||||||
|
assertEquals(
|
||||||
|
Optional.empty(),
|
||||||
|
consumerGroup.computePreferredServerAssignor(member1, null)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Member 2 has got an updated assignor but this is not reflected in the group yet so
|
||||||
|
// we pass the updated member. The assignor should be range.
|
||||||
|
assertEquals(
|
||||||
|
Optional.of("range"),
|
||||||
|
consumerGroup.computePreferredServerAssignor(null, member2)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Update the group with member 2.
|
||||||
|
consumerGroup.updateMember(member2);
|
||||||
|
|
||||||
|
// Member 1 and 2 are in the group so the assignor should be range.
|
||||||
|
assertEquals(
|
||||||
|
Optional.of("range"),
|
||||||
|
consumerGroup.preferredServerAssignor()
|
||||||
|
);
|
||||||
|
|
||||||
|
// Update the group with member 3.
|
||||||
|
consumerGroup.updateMember(member3);
|
||||||
|
|
||||||
|
// Member 1, 2 and 3 are in the group so the assignor should be range.
|
||||||
|
assertEquals(
|
||||||
|
Optional.of("range"),
|
||||||
|
consumerGroup.preferredServerAssignor()
|
||||||
|
);
|
||||||
|
|
||||||
|
// Members without assignors
|
||||||
|
ConsumerGroupMember updatedMember1 = new ConsumerGroupMember.Builder("member1")
|
||||||
|
.setServerAssignorName(null)
|
||||||
|
.build();
|
||||||
|
ConsumerGroupMember updatedMember2 = new ConsumerGroupMember.Builder("member2")
|
||||||
|
.setServerAssignorName(null)
|
||||||
|
.build();
|
||||||
|
ConsumerGroupMember updatedMember3 = new ConsumerGroupMember.Builder("member3")
|
||||||
|
.setServerAssignorName(null)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
// Member 1 has removed it assignor but this is not reflected in the group yet so
|
||||||
|
// we pass the updated member. The assignor should be range or uniform.
|
||||||
|
Optional<String> assignor = consumerGroup.computePreferredServerAssignor(member1, updatedMember1);
|
||||||
|
assertTrue(assignor.equals(Optional.of("range")) || assignor.equals(Optional.of("uniform")));
|
||||||
|
|
||||||
|
// Update the group.
|
||||||
|
consumerGroup.updateMember(updatedMember1);
|
||||||
|
|
||||||
|
// Member 2 has removed it assignor but this is not reflected in the group yet so
|
||||||
|
// we pass the updated member. The assignor should be range or uniform.
|
||||||
|
assertEquals(
|
||||||
|
Optional.of("uniform"),
|
||||||
|
consumerGroup.computePreferredServerAssignor(member2, updatedMember2)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Update the group.
|
||||||
|
consumerGroup.updateMember(updatedMember2);
|
||||||
|
|
||||||
|
// Only member 3 is left in the group so the assignor should be uniform.
|
||||||
|
assertEquals(
|
||||||
|
Optional.of("uniform"),
|
||||||
|
consumerGroup.preferredServerAssignor()
|
||||||
|
);
|
||||||
|
|
||||||
|
// Member 3 has removed it assignor but this is not reflected in the group yet so
|
||||||
|
// we pass the updated member. The assignor should be empty.
|
||||||
|
assertEquals(
|
||||||
|
Optional.empty(),
|
||||||
|
consumerGroup.computePreferredServerAssignor(member3, updatedMember3)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Update the group.
|
||||||
|
consumerGroup.updateMember(updatedMember3);
|
||||||
|
|
||||||
|
// The group is empty so the assignor should be empty as well.
|
||||||
|
assertEquals(
|
||||||
|
Optional.empty(),
|
||||||
|
consumerGroup.preferredServerAssignor()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testUpdateSubscriptionMetadata() {
|
||||||
|
Uuid fooTopicId = Uuid.randomUuid();
|
||||||
|
Uuid barTopicId = Uuid.randomUuid();
|
||||||
|
Uuid zarTopicId = Uuid.randomUuid();
|
||||||
|
|
||||||
|
TopicsImage image = new GroupMetadataManagerTest.TopicsImageBuilder()
|
||||||
|
.addTopic(fooTopicId, "foo", 1)
|
||||||
|
.addTopic(barTopicId, "bar", 2)
|
||||||
|
.addTopic(zarTopicId, "zar", 3)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
ConsumerGroupMember member1 = new ConsumerGroupMember.Builder("member1")
|
||||||
|
.setSubscribedTopicNames(Arrays.asList("foo"))
|
||||||
|
.build();
|
||||||
|
ConsumerGroupMember member2 = new ConsumerGroupMember.Builder("member2")
|
||||||
|
.setSubscribedTopicNames(Arrays.asList("bar"))
|
||||||
|
.build();
|
||||||
|
ConsumerGroupMember member3 = new ConsumerGroupMember.Builder("member3")
|
||||||
|
.setSubscribedTopicNames(Arrays.asList("zar"))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
ConsumerGroup consumerGroup = createConsumerGroup("group-foo");
|
||||||
|
|
||||||
|
// It should be empty by default.
|
||||||
|
assertEquals(
|
||||||
|
Collections.emptyMap(),
|
||||||
|
consumerGroup.computeSubscriptionMetadata(
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
image
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Compute while taking into account member 1.
|
||||||
|
assertEquals(
|
||||||
|
mkMap(
|
||||||
|
mkEntry("foo", new TopicMetadata(fooTopicId, "foo", 1))
|
||||||
|
),
|
||||||
|
consumerGroup.computeSubscriptionMetadata(
|
||||||
|
null,
|
||||||
|
member1,
|
||||||
|
image
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Updating the group with member1.
|
||||||
|
consumerGroup.updateMember(member1);
|
||||||
|
|
||||||
|
// It should return foo now.
|
||||||
|
assertEquals(
|
||||||
|
mkMap(
|
||||||
|
mkEntry("foo", new TopicMetadata(fooTopicId, "foo", 1))
|
||||||
|
),
|
||||||
|
consumerGroup.computeSubscriptionMetadata(
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
image
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Compute while taking into account removal of member 1.
|
||||||
|
assertEquals(
|
||||||
|
Collections.emptyMap(),
|
||||||
|
consumerGroup.computeSubscriptionMetadata(
|
||||||
|
member1,
|
||||||
|
null,
|
||||||
|
image
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Compute while taking into account member 2.
|
||||||
|
assertEquals(
|
||||||
|
mkMap(
|
||||||
|
mkEntry("foo", new TopicMetadata(fooTopicId, "foo", 1)),
|
||||||
|
mkEntry("bar", new TopicMetadata(barTopicId, "bar", 2))
|
||||||
|
),
|
||||||
|
consumerGroup.computeSubscriptionMetadata(
|
||||||
|
null,
|
||||||
|
member2,
|
||||||
|
image
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Updating the group with member2.
|
||||||
|
consumerGroup.updateMember(member2);
|
||||||
|
|
||||||
|
// It should return foo and bar.
|
||||||
|
assertEquals(
|
||||||
|
mkMap(
|
||||||
|
mkEntry("foo", new TopicMetadata(fooTopicId, "foo", 1)),
|
||||||
|
mkEntry("bar", new TopicMetadata(barTopicId, "bar", 2))
|
||||||
|
),
|
||||||
|
consumerGroup.computeSubscriptionMetadata(
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
image
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Compute while taking into account removal of member 2.
|
||||||
|
assertEquals(
|
||||||
|
mkMap(
|
||||||
|
mkEntry("foo", new TopicMetadata(fooTopicId, "foo", 1))
|
||||||
|
),
|
||||||
|
consumerGroup.computeSubscriptionMetadata(
|
||||||
|
member2,
|
||||||
|
null,
|
||||||
|
image
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Removing member1 results in returning bar.
|
||||||
|
assertEquals(
|
||||||
|
mkMap(
|
||||||
|
mkEntry("bar", new TopicMetadata(barTopicId, "bar", 2))
|
||||||
|
),
|
||||||
|
consumerGroup.computeSubscriptionMetadata(
|
||||||
|
member1,
|
||||||
|
null,
|
||||||
|
image
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Compute while taking into account member 3.
|
||||||
|
assertEquals(
|
||||||
|
mkMap(
|
||||||
|
mkEntry("foo", new TopicMetadata(fooTopicId, "foo", 1)),
|
||||||
|
mkEntry("bar", new TopicMetadata(barTopicId, "bar", 2)),
|
||||||
|
mkEntry("zar", new TopicMetadata(zarTopicId, "zar", 3))
|
||||||
|
),
|
||||||
|
consumerGroup.computeSubscriptionMetadata(
|
||||||
|
null,
|
||||||
|
member3,
|
||||||
|
image
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Updating group with member3.
|
||||||
|
consumerGroup.updateMember(member3);
|
||||||
|
|
||||||
|
// It should return foo, bar and zar.
|
||||||
|
assertEquals(
|
||||||
|
mkMap(
|
||||||
|
mkEntry("foo", new TopicMetadata(fooTopicId, "foo", 1)),
|
||||||
|
mkEntry("bar", new TopicMetadata(barTopicId, "bar", 2)),
|
||||||
|
mkEntry("zar", new TopicMetadata(zarTopicId, "zar", 3))
|
||||||
|
),
|
||||||
|
consumerGroup.computeSubscriptionMetadata(
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
image
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
|
@ -44,7 +44,7 @@ public class CurrentAssignmentBuilderTest {
|
||||||
ConsumerGroupMember member = new ConsumerGroupMember.Builder("member")
|
ConsumerGroupMember member = new ConsumerGroupMember.Builder("member")
|
||||||
.setMemberEpoch(10)
|
.setMemberEpoch(10)
|
||||||
.setPreviousMemberEpoch(10)
|
.setPreviousMemberEpoch(10)
|
||||||
.setNextMemberEpoch(10)
|
.setTargetMemberEpoch(10)
|
||||||
.setAssignedPartitions(mkAssignment(
|
.setAssignedPartitions(mkAssignment(
|
||||||
mkTopicAssignment(topicId1, 1, 2, 3),
|
mkTopicAssignment(topicId1, 1, 2, 3),
|
||||||
mkTopicAssignment(topicId2, 4, 5, 6)))
|
mkTopicAssignment(topicId2, 4, 5, 6)))
|
||||||
|
@ -65,7 +65,7 @@ public class CurrentAssignmentBuilderTest {
|
||||||
assertEquals(ConsumerGroupMember.MemberState.REVOKING, updatedMember.state());
|
assertEquals(ConsumerGroupMember.MemberState.REVOKING, updatedMember.state());
|
||||||
assertEquals(10, updatedMember.previousMemberEpoch());
|
assertEquals(10, updatedMember.previousMemberEpoch());
|
||||||
assertEquals(10, updatedMember.memberEpoch());
|
assertEquals(10, updatedMember.memberEpoch());
|
||||||
assertEquals(11, updatedMember.nextMemberEpoch());
|
assertEquals(11, updatedMember.targetMemberEpoch());
|
||||||
assertEquals(mkAssignment(
|
assertEquals(mkAssignment(
|
||||||
mkTopicAssignment(topicId1, 3),
|
mkTopicAssignment(topicId1, 3),
|
||||||
mkTopicAssignment(topicId2, 6)
|
mkTopicAssignment(topicId2, 6)
|
||||||
|
@ -88,7 +88,7 @@ public class CurrentAssignmentBuilderTest {
|
||||||
ConsumerGroupMember member = new ConsumerGroupMember.Builder("member")
|
ConsumerGroupMember member = new ConsumerGroupMember.Builder("member")
|
||||||
.setMemberEpoch(10)
|
.setMemberEpoch(10)
|
||||||
.setPreviousMemberEpoch(10)
|
.setPreviousMemberEpoch(10)
|
||||||
.setNextMemberEpoch(10)
|
.setTargetMemberEpoch(10)
|
||||||
.setAssignedPartitions(mkAssignment(
|
.setAssignedPartitions(mkAssignment(
|
||||||
mkTopicAssignment(topicId1, 1, 2, 3),
|
mkTopicAssignment(topicId1, 1, 2, 3),
|
||||||
mkTopicAssignment(topicId2, 4, 5, 6)))
|
mkTopicAssignment(topicId2, 4, 5, 6)))
|
||||||
|
@ -109,7 +109,7 @@ public class CurrentAssignmentBuilderTest {
|
||||||
assertEquals(ConsumerGroupMember.MemberState.ASSIGNING, updatedMember.state());
|
assertEquals(ConsumerGroupMember.MemberState.ASSIGNING, updatedMember.state());
|
||||||
assertEquals(10, updatedMember.previousMemberEpoch());
|
assertEquals(10, updatedMember.previousMemberEpoch());
|
||||||
assertEquals(11, updatedMember.memberEpoch());
|
assertEquals(11, updatedMember.memberEpoch());
|
||||||
assertEquals(11, updatedMember.nextMemberEpoch());
|
assertEquals(11, updatedMember.targetMemberEpoch());
|
||||||
assertEquals(mkAssignment(
|
assertEquals(mkAssignment(
|
||||||
mkTopicAssignment(topicId1, 1, 2, 3),
|
mkTopicAssignment(topicId1, 1, 2, 3),
|
||||||
mkTopicAssignment(topicId2, 4, 5, 6)
|
mkTopicAssignment(topicId2, 4, 5, 6)
|
||||||
|
@ -129,7 +129,7 @@ public class CurrentAssignmentBuilderTest {
|
||||||
ConsumerGroupMember member = new ConsumerGroupMember.Builder("member")
|
ConsumerGroupMember member = new ConsumerGroupMember.Builder("member")
|
||||||
.setMemberEpoch(10)
|
.setMemberEpoch(10)
|
||||||
.setPreviousMemberEpoch(10)
|
.setPreviousMemberEpoch(10)
|
||||||
.setNextMemberEpoch(10)
|
.setTargetMemberEpoch(10)
|
||||||
.setAssignedPartitions(mkAssignment(
|
.setAssignedPartitions(mkAssignment(
|
||||||
mkTopicAssignment(topicId1, 1, 2, 3),
|
mkTopicAssignment(topicId1, 1, 2, 3),
|
||||||
mkTopicAssignment(topicId2, 4, 5, 6)))
|
mkTopicAssignment(topicId2, 4, 5, 6)))
|
||||||
|
@ -150,7 +150,7 @@ public class CurrentAssignmentBuilderTest {
|
||||||
assertEquals(ConsumerGroupMember.MemberState.STABLE, updatedMember.state());
|
assertEquals(ConsumerGroupMember.MemberState.STABLE, updatedMember.state());
|
||||||
assertEquals(10, updatedMember.previousMemberEpoch());
|
assertEquals(10, updatedMember.previousMemberEpoch());
|
||||||
assertEquals(11, updatedMember.memberEpoch());
|
assertEquals(11, updatedMember.memberEpoch());
|
||||||
assertEquals(11, updatedMember.nextMemberEpoch());
|
assertEquals(11, updatedMember.targetMemberEpoch());
|
||||||
assertEquals(mkAssignment(
|
assertEquals(mkAssignment(
|
||||||
mkTopicAssignment(topicId1, 1, 2, 3),
|
mkTopicAssignment(topicId1, 1, 2, 3),
|
||||||
mkTopicAssignment(topicId2, 4, 5, 6)
|
mkTopicAssignment(topicId2, 4, 5, 6)
|
||||||
|
@ -179,7 +179,7 @@ public class CurrentAssignmentBuilderTest {
|
||||||
ConsumerGroupMember member = new ConsumerGroupMember.Builder("member")
|
ConsumerGroupMember member = new ConsumerGroupMember.Builder("member")
|
||||||
.setMemberEpoch(10)
|
.setMemberEpoch(10)
|
||||||
.setPreviousMemberEpoch(10)
|
.setPreviousMemberEpoch(10)
|
||||||
.setNextMemberEpoch(11)
|
.setTargetMemberEpoch(11)
|
||||||
.setAssignedPartitions(mkAssignment(
|
.setAssignedPartitions(mkAssignment(
|
||||||
mkTopicAssignment(topicId1, 3),
|
mkTopicAssignment(topicId1, 3),
|
||||||
mkTopicAssignment(topicId2, 6)))
|
mkTopicAssignment(topicId2, 6)))
|
||||||
|
@ -207,7 +207,7 @@ public class CurrentAssignmentBuilderTest {
|
||||||
assertEquals(ConsumerGroupMember.MemberState.REVOKING, updatedMember.state());
|
assertEquals(ConsumerGroupMember.MemberState.REVOKING, updatedMember.state());
|
||||||
assertEquals(10, updatedMember.previousMemberEpoch());
|
assertEquals(10, updatedMember.previousMemberEpoch());
|
||||||
assertEquals(10, updatedMember.memberEpoch());
|
assertEquals(10, updatedMember.memberEpoch());
|
||||||
assertEquals(11, updatedMember.nextMemberEpoch());
|
assertEquals(11, updatedMember.targetMemberEpoch());
|
||||||
assertEquals(mkAssignment(
|
assertEquals(mkAssignment(
|
||||||
mkTopicAssignment(topicId1, 3),
|
mkTopicAssignment(topicId1, 3),
|
||||||
mkTopicAssignment(topicId2, 6)
|
mkTopicAssignment(topicId2, 6)
|
||||||
|
@ -230,7 +230,7 @@ public class CurrentAssignmentBuilderTest {
|
||||||
ConsumerGroupMember member = new ConsumerGroupMember.Builder("member")
|
ConsumerGroupMember member = new ConsumerGroupMember.Builder("member")
|
||||||
.setMemberEpoch(10)
|
.setMemberEpoch(10)
|
||||||
.setPreviousMemberEpoch(10)
|
.setPreviousMemberEpoch(10)
|
||||||
.setNextMemberEpoch(11)
|
.setTargetMemberEpoch(11)
|
||||||
.setAssignedPartitions(mkAssignment(
|
.setAssignedPartitions(mkAssignment(
|
||||||
mkTopicAssignment(topicId1, 3),
|
mkTopicAssignment(topicId1, 3),
|
||||||
mkTopicAssignment(topicId2, 6)))
|
mkTopicAssignment(topicId2, 6)))
|
||||||
|
@ -260,7 +260,7 @@ public class CurrentAssignmentBuilderTest {
|
||||||
assertEquals(ConsumerGroupMember.MemberState.ASSIGNING, updatedMember.state());
|
assertEquals(ConsumerGroupMember.MemberState.ASSIGNING, updatedMember.state());
|
||||||
assertEquals(10, updatedMember.previousMemberEpoch());
|
assertEquals(10, updatedMember.previousMemberEpoch());
|
||||||
assertEquals(11, updatedMember.memberEpoch());
|
assertEquals(11, updatedMember.memberEpoch());
|
||||||
assertEquals(11, updatedMember.nextMemberEpoch());
|
assertEquals(11, updatedMember.targetMemberEpoch());
|
||||||
assertEquals(mkAssignment(
|
assertEquals(mkAssignment(
|
||||||
mkTopicAssignment(topicId1, 3),
|
mkTopicAssignment(topicId1, 3),
|
||||||
mkTopicAssignment(topicId2, 6)
|
mkTopicAssignment(topicId2, 6)
|
||||||
|
@ -280,7 +280,7 @@ public class CurrentAssignmentBuilderTest {
|
||||||
ConsumerGroupMember member = new ConsumerGroupMember.Builder("member")
|
ConsumerGroupMember member = new ConsumerGroupMember.Builder("member")
|
||||||
.setMemberEpoch(10)
|
.setMemberEpoch(10)
|
||||||
.setPreviousMemberEpoch(10)
|
.setPreviousMemberEpoch(10)
|
||||||
.setNextMemberEpoch(11)
|
.setTargetMemberEpoch(11)
|
||||||
.setAssignedPartitions(mkAssignment(
|
.setAssignedPartitions(mkAssignment(
|
||||||
mkTopicAssignment(topicId1, 3),
|
mkTopicAssignment(topicId1, 3),
|
||||||
mkTopicAssignment(topicId2, 6)))
|
mkTopicAssignment(topicId2, 6)))
|
||||||
|
@ -310,7 +310,7 @@ public class CurrentAssignmentBuilderTest {
|
||||||
assertEquals(ConsumerGroupMember.MemberState.STABLE, updatedMember.state());
|
assertEquals(ConsumerGroupMember.MemberState.STABLE, updatedMember.state());
|
||||||
assertEquals(10, updatedMember.previousMemberEpoch());
|
assertEquals(10, updatedMember.previousMemberEpoch());
|
||||||
assertEquals(11, updatedMember.memberEpoch());
|
assertEquals(11, updatedMember.memberEpoch());
|
||||||
assertEquals(11, updatedMember.nextMemberEpoch());
|
assertEquals(11, updatedMember.targetMemberEpoch());
|
||||||
assertEquals(mkAssignment(
|
assertEquals(mkAssignment(
|
||||||
mkTopicAssignment(topicId1, 3, 4, 5),
|
mkTopicAssignment(topicId1, 3, 4, 5),
|
||||||
mkTopicAssignment(topicId2, 6, 7, 8)
|
mkTopicAssignment(topicId2, 6, 7, 8)
|
||||||
|
@ -327,7 +327,7 @@ public class CurrentAssignmentBuilderTest {
|
||||||
ConsumerGroupMember member = new ConsumerGroupMember.Builder("member")
|
ConsumerGroupMember member = new ConsumerGroupMember.Builder("member")
|
||||||
.setMemberEpoch(10)
|
.setMemberEpoch(10)
|
||||||
.setPreviousMemberEpoch(10)
|
.setPreviousMemberEpoch(10)
|
||||||
.setNextMemberEpoch(11)
|
.setTargetMemberEpoch(11)
|
||||||
.setAssignedPartitions(mkAssignment(
|
.setAssignedPartitions(mkAssignment(
|
||||||
mkTopicAssignment(topicId1, 3),
|
mkTopicAssignment(topicId1, 3),
|
||||||
mkTopicAssignment(topicId2, 6)))
|
mkTopicAssignment(topicId2, 6)))
|
||||||
|
@ -358,7 +358,7 @@ public class CurrentAssignmentBuilderTest {
|
||||||
assertEquals(ConsumerGroupMember.MemberState.STABLE, updatedMember.state());
|
assertEquals(ConsumerGroupMember.MemberState.STABLE, updatedMember.state());
|
||||||
assertEquals(10, updatedMember.previousMemberEpoch());
|
assertEquals(10, updatedMember.previousMemberEpoch());
|
||||||
assertEquals(12, updatedMember.memberEpoch());
|
assertEquals(12, updatedMember.memberEpoch());
|
||||||
assertEquals(12, updatedMember.nextMemberEpoch());
|
assertEquals(12, updatedMember.targetMemberEpoch());
|
||||||
assertEquals(mkAssignment(
|
assertEquals(mkAssignment(
|
||||||
mkTopicAssignment(topicId1, 1, 2, 3),
|
mkTopicAssignment(topicId1, 1, 2, 3),
|
||||||
mkTopicAssignment(topicId2, 4, 5, 6)
|
mkTopicAssignment(topicId2, 4, 5, 6)
|
||||||
|
@ -375,7 +375,7 @@ public class CurrentAssignmentBuilderTest {
|
||||||
ConsumerGroupMember member = new ConsumerGroupMember.Builder("member")
|
ConsumerGroupMember member = new ConsumerGroupMember.Builder("member")
|
||||||
.setMemberEpoch(10)
|
.setMemberEpoch(10)
|
||||||
.setPreviousMemberEpoch(11)
|
.setPreviousMemberEpoch(11)
|
||||||
.setNextMemberEpoch(11)
|
.setTargetMemberEpoch(11)
|
||||||
.setAssignedPartitions(mkAssignment(
|
.setAssignedPartitions(mkAssignment(
|
||||||
mkTopicAssignment(topicId1, 3),
|
mkTopicAssignment(topicId1, 3),
|
||||||
mkTopicAssignment(topicId2, 6)))
|
mkTopicAssignment(topicId2, 6)))
|
||||||
|
@ -404,7 +404,7 @@ public class CurrentAssignmentBuilderTest {
|
||||||
assertEquals(ConsumerGroupMember.MemberState.ASSIGNING, updatedMember.state());
|
assertEquals(ConsumerGroupMember.MemberState.ASSIGNING, updatedMember.state());
|
||||||
assertEquals(10, updatedMember.previousMemberEpoch());
|
assertEquals(10, updatedMember.previousMemberEpoch());
|
||||||
assertEquals(11, updatedMember.memberEpoch());
|
assertEquals(11, updatedMember.memberEpoch());
|
||||||
assertEquals(11, updatedMember.nextMemberEpoch());
|
assertEquals(11, updatedMember.targetMemberEpoch());
|
||||||
assertEquals(mkAssignment(
|
assertEquals(mkAssignment(
|
||||||
mkTopicAssignment(topicId1, 3, 4, 5),
|
mkTopicAssignment(topicId1, 3, 4, 5),
|
||||||
mkTopicAssignment(topicId2, 6)
|
mkTopicAssignment(topicId2, 6)
|
||||||
|
@ -423,7 +423,7 @@ public class CurrentAssignmentBuilderTest {
|
||||||
ConsumerGroupMember member = new ConsumerGroupMember.Builder("member")
|
ConsumerGroupMember member = new ConsumerGroupMember.Builder("member")
|
||||||
.setMemberEpoch(10)
|
.setMemberEpoch(10)
|
||||||
.setPreviousMemberEpoch(11)
|
.setPreviousMemberEpoch(11)
|
||||||
.setNextMemberEpoch(11)
|
.setTargetMemberEpoch(11)
|
||||||
.setAssignedPartitions(mkAssignment(
|
.setAssignedPartitions(mkAssignment(
|
||||||
mkTopicAssignment(topicId1, 3),
|
mkTopicAssignment(topicId1, 3),
|
||||||
mkTopicAssignment(topicId2, 6)))
|
mkTopicAssignment(topicId2, 6)))
|
||||||
|
@ -447,7 +447,7 @@ public class CurrentAssignmentBuilderTest {
|
||||||
assertEquals(ConsumerGroupMember.MemberState.STABLE, updatedMember.state());
|
assertEquals(ConsumerGroupMember.MemberState.STABLE, updatedMember.state());
|
||||||
assertEquals(10, updatedMember.previousMemberEpoch());
|
assertEquals(10, updatedMember.previousMemberEpoch());
|
||||||
assertEquals(11, updatedMember.memberEpoch());
|
assertEquals(11, updatedMember.memberEpoch());
|
||||||
assertEquals(11, updatedMember.nextMemberEpoch());
|
assertEquals(11, updatedMember.targetMemberEpoch());
|
||||||
assertEquals(mkAssignment(
|
assertEquals(mkAssignment(
|
||||||
mkTopicAssignment(topicId1, 3, 4, 5),
|
mkTopicAssignment(topicId1, 3, 4, 5),
|
||||||
mkTopicAssignment(topicId2, 6, 7, 8)
|
mkTopicAssignment(topicId2, 6, 7, 8)
|
||||||
|
@ -464,7 +464,7 @@ public class CurrentAssignmentBuilderTest {
|
||||||
ConsumerGroupMember member = new ConsumerGroupMember.Builder("member")
|
ConsumerGroupMember member = new ConsumerGroupMember.Builder("member")
|
||||||
.setMemberEpoch(11)
|
.setMemberEpoch(11)
|
||||||
.setPreviousMemberEpoch(11)
|
.setPreviousMemberEpoch(11)
|
||||||
.setNextMemberEpoch(11)
|
.setTargetMemberEpoch(11)
|
||||||
.setAssignedPartitions(mkAssignment(
|
.setAssignedPartitions(mkAssignment(
|
||||||
mkTopicAssignment(topicId1, 3, 4, 5),
|
mkTopicAssignment(topicId1, 3, 4, 5),
|
||||||
mkTopicAssignment(topicId2, 6, 7, 8)))
|
mkTopicAssignment(topicId2, 6, 7, 8)))
|
||||||
|
@ -485,7 +485,7 @@ public class CurrentAssignmentBuilderTest {
|
||||||
assertEquals(ConsumerGroupMember.MemberState.STABLE, updatedMember.state());
|
assertEquals(ConsumerGroupMember.MemberState.STABLE, updatedMember.state());
|
||||||
assertEquals(11, updatedMember.previousMemberEpoch());
|
assertEquals(11, updatedMember.previousMemberEpoch());
|
||||||
assertEquals(11, updatedMember.memberEpoch());
|
assertEquals(11, updatedMember.memberEpoch());
|
||||||
assertEquals(11, updatedMember.nextMemberEpoch());
|
assertEquals(11, updatedMember.targetMemberEpoch());
|
||||||
assertEquals(mkAssignment(
|
assertEquals(mkAssignment(
|
||||||
mkTopicAssignment(topicId1, 3, 4, 5),
|
mkTopicAssignment(topicId1, 3, 4, 5),
|
||||||
mkTopicAssignment(topicId2, 6, 7, 8)
|
mkTopicAssignment(topicId2, 6, 7, 8)
|
||||||
|
@ -502,7 +502,7 @@ public class CurrentAssignmentBuilderTest {
|
||||||
ConsumerGroupMember member = new ConsumerGroupMember.Builder("member")
|
ConsumerGroupMember member = new ConsumerGroupMember.Builder("member")
|
||||||
.setMemberEpoch(10)
|
.setMemberEpoch(10)
|
||||||
.setPreviousMemberEpoch(10)
|
.setPreviousMemberEpoch(10)
|
||||||
.setNextMemberEpoch(11)
|
.setTargetMemberEpoch(11)
|
||||||
.setAssignedPartitions(mkAssignment(
|
.setAssignedPartitions(mkAssignment(
|
||||||
mkTopicAssignment(topicId1, 3),
|
mkTopicAssignment(topicId1, 3),
|
||||||
mkTopicAssignment(topicId2, 6)))
|
mkTopicAssignment(topicId2, 6)))
|
||||||
|
@ -529,7 +529,7 @@ public class CurrentAssignmentBuilderTest {
|
||||||
assertEquals(ConsumerGroupMember.MemberState.REVOKING, updatedMember.state());
|
assertEquals(ConsumerGroupMember.MemberState.REVOKING, updatedMember.state());
|
||||||
assertEquals(10, updatedMember.previousMemberEpoch());
|
assertEquals(10, updatedMember.previousMemberEpoch());
|
||||||
assertEquals(10, updatedMember.memberEpoch());
|
assertEquals(10, updatedMember.memberEpoch());
|
||||||
assertEquals(12, updatedMember.nextMemberEpoch());
|
assertEquals(12, updatedMember.targetMemberEpoch());
|
||||||
assertEquals(Collections.emptyMap(), updatedMember.assignedPartitions());
|
assertEquals(Collections.emptyMap(), updatedMember.assignedPartitions());
|
||||||
assertEquals(mkAssignment(
|
assertEquals(mkAssignment(
|
||||||
mkTopicAssignment(topicId1, 1, 2, 3),
|
mkTopicAssignment(topicId1, 1, 2, 3),
|
||||||
|
|
Loading…
Reference in New Issue