Skip to content

Commit

Permalink
Make nodetool compactionstats and sstable_tasks consistent
Browse files Browse the repository at this point in the history
patch by Aleksei Zotov; reviewed by Stefan Miklocovic and Brandon Williams for CASSANDRA-16976
  • Loading branch information
azotcsit committed Oct 14, 2021
1 parent 2ce9b13 commit 460ae34
Show file tree
Hide file tree
Showing 6 changed files with 287 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
4.1
* Make nodetool compactionstats and sstable_tasks consistent (CASSANDRA-16976)
* Add metrics and logging around index summary redistribution (CASSANDRA-17036)
* Add configuration options for minimum allowable replication factor and default replication factor (CASSANDRA-14557)
* Expose information about stored hints via a nodetool command and a virtual table (CASSANDRA-14795)
Expand Down
9 changes: 4 additions & 5 deletions doc/source/new/virtualtables.rst
Original file line number Diff line number Diff line change
Expand Up @@ -294,11 +294,10 @@ The ``sstable_tasks`` could be used to get information about running tasks. It l

::

cqlsh:system_views> SELECT * FROM sstable_tasks;
keyspace_name | table_name | task_id | kind | progress | total | unit
---------------+------------+--------------------------------------+------------+----------+----------+-------
basic | wide2 | c3909740-cdf7-11e9-a8ed-0f03de2d9ae1 | compaction | 60418761 | 70882110 | bytes
basic | wide2 | c7556770-cdf7-11e9-a8ed-0f03de2d9ae1 | compaction | 2995623 | 40314679 | bytes
cqlsh:system_views> SELECT * FROM sstable_tasks;
keyspace_name | table_name | task_id | completion_ratio | kind | progress | sstables | total | unit
---------------+------------+--------------------------------------+------------------+------------+----------+----------+----------+-------
keyspace1 | standard1 | 238f6290-1fd4-11ec-8c63-7d65848b040f | 0.212345 | compaction | 16184734 | 2 | 76219177 | bytes


As another example, to find how much time is remaining for SSTable tasks, use the following query:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.cassandra.db.compaction.CompactionInfo;
import org.apache.cassandra.db.compaction.CompactionManager;
import org.apache.cassandra.db.marshal.DoubleType;
import org.apache.cassandra.db.marshal.Int32Type;
import org.apache.cassandra.db.marshal.LongType;
import org.apache.cassandra.db.marshal.UTF8Type;
import org.apache.cassandra.db.marshal.UUIDType;
Expand All @@ -34,6 +35,7 @@ final class SSTableTasksTable extends AbstractVirtualTable
private final static String COMPLETION_RATIO = "completion_ratio";
private final static String KIND = "kind";
private final static String PROGRESS = "progress";
private final static String SSTABLES = "sstables";
private final static String TOTAL = "total";
private final static String UNIT = "unit";

Expand All @@ -49,6 +51,7 @@ final class SSTableTasksTable extends AbstractVirtualTable
.addRegularColumn(COMPLETION_RATIO, DoubleType.instance)
.addRegularColumn(KIND, UTF8Type.instance)
.addRegularColumn(PROGRESS, LongType.instance)
.addRegularColumn(SSTABLES, Int32Type.instance)
.addRegularColumn(TOTAL, LongType.instance)
.addRegularColumn(UNIT, UTF8Type.instance)
.build());
Expand All @@ -71,6 +74,7 @@ public DataSet data()
.column(COMPLETION_RATIO, completionRatio)
.column(KIND, task.getTaskType().toString().toLowerCase())
.column(PROGRESS, completed)
.column(SSTABLES, task.getSSTables().size())
.column(TOTAL, total)
.column(UNIT, task.getUnit().toString().toLowerCase());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public static void reportCompactionTable(List<Map<String,String>> compactions, i
long remainingBytes = 0;
TableBuilder table = new TableBuilder();

table.add("id", "compaction type", "keyspace", "table", "sstables", "completed", "total", "unit", "progress");
table.add("keyspace", "table", "task id", "completion ratio", "kind", "progress", "sstables", "total", "unit");
for (Map<String, String> c : compactions)
{
long total = Long.parseLong(c.get(CompactionInfo.TOTAL));
Expand All @@ -91,11 +91,11 @@ public static void reportCompactionTable(List<Map<String,String>> compactions, i
String unit = c.get(CompactionInfo.UNIT);
boolean toFileSize = humanReadable && Unit.isFileSize(unit);
String[] tables = c.get(CompactionInfo.SSTABLES).split(",");
String completedStr = toFileSize ? FileUtils.stringifyFileSize(completed) : Long.toString(completed);
String progressStr = toFileSize ? FileUtils.stringifyFileSize(completed) : Long.toString(completed);
String totalStr = toFileSize ? FileUtils.stringifyFileSize(total) : Long.toString(total);
String percentComplete = total == 0 ? "n/a" : new DecimalFormat("0.00").format((double) completed / total * 100) + "%";
String id = c.get(CompactionInfo.COMPACTION_ID);
table.add(id, taskType, keyspace, columnFamily, String.valueOf(tables.length), completedStr, totalStr, unit, percentComplete);
table.add(keyspace, columnFamily, id, percentComplete, taskType, progressStr, String.valueOf(tables.length), totalStr, unit);
remainingBytes += total - completed;
}
table.printTo(out);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.cassandra.db.virtual;

import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import com.google.common.collect.ImmutableList;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import org.apache.cassandra.cql3.CQLTester;
import org.apache.cassandra.cql3.UntypedResultSet;
import org.apache.cassandra.db.ColumnFamilyStore;
import org.apache.cassandra.db.compaction.CompactionInfo;
import org.apache.cassandra.db.compaction.CompactionManager;
import org.apache.cassandra.db.compaction.OperationType;
import org.apache.cassandra.io.sstable.format.SSTableReader;
import org.apache.cassandra.schema.MockSchema;

public class SSTableTasksTableTest extends CQLTester
{
private static final String KS_NAME = "vts";

@SuppressWarnings("FieldCanBeLocal")
private SSTableTasksTable table;

@BeforeClass
public static void setUpClass()
{
CQLTester.setUpClass();
CompactionManager.instance.disableAutoCompaction();
}

@Before
public void config()
{
table = new SSTableTasksTable(KS_NAME);
VirtualKeyspaceRegistry.instance.register(new VirtualKeyspace(KS_NAME, ImmutableList.of(table)));
}

@Test
public void testSelectAll() throws Throwable
{
createTable("CREATE TABLE %s (pk int, ck int, PRIMARY KEY (pk, ck))");
ColumnFamilyStore cfs = getCurrentColumnFamilyStore();

long bytesCompacted = 123;
long bytesTotal = 123456;
UUID compactionId = UUID.randomUUID();
List<SSTableReader> sstables = IntStream.range(0, 10)
.mapToObj(i -> MockSchema.sstable(i, i * 10L, i * 10L + 9, cfs))
.collect(Collectors.toList());
CompactionInfo.Holder compactionHolder = new CompactionInfo.Holder()
{
public CompactionInfo getCompactionInfo()
{
return new CompactionInfo(cfs.metadata(), OperationType.COMPACTION, bytesCompacted, bytesTotal, compactionId, sstables);
}

public boolean isGlobal()
{
return false;
}
};

CompactionManager.instance.active.beginCompaction(compactionHolder);
UntypedResultSet result = execute("SELECT * FROM vts.sstable_tasks");
assertRows(result, row(CQLTester.KEYSPACE, currentTable(), compactionId, 1.0 * bytesCompacted / bytesTotal,
OperationType.COMPACTION.toString().toLowerCase(), bytesCompacted, sstables.size(),
bytesTotal, CompactionInfo.Unit.BYTES.toString()));

CompactionManager.instance.active.finishCompaction(compactionHolder);
result = execute("SELECT * FROM vts.sstable_tasks");
assertEmpty(result);
}
}
179 changes: 179 additions & 0 deletions test/unit/org/apache/cassandra/tools/nodetool/CompactionStatsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.cassandra.tools.nodetool;

import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import org.junit.BeforeClass;
import org.junit.Test;

import org.apache.cassandra.cql3.CQLTester;
import org.apache.cassandra.db.ColumnFamilyStore;
import org.apache.cassandra.db.compaction.CompactionInfo;
import org.apache.cassandra.db.compaction.CompactionManager;
import org.apache.cassandra.db.compaction.OperationType;
import org.apache.cassandra.io.sstable.format.SSTableReader;
import org.apache.cassandra.schema.MockSchema;
import org.apache.cassandra.tools.ToolRunner;
import org.assertj.core.api.Assertions;

import static org.assertj.core.api.Assertions.assertThat;

public class CompactionStatsTest extends CQLTester
{
@BeforeClass
public static void setup() throws Exception
{
requireNetwork();
startJMXServer();
}

@Test
@SuppressWarnings("SingleCharacterStringConcatenation")
public void testMaybeChangeDocs()
{
// If you added, modified options or help, please update docs if necessary
ToolRunner.ToolResult tool = ToolRunner.invokeNodetool("help", "compactionstats");
tool.assertOnCleanExit();

String help = "NAME\n" +
" nodetool compactionstats - Print statistics on compactions\n" +
"\n" +
"SYNOPSIS\n" +
" nodetool [(-h <host> | --host <host>)] [(-p <port> | --port <port>)]\n" +
" [(-pp | --print-port)] [(-pw <password> | --password <password>)]\n" +
" [(-pwf <passwordFilePath> | --password-file <passwordFilePath>)]\n" +
" [(-u <username> | --username <username>)] compactionstats\n" +
" [(-H | --human-readable)]\n" +
"\n" +
"OPTIONS\n" +
" -h <host>, --host <host>\n" +
" Node hostname or ip address\n" +
"\n" +
" -H, --human-readable\n" +
" Display bytes in human readable form, i.e. KiB, MiB, GiB, TiB\n" +
"\n" +
" -p <port>, --port <port>\n" +
" Remote jmx agent port number\n" +
"\n" +
" -pp, --print-port\n" +
" Operate in 4.0 mode with hosts disambiguated by port number\n" +
"\n" +
" -pw <password>, --password <password>\n" +
" Remote jmx agent password\n" +
"\n" +
" -pwf <passwordFilePath>, --password-file <passwordFilePath>\n" +
" Path to the JMX password file\n" +
"\n" +
" -u <username>, --username <username>\n" +
" Remote jmx agent username\n" +
"\n" +
"\n";
assertThat(tool.getStdout()).isEqualTo(help);
}

@Test
public void testCompactionStats()
{
createTable("CREATE TABLE %s (pk int, ck int, PRIMARY KEY (pk, ck))");
ColumnFamilyStore cfs = getCurrentColumnFamilyStore();

long bytesCompacted = 123;
long bytesTotal = 123456;
UUID compactionId = UUID.randomUUID();
List<SSTableReader> sstables = IntStream.range(0, 10)
.mapToObj(i -> MockSchema.sstable(i, i * 10L, i * 10L + 9, cfs))
.collect(Collectors.toList());
CompactionInfo.Holder compactionHolder = new CompactionInfo.Holder()
{
public CompactionInfo getCompactionInfo()
{
return new CompactionInfo(cfs.metadata(), OperationType.COMPACTION, bytesCompacted, bytesTotal, compactionId, sstables);
}

public boolean isGlobal()
{
return false;
}
};

CompactionManager.instance.active.beginCompaction(compactionHolder);
ToolRunner.ToolResult tool = ToolRunner.invokeNodetool("compactionstats");
tool.assertOnCleanExit();
String stdout = tool.getStdout();
assertThat(stdout).contains("pending tasks: 1");
Assertions.assertThat(stdout).containsPattern("keyspace\\s+table\\s+task id\\s+completion ratio\\s+kind\\s+progress\\s+sstables\\s+total\\s+unit");
String expectedStatsPattern = String.format("%s\\s+%s\\s+%s\\s+%.2f%%\\s+%s\\s+%s\\s+%s\\s+%s\\s+%s",
CQLTester.KEYSPACE, currentTable(), compactionId, (double) bytesCompacted / bytesTotal * 100,
OperationType.COMPACTION, bytesCompacted, sstables.size(), bytesTotal, CompactionInfo.Unit.BYTES);
Assertions.assertThat(stdout).containsPattern(expectedStatsPattern);

CompactionManager.instance.active.finishCompaction(compactionHolder);
tool = ToolRunner.invokeNodetool("compactionstats");
tool.assertOnCleanExit();
stdout = tool.getStdout();
assertThat(stdout).contains("pending tasks: 0");
}

@Test
public void testCompactionStatsHumanReadable()
{
createTable("CREATE TABLE %s (pk int, ck int, PRIMARY KEY (pk, ck))");
ColumnFamilyStore cfs = getCurrentColumnFamilyStore();

long bytesCompacted = 123;
long bytesTotal = 123456;
UUID compactionId = UUID.randomUUID();
List<SSTableReader> sstables = IntStream.range(0, 10)
.mapToObj(i -> MockSchema.sstable(i, i * 10L, i * 10L + 9, cfs))
.collect(Collectors.toList());
CompactionInfo.Holder compactionHolder = new CompactionInfo.Holder()
{
public CompactionInfo getCompactionInfo()
{
return new CompactionInfo(cfs.metadata(), OperationType.COMPACTION, bytesCompacted, bytesTotal, compactionId, sstables);
}

public boolean isGlobal()
{
return false;
}
};

CompactionManager.instance.active.beginCompaction(compactionHolder);
ToolRunner.ToolResult tool = ToolRunner.invokeNodetool("compactionstats", "--human-readable");
tool.assertOnCleanExit();
String stdout = tool.getStdout();
assertThat(stdout).contains("pending tasks: 1");
Assertions.assertThat(stdout).containsPattern("keyspace\\s+table\\s+task id\\s+completion ratio\\s+kind\\s+progress\\s+sstables\\s+total\\s+unit");
String expectedStatsPattern = String.format("%s\\s+%s\\s+%s\\s+%.2f%%\\s+%s\\s+%s\\s+%s\\s+%s\\s+%s",
CQLTester.KEYSPACE, currentTable(), compactionId, (double) bytesCompacted / bytesTotal * 100,
OperationType.COMPACTION, "123 bytes", sstables.size(), "120.56 KiB", CompactionInfo.Unit.BYTES);
Assertions.assertThat(stdout).containsPattern(expectedStatsPattern);

CompactionManager.instance.active.finishCompaction(compactionHolder);
tool = ToolRunner.invokeNodetool("compactionstats", "--human-readable");
tool.assertOnCleanExit();
stdout = tool.getStdout();
assertThat(stdout).contains("pending tasks: 0");
}
}

0 comments on commit 460ae34

Please sign in to comment.