Search in sources :

Example 11 with OffsetFetchResponse

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

the class ConsumerCoordinatorTest method testFetchCommittedOffsets.

@Test
public void testFetchCommittedOffsets() {
    client.prepareResponse(groupCoordinatorResponse(node, Errors.NONE));
    coordinator.ensureCoordinatorReady(time.timer(Long.MAX_VALUE));
    long offset = 500L;
    String metadata = "blahblah";
    Optional<Integer> leaderEpoch = Optional.of(15);
    OffsetFetchResponse.PartitionData data = new OffsetFetchResponse.PartitionData(offset, leaderEpoch, metadata, Errors.NONE);
    client.prepareResponse(new OffsetFetchResponse(Errors.NONE, singletonMap(t1p, data)));
    Map<TopicPartition, OffsetAndMetadata> fetchedOffsets = coordinator.fetchCommittedOffsets(singleton(t1p), time.timer(Long.MAX_VALUE));
    assertNotNull(fetchedOffsets);
    assertEquals(new OffsetAndMetadata(offset, leaderEpoch, metadata), fetchedOffsets.get(t1p));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) OffsetFetchResponse(org.apache.kafka.common.requests.OffsetFetchResponse) TopicPartition(org.apache.kafka.common.TopicPartition) OffsetAndMetadata(org.apache.kafka.clients.consumer.OffsetAndMetadata) Test(org.junit.jupiter.api.Test)

Example 12 with OffsetFetchResponse

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

the class KafkaAdminClientTest method testListConsumerGroupOffsets.

@Test
public void testListConsumerGroupOffsets() throws Exception {
    try (AdminClientUnitTestEnv env = new AdminClientUnitTestEnv(mockCluster(1, 0))) {
        env.kafkaClient().setNodeApiVersions(NodeApiVersions.create());
        // Retriable FindCoordinatorResponse errors should be retried
        env.kafkaClient().prepareResponse(prepareFindCoordinatorResponse(Errors.COORDINATOR_NOT_AVAILABLE, Node.noNode()));
        env.kafkaClient().prepareResponse(prepareFindCoordinatorResponse(Errors.NONE, env.cluster().controller()));
        // Retriable errors should be retried
        env.kafkaClient().prepareResponse(new OffsetFetchResponse(Errors.COORDINATOR_LOAD_IN_PROGRESS, Collections.emptyMap()));
        /*
             * We need to return two responses here, one for NOT_COORDINATOR error when calling list consumer group offsets
             * api using coordinator that has moved. This will retry whole operation. So we need to again respond with a
             * FindCoordinatorResponse.
             *
             * And the same reason for the following COORDINATOR_NOT_AVAILABLE error response
             */
        env.kafkaClient().prepareResponse(new OffsetFetchResponse(Errors.NOT_COORDINATOR, Collections.emptyMap()));
        env.kafkaClient().prepareResponse(prepareFindCoordinatorResponse(Errors.NONE, env.cluster().controller()));
        env.kafkaClient().prepareResponse(new OffsetFetchResponse(Errors.COORDINATOR_NOT_AVAILABLE, Collections.emptyMap()));
        env.kafkaClient().prepareResponse(prepareFindCoordinatorResponse(Errors.NONE, env.cluster().controller()));
        TopicPartition myTopicPartition0 = new TopicPartition("my_topic", 0);
        TopicPartition myTopicPartition1 = new TopicPartition("my_topic", 1);
        TopicPartition myTopicPartition2 = new TopicPartition("my_topic", 2);
        TopicPartition myTopicPartition3 = new TopicPartition("my_topic", 3);
        final Map<TopicPartition, OffsetFetchResponse.PartitionData> responseData = new HashMap<>();
        responseData.put(myTopicPartition0, new OffsetFetchResponse.PartitionData(10, Optional.empty(), "", Errors.NONE));
        responseData.put(myTopicPartition1, new OffsetFetchResponse.PartitionData(0, Optional.empty(), "", Errors.NONE));
        responseData.put(myTopicPartition2, new OffsetFetchResponse.PartitionData(20, Optional.empty(), "", Errors.NONE));
        responseData.put(myTopicPartition3, new OffsetFetchResponse.PartitionData(OffsetFetchResponse.INVALID_OFFSET, Optional.empty(), "", Errors.NONE));
        env.kafkaClient().prepareResponse(new OffsetFetchResponse(Errors.NONE, responseData));
        final ListConsumerGroupOffsetsResult result = env.adminClient().listConsumerGroupOffsets(GROUP_ID);
        final Map<TopicPartition, OffsetAndMetadata> partitionToOffsetAndMetadata = result.partitionsToOffsetAndMetadata().get();
        assertEquals(4, partitionToOffsetAndMetadata.size());
        assertEquals(10, partitionToOffsetAndMetadata.get(myTopicPartition0).offset());
        assertEquals(0, partitionToOffsetAndMetadata.get(myTopicPartition1).offset());
        assertEquals(20, partitionToOffsetAndMetadata.get(myTopicPartition2).offset());
        assertTrue(partitionToOffsetAndMetadata.containsKey(myTopicPartition3));
        assertNull(partitionToOffsetAndMetadata.get(myTopicPartition3));
    }
}
Also used : OffsetFetchResponse(org.apache.kafka.common.requests.OffsetFetchResponse) HashMap(java.util.HashMap) TopicPartition(org.apache.kafka.common.TopicPartition) OffsetAndMetadata(org.apache.kafka.clients.consumer.OffsetAndMetadata) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 13 with OffsetFetchResponse

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

the class KafkaAdminClientTest method testListConsumerGroupOffsetsNonRetriableErrors.

@Test
public void testListConsumerGroupOffsetsNonRetriableErrors() throws Exception {
    // Non-retriable errors throw an exception
    final List<Errors> nonRetriableErrors = Arrays.asList(Errors.GROUP_AUTHORIZATION_FAILED, Errors.INVALID_GROUP_ID, Errors.GROUP_ID_NOT_FOUND);
    try (AdminClientUnitTestEnv env = new AdminClientUnitTestEnv(mockCluster(1, 0))) {
        env.kafkaClient().setNodeApiVersions(NodeApiVersions.create());
        for (Errors error : nonRetriableErrors) {
            env.kafkaClient().prepareResponse(prepareFindCoordinatorResponse(Errors.NONE, env.cluster().controller()));
            env.kafkaClient().prepareResponse(new OffsetFetchResponse(error, Collections.emptyMap()));
            ListConsumerGroupOffsetsResult errorResult = env.adminClient().listConsumerGroupOffsets(GROUP_ID);
            TestUtils.assertFutureError(errorResult.partitionsToOffsetAndMetadata(), error.exception().getClass());
        }
    }
}
Also used : Errors(org.apache.kafka.common.protocol.Errors) OffsetFetchResponse(org.apache.kafka.common.requests.OffsetFetchResponse) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Aggregations

OffsetFetchResponse (org.apache.kafka.common.requests.OffsetFetchResponse)13 Test (org.junit.jupiter.api.Test)7 TopicPartition (org.apache.kafka.common.TopicPartition)6 HashMap (java.util.HashMap)5 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)5 OffsetAndMetadata (org.apache.kafka.clients.consumer.OffsetAndMetadata)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Node (org.apache.kafka.common.Node)2 Errors (org.apache.kafka.common.protocol.Errors)2 PartitionData (org.apache.kafka.common.requests.OffsetFetchResponse.PartitionData)2 MockTime (org.apache.kafka.common.utils.MockTime)2 HashSet (java.util.HashSet)1 Map (java.util.Map)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 MockClient (org.apache.kafka.clients.MockClient)1 Cluster (org.apache.kafka.common.Cluster)1 TopicAuthorizationException (org.apache.kafka.common.errors.TopicAuthorizationException)1 Metrics (org.apache.kafka.common.metrics.Metrics)1 LogContext (org.apache.kafka.common.utils.LogContext)1 Time (org.apache.kafka.common.utils.Time)1