Search in sources :

Example 1 with ConsumerGroupMetadata

use of org.apache.kafka.clients.consumer.ConsumerGroupMetadata in project kafka by apache.

the class MockProducer method sendOffsetsToTransaction.

@Deprecated
@Override
public void sendOffsetsToTransaction(Map<TopicPartition, OffsetAndMetadata> offsets, String consumerGroupId) throws ProducerFencedException {
    Objects.requireNonNull(consumerGroupId);
    sendOffsetsToTransaction(offsets, new ConsumerGroupMetadata(consumerGroupId));
}
Also used : ConsumerGroupMetadata(org.apache.kafka.clients.consumer.ConsumerGroupMetadata)

Example 2 with ConsumerGroupMetadata

use of org.apache.kafka.clients.consumer.ConsumerGroupMetadata in project kafka by apache.

the class KafkaProducerTest method testSendTxnOffsetsWithGroupMetadata.

@Test
public void testSendTxnOffsetsWithGroupMetadata() {
    final short maxVersion = (short) 3;
    Map<String, Object> configs = new HashMap<>();
    configs.put(ProducerConfig.TRANSACTIONAL_ID_CONFIG, "some.id");
    configs.put(ProducerConfig.MAX_BLOCK_MS_CONFIG, 10000);
    configs.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9000");
    Time time = new MockTime(1);
    MetadataResponse initialUpdateResponse = RequestTestUtils.metadataUpdateWith(1, singletonMap("topic", 1));
    ProducerMetadata metadata = newMetadata(0, Long.MAX_VALUE);
    MockClient client = new MockClient(time, metadata);
    client.updateMetadata(initialUpdateResponse);
    client.setNodeApiVersions(NodeApiVersions.create(ApiKeys.TXN_OFFSET_COMMIT.id, (short) 0, maxVersion));
    Node node = metadata.fetch().nodes().get(0);
    client.throttle(node, 5000);
    client.prepareResponse(FindCoordinatorResponse.prepareResponse(Errors.NONE, "some.id", NODE));
    client.prepareResponse(initProducerIdResponse(1L, (short) 5, Errors.NONE));
    client.prepareResponse(addOffsetsToTxnResponse(Errors.NONE));
    client.prepareResponse(FindCoordinatorResponse.prepareResponse(Errors.NONE, "some.id", NODE));
    String groupId = "group";
    String memberId = "member";
    int generationId = 5;
    String groupInstanceId = "instance";
    client.prepareResponse(request -> {
        TxnOffsetCommitRequestData data = ((TxnOffsetCommitRequest) request).data();
        return data.groupId().equals(groupId) && data.memberId().equals(memberId) && data.generationId() == generationId && data.groupInstanceId().equals(groupInstanceId);
    }, txnOffsetsCommitResponse(Collections.singletonMap(new TopicPartition("topic", 0), Errors.NONE)));
    client.prepareResponse(endTxnResponse(Errors.NONE));
    try (Producer<String, String> producer = kafkaProducer(configs, new StringSerializer(), new StringSerializer(), metadata, client, null, time)) {
        producer.initTransactions();
        producer.beginTransaction();
        ConsumerGroupMetadata groupMetadata = new ConsumerGroupMetadata(groupId, generationId, memberId, Optional.of(groupInstanceId));
        producer.sendOffsetsToTransaction(Collections.emptyMap(), groupMetadata);
        producer.commitTransaction();
    }
}
Also used : ProducerMetadata(org.apache.kafka.clients.producer.internals.ProducerMetadata) HashMap(java.util.HashMap) Node(org.apache.kafka.common.Node) MockTime(org.apache.kafka.common.utils.MockTime) Time(org.apache.kafka.common.utils.Time) TxnOffsetCommitRequest(org.apache.kafka.common.requests.TxnOffsetCommitRequest) TxnOffsetCommitRequestData(org.apache.kafka.common.message.TxnOffsetCommitRequestData) ConsumerGroupMetadata(org.apache.kafka.clients.consumer.ConsumerGroupMetadata) TopicPartition(org.apache.kafka.common.TopicPartition) 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 3 with ConsumerGroupMetadata

use of org.apache.kafka.clients.consumer.ConsumerGroupMetadata in project kafka by apache.

the class MockProducerTest method shouldDropConsumerGroupOffsetsOnAbortIfTransactionsAreEnabled.

@Test
public void shouldDropConsumerGroupOffsetsOnAbortIfTransactionsAreEnabled() {
    buildMockProducer(true);
    producer.initTransactions();
    producer.beginTransaction();
    String group = "g";
    Map<TopicPartition, OffsetAndMetadata> groupCommit = new HashMap<TopicPartition, OffsetAndMetadata>() {

        {
            put(new TopicPartition(topic, 0), new OffsetAndMetadata(42L, null));
            put(new TopicPartition(topic, 1), new OffsetAndMetadata(73L, null));
        }
    };
    producer.sendOffsetsToTransaction(groupCommit, new ConsumerGroupMetadata(group));
    producer.abortTransaction();
    producer.beginTransaction();
    producer.commitTransaction();
    assertTrue(producer.consumerGroupOffsetsHistory().isEmpty());
    producer.beginTransaction();
    producer.sendOffsetsToTransaction(groupCommit, new ConsumerGroupMetadata(group));
    producer.abortTransaction();
    producer.beginTransaction();
    producer.commitTransaction();
    assertTrue(producer.consumerGroupOffsetsHistory().isEmpty());
}
Also used : ConsumerGroupMetadata(org.apache.kafka.clients.consumer.ConsumerGroupMetadata) HashMap(java.util.HashMap) TopicPartition(org.apache.kafka.common.TopicPartition) OffsetAndMetadata(org.apache.kafka.clients.consumer.OffsetAndMetadata) Test(org.junit.jupiter.api.Test)

Example 4 with ConsumerGroupMetadata

use of org.apache.kafka.clients.consumer.ConsumerGroupMetadata in project kafka by apache.

the class MockProducerTest method shouldPublishConsumerGroupOffsetsOnlyAfterCommitIfTransactionsAreEnabled.

@Test
public void shouldPublishConsumerGroupOffsetsOnlyAfterCommitIfTransactionsAreEnabled() {
    buildMockProducer(true);
    producer.initTransactions();
    producer.beginTransaction();
    String group1 = "g1";
    Map<TopicPartition, OffsetAndMetadata> group1Commit = new HashMap<TopicPartition, OffsetAndMetadata>() {

        {
            put(new TopicPartition(topic, 0), new OffsetAndMetadata(42L, null));
            put(new TopicPartition(topic, 1), new OffsetAndMetadata(73L, null));
        }
    };
    String group2 = "g2";
    Map<TopicPartition, OffsetAndMetadata> group2Commit = new HashMap<TopicPartition, OffsetAndMetadata>() {

        {
            put(new TopicPartition(topic, 0), new OffsetAndMetadata(101L, null));
            put(new TopicPartition(topic, 1), new OffsetAndMetadata(21L, null));
        }
    };
    producer.sendOffsetsToTransaction(group1Commit, new ConsumerGroupMetadata(group1));
    producer.sendOffsetsToTransaction(group2Commit, new ConsumerGroupMetadata(group2));
    assertTrue(producer.consumerGroupOffsetsHistory().isEmpty());
    Map<String, Map<TopicPartition, OffsetAndMetadata>> expectedResult = new HashMap<>();
    expectedResult.put(group1, group1Commit);
    expectedResult.put(group2, group2Commit);
    producer.commitTransaction();
    assertEquals(Collections.singletonList(expectedResult), producer.consumerGroupOffsetsHistory());
}
Also used : ConsumerGroupMetadata(org.apache.kafka.clients.consumer.ConsumerGroupMetadata) HashMap(java.util.HashMap) TopicPartition(org.apache.kafka.common.TopicPartition) OffsetAndMetadata(org.apache.kafka.clients.consumer.OffsetAndMetadata) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 5 with ConsumerGroupMetadata

use of org.apache.kafka.clients.consumer.ConsumerGroupMetadata in project kafka by apache.

the class MockProducerTest method shouldAddOffsetsWhenSendOffsetsToTransactionByGroupMetadata.

@Test
public void shouldAddOffsetsWhenSendOffsetsToTransactionByGroupMetadata() {
    buildMockProducer(true);
    producer.initTransactions();
    producer.beginTransaction();
    assertFalse(producer.sentOffsets());
    Map<TopicPartition, OffsetAndMetadata> groupCommit = new HashMap<TopicPartition, OffsetAndMetadata>() {

        {
            put(new TopicPartition(topic, 0), new OffsetAndMetadata(42L, null));
        }
    };
    producer.sendOffsetsToTransaction(groupCommit, new ConsumerGroupMetadata("groupId"));
    assertTrue(producer.sentOffsets());
}
Also used : ConsumerGroupMetadata(org.apache.kafka.clients.consumer.ConsumerGroupMetadata) HashMap(java.util.HashMap) TopicPartition(org.apache.kafka.common.TopicPartition) OffsetAndMetadata(org.apache.kafka.clients.consumer.OffsetAndMetadata) Test(org.junit.jupiter.api.Test)

Aggregations

ConsumerGroupMetadata (org.apache.kafka.clients.consumer.ConsumerGroupMetadata)64 TopicPartition (org.apache.kafka.common.TopicPartition)34 OffsetAndMetadata (org.apache.kafka.clients.consumer.OffsetAndMetadata)29 Test (org.junit.Test)28 HashMap (java.util.HashMap)25 Test (org.junit.jupiter.api.Test)24 StreamsMetricsImpl (org.apache.kafka.streams.processor.internals.metrics.StreamsMetricsImpl)14 TaskId (org.apache.kafka.streams.processor.TaskId)12 AtomicLong (java.util.concurrent.atomic.AtomicLong)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 LogContext (org.apache.kafka.common.utils.LogContext)8 Collection (java.util.Collection)7 LinkedList (java.util.LinkedList)7 Map (java.util.Map)7 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 KafkaException (org.apache.kafka.common.KafkaException)7 TimeoutException (org.apache.kafka.common.errors.TimeoutException)7 TaskCorruptedException (org.apache.kafka.streams.errors.TaskCorruptedException)7 Collections.emptySet (java.util.Collections.emptySet)6 HashSet (java.util.HashSet)6