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

CASSANDRA-19104 Standardize tablestats formatting and data units #2977

Closed
wants to merge 11 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.List;
import java.util.Map;

import org.apache.cassandra.io.util.FileUtils;
import org.apache.cassandra.utils.FBUtilities;

public class TableStatsPrinter<T extends StatsHolder>
Expand Down Expand Up @@ -74,18 +75,18 @@ public void print(TableStatsHolder data, PrintStream out)

for (StatsTable table : tables)
{
printStatsTable(table, table.tableName, "\t\t", out);
printStatsTable(table, table.tableName, "\t\t", data.humanReadable, out);
}
out.println("----------------");
}
}

protected void printStatsTable(StatsTable table, String tableDisplayName, String indent, PrintStream out)
protected void printStatsTable(StatsTable table, String tableDisplayName, String indent, boolean humanReadable, PrintStream out)
{
out.println(indent + "Table" + (table.isIndex ? " (index): " : ": ") + tableDisplayName);
out.println(indent + "SSTable count: " + table.sstableCount);
out.println(indent + "Old SSTable count: " + table.oldSSTableCount);
out.println(indent + "Max SSTable size: " + FBUtilities.prettyPrintMemory(table.maxSSTableSize));
out.println(indent + "Max SSTable size: " + formatDataSize(table.maxSSTableSize, humanReadable));
if (table.twcs != null)
out.println(indent + "SSTables Time Window: " + table.twcs);
if (table.isLeveledSstable)
Expand Down Expand Up @@ -121,9 +122,9 @@ protected void printStatsTable(StatsTable table, String tableDisplayName, String
out.println(indent + "Pending flushes: " + table.pendingFlushes);
out.println(indent + "Percent repaired: " + table.percentRepaired);

out.println(indent +"Bytes repaired: " + FBUtilities.prettyPrintMemory(table.bytesRepaired));
out.println(indent +"Bytes unrepaired: " + FBUtilities.prettyPrintMemory(table.bytesUnrepaired));
out.println(indent +"Bytes pending repair: " + FBUtilities.prettyPrintMemory(table.bytesPendingRepair));
out.println(indent + "Bytes repaired: " + formatDataSize(table.bytesRepaired, humanReadable));
out.println(indent + "Bytes unrepaired: " + formatDataSize(table.bytesUnrepaired, humanReadable));
out.println(indent + "Bytes pending repair: " + formatDataSize(table.bytesPendingRepair, humanReadable));

out.println(indent + "Bloom filter false positives: " + table.bloomFilterFalsePositives);
out.println(indent + "Bloom filter false ratio: " + FBUtilities.prettyPrintRatio(table.bloomFilterFalseRatio));
Expand All @@ -136,9 +137,9 @@ protected void printStatsTable(StatsTable table, String tableDisplayName, String
if (table.compressionMetadataOffHeapUsed)
out.println(indent + "Compression metadata off heap memory used: " + table.compressionMetadataOffHeapMemoryUsed);

out.println(indent + "Compacted partition minimum bytes: " + table.compactedPartitionMinimumBytes);
out.println(indent + "Compacted partition maximum bytes: " + table.compactedPartitionMaximumBytes);
out.println(indent + "Compacted partition mean bytes: " + table.compactedPartitionMeanBytes);
out.println(indent + "Compacted partition minimum bytes: " + formatDataSize(table.compactedPartitionMinimumBytes, humanReadable));
out.println(indent + "Compacted partition maximum bytes: " + formatDataSize(table.compactedPartitionMaximumBytes, humanReadable));
out.println(indent + "Compacted partition mean bytes: " + formatDataSize(table.compactedPartitionMeanBytes, humanReadable));
out.println(indent + "Average live cells per slice (last five minutes): " +
FBUtilities.prettyPrintAverage(table.averageLiveCellsPerSliceLastFiveMinutes));
out.println(indent + "Maximum live cells per slice (last five minutes): " + table.maximumLiveCellsPerSliceLastFiveMinutes);
Expand Down Expand Up @@ -167,6 +168,11 @@ protected void printStatsTable(StatsTable table, String tableDisplayName, String
}
out.println("");
}

private String formatDataSize(long bytes, boolean humanReadable)
{
return humanReadable ? FileUtils.stringifyFileSize(bytes) : Long.toString(bytes);
}
}

/**
Expand All @@ -192,7 +198,7 @@ public void print(TableStatsHolder data, PrintStream out)
out.println("----------------");
for (StatsTable table : tables)
{
printStatsTable(table, table.keyspaceName + "." + table.tableName, "\t", out);
printStatsTable(table, table.keyspaceName + "." + table.tableName, "\t", data.humanReadable, out);
}
out.println("----------------");
}
Expand Down
20 changes: 10 additions & 10 deletions src/java/org/apache/cassandra/utils/FBUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -890,16 +890,6 @@ public static <T> CloseableIterator<T> closeableIterator(Iterator<T> iterator)
final static Pattern BASE_NUMBER_PATTERN = Pattern.compile("NaN|[+-]?Infinity|[+-]?\\d+(\\.\\d+)?([eE]([+-]?)\\d+)?");
final static Pattern BINARY_EXPONENT = Pattern.compile("\\*2\\^([+-]?\\d+)");

/**
* Convert the given size in bytes to a human-readable value using binary (i.e. 2^10-based) modifiers.
* For example, 1.000KiB, 2.100GiB etc., up to 8.000 EiB.
* @param size Number to convert.
*/
public static String prettyPrintMemory(long size)
{
return prettyPrintMemory(size, "");
}

/**
* Formats a latency value in milliseconds for display, appending an "ms" suffix.
* The formatted output is rounded to three decimal places.
Expand Down Expand Up @@ -931,6 +921,16 @@ public static String prettyPrintAverage(double average)
return String.format("%.2f", average);
}

/**
* Convert the given size in bytes to a human-readable value using binary (i.e. 2^10-based) modifiers.
* For example, 1.000KiB, 2.100GiB etc., up to 8.000 EiB.
* @param size Number to convert.
*/
public static String prettyPrintMemory(long size)
smiklosovic marked this conversation as resolved.
Show resolved Hide resolved
{
return prettyPrintMemory(size, "");
}

/**
* Convert the given size in bytes to a human-readable value using binary (i.e. 2^10-based) modifiers.
* For example, 1.000KiB, 2.100GiB etc., up to 8.000 EiB.
Expand Down
29 changes: 29 additions & 0 deletions test/unit/org/apache/cassandra/io/util/FileUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,35 @@ public void testParseFileSize() throws Exception
6621259022467L, FileUtils.parseFileSize("6.022 TiB"));
}

@Test
public void testStringifyFileSize() throws Exception
smiklosovic marked this conversation as resolved.
Show resolved Hide resolved
{
// test straightforward conversions for each unit
assertEquals("FileUtils.stringifyFileSize() failed to stringify a whole number of bytes",
"256 bytes", FileUtils.stringifyFileSize(256L));
assertEquals("FileUtils.stringifyFileSize() failed to stringify a whole number of kibibytes",
"2 KiB", FileUtils.stringifyFileSize(2048L));
assertEquals("FileUtils.stringifyFileSize() failed to stringify a whole number of mebibytes",
"4 MiB", FileUtils.stringifyFileSize(4194304L));
assertEquals("FileUtils.stringifyFileSize() failed to stringify a whole number of gibibytes",
"3 GiB", FileUtils.stringifyFileSize(3221225472L));
assertEquals("FileUtils.stringifyFileSize() failed to stringify a whole number of tebibytes",
"5 TiB", FileUtils.stringifyFileSize(5497558138880L));
// test conversions of fractional units
assertEquals("FileUtils.stringifyFileSize() failed to stringify a rational number of kibibytes",
"1.5 KiB", FileUtils.stringifyFileSize(1536L));
assertEquals("FileUtils.stringifyFileSize() failed to stringify a rational number of kibibytes",
"4.33 KiB", FileUtils.stringifyFileSize(4434L));
assertEquals("FileUtils.stringifyFileSize() failed to stringify a rational number of mebibytes",
"2.25 MiB", FileUtils.stringifyFileSize(2359296L));
assertEquals("FileUtils.stringifyFileSize() failed to stringify a rational number of mebibytes",
"3.14 MiB", FileUtils.stringifyFileSize(3292529L));
assertEquals("FileUtils.stringifyFileSize() failed to stringify a rational number of gibibytes",
"1.21 GiB", FileUtils.stringifyFileSize(1299227607L));
assertEquals("FileUtils.stringifyFileSize() failed to stringify a rational number of tebibytes",
"6.02 TiB", FileUtils.stringifyFileSize(6621259022467L));
}

@Test
public void testTruncate() throws IOException
{
Expand Down