Skip to content

Commit

Permalink
feat: pretty describe cluster output (#487)
Browse files Browse the repository at this point in the history
Signed-off-by: Li Zhanhui <[email protected]>
  • Loading branch information
lizhanhui authored Oct 27, 2023
1 parent e4cfc09 commit 715352a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
53 changes: 52 additions & 1 deletion cli/src/main/java/com/automq/rocketmq/cli/ConsoleHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,65 @@

package com.automq.rocketmq.cli;

import apache.rocketmq.controller.v1.Cluster;
import apache.rocketmq.controller.v1.MessageQueueAssignment;
import apache.rocketmq.controller.v1.Node;
import apache.rocketmq.controller.v1.OngoingMessageQueueReassignment;
import apache.rocketmq.controller.v1.Topic;
import com.google.protobuf.Timestamp;
import de.vandermeer.asciitable.AT_Cell;
import de.vandermeer.asciitable.AT_Row;
import de.vandermeer.asciitable.AsciiTable;
import de.vandermeer.asciitable.CWC_LongestLine;
import de.vandermeer.skb.interfaces.transformers.textformat.TextAlignment;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class ConsoleHelper {

private static Date toDate(Timestamp timestamp) {
long millis = TimeUnit.SECONDS.toMillis(timestamp.getSeconds()) + TimeUnit.NANOSECONDS.toMillis(timestamp.getNanos());
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(millis);
return calendar.getTime();
}

private static void alignCentral(AT_Row row) {
for (AT_Cell cell : row.getCells()) {
cell.getContext().setTextAlignment(TextAlignment.CENTER);
}
}

public static void printCluster(Cluster cluster) {
if (null == cluster) {
return;
}

AsciiTable nodeTable = new AsciiTable();
nodeTable.addRule();
AT_Row row = nodeTable.addRow("NODE ID", "NODE NAME", "TOPIC QUANTITY", "QUEUE QUANTITY",
"STREAM QUANTITY", "LAST HEARTBEAT", "ROLE", "EPOCH", "EXPIRATION");

alignCentral(row);

for (Node node : cluster.getNodesList()) {
nodeTable.addRule();
boolean isLeader = node.getId() == cluster.getLease().getNodeId();
row = nodeTable.addRow(node.getId(), node.getName(), node.getTopicNum(), node.getQueueNum(), node.getStreamNum(),
toDate(node.getLastHeartbeat()), isLeader ? "Leader" : "Worker", isLeader ? cluster.getLease().getEpoch() : "",
isLeader ? toDate(cluster.getLease().getExpirationTimestamp()) : "");
alignCentral(row);
}
nodeTable.addRule();

CWC_LongestLine cwc = new CWC_LongestLine();
nodeTable.getRenderer().setCWC(cwc);
String render = nodeTable.render();
System.out.println(render);
}

public static void printTable(Topic topic) {
AsciiTable topicTable = new AsciiTable();
topicTable.addRule();
Expand Down Expand Up @@ -60,7 +110,8 @@ public static void printTable(Topic topic) {
reassignmentTable.addRow("SRC NODE ID", "DST NODE ID", "QUEUE ID");
reassignmentTable.addRule();
for (OngoingMessageQueueReassignment reassignment : ongoing) {
reassignmentTable.addRow(reassignment.getSrcNodeId(), reassignment.getDstNodeId(), reassignment.getQueue().getQueueId());
reassignmentTable.addRow(reassignment.getSrcNodeId(), reassignment.getDstNodeId(),
reassignment.getQueue().getQueueId());
reassignmentTable.addRule();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import apache.rocketmq.controller.v1.DescribeClusterRequest;
import com.automq.rocketmq.controller.metadata.ControllerClient;
import com.automq.rocketmq.controller.metadata.GrpcControllerClient;
import com.google.protobuf.util.JsonFormat;
import java.util.concurrent.Callable;
import picocli.CommandLine;

Expand All @@ -37,7 +36,7 @@ public Void call() throws Exception {
DescribeClusterRequest request = DescribeClusterRequest.newBuilder()
.build();
Cluster cluster = client.describeCluster(mqAdmin.endpoint, request).join();
System.out.println(JsonFormat.printer().print(cluster));
ConsoleHelper.printCluster(cluster);
}
return null;
}
Expand Down

0 comments on commit 715352a

Please sign in to comment.