aryangupta1998 commented on code in PR #8282:
URL: https://github.com/apache/ozone/pull/8282#discussion_r2045425900
##########
hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/containerlog/parser/ContainerDatanodeDatabase.java:
##########
@@ -233,5 +240,99 @@ private void dropTable(String tableName, Statement stmt)
throws SQLException {
stmt.executeUpdate(dropTableSQL);
}
+ private void createContainerLogIndex(Statement stmt) throws SQLException {
+ String createIndexSQL = queries.get("CREATE_INDEX_LATEST_STATE");
+ stmt.execute(createIndexSQL);
+ }
+
+ /**
+ * Lists containers filtered by the specified state and writes their details
to a file or console.
+ * <p>
+ * The output includes timestamp, datanode ID, container ID, BCSID, error
message, and index value,
+ * written in a human-readable table format to a file or console.
+ * - If no file path is provided and no limit is specified, results are
printed
+ * to the console with a default limit of 100 rows.
+ * - If no file path is provided but a limit is specified, results are
printed
+ * to the console with the specified limit.
+ * - If a file path is provided but no limit is specified, all matching rows
are written
+ * to the specified file.
+ * - If both a file path and a limit are provided, only the specified number
of rows are written
+ * to the file.
+ *
+ * @param state the container state to filter by (e.g., "OPEN", "CLOSED",
"QUASI_CLOSED")
+ * @param path the file path to write the output to. if {@code null}, output
is printed to the console.
+ * @param limit the maximum number of rows to return.
+ *
+ */
+
+ public void listContainersByState(String state, String path, Integer limit)
throws SQLException {
+ int count = 0;
+
+ boolean writeToFile = path != null;
+ boolean limitProvided = limit != null;
+
+ String baseQuery = queries.get("SELECT_LATEST_CONTAINER_LOGS_BY_STATE");
+ String finalQuery = (!writeToFile && !limitProvided) ? baseQuery + " LIMIT
" + DEFAULT_LIMIT
+ : (limitProvided ? baseQuery + " LIMIT ?" : baseQuery);
+
+ Path outputPath = null;
+ if (writeToFile) {
+ outputPath = Paths.get(path);
+ if (Files.exists(outputPath)) {
+ System.out.println("Warning: Output file already exists and will be
overwritten: "
+ + outputPath.toAbsolutePath());
+ }
+ }
+
+ try (Connection connection = getConnection();
+ Statement stmt = connection.createStatement()) {
+
+ createContainerLogIndex(stmt);
+
+ try (PreparedStatement pstmt = connection.prepareStatement(finalQuery)) {
+ pstmt.setString(1, state);
+ if (limitProvided) {
+ pstmt.setInt(2, limit);
+ }
+
+ try (ResultSet rs = pstmt.executeQuery();
+ BufferedWriter writer = writeToFile
+ ? Files.newBufferedWriter(outputPath, StandardCharsets.UTF_8)
+ : new BufferedWriter(new OutputStreamWriter(System.out,
StandardCharsets.UTF_8))) {
+
+ writer.write(String.format("%-25s | %-35s | %-15s | %-15s | %-40s |
%-12s%n",
+ "Timestamp", "Datanode ID", "Container ID", "BCSID", "Message",
"Index Value"));
+
writer.write("-------------------------------------------------------------------------------------"
+
+
"---------------------------------------------------------------------------------------\n");
+
+ while (rs.next()) {
+ String timestamp = rs.getString("timestamp");
+ String datanodeId = rs.getString("datanode_id");
+ long containerId = rs.getLong("container_id");
+ long latestBcsid = rs.getLong("latest_bcsid");
+ String errorMessage = rs.getString("error_message");
+ int indexValue = rs.getInt("index_value");
+ count++;
+
+ writer.write(String.format("%-25s | %-35s | %-15d | %-15d | %-40s
| %-12d%n",
+ timestamp, datanodeId, containerId, latestBcsid, errorMessage,
indexValue));
+ }
+
+ writer.write("total: " + count + "\n");
+ writer.flush();
Review Comment:
```suggestion
if (count == 0) {
writer.write("No containers found for state: " + state + "\n");
} else {
writer.write("total: " + count + "\n");
writer.flush();
}
```
--
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]