Search in sources :

Example 1 with TxnOffsetCommitRequestData

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

the class KafkaProducerTest method testSendTxnOffsetsWithGroupMetadata.

@Test
public void testSendTxnOffsetsWithGroupMetadata() {
    final short maxVersion = (short) 3;
    Map<String, Object> configs = new HashMap<>();
    configs.put(ProducerConfig.TRANSACTIONAL_ID_CONFIG, "some.id");
    configs.put(ProducerConfig.MAX_BLOCK_MS_CONFIG, 10000);
    configs.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9000");
    Time time = new MockTime(1);
    MetadataResponse initialUpdateResponse = RequestTestUtils.metadataUpdateWith(1, singletonMap("topic", 1));
    ProducerMetadata metadata = newMetadata(0, Long.MAX_VALUE);
    MockClient client = new MockClient(time, metadata);
    client.updateMetadata(initialUpdateResponse);
    client.setNodeApiVersions(NodeApiVersions.create(ApiKeys.TXN_OFFSET_COMMIT.id, (short) 0, maxVersion));
    Node node = metadata.fetch().nodes().get(0);
    client.throttle(node, 5000);
    client.prepareResponse(FindCoordinatorResponse.prepareResponse(Errors.NONE, "some.id", NODE));
    client.prepareResponse(initProducerIdResponse(1L, (short) 5, Errors.NONE));
    client.prepareResponse(addOffsetsToTxnResponse(Errors.NONE));
    client.prepareResponse(FindCoordinatorResponse.prepareResponse(Errors.NONE, "some.id", NODE));
    String groupId = "group";
    String memberId = "member";
    int generationId = 5;
    String groupInstanceId = "instance";
    client.prepareResponse(request -> {
        TxnOffsetCommitRequestData data = ((TxnOffsetCommitRequest) request).data();
        return data.groupId().equals(groupId) && data.memberId().equals(memberId) && data.generationId() == generationId && data.groupInstanceId().equals(groupInstanceId);
    }, txnOffsetsCommitResponse(Collections.singletonMap(new TopicPartition("topic", 0), Errors.NONE)));
    client.prepareResponse(endTxnResponse(Errors.NONE));
    try (Producer<String, String> producer = kafkaProducer(configs, new StringSerializer(), new StringSerializer(), metadata, client, null, time)) {
        producer.initTransactions();
        producer.beginTransaction();
        ConsumerGroupMetadata groupMetadata = new ConsumerGroupMetadata(groupId, generationId, memberId, Optional.of(groupInstanceId));
        producer.sendOffsetsToTransaction(Collections.emptyMap(), groupMetadata);
        producer.commitTransaction();
    }
}
Also used : ProducerMetadata(org.apache.kafka.clients.producer.internals.ProducerMetadata) HashMap(java.util.HashMap) Node(org.apache.kafka.common.Node) MockTime(org.apache.kafka.common.utils.MockTime) Time(org.apache.kafka.common.utils.Time) TxnOffsetCommitRequest(org.apache.kafka.common.requests.TxnOffsetCommitRequest) TxnOffsetCommitRequestData(org.apache.kafka.common.message.TxnOffsetCommitRequestData) ConsumerGroupMetadata(org.apache.kafka.clients.consumer.ConsumerGroupMetadata) TopicPartition(org.apache.kafka.common.TopicPartition) MetadataResponse(org.apache.kafka.common.requests.MetadataResponse) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) MockTime(org.apache.kafka.common.utils.MockTime) MockClient(org.apache.kafka.clients.MockClient) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Aggregations

HashMap (java.util.HashMap)1 MockClient (org.apache.kafka.clients.MockClient)1 ConsumerGroupMetadata (org.apache.kafka.clients.consumer.ConsumerGroupMetadata)1 ProducerMetadata (org.apache.kafka.clients.producer.internals.ProducerMetadata)1 Node (org.apache.kafka.common.Node)1 TopicPartition (org.apache.kafka.common.TopicPartition)1 TxnOffsetCommitRequestData (org.apache.kafka.common.message.TxnOffsetCommitRequestData)1 MetadataResponse (org.apache.kafka.common.requests.MetadataResponse)1 TxnOffsetCommitRequest (org.apache.kafka.common.requests.TxnOffsetCommitRequest)1 StringSerializer (org.apache.kafka.common.serialization.StringSerializer)1 MockTime (org.apache.kafka.common.utils.MockTime)1 Time (org.apache.kafka.common.utils.Time)1 Test (org.junit.jupiter.api.Test)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1