maedhroz commented on code in PR #2961: URL: https://github.com/apache/cassandra/pull/2961#discussion_r1420808852
########## test/distributed/org/apache/cassandra/distributed/test/guardrails/GuardrailNonPartitionKeyRestrictedQueriesTest.java: ########## @@ -0,0 +1,202 @@ +/* + * 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.distributed.test.guardrails; + +import java.io.IOException; +import java.util.concurrent.TimeUnit; + +import com.google.common.util.concurrent.Uninterruptibles; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import org.apache.cassandra.db.ColumnFamilyStore; +import org.apache.cassandra.db.Keyspace; +import org.apache.cassandra.db.guardrails.Guardrails; +import org.apache.cassandra.distributed.Cluster; +import org.apache.cassandra.distributed.api.ConsistencyLevel; +import org.apache.cassandra.distributed.api.IIsolatedExecutor; + +import static org.junit.Assert.assertEquals; + +public class GuardrailNonPartitionKeyRestrictedQueriesTest extends GuardrailTester +{ + private static Cluster cluster; + + @BeforeClass + public static void setupCluster() throws IOException + { + cluster = init(Cluster.build(3) + .withConfig(c -> c.set("default_secondary_index", "sai")) + .withDataDirCount(1) + .start()); + } + + @AfterClass + public static void teardownCluster() + { + if (cluster != null) + cluster.close(); + } + + @Before + public void beforeTest() + { + super.beforeTest(); + prepareSchema(); + } + + @Override + protected Cluster getCluster() + { + return cluster; + } + + @Test + public void testWarnThreshold() + { + enableGuardrail(); + setThresholds(2, 5); + + // flushing just 1 table will not trigger any threshold + final long valueToQuery = createSSTables(1); + assertNumberOfSSTables(1); + + assertLogsOnAction(() -> execute("SELECT * from %s WHERE v1 = " + valueToQuery), false, false); + + // create 3 more SSTables on each node, that means each node will have in total 4 SSTables (one from previous run) + // this will trigger warn threshold + long valueToQuery2 = createSSTables(3); + assertNumberOfSSTables(4); + + // we violated warn threshold of 2 so all nodes will warn + assertLogsOnAction(() -> execute("SELECT * from %s WHERE v1 = " + valueToQuery2), true, false, 1); + } + + private void enableGuardrail() + { + cluster.forEach(instance -> instance.runOnInstance((IIsolatedExecutor.SerializableRunnable) () -> Guardrails.instance.setNonPartitionRestrictedQueryEnabled(true))); + assertGuardrailState(true); + } + + private void disableGuardrail() + { + cluster.forEach(instance -> instance.runOnInstance((IIsolatedExecutor.SerializableRunnable) () -> Guardrails.instance.setNonPartitionRestrictedQueryEnabled(false))); + assertGuardrailState(false); + } + + private void prepareSchema() + { + schemaChange("CREATE TABLE %s (k bigint, c bigint, v1 bigint, v2 bigint, PRIMARY KEY (k, c)) " + + "WITH compaction = {'class': 'org.apache.cassandra.db.compaction.LeveledCompactionStrategy'};"); + + schemaChange("CREATE CUSTOM INDEX v1_sai_idx ON %s (v1)"); + schemaChange("CREATE CUSTOM INDEX v2_sai_idx ON %s (v2)"); + + for (int i = 1; i < 4; i++) + { + cluster.get(i).acceptsOnInstance((IIsolatedExecutor.SerializableConsumer<String>) (t) -> { + for (ColumnFamilyStore cs : Keyspace.open("distributed_test_keyspace").getColumnFamilyStores()) + { + if (cs.name.equals(t)) + { + cs.disableAutoCompaction(); + } + } + }).accept(tableName); + } + + for (int i = 1; i < 4; i++) + { + Assert.assertTrue(cluster.get(i).applyOnInstance((IIsolatedExecutor.SerializableFunction<String, Boolean>) (t) -> + { + for (ColumnFamilyStore cs : Keyspace.open("distributed_test_keyspace").getColumnFamilyStores()) + { + if (cs.name.equals(t)) + { + return cs.isAutoCompactionDisabled(); + } + } + + return false; + }, tableName)); + } + } + + private long createSSTables(int numberOfSSTables, int... nodesToFlush) + { + Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS); + + long k = System.currentTimeMillis(); + long value = k + 10; + for (int i = 0; i < numberOfSSTables; i++) + { + // this will be replicated to each node + execute("INSERT INTO %s (k, c, v1, v2) VALUES (?, ?, ?, ?)", k, k, value, value + 10); + + if (nodesToFlush.length == 0) + cluster.forEach((instance) -> instance.flush(KEYSPACE)); + else + cluster.get(nodesToFlush).forEach((instance) -> instance.flush(KEYSPACE)); + } + + return value; + } + + private void execute(String query, Object... args) + { + cluster.coordinator(1).execute(format(query), ConsistencyLevel.ALL, args); Review Comment: While I don't think it's harmful to look at the logging side effects when these trigger, that can be brittle, and it looks like `assertLogsOnAction()` is only checking generally for ERROR or WARN, not with any qualifiers that relate to the guardrail? Either way, the basic assertion we need to make in the warning case is on the warnings returned to the client. In the lines above, you can access them this way: ``` SimpleQueryResult result = cluster.coordinator(1).executeWithResult(format(query), ConsistencyLevel.ALL, result.warnings(); // assert on the contents of this ``` In other words, I'm fine w/ keeping the logs checking and tying the assertions directly to specific messages for the features we're testing, but it might make more sense to focus on the actual client warnings and errors. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]

