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-17225 Add batch_metrics virtual table #1372

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -21,6 +21,7 @@
import com.codahale.metrics.Snapshot;
import org.apache.cassandra.cql3.statements.BatchStatement;
import org.apache.cassandra.db.marshal.DoubleType;
import org.apache.cassandra.db.marshal.LongType;
import org.apache.cassandra.db.marshal.UTF8Type;
import org.apache.cassandra.dht.LocalPartitioner;
import org.apache.cassandra.metrics.BatchMetrics;
Expand All @@ -46,7 +47,7 @@ public class BatchMetricsTable extends AbstractVirtualTable
.addRegularColumn(P50, DoubleType.instance)
.addRegularColumn(P99, DoubleType.instance)
.addRegularColumn(P999, DoubleType.instance)
.addRegularColumn(MAX, DoubleType.instance)
.addRegularColumn(MAX, LongType.instance)
.build());
}

Expand Down
Expand Up @@ -25,7 +25,6 @@
import org.junit.BeforeClass;
import org.junit.Test;

import com.codahale.metrics.Histogram;
import com.codahale.metrics.Snapshot;
import com.datastax.driver.core.ResultSet;
import org.apache.cassandra.cql3.CQLTester;
Expand All @@ -38,7 +37,6 @@
public class BatchMetricsTableTest extends CQLTester
{
private static final String KS_NAME = "vts";
private BatchMetricsTable table;

@BeforeClass
public static void setUpClass()
Expand All @@ -49,42 +47,45 @@ public static void setUpClass()
@Before
public void config()
{
table = new BatchMetricsTable(KS_NAME);
BatchMetricsTable table = new BatchMetricsTable(KS_NAME);
VirtualKeyspaceRegistry.instance.register(new VirtualKeyspace(KS_NAME, ImmutableList.of(table)));
}

@Test
void testSelectAll() throws Throwable
public void testSelectAll() throws Throwable
{
String ks = createKeyspace("CREATE KEYSPACE %s WITH replication={ 'class' : 'SimpleStrategy', 'replication_factor' : 1 }");
String tbl = createTable(ks, "CREATE TABLE %s (id int primary key, val int)");
BatchMetrics metrics = BatchStatement.metrics;

String batch = format("BEGIN BATCH " +
"INSERT INTO %s.%s (id, val) VALUES (0, 0) USING TTL %d; " +
"INSERT INTO %s.%s (id, val) VALUES (1, 1) USING TTL %d; " +
"APPLY BATCH;",
ks, tbl, 1,
ks, tbl, 1);
execute(batch);
for (int i = 0; i < 10; i++)
{
metrics.partitionsPerLoggedBatch.update(i);
metrics.partitionsPerUnloggedBatch.update(i + 10);
metrics.partitionsPerCounterBatch.update(i * 10);
}

BatchMetrics metrics = BatchStatement.metrics;
ResultSet result = executeNet("SELECT * FROM vts.batch_metrics");
ResultSet result = executeNet(format("SELECT * FROM %s.batch_metrics", KS_NAME));
assertEquals(5, result.getColumnDefinitions().size());
AtomicInteger rowCount = new AtomicInteger(0);
result.forEach(r -> {
Snapshot snapshot;
if(r.getString("name").equals("partitions_per_logged_batch")) {
snapshot = metrics.partitionsPerLoggedBatch.getSnapshot();
} else if(r.getString("name").equals("partitions_per_unlogged_batch")) {
snapshot = metrics.partitionsPerUnloggedBatch.getSnapshot();
} else {
snapshot = metrics.partitionsPerCounterBatch.getSnapshot();
}
assertEquals(r.getDouble("p50"), snapshot.getMedian(), 1.0);
assertEquals(r.getDouble("p99"), snapshot.get99thPercentile(), 1.0);
Snapshot snapshot = getExpectedHistogramSnapshot(metrics, r.getString("name"));
assertEquals(snapshot.getMedian(), r.getDouble("p50th"), 0.0);
azotcsit marked this conversation as resolved.
Show resolved Hide resolved
assertEquals(snapshot.get99thPercentile(), r.getDouble("p99th"), 0.0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we have p999th and max here tested as well?

rowCount.addAndGet(1);
});

assertEquals(3, rowCount.get());
}

private Snapshot getExpectedHistogramSnapshot(BatchMetrics metrics, String tableName)
{
if ("partitions_per_logged_batch".equals(tableName))
{
return metrics.partitionsPerLoggedBatch.getSnapshot();
}
else if ("partitions_per_unlogged_batch".equals(tableName))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would probably skip the else :-) Just it reads better something like that:

        if ("partitions_per_logged_batch".equals(tableName))
            return metrics.partitionsPerLoggedBatch.getSnapshot();
        
        
        if ("partitions_per_unlogged_batch".equals(tableName))
            return metrics.partitionsPerUnloggedBatch.getSnapshot();

        return metrics.partitionsPerCounterBatch.getSnapshot();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see both approaches used in the code base, I'm personally on the "else" camp usually - to me it makes it easier to follow the flow, with the added bonus of being able to hide the whole conditional flow in the editor with a single click (does not matter in this case, but in general).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also prefer avoiding any unecessary code as it can only create confusion and for simple condition braces are normally skipped.

{
return metrics.partitionsPerUnloggedBatch.getSnapshot();
}
return metrics.partitionsPerCounterBatch.getSnapshot();
}
}