Search in sources :

Example 1 with EndTxnResponse

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

the class SenderTest method testIncompleteTransactionAbortOnShutdown.

@Test
public void testIncompleteTransactionAbortOnShutdown() {
    // create a sender with retries = 1
    int maxRetries = 1;
    Metrics m = new Metrics();
    SenderMetricsRegistry senderMetrics = new SenderMetricsRegistry(m);
    try {
        TransactionManager txnManager = new TransactionManager(logContext, "testIncompleteTransactionAbortOnShutdown", 6000, 100, apiVersions);
        Sender sender = new Sender(logContext, client, metadata, this.accumulator, false, MAX_REQUEST_SIZE, ACKS_ALL, maxRetries, senderMetrics, time, REQUEST_TIMEOUT, RETRY_BACKOFF_MS, txnManager, apiVersions);
        ProducerIdAndEpoch producerIdAndEpoch = new ProducerIdAndEpoch(123456L, (short) 0);
        TopicPartition tp = new TopicPartition("testIncompleteTransactionAbortOnShutdown", 1);
        setupWithTransactionState(txnManager);
        doInitTransactions(txnManager, producerIdAndEpoch);
        txnManager.beginTransaction();
        txnManager.maybeAddPartition(tp);
        client.prepareResponse(new AddPartitionsToTxnResponse(0, Collections.singletonMap(tp, Errors.NONE)));
        sender.runOnce();
        sender.initiateClose();
        AssertEndTxnRequestMatcher endTxnMatcher = new AssertEndTxnRequestMatcher(TransactionResult.ABORT);
        client.prepareResponse(endTxnMatcher, new EndTxnResponse(new EndTxnResponseData().setErrorCode(Errors.NONE.code()).setThrottleTimeMs(0)));
        sender.run();
        assertTrue(endTxnMatcher.matched, "Response didn't match in test");
    } finally {
        m.close();
    }
}
Also used : Metrics(org.apache.kafka.common.metrics.Metrics) EndTxnResponseData(org.apache.kafka.common.message.EndTxnResponseData) TopicPartition(org.apache.kafka.common.TopicPartition) EndTxnResponse(org.apache.kafka.common.requests.EndTxnResponse) AddPartitionsToTxnResponse(org.apache.kafka.common.requests.AddPartitionsToTxnResponse) ProducerIdAndEpoch(org.apache.kafka.common.utils.ProducerIdAndEpoch) Test(org.junit.jupiter.api.Test)

Example 2 with EndTxnResponse

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

the class SenderTest method testTransactionalRequestsSentOnShutdown.

@Test
public void testTransactionalRequestsSentOnShutdown() {
    // create a sender with retries = 1
    int maxRetries = 1;
    Metrics m = new Metrics();
    SenderMetricsRegistry senderMetrics = new SenderMetricsRegistry(m);
    try {
        TransactionManager txnManager = new TransactionManager(logContext, "testTransactionalRequestsSentOnShutdown", 6000, 100, apiVersions);
        Sender sender = new Sender(logContext, client, metadata, this.accumulator, false, MAX_REQUEST_SIZE, ACKS_ALL, maxRetries, senderMetrics, time, REQUEST_TIMEOUT, RETRY_BACKOFF_MS, txnManager, apiVersions);
        ProducerIdAndEpoch producerIdAndEpoch = new ProducerIdAndEpoch(123456L, (short) 0);
        TopicPartition tp = new TopicPartition("testTransactionalRequestsSentOnShutdown", 1);
        setupWithTransactionState(txnManager);
        doInitTransactions(txnManager, producerIdAndEpoch);
        txnManager.beginTransaction();
        txnManager.maybeAddPartition(tp);
        client.prepareResponse(new AddPartitionsToTxnResponse(0, Collections.singletonMap(tp, Errors.NONE)));
        sender.runOnce();
        sender.initiateClose();
        txnManager.beginCommit();
        AssertEndTxnRequestMatcher endTxnMatcher = new AssertEndTxnRequestMatcher(TransactionResult.COMMIT);
        client.prepareResponse(endTxnMatcher, new EndTxnResponse(new EndTxnResponseData().setErrorCode(Errors.NONE.code()).setThrottleTimeMs(0)));
        sender.run();
        assertTrue(endTxnMatcher.matched, "Response didn't match in test");
    } finally {
        m.close();
    }
}
Also used : Metrics(org.apache.kafka.common.metrics.Metrics) EndTxnResponseData(org.apache.kafka.common.message.EndTxnResponseData) TopicPartition(org.apache.kafka.common.TopicPartition) EndTxnResponse(org.apache.kafka.common.requests.EndTxnResponse) AddPartitionsToTxnResponse(org.apache.kafka.common.requests.AddPartitionsToTxnResponse) ProducerIdAndEpoch(org.apache.kafka.common.utils.ProducerIdAndEpoch) Test(org.junit.jupiter.api.Test)

Example 3 with EndTxnResponse

use of org.apache.kafka.common.requests.EndTxnResponse 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)

Aggregations

TopicPartition (org.apache.kafka.common.TopicPartition)3 EndTxnResponseData (org.apache.kafka.common.message.EndTxnResponseData)3 Metrics (org.apache.kafka.common.metrics.Metrics)3 EndTxnResponse (org.apache.kafka.common.requests.EndTxnResponse)3 Test (org.junit.jupiter.api.Test)3 AddPartitionsToTxnResponse (org.apache.kafka.common.requests.AddPartitionsToTxnResponse)2 ProducerIdAndEpoch (org.apache.kafka.common.utils.ProducerIdAndEpoch)2 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