All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.kafka.clients.consumer.CooperativeStickyAssignor Maven / Gradle / Ivy

There is a newer version: 1.2.2.1-jre17
Show newest version
/*
 * 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.clients.consumer;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import org.apache.kafka.clients.consumer.internals.AbstractStickyAssignor;
import org.apache.kafka.common.TopicPartition;

/**
 * A cooperative version of the {@link AbstractStickyAssignor AbstractStickyAssignor}. This follows the same (sticky)
 * assignment logic as {@link StickyAssignor StickyAssignor} but allows for cooperative rebalancing while the
 * {@link StickyAssignor StickyAssignor} follows the eager rebalancing protocol. See
 * {@link ConsumerPartitionAssignor.RebalanceProtocol} for an explanation of the rebalancing protocols.
 * 

* Users should prefer this assignor for newer clusters. *

* To turn on cooperative rebalancing you must set all your consumers to use this {@code PartitionAssignor}, * or implement a custom one that returns {@code RebalanceProtocol.COOPERATIVE} in * {@link CooperativeStickyAssignor#supportedProtocols supportedProtocols()}. *

* IMPORTANT: if upgrading from 2.3 or earlier, you must follow a specific upgrade path in order to safely turn on * cooperative rebalancing. See the upgrade guide for details. */ public class CooperativeStickyAssignor extends AbstractStickyAssignor { @Override public String name() { return "cooperative-sticky"; } @Override public List supportedProtocols() { return Arrays.asList(RebalanceProtocol.COOPERATIVE, RebalanceProtocol.EAGER); } @Override protected MemberData memberData(Subscription subscription) { return new MemberData(subscription.ownedPartitions(), Optional.empty()); } @Override public Map> assign(Map partitionsPerTopic, Map subscriptions) { Map> assignments = super.assign(partitionsPerTopic, subscriptions); Map partitionsTransferringOwnership = super.partitionsTransferringOwnership == null ? computePartitionsTransferringOwnership(subscriptions, assignments) : super.partitionsTransferringOwnership; adjustAssignment(assignments, partitionsTransferringOwnership); return assignments; } // Following the cooperative rebalancing protocol requires removing partitions that must first be revoked from the assignment private void adjustAssignment(Map> assignments, Map partitionsTransferringOwnership) { for (Map.Entry partitionEntry : partitionsTransferringOwnership.entrySet()) { assignments.get(partitionEntry.getValue()).remove(partitionEntry.getKey()); } } private Map computePartitionsTransferringOwnership(Map subscriptions, Map> assignments) { Map allAddedPartitions = new HashMap<>(); Set allRevokedPartitions = new HashSet<>(); for (final Map.Entry> entry : assignments.entrySet()) { String consumer = entry.getKey(); List ownedPartitions = subscriptions.get(consumer).ownedPartitions(); List assignedPartitions = entry.getValue(); Set ownedPartitionsSet = new HashSet<>(ownedPartitions); for (TopicPartition tp : assignedPartitions) { if (!ownedPartitionsSet.contains(tp)) allAddedPartitions.put(tp, consumer); } Set assignedPartitionsSet = new HashSet<>(assignedPartitions); for (TopicPartition tp : ownedPartitions) { if (!assignedPartitionsSet.contains(tp)) allRevokedPartitions.add(tp); } } allAddedPartitions.keySet().retainAll(allRevokedPartitions); return allAddedPartitions; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy