Search in sources :

Example 1 with TxnOffsetCommitRequestTopic

use of org.apache.kafka.common.message.TxnOffsetCommitRequestData.TxnOffsetCommitRequestTopic in project kafka by apache.

the class MessageTest method testTxnOffsetCommitRequestVersions.

@Test
public void testTxnOffsetCommitRequestVersions() throws Exception {
    String groupId = "groupId";
    String topicName = "topic";
    String metadata = "metadata";
    String txnId = "transactionalId";
    int producerId = 25;
    short producerEpoch = 10;
    String instanceId = "instance";
    String memberId = "member";
    int generationId = 1;
    int partition = 2;
    int offset = 100;
    testAllMessageRoundTrips(new TxnOffsetCommitRequestData().setGroupId(groupId).setTransactionalId(txnId).setProducerId(producerId).setProducerEpoch(producerEpoch).setTopics(Collections.singletonList(new TxnOffsetCommitRequestTopic().setName(topicName).setPartitions(Collections.singletonList(new TxnOffsetCommitRequestPartition().setPartitionIndex(partition).setCommittedMetadata(metadata).setCommittedOffset(offset))))));
    Supplier<TxnOffsetCommitRequestData> request = () -> new TxnOffsetCommitRequestData().setGroupId(groupId).setTransactionalId(txnId).setProducerId(producerId).setProducerEpoch(producerEpoch).setGroupInstanceId(instanceId).setMemberId(memberId).setGenerationId(generationId).setTopics(Collections.singletonList(new TxnOffsetCommitRequestTopic().setName(topicName).setPartitions(Collections.singletonList(new TxnOffsetCommitRequestPartition().setPartitionIndex(partition).setCommittedLeaderEpoch(10).setCommittedMetadata(metadata).setCommittedOffset(offset)))));
    for (short version : ApiKeys.TXN_OFFSET_COMMIT.allVersions()) {
        TxnOffsetCommitRequestData requestData = request.get();
        if (version < 2) {
            requestData.topics().get(0).partitions().get(0).setCommittedLeaderEpoch(-1);
        }
        if (version < 3) {
            final short finalVersion = version;
            assertThrows(UnsupportedVersionException.class, () -> testEquivalentMessageRoundTrip(finalVersion, requestData));
            requestData.setGroupInstanceId(null);
            assertThrows(UnsupportedVersionException.class, () -> testEquivalentMessageRoundTrip(finalVersion, requestData));
            requestData.setMemberId("");
            assertThrows(UnsupportedVersionException.class, () -> testEquivalentMessageRoundTrip(finalVersion, requestData));
            requestData.setGenerationId(-1);
        }
        testAllMessageRoundTripsFromVersion(version, requestData);
    }
}
Also used : TxnOffsetCommitRequestTopic(org.apache.kafka.common.message.TxnOffsetCommitRequestData.TxnOffsetCommitRequestTopic) TxnOffsetCommitRequestPartition(org.apache.kafka.common.message.TxnOffsetCommitRequestData.TxnOffsetCommitRequestPartition) Test(org.junit.jupiter.api.Test)

Example 2 with TxnOffsetCommitRequestTopic

use of org.apache.kafka.common.message.TxnOffsetCommitRequestData.TxnOffsetCommitRequestTopic in project kafka by apache.

the class TxnOffsetCommitRequest method offsets.

public Map<TopicPartition, CommittedOffset> offsets() {
    List<TxnOffsetCommitRequestTopic> topics = data.topics();
    Map<TopicPartition, CommittedOffset> offsetMap = new HashMap<>();
    for (TxnOffsetCommitRequestTopic topic : topics) {
        for (TxnOffsetCommitRequestPartition partition : topic.partitions()) {
            offsetMap.put(new TopicPartition(topic.name(), partition.partitionIndex()), new CommittedOffset(partition.committedOffset(), partition.committedMetadata(), RequestUtils.getLeaderEpoch(partition.committedLeaderEpoch())));
        }
    }
    return offsetMap;
}
Also used : TxnOffsetCommitRequestTopic(org.apache.kafka.common.message.TxnOffsetCommitRequestData.TxnOffsetCommitRequestTopic) HashMap(java.util.HashMap) TopicPartition(org.apache.kafka.common.TopicPartition) TxnOffsetCommitRequestPartition(org.apache.kafka.common.message.TxnOffsetCommitRequestData.TxnOffsetCommitRequestPartition)

Example 3 with TxnOffsetCommitRequestTopic

use of org.apache.kafka.common.message.TxnOffsetCommitRequestData.TxnOffsetCommitRequestTopic in project kafka by apache.

the class TxnOffsetCommitRequest method getErrorResponseTopics.

static List<TxnOffsetCommitResponseTopic> getErrorResponseTopics(List<TxnOffsetCommitRequestTopic> requestTopics, Errors e) {
    List<TxnOffsetCommitResponseTopic> responseTopicData = new ArrayList<>();
    for (TxnOffsetCommitRequestTopic entry : requestTopics) {
        List<TxnOffsetCommitResponsePartition> responsePartitions = new ArrayList<>();
        for (TxnOffsetCommitRequestPartition requestPartition : entry.partitions()) {
            responsePartitions.add(new TxnOffsetCommitResponsePartition().setPartitionIndex(requestPartition.partitionIndex()).setErrorCode(e.code()));
        }
        responseTopicData.add(new TxnOffsetCommitResponseTopic().setName(entry.name()).setPartitions(responsePartitions));
    }
    return responseTopicData;
}
Also used : TxnOffsetCommitRequestTopic(org.apache.kafka.common.message.TxnOffsetCommitRequestData.TxnOffsetCommitRequestTopic) TxnOffsetCommitResponseTopic(org.apache.kafka.common.message.TxnOffsetCommitResponseData.TxnOffsetCommitResponseTopic) TxnOffsetCommitResponsePartition(org.apache.kafka.common.message.TxnOffsetCommitResponseData.TxnOffsetCommitResponsePartition) ArrayList(java.util.ArrayList) TxnOffsetCommitRequestPartition(org.apache.kafka.common.message.TxnOffsetCommitRequestData.TxnOffsetCommitRequestPartition)

Example 4 with TxnOffsetCommitRequestTopic

use of org.apache.kafka.common.message.TxnOffsetCommitRequestData.TxnOffsetCommitRequestTopic in project kafka by apache.

the class TxnOffsetCommitRequest method getTopics.

static List<TxnOffsetCommitRequestTopic> getTopics(Map<TopicPartition, CommittedOffset> pendingTxnOffsetCommits) {
    Map<String, List<TxnOffsetCommitRequestPartition>> topicPartitionMap = new HashMap<>();
    for (Map.Entry<TopicPartition, CommittedOffset> entry : pendingTxnOffsetCommits.entrySet()) {
        TopicPartition topicPartition = entry.getKey();
        CommittedOffset offset = entry.getValue();
        List<TxnOffsetCommitRequestPartition> partitions = topicPartitionMap.getOrDefault(topicPartition.topic(), new ArrayList<>());
        partitions.add(new TxnOffsetCommitRequestPartition().setPartitionIndex(topicPartition.partition()).setCommittedOffset(offset.offset).setCommittedLeaderEpoch(offset.leaderEpoch.orElse(RecordBatch.NO_PARTITION_LEADER_EPOCH)).setCommittedMetadata(offset.metadata));
        topicPartitionMap.put(topicPartition.topic(), partitions);
    }
    return topicPartitionMap.entrySet().stream().map(entry -> new TxnOffsetCommitRequestTopic().setName(entry.getKey()).setPartitions(entry.getValue())).collect(Collectors.toList());
}
Also used : TopicPartition(org.apache.kafka.common.TopicPartition) TxnOffsetCommitRequestTopic(org.apache.kafka.common.message.TxnOffsetCommitRequestData.TxnOffsetCommitRequestTopic) ByteBufferAccessor(org.apache.kafka.common.protocol.ByteBufferAccessor) HashMap(java.util.HashMap) TxnOffsetCommitResponseData(org.apache.kafka.common.message.TxnOffsetCommitResponseData) ApiKeys(org.apache.kafka.common.protocol.ApiKeys) Collectors(java.util.stream.Collectors) ByteBuffer(java.nio.ByteBuffer) TxnOffsetCommitResponsePartition(org.apache.kafka.common.message.TxnOffsetCommitResponseData.TxnOffsetCommitResponsePartition) ArrayList(java.util.ArrayList) Objects(java.util.Objects) TxnOffsetCommitRequestPartition(org.apache.kafka.common.message.TxnOffsetCommitRequestData.TxnOffsetCommitRequestPartition) List(java.util.List) RecordBatch(org.apache.kafka.common.record.RecordBatch) Map(java.util.Map) UnsupportedVersionException(org.apache.kafka.common.errors.UnsupportedVersionException) Errors(org.apache.kafka.common.protocol.Errors) Optional(java.util.Optional) TxnOffsetCommitRequestData(org.apache.kafka.common.message.TxnOffsetCommitRequestData) TxnOffsetCommitResponseTopic(org.apache.kafka.common.message.TxnOffsetCommitResponseData.TxnOffsetCommitResponseTopic) TxnOffsetCommitRequestTopic(org.apache.kafka.common.message.TxnOffsetCommitRequestData.TxnOffsetCommitRequestTopic) HashMap(java.util.HashMap) TxnOffsetCommitRequestPartition(org.apache.kafka.common.message.TxnOffsetCommitRequestData.TxnOffsetCommitRequestPartition) TopicPartition(org.apache.kafka.common.TopicPartition) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with TxnOffsetCommitRequestTopic

use of org.apache.kafka.common.message.TxnOffsetCommitRequestData.TxnOffsetCommitRequestTopic in project kafka by apache.

the class TxnOffsetCommitRequestTest method testConstructor.

@Test
@Override
public void testConstructor() {
    Map<TopicPartition, Errors> errorsMap = new HashMap<>();
    errorsMap.put(new TopicPartition(topicOne, partitionOne), Errors.NOT_COORDINATOR);
    errorsMap.put(new TopicPartition(topicTwo, partitionTwo), Errors.NOT_COORDINATOR);
    List<TxnOffsetCommitRequestTopic> expectedTopics = Arrays.asList(new TxnOffsetCommitRequestTopic().setName(topicOne).setPartitions(Collections.singletonList(new TxnOffsetCommitRequestPartition().setPartitionIndex(partitionOne).setCommittedOffset(offset).setCommittedLeaderEpoch(leaderEpoch).setCommittedMetadata(metadata))), new TxnOffsetCommitRequestTopic().setName(topicTwo).setPartitions(Collections.singletonList(new TxnOffsetCommitRequestPartition().setPartitionIndex(partitionTwo).setCommittedOffset(offset).setCommittedLeaderEpoch(leaderEpoch).setCommittedMetadata(metadata))));
    for (short version : ApiKeys.TXN_OFFSET_COMMIT.allVersions()) {
        final TxnOffsetCommitRequest request;
        if (version < 3) {
            request = builder.build(version);
        } else {
            request = builderWithGroupMetadata.build(version);
        }
        assertEquals(offsets, request.offsets());
        assertEquals(expectedTopics, TxnOffsetCommitRequest.getTopics(request.offsets()));
        TxnOffsetCommitResponse response = request.getErrorResponse(throttleTimeMs, Errors.NOT_COORDINATOR.exception());
        assertEquals(errorsMap, response.errors());
        assertEquals(Collections.singletonMap(Errors.NOT_COORDINATOR, 2), response.errorCounts());
        assertEquals(throttleTimeMs, response.throttleTimeMs());
    }
}
Also used : TxnOffsetCommitRequestTopic(org.apache.kafka.common.message.TxnOffsetCommitRequestData.TxnOffsetCommitRequestTopic) Errors(org.apache.kafka.common.protocol.Errors) HashMap(java.util.HashMap) TopicPartition(org.apache.kafka.common.TopicPartition) TxnOffsetCommitRequestPartition(org.apache.kafka.common.message.TxnOffsetCommitRequestData.TxnOffsetCommitRequestPartition) Test(org.junit.jupiter.api.Test)

Aggregations

TxnOffsetCommitRequestPartition (org.apache.kafka.common.message.TxnOffsetCommitRequestData.TxnOffsetCommitRequestPartition)5 TxnOffsetCommitRequestTopic (org.apache.kafka.common.message.TxnOffsetCommitRequestData.TxnOffsetCommitRequestTopic)5 HashMap (java.util.HashMap)3 TopicPartition (org.apache.kafka.common.TopicPartition)3 ArrayList (java.util.ArrayList)2 TxnOffsetCommitResponsePartition (org.apache.kafka.common.message.TxnOffsetCommitResponseData.TxnOffsetCommitResponsePartition)2 TxnOffsetCommitResponseTopic (org.apache.kafka.common.message.TxnOffsetCommitResponseData.TxnOffsetCommitResponseTopic)2 Errors (org.apache.kafka.common.protocol.Errors)2 Test (org.junit.jupiter.api.Test)2 ByteBuffer (java.nio.ByteBuffer)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 UnsupportedVersionException (org.apache.kafka.common.errors.UnsupportedVersionException)1 TxnOffsetCommitRequestData (org.apache.kafka.common.message.TxnOffsetCommitRequestData)1 TxnOffsetCommitResponseData (org.apache.kafka.common.message.TxnOffsetCommitResponseData)1 ApiKeys (org.apache.kafka.common.protocol.ApiKeys)1 ByteBufferAccessor (org.apache.kafka.common.protocol.ByteBufferAccessor)1