kerneltime commented on code in PR #8050:
URL: https://github.com/apache/ozone/pull/8050#discussion_r2008014980


##########
hadoop-hdds/tools/src/main/java/org/apache/hadoop/hdds/scm/cli/container/ListSubcommand.java:
##########
@@ -114,44 +116,104 @@ public void execute(ScmClient scmClient) throws 
IOException {
         .getInt(ScmConfigKeys.OZONE_SCM_CONTAINER_LIST_MAX_COUNT,
             ScmConfigKeys.OZONE_SCM_CONTAINER_LIST_MAX_COUNT_DEFAULT);
 
-    ContainerListResult containerListAndTotalCount;
+    // Use SequenceWriter to output JSON array format for all cases
+    SequenceWriter sequenceWriter = WRITER.writeValues(new 
NonClosingOutputStream(System.out));
+    sequenceWriter.init(true); // Initialize as a JSON array
 
     if (!all) {
+      // Regular listing with count limit
       if (count > maxCountAllowed) {
         System.err.printf("Attempting to list the first %d records of 
containers." +
             " However it exceeds the cluster's current limit of %d. The 
results will be capped at the" +
-            " maximum allowed count.%n", count, 
ScmConfigKeys.OZONE_SCM_CONTAINER_LIST_MAX_COUNT_DEFAULT);
+            " maximum allowed count.%n", count, maxCountAllowed);
         count = maxCountAllowed;
       }
-      containerListAndTotalCount = scmClient.listContainer(startId, count, 
state, type, repConfig);
-      for (ContainerInfo container : 
containerListAndTotalCount.getContainerInfoList()) {
-        outputContainerInfo(container);
-      }
 
-      if (containerListAndTotalCount.getTotalCount() > count) {
+      ContainerListResult containerListResult =
+          scmClient.listContainer(startId, count, state, type, repConfig);
+
+      writeContainers(sequenceWriter, 
containerListResult.getContainerInfoList());
+
+      closeStream(sequenceWriter);
+      if (containerListResult.getTotalCount() > count) {
         System.err.printf("Displaying %d out of %d containers. " +
-                        "Container list has more containers.%n",
-                count, containerListAndTotalCount.getTotalCount());
+                "Container list has more containers.%n",
+            count, containerListResult.getTotalCount());
       }
     } else {
-      // Batch size is either count passed through cli or maxCountAllowed
+      // List all containers by fetching in batches
       int batchSize = (count > 0) ? count : maxCountAllowed;
-      long currentStartId = startId;
-      int fetchedCount;
-
-      do {
-        // Fetch containers in batches of 'batchSize'
-        containerListAndTotalCount = scmClient.listContainer(currentStartId, 
batchSize, state, type, repConfig);
-        fetchedCount = 
containerListAndTotalCount.getContainerInfoList().size();
-
-        for (ContainerInfo container : 
containerListAndTotalCount.getContainerInfoList()) {
-          outputContainerInfo(container);
-        }
-
-        if (fetchedCount > 0) {
-          currentStartId = 
containerListAndTotalCount.getContainerInfoList().get(fetchedCount - 
1).getContainerID() + 1;
-        }
-      } while (fetchedCount > 0);
+      listAllContainers(scmClient, sequenceWriter, batchSize, repConfig);
+      closeStream(sequenceWriter);
+    }
+  }
+
+  private void writeContainers(SequenceWriter writer, List<ContainerInfo> 
containers)
+      throws IOException {
+    for (ContainerInfo container : containers) {
+      writer.write(container);
+    }
+  }
+
+  private void closeStream(SequenceWriter writer) throws IOException {
+    writer.flush();
+    writer.close();
+    // Add the final newline
+    System.out.println();
+  }
+
+  private void listAllContainers(ScmClient scmClient, SequenceWriter writer,
+                                 int batchSize, ReplicationConfig repConfig)

Review Comment:
   I can change this in a followup.



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to