Search in sources :

Example 1 with StopReplicaPartitionState

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

the class RequestResponseTest method createStopReplicaRequest.

private StopReplicaRequest createStopReplicaRequest(short version, boolean deletePartitions) {
    List<StopReplicaTopicState> topicStates = new ArrayList<>();
    StopReplicaTopicState topic1 = new StopReplicaTopicState().setTopicName("topic1").setPartitionStates(singletonList(new StopReplicaPartitionState().setPartitionIndex(0).setLeaderEpoch(1).setDeletePartition(deletePartitions)));
    topicStates.add(topic1);
    StopReplicaTopicState topic2 = new StopReplicaTopicState().setTopicName("topic2").setPartitionStates(singletonList(new StopReplicaPartitionState().setPartitionIndex(1).setLeaderEpoch(2).setDeletePartition(deletePartitions)));
    topicStates.add(topic2);
    return new StopReplicaRequest.Builder(version, 0, 1, 0, deletePartitions, topicStates).build(version);
}
Also used : StopReplicaTopicState(org.apache.kafka.common.message.StopReplicaRequestData.StopReplicaTopicState) ArrayList(java.util.ArrayList) StopReplicaPartitionState(org.apache.kafka.common.message.StopReplicaRequestData.StopReplicaPartitionState)

Example 2 with StopReplicaPartitionState

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

the class StopReplicaRequestTest method testTopicStatesNormalization.

@Test
public void testTopicStatesNormalization() {
    List<StopReplicaTopicState> topicStates = topicStates(true);
    for (short version : STOP_REPLICA.allVersions()) {
        // Create a request for version to get its serialized form
        StopReplicaRequest baseRequest = new StopReplicaRequest.Builder(version, 0, 1, 0, true, topicStates).build(version);
        // Construct the request from the buffer
        StopReplicaRequest request = StopReplicaRequest.parse(baseRequest.serialize(), version);
        Map<TopicPartition, StopReplicaPartitionState> partitionStates = StopReplicaRequestTest.partitionStates(request.topicStates());
        assertEquals(6, partitionStates.size());
        for (StopReplicaTopicState expectedTopicState : topicStates) {
            for (StopReplicaPartitionState expectedPartitionState : expectedTopicState.partitionStates()) {
                TopicPartition tp = new TopicPartition(expectedTopicState.topicName(), expectedPartitionState.partitionIndex());
                StopReplicaPartitionState partitionState = partitionStates.get(tp);
                assertEquals(expectedPartitionState.partitionIndex(), partitionState.partitionIndex());
                assertTrue(partitionState.deletePartition());
                if (version >= 3) {
                    assertEquals(expectedPartitionState.leaderEpoch(), partitionState.leaderEpoch());
                } else {
                    assertEquals(-1, partitionState.leaderEpoch());
                }
            }
        }
    }
}
Also used : StopReplicaTopicState(org.apache.kafka.common.message.StopReplicaRequestData.StopReplicaTopicState) TopicPartition(org.apache.kafka.common.TopicPartition) StopReplicaPartitionState(org.apache.kafka.common.message.StopReplicaRequestData.StopReplicaPartitionState) Test(org.junit.jupiter.api.Test)

Example 3 with StopReplicaPartitionState

use of org.apache.kafka.common.message.StopReplicaRequestData.StopReplicaPartitionState 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 4 with StopReplicaPartitionState

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

the class StopReplicaRequest method getErrorResponse.

@Override
public StopReplicaResponse getErrorResponse(int throttleTimeMs, Throwable e) {
    Errors error = Errors.forException(e);
    StopReplicaResponseData data = new StopReplicaResponseData();
    data.setErrorCode(error.code());
    List<StopReplicaPartitionError> partitions = new ArrayList<>();
    for (StopReplicaTopicState topic : topicStates()) {
        for (StopReplicaPartitionState partition : topic.partitionStates()) {
            partitions.add(new StopReplicaPartitionError().setTopicName(topic.topicName()).setPartitionIndex(partition.partitionIndex()).setErrorCode(error.code()));
        }
    }
    data.setPartitionErrors(partitions);
    return new StopReplicaResponse(data);
}
Also used : Errors(org.apache.kafka.common.protocol.Errors) StopReplicaPartitionError(org.apache.kafka.common.message.StopReplicaResponseData.StopReplicaPartitionError) StopReplicaTopicState(org.apache.kafka.common.message.StopReplicaRequestData.StopReplicaTopicState) ArrayList(java.util.ArrayList) StopReplicaPartitionState(org.apache.kafka.common.message.StopReplicaRequestData.StopReplicaPartitionState) StopReplicaResponseData(org.apache.kafka.common.message.StopReplicaResponseData)

Example 5 with StopReplicaPartitionState

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

the class StopReplicaRequestTest method topicStates.

private List<StopReplicaTopicState> topicStates(boolean deletePartition) {
    List<StopReplicaTopicState> topicStates = new ArrayList<>();
    StopReplicaTopicState topic0 = new StopReplicaTopicState().setTopicName("topic0");
    topic0.partitionStates().add(new StopReplicaPartitionState().setPartitionIndex(0).setLeaderEpoch(0).setDeletePartition(deletePartition));
    topic0.partitionStates().add(new StopReplicaPartitionState().setPartitionIndex(1).setLeaderEpoch(1).setDeletePartition(deletePartition));
    topicStates.add(topic0);
    StopReplicaTopicState topic1 = new StopReplicaTopicState().setTopicName("topic1");
    topic1.partitionStates().add(new StopReplicaPartitionState().setPartitionIndex(2).setLeaderEpoch(2).setDeletePartition(deletePartition));
    topic1.partitionStates().add(new StopReplicaPartitionState().setPartitionIndex(3).setLeaderEpoch(3).setDeletePartition(deletePartition));
    topicStates.add(topic1);
    StopReplicaTopicState topic3 = new StopReplicaTopicState().setTopicName("topic1");
    topic3.partitionStates().add(new StopReplicaPartitionState().setPartitionIndex(4).setLeaderEpoch(-2).setDeletePartition(deletePartition));
    topic3.partitionStates().add(new StopReplicaPartitionState().setPartitionIndex(5).setLeaderEpoch(-2).setDeletePartition(deletePartition));
    topicStates.add(topic3);
    return topicStates;
}
Also used : StopReplicaTopicState(org.apache.kafka.common.message.StopReplicaRequestData.StopReplicaTopicState) ArrayList(java.util.ArrayList) StopReplicaPartitionState(org.apache.kafka.common.message.StopReplicaRequestData.StopReplicaPartitionState)

Aggregations

StopReplicaPartitionState (org.apache.kafka.common.message.StopReplicaRequestData.StopReplicaPartitionState)9 StopReplicaTopicState (org.apache.kafka.common.message.StopReplicaRequestData.StopReplicaTopicState)9 ArrayList (java.util.ArrayList)5 TopicPartition (org.apache.kafka.common.TopicPartition)4 Test (org.junit.jupiter.api.Test)4 StopReplicaPartitionError (org.apache.kafka.common.message.StopReplicaResponseData.StopReplicaPartitionError)3 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 StopReplicaRequestData (org.apache.kafka.common.message.StopReplicaRequestData)2 StopReplicaPartitionV0 (org.apache.kafka.common.message.StopReplicaRequestData.StopReplicaPartitionV0)2 StopReplicaTopicV1 (org.apache.kafka.common.message.StopReplicaRequestData.StopReplicaTopicV1)2 StopReplicaResponseData (org.apache.kafka.common.message.StopReplicaResponseData)2 Errors (org.apache.kafka.common.protocol.Errors)2 ByteBuffer (java.nio.ByteBuffer)1 List (java.util.List)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 ClusterAuthorizationException (org.apache.kafka.common.errors.ClusterAuthorizationException)1 ApiKeys (org.apache.kafka.common.protocol.ApiKeys)1