aliehsaeedii commented on code in PR #24193:
URL: https://github.com/apache/kafka/pull/24193#discussion_r4611580332


##########
tools/src/main/java/org/apache/kafka/tools/streams/StreamsGroupCommand.java:
##########
@@ -470,31 +473,66 @@ Map<TopicPartition, OffsetsInfo> 
getOffsets(StreamsGroupDescription description)
                 earliest.put(tp, OffsetSpec.earliest());
                 latest.put(tp, OffsetSpec.latest());
             }
-            Map<TopicPartition, ListOffsetsResult.ListOffsetsResultInfo> 
earliestResult = adminClient.listOffsets(
+            Map<TopicPartition, ListOffsetsResult.ListOffsetsResultInfo> 
earliestResult = offsetsOfExistingTopics(adminClient.listOffsets(
                 earliest,
                 withTimeoutMs(new ListOffsetsOptions())
-            ).all().get();
-            Map<TopicPartition, ListOffsetsResult.ListOffsetsResultInfo> 
latestResult = adminClient.listOffsets(
+            ), allTp);
+            Map<TopicPartition, ListOffsetsResult.ListOffsetsResultInfo> 
latestResult = offsetsOfExistingTopics(adminClient.listOffsets(
                 latest,
                 withTimeoutMs(new ListOffsetsOptions())
-            ).all().get();
+            ), allTp);
             Map<TopicPartition, OffsetAndMetadata> committedOffsets = 
getCommittedOffsets(description.groupId());
 
             Map<TopicPartition, OffsetsInfo> output = new HashMap<>();
             for (Map.Entry<TopicPartition, 
ListOffsetsResult.ListOffsetsResultInfo> tp : earliestResult.entrySet()) {
+                if (!latestResult.containsKey(tp.getKey())) {
+                    continue;
+                }
                 final Optional<Long> currentOffset = 
committedOffsets.containsKey(tp.getKey()) ? 
Optional.of(committedOffsets.get(tp.getKey()).offset()) : Optional.empty();
                 final Optional<Integer> leaderEpoch = 
committedOffsets.containsKey(tp.getKey()) ? 
committedOffsets.get(tp.getKey()).leaderEpoch() : Optional.empty();
                 final long lag = currentOffset.map(current -> 
latestResult.get(tp.getKey()).offset() - current).orElseGet(() -> 
latestResult.get(tp.getKey()).offset() - 
earliestResult.get(tp.getKey()).offset());
                 output.put(tp.getKey(),
                     new OffsetsInfo(
                         currentOffset,
                         leaderEpoch,
-                        latestResult.get(tp.getKey()).offset(),
-                        lag));
+                        Optional.of(latestResult.get(tp.getKey()).offset()),
+                        Optional.of(lag)));
+            }
+            // Partitions of a deleted topic have no log-end offset or lag, 
but still show their committed offset.
+            for (TopicPartition tp : allTp) {
+                if (!output.containsKey(tp)) {
+                    final Optional<OffsetAndMetadata> committed = 
Optional.ofNullable(committedOffsets.get(tp));
+                    output.put(tp, new 
OffsetsInfo(committed.map(OffsetAndMetadata::offset), 
committed.flatMap(OffsetAndMetadata::leaderEpoch), Optional.empty(), 
Optional.empty()));
+                }
             }
             return output;
         }
 
+        /**
+         * Like {@code result.all().get()}, but leaves out partitions whose 
topic no longer exists instead of failing.
+         *
+         * <p>A member that stopped without leaving the group (e.g. it 
crashed) keeps its assignment until its
+         * session times out, so the assignment can still reference a topic 
that has since been deleted. The admin
+         * client fails only that topic's partitions with {@link 
UnknownTopicOrPartitionException}, so one deleted
+         * topic must not fail the describe of the whole group (and of the 
remaining groups with {@code --all-groups}).
+         */
+        private static Map<TopicPartition, 
ListOffsetsResult.ListOffsetsResultInfo> offsetsOfExistingTopics(
+            ListOffsetsResult result,
+            Set<TopicPartition> partitions
+        ) throws ExecutionException, InterruptedException {
+            Map<TopicPartition, ListOffsetsResult.ListOffsetsResultInfo> 
offsets = new HashMap<>();
+            for (TopicPartition tp : partitions) {
+                try {
+                    offsets.put(tp, result.partitionResult(tp).get());
+                } catch (ExecutionException e) {
+                    if (!(e.getCause() instanceof 
UnknownTopicOrPartitionException)) {

Review Comment:
   This only helps when the whole topic is gone. If the topic is recreated with 
fewer partitions, the admin client retries the missing partition until the 
timeout, and `--describe` still fails with a `TimeoutException`. The open 
consumer-group PR for the same problem (#24098) checks `describeTopics` first 
and treats missing partitions like deleted ones. Could we do the same here, so 
both tools behave alike?



##########
tools/src/main/java/org/apache/kafka/tools/streams/StreamsGroupCommand.java:
##########
@@ -470,31 +473,66 @@ Map<TopicPartition, OffsetsInfo> 
getOffsets(StreamsGroupDescription description)
                 earliest.put(tp, OffsetSpec.earliest());
                 latest.put(tp, OffsetSpec.latest());
             }
-            Map<TopicPartition, ListOffsetsResult.ListOffsetsResultInfo> 
earliestResult = adminClient.listOffsets(
+            Map<TopicPartition, ListOffsetsResult.ListOffsetsResultInfo> 
earliestResult = offsetsOfExistingTopics(adminClient.listOffsets(
                 earliest,
                 withTimeoutMs(new ListOffsetsOptions())
-            ).all().get();
-            Map<TopicPartition, ListOffsetsResult.ListOffsetsResultInfo> 
latestResult = adminClient.listOffsets(
+            ), allTp);
+            Map<TopicPartition, ListOffsetsResult.ListOffsetsResultInfo> 
latestResult = offsetsOfExistingTopics(adminClient.listOffsets(
                 latest,
                 withTimeoutMs(new ListOffsetsOptions())
-            ).all().get();
+            ), allTp);
             Map<TopicPartition, OffsetAndMetadata> committedOffsets = 
getCommittedOffsets(description.groupId());
 
             Map<TopicPartition, OffsetsInfo> output = new HashMap<>();
             for (Map.Entry<TopicPartition, 
ListOffsetsResult.ListOffsetsResultInfo> tp : earliestResult.entrySet()) {
+                if (!latestResult.containsKey(tp.getKey())) {
+                    continue;
+                }
                 final Optional<Long> currentOffset = 
committedOffsets.containsKey(tp.getKey()) ? 
Optional.of(committedOffsets.get(tp.getKey()).offset()) : Optional.empty();
                 final Optional<Integer> leaderEpoch = 
committedOffsets.containsKey(tp.getKey()) ? 
committedOffsets.get(tp.getKey()).leaderEpoch() : Optional.empty();
                 final long lag = currentOffset.map(current -> 
latestResult.get(tp.getKey()).offset() - current).orElseGet(() -> 
latestResult.get(tp.getKey()).offset() - 
earliestResult.get(tp.getKey()).offset());
                 output.put(tp.getKey(),
                     new OffsetsInfo(
                         currentOffset,
                         leaderEpoch,
-                        latestResult.get(tp.getKey()).offset(),
-                        lag));
+                        Optional.of(latestResult.get(tp.getKey()).offset()),
+                        Optional.of(lag)));
+            }
+            // Partitions of a deleted topic have no log-end offset or lag, 
but still show their committed offset.
+            for (TopicPartition tp : allTp) {
+                if (!output.containsKey(tp)) {
+                    final Optional<OffsetAndMetadata> committed = 
Optional.ofNullable(committedOffsets.get(tp));
+                    output.put(tp, new 
OffsetsInfo(committed.map(OffsetAndMetadata::offset), 
committed.flatMap(OffsetAndMetadata::leaderEpoch), Optional.empty(), 
Optional.empty()));
+                }
             }
             return output;
         }
 
+        /**
+         * Like {@code result.all().get()}, but leaves out partitions whose 
topic no longer exists instead of failing.
+         *
+         * <p>A member that stopped without leaving the group (e.g. it 
crashed) keeps its assignment until its
+         * session times out, so the assignment can still reference a topic 
that has since been deleted. The admin
+         * client fails only that topic's partitions with {@link 
UnknownTopicOrPartitionException}, so one deleted
+         * topic must not fail the describe of the whole group (and of the 
remaining groups with {@code --all-groups}).
+         */
+        private static Map<TopicPartition, 
ListOffsetsResult.ListOffsetsResultInfo> offsetsOfExistingTopics(
+            ListOffsetsResult result,
+            Set<TopicPartition> partitions
+        ) throws ExecutionException, InterruptedException {
+            Map<TopicPartition, ListOffsetsResult.ListOffsetsResultInfo> 
offsets = new HashMap<>();
+            for (TopicPartition tp : partitions) {
+                try {
+                    offsets.put(tp, result.partitionResult(tp).get());

Review Comment:
   For an unknown topic, `PartitionLeaderStrategy` logs at ERROR before it 
fails the future. With the default tools log config, I think the command now 
succeeds but prints that ERROR to stderr twice per deleted topic (once for 
earliest, once for latest). Did you see this in your local run? The 
`describeTopics` check suggested above would avoid it too.



##########
tools/src/main/java/org/apache/kafka/tools/streams/StreamsGroupCommand.java:
##########
@@ -470,31 +473,66 @@ Map<TopicPartition, OffsetsInfo> 
getOffsets(StreamsGroupDescription description)
                 earliest.put(tp, OffsetSpec.earliest());
                 latest.put(tp, OffsetSpec.latest());
             }
-            Map<TopicPartition, ListOffsetsResult.ListOffsetsResultInfo> 
earliestResult = adminClient.listOffsets(
+            Map<TopicPartition, ListOffsetsResult.ListOffsetsResultInfo> 
earliestResult = offsetsOfExistingTopics(adminClient.listOffsets(
                 earliest,
                 withTimeoutMs(new ListOffsetsOptions())
-            ).all().get();
-            Map<TopicPartition, ListOffsetsResult.ListOffsetsResultInfo> 
latestResult = adminClient.listOffsets(
+            ), allTp);
+            Map<TopicPartition, ListOffsetsResult.ListOffsetsResultInfo> 
latestResult = offsetsOfExistingTopics(adminClient.listOffsets(
                 latest,
                 withTimeoutMs(new ListOffsetsOptions())
-            ).all().get();
+            ), allTp);
             Map<TopicPartition, OffsetAndMetadata> committedOffsets = 
getCommittedOffsets(description.groupId());
 
             Map<TopicPartition, OffsetsInfo> output = new HashMap<>();
             for (Map.Entry<TopicPartition, 
ListOffsetsResult.ListOffsetsResultInfo> tp : earliestResult.entrySet()) {
+                if (!latestResult.containsKey(tp.getKey())) {
+                    continue;
+                }
                 final Optional<Long> currentOffset = 
committedOffsets.containsKey(tp.getKey()) ? 
Optional.of(committedOffsets.get(tp.getKey()).offset()) : Optional.empty();
                 final Optional<Integer> leaderEpoch = 
committedOffsets.containsKey(tp.getKey()) ? 
committedOffsets.get(tp.getKey()).leaderEpoch() : Optional.empty();
                 final long lag = currentOffset.map(current -> 
latestResult.get(tp.getKey()).offset() - current).orElseGet(() -> 
latestResult.get(tp.getKey()).offset() - 
earliestResult.get(tp.getKey()).offset());
                 output.put(tp.getKey(),
                     new OffsetsInfo(
                         currentOffset,
                         leaderEpoch,
-                        latestResult.get(tp.getKey()).offset(),
-                        lag));
+                        Optional.of(latestResult.get(tp.getKey()).offset()),
+                        Optional.of(lag)));
+            }
+            // Partitions of a deleted topic have no log-end offset or lag, 
but still show their committed offset.
+            for (TopicPartition tp : allTp) {

Review Comment:
   nit: we could use one loop over `allTp` that builds the full entry when both 
earliest and latest exist and the committed-only entry otherwise. Then we don't 
need the `continue` above or this second pass.



##########
tools/src/test/java/org/apache/kafka/tools/streams/StreamsGroupCommandTest.java:
##########
@@ -337,11 +331,75 @@ public void testDescribeStreamsGroupsGetOffsets() throws 
Exception {
         Map<TopicPartition, StreamsGroupCommand.OffsetsInfo> lags = 
service.getOffsets(x);
 
         assertEquals(1, lags.size());
-        assertEquals(new StreamsGroupCommand.OffsetsInfo(Optional.of(12L), 
Optional.of(0), 30L, 18L), lags.get(new TopicPartition("topic1", 0)));
+        assertEquals(new StreamsGroupCommand.OffsetsInfo(Optional.of(12L), 
Optional.of(0), Optional.of(30L), Optional.of(18L)), lags.get(new 
TopicPartition("topic1", 0)));
 
         service.close();
     }
 
+    @Test
+    public void testDescribeStreamsGroupsGetOffsetsWithDeletedTopic() throws 
Exception {

Review Comment:
   Could you add a case where one partition fails with some other error (e.g. 
`TopicAuthorizationException`) and check that `getOffsets` still throws? That 
rethrow is what keeps this fix narrow, and no test covers it right now.



##########
tools/src/main/java/org/apache/kafka/tools/streams/StreamsGroupCommand.java:
##########
@@ -442,15 +442,18 @@ private void printOffsets(StreamsGroupDescription 
description, boolean verbose)
                     String fmt = "%" + -groupLen + "s %" + -maxTopicLen + "s 
%" + -maxPartitionLen + "s %s\n";
                     System.out.printf(fmt, "GROUP", "TOPIC", "PARTITION", 
"OFFSET-LAG");
                     for (Map.Entry<TopicPartition, OffsetsInfo> offset : 
offsets.entrySet()) {
-                        System.out.printf(fmt, description.groupId(), 
offset.getKey().topic(), offset.getKey().partition(), offset.getValue().lag);
+                        System.out.printf(fmt, description.groupId(), 
offset.getKey().topic(), offset.getKey().partition(),
+                            
offset.getValue().lag.map(Object::toString).orElse(MISSING_COLUMN_VALUE));
                     }
                 } else {
                     String fmt = "%" + (-groupLen) + "s %" + (-maxTopicLen) + 
"s %-10s %-15s %-15s %-15s %-15s%n";
                     System.out.printf(fmt, "GROUP", "TOPIC", "PARTITION", 
"CURRENT-OFFSET", "LEADER-EPOCH", "LOG-END-OFFSET", "OFFSET-LAG");
                     for (Map.Entry<TopicPartition, OffsetsInfo> offset : 
offsets.entrySet()) {
                         System.out.printf(fmt, description.groupId(), 
offset.getKey().topic(), offset.getKey().partition(),
-                            
offset.getValue().currentOffset.map(Object::toString).orElse("-"), 
offset.getValue().leaderEpoch.map(Object::toString).orElse("-"),
-                            offset.getValue().logEndOffset, 
offset.getValue().lag);
+                            
offset.getValue().currentOffset.map(Object::toString).orElse(MISSING_COLUMN_VALUE),
+                            
offset.getValue().leaderEpoch.map(Object::toString).orElse(MISSING_COLUMN_VALUE),
+                            
offset.getValue().logEndOffset.map(Object::toString).orElse(MISSING_COLUMN_VALUE),

Review Comment:
   No test checks the printed output for a deleted topic, i.e. `-` in 
LOG-END-OFFSET and OFFSET-LAG. A small stdout check, or a case in 
`DescribeStreamsGroupTest`, would cover the part users actually see.



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