lianetm commented on code in PR #21037: URL: https://github.com/apache/kafka/pull/21037#discussion_r2714404439
########## clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/PlaintextConsumerCloseTest.java: ########## @@ -0,0 +1,135 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.kafka.clients.consumer; + +import org.apache.kafka.common.TopicPartition; +import org.apache.kafka.common.serialization.ByteArrayDeserializer; +import org.apache.kafka.common.test.ClusterInstance; +import org.apache.kafka.common.test.api.ClusterConfigProperty; +import org.apache.kafka.common.test.api.ClusterTest; +import org.apache.kafka.common.test.api.ClusterTestDefaults; +import org.apache.kafka.common.test.api.Type; + +import java.time.Duration; +import java.util.Locale; +import java.util.Map; +import java.util.Set; + +import static org.apache.kafka.clients.ClientsTestUtils.BaseConsumerTestcase.BROKER_COUNT; +import static org.apache.kafka.clients.ClientsTestUtils.consumeRecords; +import static org.apache.kafka.clients.ClientsTestUtils.sendRecords; +import static org.apache.kafka.clients.consumer.ConsumerConfig.AUTO_OFFSET_RESET_CONFIG; +import static org.apache.kafka.clients.consumer.ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG; +import static org.apache.kafka.clients.consumer.ConsumerConfig.DEFAULT_FETCH_MAX_WAIT_MS; +import static org.apache.kafka.clients.consumer.ConsumerConfig.GROUP_ID_CONFIG; +import static org.apache.kafka.clients.consumer.ConsumerConfig.GROUP_PROTOCOL_CONFIG; +import static org.apache.kafka.clients.consumer.ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG; +import static org.apache.kafka.clients.consumer.ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG; +import static org.apache.kafka.coordinator.group.GroupCoordinatorConfig.GROUP_INITIAL_REBALANCE_DELAY_MS_CONFIG; +import static org.apache.kafka.coordinator.group.GroupCoordinatorConfig.GROUP_MAX_SESSION_TIMEOUT_MS_CONFIG; +import static org.apache.kafka.coordinator.group.GroupCoordinatorConfig.GROUP_MIN_SESSION_TIMEOUT_MS_CONFIG; +import static org.apache.kafka.coordinator.group.GroupCoordinatorConfig.OFFSETS_TOPIC_PARTITIONS_CONFIG; +import static org.junit.jupiter.api.Assertions.assertTrue; + +@ClusterTestDefaults( + types = {Type.KRAFT}, + brokers = BROKER_COUNT, + serverProperties = { + @ClusterConfigProperty(key = OFFSETS_TOPIC_PARTITIONS_CONFIG, value = "1"), + @ClusterConfigProperty(key = GROUP_MIN_SESSION_TIMEOUT_MS_CONFIG, value = "100"), + @ClusterConfigProperty(key = GROUP_MAX_SESSION_TIMEOUT_MS_CONFIG, value = "60000"), + @ClusterConfigProperty(key = GROUP_INITIAL_REBALANCE_DELAY_MS_CONFIG, value = "10"), + } +) +public class PlaintextConsumerCloseTest { + + private final ClusterInstance cluster; + + public PlaintextConsumerCloseTest(ClusterInstance cluster) { + this.cluster = cluster; + } + + @ClusterTest + public void testClassicConsumerCloseWithDefaultTakesAtLeastFetchMaxWaitMs() throws Exception { + testCloseWithDefaultTakesAtLeastFetchMaxWaitMs(GroupProtocol.CLASSIC); + } + + @ClusterTest + public void testAsyncConsumerCloseWithDefaultTakesAtLeastFetchMaxWaitMs() throws Exception { + testCloseWithDefaultTakesAtLeastFetchMaxWaitMs(GroupProtocol.CONSUMER); + } + + private void testCloseWithDefaultTakesAtLeastFetchMaxWaitMs(GroupProtocol groupProtocol) throws Exception { + long closeMs = calculateConsumerCloseDelay(groupProtocol, Consumer::close); + assertTrue( + closeMs >= DEFAULT_FETCH_MAX_WAIT_MS, + "Closing a consumer with the default close() should take longer than " + DEFAULT_FETCH_MAX_WAIT_MS + " ms, but actually took " + closeMs + " ms" Review Comment: nothing wrong with the test, but I'm a bit surprised by this behaviour actually (is it right?) I get that we end up blocking on `fetch.max.wait` because we wait for the response to a fetch request we sent to close sessions (and that will wait for data on the broker side), correct? But then, the consumer does not need data from that response really (it doesn't do anything with the response, just logging success/failure) https://github.com/apache/kafka/blob/718202dbf4b5dde65365051566a2d209da83f248/clients/src/main/java/org/apache/kafka/clients/consumer/internals/AbstractFetch.java#L281-L296 And we are discovering that we pay a price for waiting on that response, so wonder if we should address that? Not sure there is a way on the broker side (close fetch session is a full fetch request like any other I believe, just with the extra of closing, could be wrong). But on the client, we do know we won't use anything from that response, so should we review? I guess we could decide not to wait on the close session response and it would avoid this unintended block, but there could be downsides too, wdyt? -- update another option that comes to mind is that we could continue waiting for the fetch response, but sending a fetch.max.wait = 0. That would effectively that the fetch does not want to wait I expect, which is what we want for that fetch on closing the consumer. Would that work? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
