Search in sources :

Example 1 with InitProducerIdResponse

use of org.apache.kafka.common.requests.InitProducerIdResponse in project kafka by apache.

the class TransactionManagerTest method prepareInitPidResponse.

private void prepareInitPidResponse(Errors error, boolean shouldDisconnect, long producerId, short producerEpoch) {
    InitProducerIdResponseData responseData = new InitProducerIdResponseData().setErrorCode(error.code()).setProducerEpoch(producerEpoch).setProducerId(producerId).setThrottleTimeMs(0);
    client.prepareResponse(body -> {
        InitProducerIdRequest initProducerIdRequest = (InitProducerIdRequest) body;
        assertEquals(transactionalId, initProducerIdRequest.data().transactionalId());
        assertEquals(transactionTimeoutMs, initProducerIdRequest.data().transactionTimeoutMs());
        return true;
    }, new InitProducerIdResponse(responseData), shouldDisconnect);
}
Also used : InitProducerIdResponseData(org.apache.kafka.common.message.InitProducerIdResponseData) InitProducerIdRequest(org.apache.kafka.common.requests.InitProducerIdRequest) InitProducerIdResponse(org.apache.kafka.common.requests.InitProducerIdResponse)

Example 2 with InitProducerIdResponse

use of org.apache.kafka.common.requests.InitProducerIdResponse in project kafka by apache.

the class TransactionManagerTest method initializeIdempotentProducerId.

private void initializeIdempotentProducerId(long producerId, short epoch) {
    InitProducerIdResponseData responseData = new InitProducerIdResponseData().setErrorCode(Errors.NONE.code()).setProducerEpoch(epoch).setProducerId(producerId).setThrottleTimeMs(0);
    client.prepareResponse(body -> {
        InitProducerIdRequest initProducerIdRequest = (InitProducerIdRequest) body;
        assertNull(initProducerIdRequest.data().transactionalId());
        return true;
    }, new InitProducerIdResponse(responseData), false);
    runUntil(transactionManager::hasProducerId);
}
Also used : InitProducerIdResponseData(org.apache.kafka.common.message.InitProducerIdResponseData) InitProducerIdRequest(org.apache.kafka.common.requests.InitProducerIdRequest) InitProducerIdResponse(org.apache.kafka.common.requests.InitProducerIdResponse)

Example 3 with InitProducerIdResponse

use of org.apache.kafka.common.requests.InitProducerIdResponse in project kafka by apache.

the class KafkaProducerTest method testCommitTransactionWithRecordTooLargeException.

@Test
public void testCommitTransactionWithRecordTooLargeException() throws Exception {
    Map<String, Object> configs = new HashMap<>();
    configs.put(ProducerConfig.TRANSACTIONAL_ID_CONFIG, "some.id");
    configs.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9000");
    configs.put(ProducerConfig.MAX_REQUEST_SIZE_CONFIG, 1000);
    Time time = new MockTime(1);
    MetadataResponse initialUpdateResponse = RequestTestUtils.metadataUpdateWith(1, singletonMap("topic", 1));
    ProducerMetadata metadata = mock(ProducerMetadata.class);
    MockClient client = new MockClient(time, metadata);
    client.updateMetadata(initialUpdateResponse);
    client.prepareResponse(FindCoordinatorResponse.prepareResponse(Errors.NONE, "some.id", NODE));
    client.prepareResponse(initProducerIdResponse(1L, (short) 5, Errors.NONE));
    when(metadata.fetch()).thenReturn(onePartitionCluster);
    String largeString = IntStream.range(0, 1000).mapToObj(i -> "*").collect(Collectors.joining());
    ProducerRecord<String, String> largeRecord = new ProducerRecord<>(topic, "large string", largeString);
    try (KafkaProducer<String, String> producer = kafkaProducer(configs, new StringSerializer(), new StringSerializer(), metadata, client, null, time)) {
        producer.initTransactions();
        client.prepareResponse(endTxnResponse(Errors.NONE));
        producer.beginTransaction();
        TestUtils.assertFutureError(producer.send(largeRecord), RecordTooLargeException.class);
        assertThrows(KafkaException.class, producer::commitTransaction);
    }
}
Also used : Selectable(org.apache.kafka.common.network.Selectable) MockTime(org.apache.kafka.common.utils.MockTime) Arrays(java.util.Arrays) ConsumerGroupMetadata(org.apache.kafka.clients.consumer.ConsumerGroupMetadata) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Sender(org.apache.kafka.clients.producer.internals.Sender) KafkaException(org.apache.kafka.common.KafkaException) Cluster(org.apache.kafka.common.Cluster) Future(java.util.concurrent.Future) RecordBatch(org.apache.kafka.common.record.RecordBatch) LogContext(org.apache.kafka.common.utils.LogContext) Duration(java.time.Duration) Map(java.util.Map) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) MockProducerInterceptor(org.apache.kafka.test.MockProducerInterceptor) CommonClientConfigs(org.apache.kafka.clients.CommonClientConfigs) Sensor(org.apache.kafka.common.metrics.Sensor) TestUtils(org.apache.kafka.test.TestUtils) InvalidTopicException(org.apache.kafka.common.errors.InvalidTopicException) ConsumerConfig(org.apache.kafka.clients.consumer.ConsumerConfig) PartitionInfo(org.apache.kafka.common.PartitionInfo) StandardCharsets(java.nio.charset.StandardCharsets) Executors(java.util.concurrent.Executors) CountDownLatch(java.util.concurrent.CountDownLatch) Metrics(org.apache.kafka.common.metrics.Metrics) Stream(java.util.stream.Stream) MockMetricsReporter(org.apache.kafka.test.MockMetricsReporter) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Errors(org.apache.kafka.common.protocol.Errors) Node(org.apache.kafka.common.Node) MetadataResponse(org.apache.kafka.common.requests.MetadataResponse) ArgumentMatchers.notNull(org.mockito.ArgumentMatchers.notNull) NodeApiVersions(org.apache.kafka.clients.NodeApiVersions) Mockito.mock(org.mockito.Mockito.mock) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Assertions.fail(org.junit.jupiter.api.Assertions.fail) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) RequestTestUtils(org.apache.kafka.common.requests.RequestTestUtils) RecordHeader(org.apache.kafka.common.header.internals.RecordHeader) ArrayList(java.util.ArrayList) InterruptException(org.apache.kafka.common.errors.InterruptException) EndTxnResponseData(org.apache.kafka.common.message.EndTxnResponseData) ByteArraySerializer(org.apache.kafka.common.serialization.ByteArraySerializer) FindCoordinatorRequest(org.apache.kafka.common.requests.FindCoordinatorRequest) ManagementFactory(java.lang.management.ManagementFactory) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) SslConfigs(org.apache.kafka.common.config.SslConfigs) ProduceResponse(org.apache.kafka.common.requests.ProduceResponse) ProducerInterceptors(org.apache.kafka.clients.producer.internals.ProducerInterceptors) ValueSource(org.junit.jupiter.params.provider.ValueSource) TxnOffsetCommitResponse(org.apache.kafka.common.requests.TxnOffsetCommitResponse) Properties(java.util.Properties) Mockito.times(org.mockito.Mockito.times) ExecutionException(java.util.concurrent.ExecutionException) Assertions.assertArrayEquals(org.junit.jupiter.api.Assertions.assertArrayEquals) AddOffsetsToTxnResponse(org.apache.kafka.common.requests.AddOffsetsToTxnResponse) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Serializer(org.apache.kafka.common.serialization.Serializer) Avg(org.apache.kafka.common.metrics.stats.Avg) TxnOffsetCommitRequestData(org.apache.kafka.common.message.TxnOffsetCommitRequestData) Exchanger(java.util.concurrent.Exchanger) FindCoordinatorResponse(org.apache.kafka.common.requests.FindCoordinatorResponse) JoinGroupRequest(org.apache.kafka.common.requests.JoinGroupRequest) Assertions.assertNotEquals(org.junit.jupiter.api.Assertions.assertNotEquals) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) KafkaClient(org.apache.kafka.clients.KafkaClient) Metric(org.apache.kafka.common.Metric) MetricName(org.apache.kafka.common.MetricName) TopicPartition(org.apache.kafka.common.TopicPartition) InitProducerIdResponseData(org.apache.kafka.common.message.InitProducerIdResponseData) Time(org.apache.kafka.common.utils.Time) EndTxnResponse(org.apache.kafka.common.requests.EndTxnResponse) Collection(java.util.Collection) ObjectName(javax.management.ObjectName) ClusterResourceListeners(org.apache.kafka.common.internals.ClusterResourceListeners) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) List(java.util.List) InitProducerIdResponse(org.apache.kafka.common.requests.InitProducerIdResponse) OffsetAndMetadata(org.apache.kafka.clients.consumer.OffsetAndMetadata) RecordTooLargeException(org.apache.kafka.common.errors.RecordTooLargeException) Optional(java.util.Optional) IntStream(java.util.stream.IntStream) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) ProducerMetadata(org.apache.kafka.clients.producer.internals.ProducerMetadata) MBeanServer(javax.management.MBeanServer) Collections.singletonMap(java.util.Collections.singletonMap) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) ExecutorService(java.util.concurrent.ExecutorService) MockPartitioner(org.apache.kafka.test.MockPartitioner) Collections.emptyMap(java.util.Collections.emptyMap) TimeoutException(org.apache.kafka.common.errors.TimeoutException) MockClient(org.apache.kafka.clients.MockClient) TxnOffsetCommitRequest(org.apache.kafka.common.requests.TxnOffsetCommitRequest) Mockito.when(org.mockito.Mockito.when) ApiKeys(org.apache.kafka.common.protocol.ApiKeys) ConfigException(org.apache.kafka.common.config.ConfigException) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) MockSerializer(org.apache.kafka.test.MockSerializer) Collections(java.util.Collections) AddOffsetsToTxnResponseData(org.apache.kafka.common.message.AddOffsetsToTxnResponseData) ProducerMetadata(org.apache.kafka.clients.producer.internals.ProducerMetadata) HashMap(java.util.HashMap) MockTime(org.apache.kafka.common.utils.MockTime) Time(org.apache.kafka.common.utils.Time) 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)

Example 4 with InitProducerIdResponse

use of org.apache.kafka.common.requests.InitProducerIdResponse in project apache-kafka-on-k8s by banzaicloud.

the class Sender method maybeWaitForProducerId.

private void maybeWaitForProducerId() {
    while (!transactionManager.hasProducerId() && !transactionManager.hasError()) {
        try {
            Node node = awaitLeastLoadedNodeReady(requestTimeout);
            if (node != null) {
                ClientResponse response = sendAndAwaitInitProducerIdRequest(node);
                InitProducerIdResponse initProducerIdResponse = (InitProducerIdResponse) response.responseBody();
                Errors error = initProducerIdResponse.error();
                if (error == Errors.NONE) {
                    ProducerIdAndEpoch producerIdAndEpoch = new ProducerIdAndEpoch(initProducerIdResponse.producerId(), initProducerIdResponse.epoch());
                    transactionManager.setProducerIdAndEpoch(producerIdAndEpoch);
                    return;
                } else if (error.exception() instanceof RetriableException) {
                    log.debug("Retriable error from InitProducerId response", error.message());
                } else {
                    transactionManager.transitionToFatalError(error.exception());
                    break;
                }
            } else {
                log.debug("Could not find an available broker to send InitProducerIdRequest to. " + "We will back off and try again.");
            }
        } catch (UnsupportedVersionException e) {
            transactionManager.transitionToFatalError(e);
            break;
        } catch (IOException e) {
            log.debug("Broker {} disconnected while awaiting InitProducerId response", e);
        }
        log.trace("Retry InitProducerIdRequest in {}ms.", retryBackoffMs);
        time.sleep(retryBackoffMs);
        metadata.requestUpdate();
    }
}
Also used : ClientResponse(org.apache.kafka.clients.ClientResponse) Errors(org.apache.kafka.common.protocol.Errors) Node(org.apache.kafka.common.Node) InitProducerIdResponse(org.apache.kafka.common.requests.InitProducerIdResponse) IOException(java.io.IOException) RetriableException(org.apache.kafka.common.errors.RetriableException) UnsupportedVersionException(org.apache.kafka.common.errors.UnsupportedVersionException)

Example 5 with InitProducerIdResponse

use of org.apache.kafka.common.requests.InitProducerIdResponse in project apache-kafka-on-k8s by banzaicloud.

the class SenderTest method prepareAndReceiveInitProducerId.

private void prepareAndReceiveInitProducerId(long producerId, Errors error) {
    short producerEpoch = 0;
    if (error != Errors.NONE)
        producerEpoch = RecordBatch.NO_PRODUCER_EPOCH;
    client.prepareResponse(new MockClient.RequestMatcher() {

        @Override
        public boolean matches(AbstractRequest body) {
            return body instanceof InitProducerIdRequest && ((InitProducerIdRequest) body).transactionalId() == null;
        }
    }, new InitProducerIdResponse(0, error, producerId, producerEpoch));
    sender.run(time.milliseconds());
}
Also used : AbstractRequest(org.apache.kafka.common.requests.AbstractRequest) InitProducerIdRequest(org.apache.kafka.common.requests.InitProducerIdRequest) InitProducerIdResponse(org.apache.kafka.common.requests.InitProducerIdResponse) MockClient(org.apache.kafka.clients.MockClient)

Aggregations

InitProducerIdResponse (org.apache.kafka.common.requests.InitProducerIdResponse)5 InitProducerIdResponseData (org.apache.kafka.common.message.InitProducerIdResponseData)3 InitProducerIdRequest (org.apache.kafka.common.requests.InitProducerIdRequest)3 MockClient (org.apache.kafka.clients.MockClient)2 IOException (java.io.IOException)1 ManagementFactory (java.lang.management.ManagementFactory)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Duration (java.time.Duration)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Collections.emptyMap (java.util.Collections.emptyMap)1 Collections.singletonMap (java.util.Collections.singletonMap)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Properties (java.util.Properties)1 CountDownLatch (java.util.concurrent.CountDownLatch)1