Search in sources :

Example 6 with ProducerIdAndEpoch

use of org.apache.kafka.common.utils.ProducerIdAndEpoch in project kafka by apache.

the class SenderTest method testForceShutdownWithIncompleteTransaction.

@Timeout(10L)
@Test
public void testForceShutdownWithIncompleteTransaction() {
    // create a sender with retries = 1
    int maxRetries = 1;
    Metrics m = new Metrics();
    SenderMetricsRegistry senderMetrics = new SenderMetricsRegistry(m);
    try {
        TransactionManager txnManager = new TransactionManager(logContext, "testForceShutdownWithIncompleteTransaction", 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("testForceShutdownWithIncompleteTransaction", 1);
        setupWithTransactionState(txnManager);
        doInitTransactions(txnManager, producerIdAndEpoch);
        txnManager.beginTransaction();
        txnManager.maybeAddPartition(tp);
        client.prepareResponse(new AddPartitionsToTxnResponse(0, Collections.singletonMap(tp, Errors.NONE)));
        sender.runOnce();
        // Try to commit the transaction but it won't happen as we'll forcefully close the sender
        TransactionalRequestResult commitResult = txnManager.beginCommit();
        sender.forceClose();
        sender.run();
        assertThrows(KafkaException.class, commitResult::await, "The test expected to throw a KafkaException for forcefully closing the sender");
    } finally {
        m.close();
    }
}
Also used : Metrics(org.apache.kafka.common.metrics.Metrics) TopicPartition(org.apache.kafka.common.TopicPartition) AddPartitionsToTxnResponse(org.apache.kafka.common.requests.AddPartitionsToTxnResponse) ProducerIdAndEpoch(org.apache.kafka.common.utils.ProducerIdAndEpoch) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 7 with ProducerIdAndEpoch

use of org.apache.kafka.common.utils.ProducerIdAndEpoch 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 8 with ProducerIdAndEpoch

use of org.apache.kafka.common.utils.ProducerIdAndEpoch in project kafka by apache.

the class SenderTest method testTransactionAbortedExceptionOnAbortWithoutError.

@Test
public void testTransactionAbortedExceptionOnAbortWithoutError() throws InterruptedException, ExecutionException {
    ProducerIdAndEpoch producerIdAndEpoch = new ProducerIdAndEpoch(123456L, (short) 0);
    TransactionManager txnManager = new TransactionManager(logContext, "testTransactionAbortedExceptionOnAbortWithoutError", 60000, 100, apiVersions);
    setupWithTransactionState(txnManager, false, null);
    doInitTransactions(txnManager, producerIdAndEpoch);
    // Begin the transaction
    txnManager.beginTransaction();
    txnManager.maybeAddPartition(tp0);
    client.prepareResponse(new AddPartitionsToTxnResponse(0, Collections.singletonMap(tp0, Errors.NONE)));
    // Run it once so that the partition is added to the transaction.
    sender.runOnce();
    // Append a record to the accumulator.
    FutureRecordMetadata metadata = appendToAccumulator(tp0, time.milliseconds(), "key", "value");
    // Now abort the transaction manually.
    txnManager.beginAbort();
    // Try to send.
    // This should abort the existing transaction and
    // drain all the unsent batches with a TransactionAbortedException.
    sender.runOnce();
    // Now attempt to fetch the result for the record.
    TestUtils.assertFutureThrows(metadata, TransactionAbortedException.class);
}
Also used : AddPartitionsToTxnResponse(org.apache.kafka.common.requests.AddPartitionsToTxnResponse) ProducerIdAndEpoch(org.apache.kafka.common.utils.ProducerIdAndEpoch) Test(org.junit.jupiter.api.Test)

Example 9 with ProducerIdAndEpoch

use of org.apache.kafka.common.utils.ProducerIdAndEpoch in project kafka by apache.

the class SenderTest method testNodeNotReady.

/**
 * Tests the code path where the target node to send FindCoordinator or InitProducerId
 * is not ready.
 */
@Test
public void testNodeNotReady() {
    final long producerId = 123456L;
    time = new MockTime(10);
    client = new MockClient(time, metadata);
    TransactionManager transactionManager = new TransactionManager(new LogContext(), "testNodeNotReady", 60000, 100L, new ApiVersions());
    setupWithTransactionState(transactionManager, false, null, true);
    ProducerIdAndEpoch producerIdAndEpoch = new ProducerIdAndEpoch(producerId, (short) 0);
    transactionManager.initializeTransactions();
    sender.runOnce();
    Node node = metadata.fetch().nodes().get(0);
    client.delayReady(node, REQUEST_TIMEOUT + 20);
    prepareFindCoordinatorResponse(Errors.NONE, "testNodeNotReady");
    sender.runOnce();
    sender.runOnce();
    assertNotNull(transactionManager.coordinator(CoordinatorType.TRANSACTION), "Coordinator not found");
    client.throttle(node, REQUEST_TIMEOUT + 20);
    prepareFindCoordinatorResponse(Errors.NONE, "Coordinator not found");
    prepareInitProducerResponse(Errors.NONE, producerIdAndEpoch.producerId, producerIdAndEpoch.epoch);
    waitForProducerId(transactionManager, producerIdAndEpoch);
}
Also used : Node(org.apache.kafka.common.Node) NodeApiVersions(org.apache.kafka.clients.NodeApiVersions) ApiVersions(org.apache.kafka.clients.ApiVersions) LogContext(org.apache.kafka.common.utils.LogContext) ProducerIdAndEpoch(org.apache.kafka.common.utils.ProducerIdAndEpoch) MockTime(org.apache.kafka.common.utils.MockTime) MockClient(org.apache.kafka.clients.MockClient) Test(org.junit.jupiter.api.Test)

Example 10 with ProducerIdAndEpoch

use of org.apache.kafka.common.utils.ProducerIdAndEpoch in project kafka by apache.

the class TransactionManagerTest method testNoFailedBatchHandlingWhenTxnManagerIsInFatalError.

@Test
public void testNoFailedBatchHandlingWhenTxnManagerIsInFatalError() {
    initializeTransactionManager(Optional.empty());
    long producerId = 15L;
    short epoch = 5;
    initializeIdempotentProducerId(producerId, epoch);
    TopicPartition tp0 = new TopicPartition("foo", 0);
    ProducerBatch b1 = writeIdempotentBatchWithValue(transactionManager, tp0, "1");
    // Handling b1 should bump the epoch after OutOfOrderSequenceException
    transactionManager.handleFailedBatch(b1, new OutOfOrderSequenceException("out of sequence"), false);
    transactionManager.bumpIdempotentEpochAndResetIdIfNeeded();
    ProducerIdAndEpoch idAndEpochAfterFirstBatch = new ProducerIdAndEpoch(producerId, (short) (epoch + 1));
    assertEquals(idAndEpochAfterFirstBatch, transactionManager.producerIdAndEpoch());
    transactionManager.transitionToFatalError(new KafkaException());
    // The second batch should not bump the epoch as txn manager is already in fatal error state
    ProducerBatch b2 = writeIdempotentBatchWithValue(transactionManager, tp0, "2");
    transactionManager.handleFailedBatch(b2, new TimeoutException(), true);
    transactionManager.bumpIdempotentEpochAndResetIdIfNeeded();
    assertEquals(idAndEpochAfterFirstBatch, transactionManager.producerIdAndEpoch());
}
Also used : TopicPartition(org.apache.kafka.common.TopicPartition) KafkaException(org.apache.kafka.common.KafkaException) ProducerIdAndEpoch(org.apache.kafka.common.utils.ProducerIdAndEpoch) OutOfOrderSequenceException(org.apache.kafka.common.errors.OutOfOrderSequenceException) TimeoutException(org.apache.kafka.common.errors.TimeoutException) Test(org.junit.jupiter.api.Test)

Aggregations

ProducerIdAndEpoch (org.apache.kafka.common.utils.ProducerIdAndEpoch)25 Test (org.junit.jupiter.api.Test)20 TopicPartition (org.apache.kafka.common.TopicPartition)11 AddPartitionsToTxnResponse (org.apache.kafka.common.requests.AddPartitionsToTxnResponse)8 Metrics (org.apache.kafka.common.metrics.Metrics)5 TimeoutException (org.apache.kafka.common.errors.TimeoutException)4 Node (org.apache.kafka.common.Node)3 ProduceResponse (org.apache.kafka.common.requests.ProduceResponse)3 ArrayList (java.util.ArrayList)2 ApiVersions (org.apache.kafka.clients.ApiVersions)2 MockClient (org.apache.kafka.clients.MockClient)2 NodeApiVersions (org.apache.kafka.clients.NodeApiVersions)2 RecordMetadata (org.apache.kafka.clients.producer.RecordMetadata)2 EndTxnResponseData (org.apache.kafka.common.message.EndTxnResponseData)2 EndTxnResponse (org.apache.kafka.common.requests.EndTxnResponse)2 MetadataResponse (org.apache.kafka.common.requests.MetadataResponse)2 LogContext (org.apache.kafka.common.utils.LogContext)2 ArrayDeque (java.util.ArrayDeque)1 Arrays.asList (java.util.Arrays.asList)1 List (java.util.List)1