public void execute()

in src/java/org/apache/cassandra/tools/nodetool/TableHistograms.java [50:177]


    public void execute(NodeProbe probe)
    {
        PrintStream out = probe.output().out;
        Multimap<String, String> tablesList = HashMultimap.create();

        // a <keyspace, set<table>> mapping for verification or as reference if none provided
        Multimap<String, String> allTables = HashMultimap.create();
        Iterator<Map.Entry<String, ColumnFamilyStoreMBean>> tableMBeans = probe.getColumnFamilyStoreMBeanProxies();
        while (tableMBeans.hasNext())
        {
            Map.Entry<String, ColumnFamilyStoreMBean> entry = tableMBeans.next();
            allTables.put(entry.getKey(), entry.getValue().getTableName());
        }

        if (args.size() == 2 && args.stream().noneMatch(arg -> arg.contains(".")))
        {
            tablesList.put(args.get(0), args.get(1));
        }
        else if (args.size() == 1)
        {
            Pair<String, String> ksTbPair = parseTheKsTbPair(args.get(0));
            tablesList.put(ksTbPair.left, ksTbPair.right);
        }
        else if (args.size() == 0)
        {
            // use all tables
            tablesList = allTables;
        }
        else
        {
            throw new IllegalArgumentException("tablehistograms requires <keyspace> <table> or <keyspace.table> format argument.");
        }

        // verify that all tables to list exist
        for (String keyspace : tablesList.keys())
        {
            for (String table : tablesList.get(keyspace))
            {
                if (!allTables.containsEntry(keyspace, table))
                    throw new IllegalArgumentException("Unknown table " + keyspace + '.' + table);
            }
        }

        for (String keyspace : tablesList.keys().elementSet())
        {
            for (String table : tablesList.get(keyspace))
            {
                // calculate percentile of row size and column count
                long[] estimatedPartitionSize = (long[]) probe.getColumnFamilyMetric(keyspace, table, "EstimatedPartitionSizeHistogram");
                long[] estimatedColumnCount = (long[]) probe.getColumnFamilyMetric(keyspace, table, "EstimatedColumnCountHistogram");

                // build arrays to store percentile values
                double[] estimatedRowSizePercentiles = new double[7];
                double[] estimatedColumnCountPercentiles = new double[7];
                double[] offsetPercentiles = new double[]{0.5, 0.75, 0.95, 0.98, 0.99};

                if (ArrayUtils.isEmpty(estimatedPartitionSize) || ArrayUtils.isEmpty(estimatedColumnCount))
                {
                    out.println("No SSTables exists, unable to calculate 'Partition Size' and 'Cell Count' percentiles");

                    for (int i = 0; i < 7; i++)
                    {
                        estimatedRowSizePercentiles[i] = Double.NaN;
                        estimatedColumnCountPercentiles[i] = Double.NaN;
                    }
                }
                else
                {
                    EstimatedHistogram partitionSizeHist = new EstimatedHistogram(estimatedPartitionSize);
                    EstimatedHistogram columnCountHist = new EstimatedHistogram(estimatedColumnCount);

                    if (partitionSizeHist.isOverflowed())
                    {
                        out.println(String.format("Row sizes are larger than %s, unable to calculate percentiles", partitionSizeHist.getLargestBucketOffset()));
                        for (int i = 0; i < offsetPercentiles.length; i++)
                            estimatedRowSizePercentiles[i] = Double.NaN;
                    }
                    else
                    {
                        for (int i = 0; i < offsetPercentiles.length; i++)
                            estimatedRowSizePercentiles[i] = partitionSizeHist.percentile(offsetPercentiles[i]);
                    }

                    if (columnCountHist.isOverflowed())
                    {
                        out.println(String.format("Column counts are larger than %s, unable to calculate percentiles", columnCountHist.getLargestBucketOffset()));
                        for (int i = 0; i < estimatedColumnCountPercentiles.length; i++)
                            estimatedColumnCountPercentiles[i] = Double.NaN;
                    }
                    else
                    {
                        for (int i = 0; i < offsetPercentiles.length; i++)
                            estimatedColumnCountPercentiles[i] = columnCountHist.percentile(offsetPercentiles[i]);
                    }

                    // min value
                    estimatedRowSizePercentiles[5] = partitionSizeHist.min();
                    estimatedColumnCountPercentiles[5] = columnCountHist.min();
                    // max value
                    estimatedRowSizePercentiles[6] = partitionSizeHist.max();
                    estimatedColumnCountPercentiles[6] = columnCountHist.max();
                }

                String[] percentiles = new String[]{"50%", "75%", "95%", "98%", "99%", "Min", "Max"};
                Double[] readLatency = probe.metricPercentilesAsArray((CassandraMetricsRegistry.JmxTimerMBean) probe.getColumnFamilyMetric(keyspace, table, "ReadLatency"));
                Double[] writeLatency = probe.metricPercentilesAsArray((CassandraMetricsRegistry.JmxTimerMBean) probe.getColumnFamilyMetric(keyspace, table, "WriteLatency"));
                Double[] sstablesPerRead = probe.metricPercentilesAsArray((CassandraMetricsRegistry.JmxHistogramMBean) probe.getColumnFamilyMetric(keyspace, table, "SSTablesPerReadHistogram"));

                out.println(format("%s/%s histograms", keyspace, table));
                out.println(format("%-10s%18s%18s%18s%18s%18s",
                        "Percentile", "Read Latency", "Write Latency", "SSTables", "Partition Size", "Cell Count"));
                out.println(format("%-10s%18s%18s%18s%18s%18s",
                        "", "(micros)", "(micros)", "", "(bytes)", ""));

                for (int i = 0; i < percentiles.length; i++)
                {
                    out.println(format("%-10s%18.2f%18.2f%18.2f%18.0f%18.0f",
                            percentiles[i],
                            readLatency[i],
                            writeLatency[i],
                            sstablesPerRead[i],
                            estimatedRowSizePercentiles[i],
                            estimatedColumnCountPercentiles[i]));
                }
                out.println();
            }
        }
    }