Search in sources :

Example 1 with ListOffsetRequest

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

the class KafkaConsumerTest method testFetchProgressWithMissingPartitionPosition.

@Test
public void testFetchProgressWithMissingPartitionPosition() {
    // Verifies that we can make progress on one partition while we are awaiting
    // a reset on another partition.
    Time time = new MockTime();
    Cluster cluster = TestUtils.singletonCluster(topic, 2);
    Node node = cluster.nodes().get(0);
    Metadata metadata = createMetadata();
    metadata.update(cluster, Collections.<String>emptySet(), time.milliseconds());
    MockClient client = new MockClient(time, metadata);
    client.setNode(node);
    KafkaConsumer<String, String> consumer = newConsumerNoAutoCommit(time, client, metadata);
    consumer.assign(Arrays.asList(tp0, tp1));
    consumer.seekToEnd(singleton(tp0));
    consumer.seekToBeginning(singleton(tp1));
    client.prepareResponse(new MockClient.RequestMatcher() {

        @Override
        public boolean matches(AbstractRequest body) {
            ListOffsetRequest request = (ListOffsetRequest) body;
            Map<TopicPartition, Long> expectedTimestamps = new HashMap<>();
            expectedTimestamps.put(tp0, ListOffsetRequest.LATEST_TIMESTAMP);
            expectedTimestamps.put(tp1, ListOffsetRequest.EARLIEST_TIMESTAMP);
            return expectedTimestamps.equals(request.partitionTimestamps());
        }
    }, listOffsetsResponse(Collections.singletonMap(tp0, 50L), Collections.singletonMap(tp1, Errors.NOT_LEADER_FOR_PARTITION)));
    client.prepareResponse(new MockClient.RequestMatcher() {

        @Override
        public boolean matches(AbstractRequest body) {
            FetchRequest request = (FetchRequest) body;
            return request.fetchData().keySet().equals(singleton(tp0)) && request.fetchData().get(tp0).fetchOffset == 50L;
        }
    }, fetchResponse(tp0, 50L, 5));
    ConsumerRecords<String, String> records = consumer.poll(5);
    assertEquals(5, records.count());
    assertEquals(singleton(tp0), records.partitions());
}
Also used : Node(org.apache.kafka.common.Node) AbstractRequest(org.apache.kafka.common.requests.AbstractRequest) Metadata(org.apache.kafka.clients.Metadata) Cluster(org.apache.kafka.common.Cluster) MockTime(org.apache.kafka.common.utils.MockTime) Time(org.apache.kafka.common.utils.Time) ListOffsetRequest(org.apache.kafka.common.requests.ListOffsetRequest) FetchRequest(org.apache.kafka.common.requests.FetchRequest) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Collections.singletonMap(java.util.Collections.singletonMap) MockTime(org.apache.kafka.common.utils.MockTime) MockClient(org.apache.kafka.clients.MockClient) Test(org.junit.Test)

Example 2 with ListOffsetRequest

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

the class FetcherTest method testListOffsetsSendsIsolationLevel.

@Test
public void testListOffsetsSendsIsolationLevel() {
    for (final IsolationLevel isolationLevel : IsolationLevel.values()) {
        Fetcher<byte[], byte[]> fetcher = createFetcher(subscriptions, new Metrics(), new ByteArrayDeserializer(), new ByteArrayDeserializer(), Integer.MAX_VALUE, isolationLevel);
        subscriptions.assignFromUser(singleton(tp0));
        subscriptions.requestOffsetReset(tp0, OffsetResetStrategy.LATEST);
        client.prepareResponse(new MockClient.RequestMatcher() {

            @Override
            public boolean matches(AbstractRequest body) {
                ListOffsetRequest request = (ListOffsetRequest) body;
                return request.isolationLevel() == isolationLevel;
            }
        }, listOffsetResponse(Errors.NONE, 1L, 5L));
        fetcher.resetOffsetsIfNeeded();
        consumerClient.pollNoWakeup();
        assertFalse(subscriptions.isOffsetResetNeeded(tp0));
        assertTrue(subscriptions.isFetchable(tp0));
        assertEquals(5, subscriptions.position(tp0).longValue());
    }
}
Also used : Metrics(org.apache.kafka.common.metrics.Metrics) IsolationLevel(org.apache.kafka.common.requests.IsolationLevel) AbstractRequest(org.apache.kafka.common.requests.AbstractRequest) ListOffsetRequest(org.apache.kafka.common.requests.ListOffsetRequest) ByteArrayDeserializer(org.apache.kafka.common.serialization.ByteArrayDeserializer) MockClient(org.apache.kafka.clients.MockClient) Test(org.junit.Test)

Aggregations

MockClient (org.apache.kafka.clients.MockClient)2 AbstractRequest (org.apache.kafka.common.requests.AbstractRequest)2 ListOffsetRequest (org.apache.kafka.common.requests.ListOffsetRequest)2 Test (org.junit.Test)2 Collections.singletonMap (java.util.Collections.singletonMap)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Metadata (org.apache.kafka.clients.Metadata)1 Cluster (org.apache.kafka.common.Cluster)1 Node (org.apache.kafka.common.Node)1 Metrics (org.apache.kafka.common.metrics.Metrics)1 FetchRequest (org.apache.kafka.common.requests.FetchRequest)1 IsolationLevel (org.apache.kafka.common.requests.IsolationLevel)1 ByteArrayDeserializer (org.apache.kafka.common.serialization.ByteArrayDeserializer)1 MockTime (org.apache.kafka.common.utils.MockTime)1 Time (org.apache.kafka.common.utils.Time)1