josh-mckenzie commented on a change in pull request #1213:
URL: https://github.com/apache/cassandra/pull/1213#discussion_r731052645



##########
File path: test/unit/org/apache/cassandra/service/PartitionDenylistTest.java
##########
@@ -0,0 +1,469 @@
+/*
+ * 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.service;
+
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import org.apache.cassandra.config.DatabaseDescriptor;
+import org.apache.cassandra.cql3.CQLTester;
+import org.apache.cassandra.cql3.UntypedResultSet;
+import org.apache.cassandra.cql3.statements.schema.CreateTableStatement;
+import org.apache.cassandra.db.ConsistencyLevel;
+import org.apache.cassandra.exceptions.InvalidRequestException;
+import org.apache.cassandra.schema.KeyspaceMetadata;
+import org.apache.cassandra.schema.KeyspaceParams;
+import org.apache.cassandra.schema.Schema;
+import org.apache.cassandra.schema.Tables;
+
+import static org.apache.cassandra.cql3.QueryProcessor.process;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+public class PartitionDenylistTest
+{
+    private final static String ks_cql = "partition_denylist_keyspace";
+
+    @BeforeClass
+    public static void init()
+    {
+        CQLTester.prepareServer();
+
+        KeyspaceMetadata schema = KeyspaceMetadata.create(ks_cql,
+                                                          
KeyspaceParams.simple(1),
+                                                          Tables.of(
+            CreateTableStatement.parse("CREATE TABLE table1 ("
+                                       + "keyone text, "
+                                       + "keytwo text, "
+                                       + "qux text, "
+                                       + "quz text, "
+                                       + "foo text, "
+                                       + "PRIMARY KEY((keyone, keytwo), qux, 
quz) ) ", ks_cql).build(),
+            CreateTableStatement.parse("CREATE TABLE table2 ("
+                                       + "keyone text, "
+                                       + "keytwo text, "
+                                       + "keythree text, "
+                                       + "value text, "
+                                       + "PRIMARY KEY((keyone, keytwo), 
keythree) ) ", ks_cql).build(),
+            CreateTableStatement.parse("CREATE TABLE table3 ("
+                                       + "keyone text, "
+                                       + "keytwo text, "
+                                       + "keythree text, "
+                                       + "value text, "
+                                       + "PRIMARY KEY((keyone, keytwo), 
keythree) ) ", ks_cql).build()
+        ));
+        Schema.instance.load(schema);
+        DatabaseDescriptor.setEnablePartitionDenylist(true);
+        DatabaseDescriptor.setEnableDenylistRangeReads(true);
+        DatabaseDescriptor.setDenylistConsistencyLevel(ConsistencyLevel.ONE);
+        DatabaseDescriptor.setDenylistRefreshSeconds(1);
+        StorageService.instance.initServer(0);
+    }
+
+    @Before
+    public void setup()
+    {
+        DatabaseDescriptor.setEnablePartitionDenylist(true);
+        resetDenylist();
+
+        process("INSERT INTO " + ks_cql + ".table1 (keyone, keytwo, qux, quz, 
foo) VALUES ('aaa', 'bbb', 'ccc', 'ddd', 'v')", ConsistencyLevel.ONE);
+        process("INSERT INTO " + ks_cql + ".table1 (keyone, keytwo, qux, quz, 
foo) VALUES ('bbb', 'ccc', 'ccc', 'ddd', 'v')", ConsistencyLevel.ONE);
+        process("INSERT INTO " + ks_cql + ".table1 (keyone, keytwo, qux, quz, 
foo) VALUES ('ccc', 'ddd', 'ccc', 'ddd', 'v')", ConsistencyLevel.ONE);
+        process("INSERT INTO " + ks_cql + ".table1 (keyone, keytwo, qux, quz, 
foo) VALUES ('ddd', 'eee', 'ccc', 'ddd', 'v')", ConsistencyLevel.ONE);
+        process("INSERT INTO " + ks_cql + ".table1 (keyone, keytwo, qux, quz, 
foo) VALUES ('eee', 'fff', 'ccc', 'ddd', 'v')", ConsistencyLevel.ONE);
+        process("INSERT INTO " + ks_cql + ".table1 (keyone, keytwo, qux, quz, 
foo) VALUES ('fff', 'ggg', 'ccc', 'ddd', 'v')", ConsistencyLevel.ONE);
+        process("INSERT INTO " + ks_cql + ".table1 (keyone, keytwo, qux, quz, 
foo) VALUES ('ggg', 'hhh', 'ccc', 'ddd', 'v')", ConsistencyLevel.ONE);
+        process("INSERT INTO " + ks_cql + ".table1 (keyone, keytwo, qux, quz, 
foo) VALUES ('hhh', 'iii', 'ccc', 'ddd', 'v')", ConsistencyLevel.ONE);
+        process("INSERT INTO " + ks_cql + ".table1 (keyone, keytwo, qux, quz, 
foo) VALUES ('iii', 'jjj', 'ccc', 'ddd', 'v')", ConsistencyLevel.ONE);
+        process("INSERT INTO " + ks_cql + ".table1 (keyone, keytwo, qux, quz, 
foo) VALUES ('jjj', 'kkk', 'ccc', 'ddd', 'v')", ConsistencyLevel.ONE);
+
+
+        for (int i = 0; i < 50; i++)
+            process(String.format("INSERT INTO " + ks_cql + ".table2 (keyone, 
keytwo, keythree, value) VALUES ('%d', '%d', '%d', '%d')", i, i, i, i), 
ConsistencyLevel.ONE);
+
+        for (int i = 0; i < 50; i++)
+            process(String.format("INSERT INTO " + ks_cql + ".table3 (keyone, 
keytwo, keythree, value) VALUES ('%d', '%d', '%d', '%d')", i, i, i, i), 
ConsistencyLevel.ONE);
+
+        denylist("table1", "bbb:ccc");
+        refreshList();
+    }
+
+
+    private static void denylist(String table, final String key)
+    {
+        StorageProxy.instance.denylistKey(ks_cql, table, key);
+    }
+
+    private static void refreshList()
+    {
+        StorageProxy.instance.loadPartitionDenylist();
+    }
+
+    /**
+     * @return Whether the *attempt* to remove the denylisted key and refresh 
succeeded. Doesn't necessarily indicate the key
+     * was previously blocked and found.
+     */
+    private static boolean removeDenylist(final String ks, final String table, 
final String key)
+    {
+        return StorageProxy.instance.removeDenylistKey(ks, table, key);
+    }
+
+    @Test
+    public void testRead()
+    {
+        process("SELECT * FROM " + ks_cql + ".table1 WHERE keyone='aaa' and 
keytwo='bbb'", ConsistencyLevel.ONE);
+    }
+
+    @Test
+    public void testReadDenylisted()
+    {
+        assertThatThrownBy(() -> process("SELECT * FROM " + ks_cql + ".table1 
WHERE keyone='bbb' and keytwo='ccc'", ConsistencyLevel.ONE))
+                           .isInstanceOf(InvalidRequestException.class)
+                           .hasMessageContaining("Unable to read denylisted 
partition");
+    }
+
+    @Test
+    public void testReadUndenylisted()
+    {
+        resetDenylist();
+        process("SELECT * FROM " + ks_cql + ".table1 WHERE keyone='bbb' and 
keytwo='ccc'", ConsistencyLevel.ONE);
+    }
+
+    @Test
+    public void testWrite()
+    {
+        process("INSERT INTO " + ks_cql + ".table1 (keyone, keytwo, qux, quz, 
foo) VALUES ('eee', 'fff', 'ccc', 'ddd', 'v')", ConsistencyLevel.ONE);
+        process("DELETE FROM " + ks_cql + ".table1 WHERE keyone='eee' and 
keytwo='fff'", ConsistencyLevel.ONE);
+    }
+
+    @Test
+    public void testWriteDenylisted()
+    {
+        assertThatThrownBy(() -> process("INSERT INTO " + ks_cql + ".table1 
(keyone, keytwo, qux, quz, foo) VALUES ('bbb', 'ccc', 'eee', 'fff', 'w')", 
ConsistencyLevel.ONE))
+                           .isInstanceOf(InvalidRequestException.class)
+                           .hasMessageContaining("Unable to write to 
denylisted partition");
+    }
+
+    @Test
+    public void testCASWriteDenylisted()
+    {
+        assertThatThrownBy(() -> process("UPDATE " + ks_cql + ".table1 SET 
foo='w' WHERE keyone='bbb' AND keytwo='ccc' AND qux='eee' AND quz='fff' IF 
foo='v'", ConsistencyLevel.LOCAL_SERIAL))
+                           .isInstanceOf(InvalidRequestException.class)
+                           .hasMessageContaining("Unable to CAS write to 
denylisted partition");
+    }
+
+    @Test
+    public void testWriteUndenylisted()
+    {
+        resetDenylist();
+        process("INSERT INTO " + ks_cql + ".table1 (keyone, keytwo, qux, quz, 
foo) VALUES ('bbb', 'ccc', 'eee', 'fff', 'w')", ConsistencyLevel.ONE);
+    }
+
+    @Test
+    public void testRangeSlice()
+    {
+        UntypedResultSet rows;
+        rows = process("SELECT * FROM " + ks_cql + ".table1 WHERE 
token(keyone, keytwo) < token('bbb', 'ccc')", ConsistencyLevel.ONE);
+        Assert.assertEquals(1, rows.size());
+
+        // 10 entries total in our table
+        rows = process("SELECT * FROM " + ks_cql + ".table1 WHERE 
token(keyone, keytwo) > token('bbb', 'ccc')", ConsistencyLevel.ONE);
+        Assert.assertEquals(8, rows.size());
+
+        rows = process("SELECT * FROM " + ks_cql + ".table1 WHERE 
token(keyone, keytwo) >= token('aaa', 'bbb') and token(keyone, keytwo) < 
token('bbb', 'ccc')", ConsistencyLevel.ONE);
+        Assert.assertEquals(1, rows.size());
+
+        rows = process("SELECT * FROM " + ks_cql + ".table1 WHERE 
token(keyone, keytwo) > token('bbb', 'ccc') and token(keyone, keytwo) <= 
token('ddd', 'eee')", ConsistencyLevel.ONE);
+        Assert.assertEquals(2, rows.size());
+    }
+
+    @Test
+    public void testRangeDenylisted()
+    {
+        assertThatThrownBy(() -> process("SELECT * FROM " + ks_cql + 
".table1", ConsistencyLevel.ONE))
+                           .isInstanceOf(InvalidRequestException.class)
+                           .hasMessageContaining("Unable to read range [, ) 
containing 1 denylisted keys");

Review comment:
       I think the primary struggle is that in StorageProxy.getRangeSlice, we 
have the PartitionRangeReadCommand and not much else to determine the original 
root query that led to the range query we're attempting. So throwing at this 
level of abstraction, we know it's a range query that has a denied key inside 
it.
   
   Not super friendly to end users, I agree, but the "containing %d deny listed 
keys in %s/%s" should be enough for end users to be able to figure out what 
happened pretty cleanly.




-- 
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]

Reply via email to