Search in sources :

Example 1 with StopReplicaPartitionV0

use of org.apache.kafka.common.message.StopReplicaRequestData.StopReplicaPartitionV0 in project kafka by apache.

the class StopReplicaRequest method topicStates.

/**
 * Note that this method has allocation overhead per iterated element, so callers should copy the result into
 * another collection if they need to iterate more than once.
 *
 * Implementation note: we should strive to avoid allocation overhead per element, see
 * `UpdateMetadataRequest.partitionStates()` for the preferred approach. That's not possible in this case and
 * StopReplicaRequest should be relatively rare in comparison to other request types.
 */
public Iterable<StopReplicaTopicState> topicStates() {
    if (version() < 1) {
        Map<String, StopReplicaTopicState> topicStates = new HashMap<>();
        for (StopReplicaPartitionV0 partition : data.ungroupedPartitions()) {
            StopReplicaTopicState topicState = topicStates.computeIfAbsent(partition.topicName(), topic -> new StopReplicaTopicState().setTopicName(topic));
            topicState.partitionStates().add(new StopReplicaPartitionState().setPartitionIndex(partition.partitionIndex()).setDeletePartition(data.deletePartitions()));
        }
        return topicStates.values();
    } else if (version() < 3) {
        return () -> new MappedIterator<>(data.topics().iterator(), topic -> new StopReplicaTopicState().setTopicName(topic.name()).setPartitionStates(topic.partitionIndexes().stream().map(partition -> new StopReplicaPartitionState().setPartitionIndex(partition).setDeletePartition(data.deletePartitions())).collect(Collectors.toList())));
    } else {
        return data.topicStates();
    }
}
Also used : StopReplicaPartitionV0(org.apache.kafka.common.message.StopReplicaRequestData.StopReplicaPartitionV0) Utils(org.apache.kafka.common.utils.Utils) TopicPartition(org.apache.kafka.common.TopicPartition) StopReplicaPartitionV0(org.apache.kafka.common.message.StopReplicaRequestData.StopReplicaPartitionV0) StopReplicaPartitionError(org.apache.kafka.common.message.StopReplicaResponseData.StopReplicaPartitionError) ByteBufferAccessor(org.apache.kafka.common.protocol.ByteBufferAccessor) HashMap(java.util.HashMap) StopReplicaTopicV1(org.apache.kafka.common.message.StopReplicaRequestData.StopReplicaTopicV1) MappedIterator(org.apache.kafka.common.utils.MappedIterator) ApiKeys(org.apache.kafka.common.protocol.ApiKeys) Collectors(java.util.stream.Collectors) ByteBuffer(java.nio.ByteBuffer) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) Errors(org.apache.kafka.common.protocol.Errors) StopReplicaTopicState(org.apache.kafka.common.message.StopReplicaRequestData.StopReplicaTopicState) StopReplicaResponseData(org.apache.kafka.common.message.StopReplicaResponseData) StopReplicaRequestData(org.apache.kafka.common.message.StopReplicaRequestData) StopReplicaPartitionState(org.apache.kafka.common.message.StopReplicaRequestData.StopReplicaPartitionState) StopReplicaTopicState(org.apache.kafka.common.message.StopReplicaRequestData.StopReplicaTopicState) HashMap(java.util.HashMap) StopReplicaPartitionState(org.apache.kafka.common.message.StopReplicaRequestData.StopReplicaPartitionState)

Example 2 with StopReplicaPartitionV0

use of org.apache.kafka.common.message.StopReplicaRequestData.StopReplicaPartitionV0 in project kafka by apache.

the class StopReplicaRequestTest method testBuilderNormalization.

private void testBuilderNormalization(boolean deletePartitions) {
    List<StopReplicaTopicState> topicStates = topicStates(deletePartitions);
    Map<TopicPartition, StopReplicaPartitionState> expectedPartitionStates = StopReplicaRequestTest.partitionStates(topicStates);
    for (short version : STOP_REPLICA.allVersions()) {
        StopReplicaRequest request = new StopReplicaRequest.Builder(version, 0, 1, 0, deletePartitions, topicStates).build(version);
        StopReplicaRequestData data = request.data();
        if (version < 1) {
            Set<TopicPartition> partitions = new HashSet<>();
            for (StopReplicaPartitionV0 partition : data.ungroupedPartitions()) {
                partitions.add(new TopicPartition(partition.topicName(), partition.partitionIndex()));
            }
            assertEquals(expectedPartitionStates.keySet(), partitions);
            assertEquals(deletePartitions, data.deletePartitions());
        } else if (version < 3) {
            Set<TopicPartition> partitions = new HashSet<>();
            for (StopReplicaTopicV1 topic : data.topics()) {
                for (Integer partition : topic.partitionIndexes()) {
                    partitions.add(new TopicPartition(topic.name(), partition));
                }
            }
            assertEquals(expectedPartitionStates.keySet(), partitions);
            assertEquals(deletePartitions, data.deletePartitions());
        } else {
            Map<TopicPartition, StopReplicaPartitionState> partitionStates = StopReplicaRequestTest.partitionStates(data.topicStates());
            assertEquals(expectedPartitionStates, partitionStates);
            // Always false from V3 on
            assertFalse(data.deletePartitions());
        }
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) StopReplicaRequestData(org.apache.kafka.common.message.StopReplicaRequestData) StopReplicaPartitionV0(org.apache.kafka.common.message.StopReplicaRequestData.StopReplicaPartitionV0) StopReplicaTopicState(org.apache.kafka.common.message.StopReplicaRequestData.StopReplicaTopicState) TopicPartition(org.apache.kafka.common.TopicPartition) StopReplicaPartitionState(org.apache.kafka.common.message.StopReplicaRequestData.StopReplicaPartitionState) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet) StopReplicaTopicV1(org.apache.kafka.common.message.StopReplicaRequestData.StopReplicaTopicV1)

Aggregations

HashMap (java.util.HashMap)2 Map (java.util.Map)2 TopicPartition (org.apache.kafka.common.TopicPartition)2 StopReplicaRequestData (org.apache.kafka.common.message.StopReplicaRequestData)2 StopReplicaPartitionState (org.apache.kafka.common.message.StopReplicaRequestData.StopReplicaPartitionState)2 StopReplicaPartitionV0 (org.apache.kafka.common.message.StopReplicaRequestData.StopReplicaPartitionV0)2 StopReplicaTopicState (org.apache.kafka.common.message.StopReplicaRequestData.StopReplicaTopicState)2 StopReplicaTopicV1 (org.apache.kafka.common.message.StopReplicaRequestData.StopReplicaTopicV1)2 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 StopReplicaResponseData (org.apache.kafka.common.message.StopReplicaResponseData)1 StopReplicaPartitionError (org.apache.kafka.common.message.StopReplicaResponseData.StopReplicaPartitionError)1 ApiKeys (org.apache.kafka.common.protocol.ApiKeys)1 ByteBufferAccessor (org.apache.kafka.common.protocol.ByteBufferAccessor)1 Errors (org.apache.kafka.common.protocol.Errors)1 MappedIterator (org.apache.kafka.common.utils.MappedIterator)1