TaiJuWu commented on code in PR #24425:
URL: https://github.com/apache/kafka/pull/24425#discussion_r4725692286


##########
clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/ShareConsumerTest.java:
##########
@@ -2069,6 +2072,79 @@ public void testShareGroupShareSessionCacheIsFull() {
         }
     }
 
+    @ClusterTest(
+        serverProperties = {
+            // Close idle connections quickly so that the connection of a 
share consumer which stops polling is
+            // closed by the broker, which is how the broker observes a client 
disconnect.
+            @ClusterConfigProperty(key = "connections.max.idle.ms", value = 
"3000"),
+            // Keep heartbeats more frequent than the idle timeout so that 
only the share fetch connection goes idle.
+            @ClusterConfigProperty(key = "group.share.heartbeat.interval.ms", 
value = "1000"),
+            @ClusterConfigProperty(key = 
"group.share.min.heartbeat.interval.ms", value = "1000"),
+            // Long record lock so that a re-delivery within the test can only 
be due to the release on disconnect.
+            @ClusterConfigProperty(key = 
"group.share.record.lock.duration.ms", value = "60000")
+        }
+    )
+    public void 
testShareSessionEvictedAndAcquiredRecordsReleasedOnClientDisconnect() throws 
Exception {
+        String groupId = "group1";
+        alterShareAutoOffsetReset(groupId, "earliest");
+        assertEquals(1, cluster.brokers().size());
+        ShareSessionCache shareSessionCache = ((BrokerServer) 
cluster.brokers().get(0)).sharePartitionManager().shareSessionCache();
+        assertEquals(0, shareSessionCache.size());
+
+        try (Producer<byte[], byte[]> producer = createProducer();
+             ShareConsumer<byte[], byte[]> shareConsumer = 
createShareConsumer(groupId)) {
+            producer.send(new ProducerRecord<>(tp.topic(), tp.partition(), 
null, "key".getBytes(), "value".getBytes()));
+            producer.flush();
+
+            Map<TopicPartition, Set<Long>> partitionOffsetsMap = new 
HashMap<>();
+            Map<TopicPartition, Exception> partitionExceptionMap = new 
HashMap<>();
+            shareConsumer.setAcknowledgementCommitCallback(new 
TestableAcknowledgementCommitCallback(partitionOffsetsMap, 
partitionExceptionMap));
+
+            shareConsumer.subscribe(Set.of(tp.topic()));
+            ConsumerRecords<byte[], byte[]> records = 
waitedPoll(shareConsumer, 2500L, 1);
+            assertEquals(1, records.count());
+            assertEquals(0, records.iterator().next().offset());
+            // The share session for the member is established on the broker.
+            assertEquals(1, shareSessionCache.size());
+            assertEquals(1, shareSessionCache.numMembers(groupId));
+
+            // The record is left acquired, as it is only acknowledged on the 
next poll, and the consumer stops
+            // polling, hence no share fetch requests are sent and the broker 
closes the idle connection. The broker
+            // must evict the share session on the disconnect and leave no 
members for the group in the cache.
+            waitForCondition(() -> shareSessionCache.size() == 0 && 
shareSessionCache.numMembers(groupId) == null,
+                DEFAULT_MAX_WAIT_MS, "Share session was not evicted on client 
disconnect");

Review Comment:
   This integration test doesn't cover the race scenario, right? 
   It waits for the session to be evicted before polling again, so the 
reconnect always happens after the disconnect is handled.



-- 
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]

Reply via email to