Search in sources :

Example 1 with EndTransactionMarker

use of org.apache.kafka.common.record.EndTransactionMarker in project apache-kafka-on-k8s by banzaicloud.

the class FetcherTest method commitTransaction.

private int commitTransaction(ByteBuffer buffer, long producerId, long baseOffset) {
    short producerEpoch = 0;
    int partitionLeaderEpoch = 0;
    MemoryRecords.writeEndTransactionalMarker(buffer, baseOffset, time.milliseconds(), partitionLeaderEpoch, producerId, producerEpoch, new EndTransactionMarker(ControlRecordType.COMMIT, 0));
    return 1;
}
Also used : EndTransactionMarker(org.apache.kafka.common.record.EndTransactionMarker)

Example 2 with EndTransactionMarker

use of org.apache.kafka.common.record.EndTransactionMarker in project apache-kafka-on-k8s by banzaicloud.

the class FetcherTest method abortTransaction.

private int abortTransaction(ByteBuffer buffer, long producerId, long baseOffset) {
    short producerEpoch = 0;
    int partitionLeaderEpoch = 0;
    MemoryRecords.writeEndTransactionalMarker(buffer, baseOffset, time.milliseconds(), partitionLeaderEpoch, producerId, producerEpoch, new EndTransactionMarker(ControlRecordType.ABORT, 0));
    return 1;
}
Also used : EndTransactionMarker(org.apache.kafka.common.record.EndTransactionMarker)

Example 3 with EndTransactionMarker

use of org.apache.kafka.common.record.EndTransactionMarker in project kafka by apache.

the class FetcherTest method abortTransaction.

private int abortTransaction(ByteBuffer buffer, long producerId, long baseOffset) {
    short producerEpoch = 0;
    int partitionLeaderEpoch = 0;
    MemoryRecords.writeEndTransactionalMarker(buffer, baseOffset, time.milliseconds(), partitionLeaderEpoch, producerId, producerEpoch, new EndTransactionMarker(ControlRecordType.ABORT, 0));
    return 1;
}
Also used : EndTransactionMarker(org.apache.kafka.common.record.EndTransactionMarker)

Example 4 with EndTransactionMarker

use of org.apache.kafka.common.record.EndTransactionMarker in project kafka by apache.

the class FetcherTest method testFetcherIgnoresControlRecords.

@Test
public void testFetcherIgnoresControlRecords() {
    buildFetcher();
    assignFromUser(singleton(tp0));
    subscriptions.seek(tp0, 0);
    // normal fetch
    assertEquals(1, fetcher.sendFetches());
    assertFalse(fetcher.hasCompletedFetches());
    long producerId = 1;
    short producerEpoch = 0;
    int baseSequence = 0;
    int partitionLeaderEpoch = 0;
    ByteBuffer buffer = ByteBuffer.allocate(1024);
    MemoryRecordsBuilder builder = MemoryRecords.idempotentBuilder(buffer, CompressionType.NONE, 0L, producerId, producerEpoch, baseSequence);
    builder.append(0L, "key".getBytes(), null);
    builder.close();
    MemoryRecords.writeEndTransactionalMarker(buffer, 1L, time.milliseconds(), partitionLeaderEpoch, producerId, producerEpoch, new EndTransactionMarker(ControlRecordType.ABORT, 0));
    buffer.flip();
    client.prepareResponse(fullFetchResponse(tidp0, MemoryRecords.readableRecords(buffer), Errors.NONE, 100L, 0));
    consumerClient.poll(time.timer(0));
    assertTrue(fetcher.hasCompletedFetches());
    Map<TopicPartition, List<ConsumerRecord<byte[], byte[]>>> partitionRecords = fetchedRecords();
    assertTrue(partitionRecords.containsKey(tp0));
    List<ConsumerRecord<byte[], byte[]>> records = partitionRecords.get(tp0);
    assertEquals(1, records.size());
    assertEquals(2L, subscriptions.position(tp0).offset);
    ConsumerRecord<byte[], byte[]> record = records.get(0);
    assertArrayEquals("key".getBytes(), record.key());
}
Also used : EndTransactionMarker(org.apache.kafka.common.record.EndTransactionMarker) TopicPartition(org.apache.kafka.common.TopicPartition) MemoryRecordsBuilder(org.apache.kafka.common.record.MemoryRecordsBuilder) Collections.singletonList(java.util.Collections.singletonList) Arrays.asList(java.util.Arrays.asList) ArrayList(java.util.ArrayList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) ByteBuffer(java.nio.ByteBuffer) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) Test(org.junit.jupiter.api.Test)

Example 5 with EndTransactionMarker

use of org.apache.kafka.common.record.EndTransactionMarker in project apache-kafka-on-k8s by banzaicloud.

the class FetcherTest method testFetcherIgnoresControlRecords.

@Test
public void testFetcherIgnoresControlRecords() {
    subscriptions.assignFromUser(singleton(tp0));
    subscriptions.seek(tp0, 0);
    // normal fetch
    assertEquals(1, fetcher.sendFetches());
    assertFalse(fetcher.hasCompletedFetches());
    long producerId = 1;
    short producerEpoch = 0;
    int baseSequence = 0;
    int partitionLeaderEpoch = 0;
    ByteBuffer buffer = ByteBuffer.allocate(1024);
    MemoryRecordsBuilder builder = MemoryRecords.idempotentBuilder(buffer, CompressionType.NONE, 0L, producerId, producerEpoch, baseSequence);
    builder.append(0L, "key".getBytes(), null);
    builder.close();
    MemoryRecords.writeEndTransactionalMarker(buffer, 1L, time.milliseconds(), partitionLeaderEpoch, producerId, producerEpoch, new EndTransactionMarker(ControlRecordType.ABORT, 0));
    buffer.flip();
    client.prepareResponse(fullFetchResponse(tp0, MemoryRecords.readableRecords(buffer), Errors.NONE, 100L, 0));
    consumerClient.poll(0);
    assertTrue(fetcher.hasCompletedFetches());
    Map<TopicPartition, List<ConsumerRecord<byte[], byte[]>>> partitionRecords = fetcher.fetchedRecords();
    assertTrue(partitionRecords.containsKey(tp0));
    List<ConsumerRecord<byte[], byte[]>> records = partitionRecords.get(tp0);
    assertEquals(1, records.size());
    assertEquals(2L, subscriptions.position(tp0).longValue());
    ConsumerRecord<byte[], byte[]> record = records.get(0);
    assertArrayEquals("key".getBytes(), record.key());
}
Also used : EndTransactionMarker(org.apache.kafka.common.record.EndTransactionMarker) TopicPartition(org.apache.kafka.common.TopicPartition) MemoryRecordsBuilder(org.apache.kafka.common.record.MemoryRecordsBuilder) List(java.util.List) ArrayList(java.util.ArrayList) ByteBuffer(java.nio.ByteBuffer) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) Test(org.junit.Test)

Aggregations

EndTransactionMarker (org.apache.kafka.common.record.EndTransactionMarker)6 ByteBuffer (java.nio.ByteBuffer)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 ConsumerRecord (org.apache.kafka.clients.consumer.ConsumerRecord)2 TopicPartition (org.apache.kafka.common.TopicPartition)2 MemoryRecordsBuilder (org.apache.kafka.common.record.MemoryRecordsBuilder)2 Arrays.asList (java.util.Arrays.asList)1 Collections.emptyList (java.util.Collections.emptyList)1 Collections.singletonList (java.util.Collections.singletonList)1 Test (org.junit.Test)1 Test (org.junit.jupiter.api.Test)1