diff --git a/group-coordinator/src/main/java/org/apache/kafka/coordinator/group/GroupCoordinatorService.java b/group-coordinator/src/main/java/org/apache/kafka/coordinator/group/GroupCoordinatorService.java index b092316bb319a..b184ab9b83b7f 100644 --- a/group-coordinator/src/main/java/org/apache/kafka/coordinator/group/GroupCoordinatorService.java +++ b/group-coordinator/src/main/java/org/apache/kafka/coordinator/group/GroupCoordinatorService.java @@ -501,6 +501,10 @@ public CompletableFuture listGroups( final Set existingPartitionSet = runtime.partitions(); final AtomicInteger cnt = new AtomicInteger(existingPartitionSet.size()); + if (existingPartitionSet.isEmpty()) { + return CompletableFuture.completedFuture(new ListGroupsResponseData()); + } + for (TopicPartition tp : existingPartitionSet) { runtime.scheduleReadOperation( "list-groups", diff --git a/group-coordinator/src/test/java/org/apache/kafka/coordinator/group/GroupCoordinatorServiceTest.java b/group-coordinator/src/test/java/org/apache/kafka/coordinator/group/GroupCoordinatorServiceTest.java index e3e13b8268599..98174b07508fe 100644 --- a/group-coordinator/src/test/java/org/apache/kafka/coordinator/group/GroupCoordinatorServiceTest.java +++ b/group-coordinator/src/test/java/org/apache/kafka/coordinator/group/GroupCoordinatorServiceTest.java @@ -832,6 +832,30 @@ public void testListGroupsFailedImmediately() assertEquals(Collections.emptyList(), listGroupsResponseData.groups()); } + @Test + public void testListGroupsWithEmptyTopicPartitions() throws ExecutionException, InterruptedException { + CoordinatorRuntime runtime = mockRuntime(); + GroupCoordinatorService service = new GroupCoordinatorService( + new LogContext(), + createConfig(), + runtime + ); + int partitionCount = 0; + service.startup(() -> partitionCount); + + ListGroupsRequestData request = new ListGroupsRequestData(); + + CompletableFuture future = service.listGroups( + requestContext(ApiKeys.LIST_GROUPS), + request + ); + + assertEquals( + new ListGroupsResponseData(), + future.get() + ); + } + @Test public void testListGroupsWhenNotStarted() throws ExecutionException, InterruptedException { CoordinatorRuntime runtime = mockRuntime();