Fix compactionstats layout for long ks/cf names Patch by Nicolas Lalevée, reviewed by brandonwilliams for CASSANDRA-7263
Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/4c510dc6 Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/4c510dc6 Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/4c510dc6 Branch: refs/heads/trunk Commit: 4c510dc6cc1ea3fdb21180cfc74ab6c29b78c24f Parents: 1040b52 Author: Brandon Williams <brandonwilli...@apache.org> Authored: Thu Aug 14 10:51:57 2014 -0500 Committer: Brandon Williams <brandonwilli...@apache.org> Committed: Thu Aug 14 10:51:57 2014 -0500 ---------------------------------------------------------------------- .../org/apache/cassandra/tools/NodeTool.java | 66 ++++++++++++++------ 1 file changed, 48 insertions(+), 18 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cassandra/blob/4c510dc6/src/java/org/apache/cassandra/tools/NodeTool.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/tools/NodeTool.java b/src/java/org/apache/cassandra/tools/NodeTool.java index b25edd9..6aca507 100644 --- a/src/java/org/apache/cassandra/tools/NodeTool.java +++ b/src/java/org/apache/cassandra/tools/NodeTool.java @@ -1129,26 +1129,56 @@ public class NodeTool int compactionThroughput = probe.getCompactionThroughput(); CompactionManagerMBean cm = probe.getCompactionManagerProxy(); System.out.println("pending tasks: " + probe.getCompactionMetric("PendingTasks")); - if (cm.getCompactions().size() > 0) - System.out.printf("%25s%16s%16s%16s%16s%10s%10s%n", "compaction type", "keyspace", "table", "completed", "total", "unit", "progress"); long remainingBytes = 0; - for (Map<String, String> c : cm.getCompactions()) - { - String percentComplete = new Long(c.get("total")) == 0 - ? "n/a" - : new DecimalFormat("0.00").format((double) new Long(c.get("completed")) / new Long(c.get("total")) * 100) + "%"; - System.out.printf("%25s%16s%16s%16s%16s%10s%10s%n", c.get("taskType"), c.get("keyspace"), c.get("columnfamily"), c.get("completed"), c.get("total"), c.get("unit"), percentComplete); - if (c.get("taskType").equals(OperationType.COMPACTION.toString())) - remainingBytes += (new Long(c.get("total")) - new Long(c.get("completed"))); + if (cm.getCompactions().size() > 0) + { + List<String[]> lines = new ArrayList<>(); + int[] columnSizes = new int[] { 0, 0, 0, 0, 0, 0, 0 }; + + addLine(lines, columnSizes, "compaction type", "keyspace", "table", "completed", "total", "unit", "progress"); + for (Map<String, String> c : cm.getCompactions()) + { + long total = Long.parseLong(c.get("total")); + long completed = Long.parseLong(c.get("completed")); + String taskType = c.get("taskType"); + String keyspace = c.get("keyspace"); + String columnFamily = c.get("columnfamily"); + String unit = c.get("unit"); + String percentComplete = total == 0 ? "n/a" : new DecimalFormat("0.00").format((double) completed / total * 100) + "%"; + addLine(lines, columnSizes, taskType, keyspace, columnFamily, Long.toString(completed), Long.toString(total), unit, percentComplete); + if (taskType.equals(OperationType.COMPACTION.toString())) + remainingBytes += total - completed; + } + + StringBuilder buffer = new StringBuilder(); + for (int columnSize : columnSizes) { + buffer.append("%"); + buffer.append(columnSize + 3); + buffer.append("s"); + } + buffer.append("%n"); + String format = buffer.toString(); + + for (String[] line : lines) + { + System.out.printf(format, line[0], line[1], line[2], line[3], line[4], line[5], line[6]); + } + + String remainingTime = "n/a"; + if (compactionThroughput != 0) + { + long remainingTimeInSecs = remainingBytes / (1024L * 1024L * compactionThroughput); + remainingTime = format("%dh%02dm%02ds", remainingTimeInSecs / 3600, (remainingTimeInSecs % 3600) / 60, (remainingTimeInSecs % 60)); + } + System.out.printf("%25s%10s%n", "Active compaction remaining time : ", remainingTime); + } + } + + private void addLine(List<String[]> lines, int[] columnSizes, String... columns) { + lines.add(columns); + for (int i = 0; i < columns.length; i++) { + columnSizes[i] = Math.max(columnSizes[i], columns[i].length()); } - long remainingTimeInSecs = compactionThroughput == 0 || remainingBytes == 0 - ? -1 - : (remainingBytes) / (1024L * 1024L * compactionThroughput); - String remainingTime = remainingTimeInSecs < 0 - ? "n/a" - : format("%dh%02dm%02ds", remainingTimeInSecs / 3600, (remainingTimeInSecs % 3600) / 60, (remainingTimeInSecs % 60)); - - System.out.printf("%25s%10s%n", "Active compaction remaining time : ", remainingTime); } }