Search in sources :

Example 6 with ProduceRequest

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

the class TransactionManagerTest method produceRequestMatcher.

private MockClient.RequestMatcher produceRequestMatcher(final long pid, final short epoch) {
    return new MockClient.RequestMatcher() {

        @Override
        public boolean matches(AbstractRequest body) {
            ProduceRequest produceRequest = (ProduceRequest) body;
            MemoryRecords records = produceRequest.partitionRecordsOrFail().get(tp0);
            assertNotNull(records);
            Iterator<MutableRecordBatch> batchIterator = records.batches().iterator();
            assertTrue(batchIterator.hasNext());
            MutableRecordBatch batch = batchIterator.next();
            assertFalse(batchIterator.hasNext());
            assertTrue(batch.isTransactional());
            assertEquals(pid, batch.producerId());
            assertEquals(epoch, batch.producerEpoch());
            assertEquals(transactionalId, produceRequest.transactionalId());
            return true;
        }
    };
}
Also used : MutableRecordBatch(org.apache.kafka.common.record.MutableRecordBatch) ProduceRequest(org.apache.kafka.common.requests.ProduceRequest) AbstractRequest(org.apache.kafka.common.requests.AbstractRequest) MemoryRecords(org.apache.kafka.common.record.MemoryRecords)

Example 7 with ProduceRequest

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

the class SenderTest method testSequenceNumberIncrement.

@Test
public void testSequenceNumberIncrement() throws InterruptedException {
    final long producerId = 343434L;
    TransactionManager transactionManager = createTransactionManager();
    setupWithTransactionState(transactionManager);
    prepareAndReceiveInitProducerId(producerId, Errors.NONE);
    assertTrue(transactionManager.hasProducerId());
    int maxRetries = 10;
    Metrics m = new Metrics();
    SenderMetricsRegistry senderMetrics = new SenderMetricsRegistry(m);
    Sender sender = new Sender(logContext, client, metadata, this.accumulator, true, MAX_REQUEST_SIZE, ACKS_ALL, maxRetries, senderMetrics, time, REQUEST_TIMEOUT, RETRY_BACKOFF_MS, transactionManager, apiVersions);
    Future<RecordMetadata> responseFuture = appendToAccumulator(tp0);
    client.prepareResponse(body -> {
        if (body instanceof ProduceRequest) {
            ProduceRequest request = (ProduceRequest) body;
            MemoryRecords records = partitionRecords(request).get(tp0);
            Iterator<MutableRecordBatch> batchIterator = records.batches().iterator();
            assertTrue(batchIterator.hasNext());
            RecordBatch batch = batchIterator.next();
            assertFalse(batchIterator.hasNext());
            assertEquals(0, batch.baseSequence());
            assertEquals(producerId, batch.producerId());
            assertEquals(0, batch.producerEpoch());
            return true;
        }
        return false;
    }, produceResponse(tp0, 0, Errors.NONE, 0));
    // connect.
    sender.runOnce();
    // send.
    sender.runOnce();
    // receive response
    sender.runOnce();
    assertTrue(responseFuture.isDone());
    assertEquals(OptionalInt.of(0), transactionManager.lastAckedSequence(tp0));
    assertEquals(1L, (long) transactionManager.sequenceNumber(tp0));
}
Also used : ProduceRequest(org.apache.kafka.common.requests.ProduceRequest) RecordBatch(org.apache.kafka.common.record.RecordBatch) MutableRecordBatch(org.apache.kafka.common.record.MutableRecordBatch) RecordMetadata(org.apache.kafka.clients.producer.RecordMetadata) MutableRecordBatch(org.apache.kafka.common.record.MutableRecordBatch) Metrics(org.apache.kafka.common.metrics.Metrics) MemoryRecords(org.apache.kafka.common.record.MemoryRecords) Test(org.junit.jupiter.api.Test)

Example 8 with ProduceRequest

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

the class SenderTest method testCanRetryWithoutIdempotence.

@Test
public void testCanRetryWithoutIdempotence() throws Exception {
    // do a successful retry
    Future<RecordMetadata> future = appendToAccumulator(tp0, 0L, "key", "value");
    // connect
    sender.runOnce();
    // send produce request
    sender.runOnce();
    String id = client.requests().peek().destination();
    Node node = new Node(Integer.parseInt(id), "localhost", 0);
    assertEquals(1, client.inFlightRequestCount());
    assertTrue(client.hasInFlightRequests());
    assertEquals(1, sender.inFlightBatches(tp0).size());
    assertTrue(client.isReady(node, time.milliseconds()), "Client ready status should be true");
    assertFalse(future.isDone());
    client.respond(body -> {
        ProduceRequest request = (ProduceRequest) body;
        assertFalse(RequestTestUtils.hasIdempotentRecords(request));
        return true;
    }, produceResponse(tp0, -1L, Errors.TOPIC_AUTHORIZATION_FAILED, 0));
    sender.runOnce();
    assertTrue(future.isDone());
    try {
        future.get();
    } catch (Exception e) {
        assertTrue(e.getCause() instanceof TopicAuthorizationException);
    }
}
Also used : RecordMetadata(org.apache.kafka.clients.producer.RecordMetadata) ProduceRequest(org.apache.kafka.common.requests.ProduceRequest) Node(org.apache.kafka.common.Node) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) TransactionAbortedException(org.apache.kafka.common.errors.TransactionAbortedException) KafkaException(org.apache.kafka.common.KafkaException) UnsupportedForMessageFormatException(org.apache.kafka.common.errors.UnsupportedForMessageFormatException) ExecutionException(java.util.concurrent.ExecutionException) UnsupportedVersionException(org.apache.kafka.common.errors.UnsupportedVersionException) ClusterAuthorizationException(org.apache.kafka.common.errors.ClusterAuthorizationException) RecordTooLargeException(org.apache.kafka.common.errors.RecordTooLargeException) NetworkException(org.apache.kafka.common.errors.NetworkException) InvalidRecordException(org.apache.kafka.common.InvalidRecordException) TimeoutException(org.apache.kafka.common.errors.TimeoutException) TopicAuthorizationException(org.apache.kafka.common.errors.TopicAuthorizationException) InvalidRequestException(org.apache.kafka.common.errors.InvalidRequestException) TopicAuthorizationException(org.apache.kafka.common.errors.TopicAuthorizationException) Test(org.junit.jupiter.api.Test)

Example 9 with ProduceRequest

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

the class SenderTest method testMessageFormatDownConversion.

@Test
public void testMessageFormatDownConversion() throws Exception {
    // this test case verifies the behavior when the version of the produce request supported by the
    // broker changes after the record set is created
    long offset = 0;
    // start off support produce request v3
    apiVersions.update("0", NodeApiVersions.create());
    Future<RecordMetadata> future = appendToAccumulator(tp0, 0L, "key", "value");
    // now the partition leader supports only v2
    apiVersions.update("0", NodeApiVersions.create(ApiKeys.PRODUCE.id, (short) 0, (short) 2));
    client.prepareResponse(body -> {
        ProduceRequest request = (ProduceRequest) body;
        if (request.version() != 2)
            return false;
        MemoryRecords records = partitionRecords(request).get(tp0);
        return records != null && records.sizeInBytes() > 0 && records.hasMatchingMagic(RecordBatch.MAGIC_VALUE_V1);
    }, produceResponse(tp0, offset, Errors.NONE, 0));
    // connect
    sender.runOnce();
    // send produce request
    sender.runOnce();
    assertTrue(future.isDone(), "Request should be completed");
    assertEquals(offset, future.get().offset());
}
Also used : RecordMetadata(org.apache.kafka.clients.producer.RecordMetadata) ProduceRequest(org.apache.kafka.common.requests.ProduceRequest) MemoryRecords(org.apache.kafka.common.record.MemoryRecords) Test(org.junit.jupiter.api.Test)

Example 10 with ProduceRequest

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

the class SenderTest method testCanRetryWithoutIdempotence.

@Test
public void testCanRetryWithoutIdempotence() throws Exception {
    // do a successful retry
    Future<RecordMetadata> future = accumulator.append(tp0, 0L, "key".getBytes(), "value".getBytes(), null, null, MAX_BLOCK_TIMEOUT).future;
    // connect
    sender.run(time.milliseconds());
    // send produce request
    sender.run(time.milliseconds());
    String id = client.requests().peek().destination();
    Node node = new Node(Integer.parseInt(id), "localhost", 0);
    assertEquals(1, client.inFlightRequestCount());
    assertTrue(client.hasInFlightRequests());
    assertTrue("Client ready status should be true", client.isReady(node, 0L));
    assertFalse(future.isDone());
    client.respond(new MockClient.RequestMatcher() {

        @Override
        public boolean matches(AbstractRequest body) {
            ProduceRequest request = (ProduceRequest) body;
            assertFalse(request.isIdempotent());
            return true;
        }
    }, produceResponse(tp0, -1L, Errors.TOPIC_AUTHORIZATION_FAILED, 0));
    sender.run(time.milliseconds());
    assertTrue(future.isDone());
    try {
        future.get();
    } catch (Exception e) {
        assertTrue(e.getCause() instanceof TopicAuthorizationException);
    }
}
Also used : RecordMetadata(org.apache.kafka.clients.producer.RecordMetadata) ProduceRequest(org.apache.kafka.common.requests.ProduceRequest) Node(org.apache.kafka.common.Node) AbstractRequest(org.apache.kafka.common.requests.AbstractRequest) ClusterAuthorizationException(org.apache.kafka.common.errors.ClusterAuthorizationException) OutOfOrderSequenceException(org.apache.kafka.common.errors.OutOfOrderSequenceException) UnsupportedForMessageFormatException(org.apache.kafka.common.errors.UnsupportedForMessageFormatException) RecordTooLargeException(org.apache.kafka.common.errors.RecordTooLargeException) NetworkException(org.apache.kafka.common.errors.NetworkException) TimeoutException(org.apache.kafka.common.errors.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException) TopicAuthorizationException(org.apache.kafka.common.errors.TopicAuthorizationException) UnsupportedVersionException(org.apache.kafka.common.errors.UnsupportedVersionException) TopicAuthorizationException(org.apache.kafka.common.errors.TopicAuthorizationException) MockClient(org.apache.kafka.clients.MockClient) Test(org.junit.Test)

Aggregations

ProduceRequest (org.apache.kafka.common.requests.ProduceRequest)15 RecordMetadata (org.apache.kafka.clients.producer.RecordMetadata)12 AbstractRequest (org.apache.kafka.common.requests.AbstractRequest)10 MemoryRecords (org.apache.kafka.common.record.MemoryRecords)9 MockClient (org.apache.kafka.clients.MockClient)8 Node (org.apache.kafka.common.Node)8 Test (org.junit.Test)7 MutableRecordBatch (org.apache.kafka.common.record.MutableRecordBatch)6 Test (org.junit.jupiter.api.Test)5 TopicPartition (org.apache.kafka.common.TopicPartition)4 RecordBatch (org.apache.kafka.common.record.RecordBatch)4 HashMap (java.util.HashMap)3 LinkedHashMap (java.util.LinkedHashMap)3 ExecutionException (java.util.concurrent.ExecutionException)3 ClusterAuthorizationException (org.apache.kafka.common.errors.ClusterAuthorizationException)3 NetworkException (org.apache.kafka.common.errors.NetworkException)3 RecordTooLargeException (org.apache.kafka.common.errors.RecordTooLargeException)3 TimeoutException (org.apache.kafka.common.errors.TimeoutException)3 TopicAuthorizationException (org.apache.kafka.common.errors.TopicAuthorizationException)3 UnsupportedForMessageFormatException (org.apache.kafka.common.errors.UnsupportedForMessageFormatException)3