Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: pretty describe cluster output #487

Merged
merged 2 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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