azotcsit commented on a change in pull request #1213:
URL: https://github.com/apache/cassandra/pull/1213#discussion_r731791589
##########
File path: src/java/org/apache/cassandra/service/StorageProxy.java
##########
@@ -2070,6 +2122,22 @@ public static PartitionIterator
getRangeSlice(PartitionRangeReadCommand command,
ConsistencyLevel
consistencyLevel,
long queryStartNanoTime)
{
+ if (DatabaseDescriptor.getEnablePartitionDenylist() &&
DatabaseDescriptor.getEnableDenylistRangeReads())
+ {
+ final int denylisted =
partitionDenylist.getDeniedKeysInRangeCount(command.metadata().id,
command.dataRange().keyRange());
+ if (denylisted > 0)
+ {
+ denylistMetrics.incrementRangeReadsRejected();
+ AbstractBounds<PartitionPosition> keyRange =
command.dataRange().keyRange();
+ Token.TokenFactory tokenFactory =
StorageService.instance.getTokenMetadata().partitioner.getTokenFactory();
+ throw new InvalidRequestException(String.format("Unable to
read range %c%s, %s%c containing %d denylisted keys in %s/%s",
Review comment:
I checked the code closer and I think `command.loggableTokens()` might
be a good fit here. Basically it has exactly the same logic as you do manually.
##########
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:
Makes sense :+1:
##########
File path: src/java/org/apache/cassandra/service/StorageProxy.java
##########
@@ -1054,6 +1074,25 @@ public static void mutateWithTriggers(List<? extends
IMutation> mutations,
long queryStartNanoTime)
throws WriteTimeoutException, WriteFailureException, UnavailableException,
OverloadedException, InvalidRequestException
{
+ if (DatabaseDescriptor.getEnablePartitionDenylist() &&
DatabaseDescriptor.getEnableDenylistWrites())
+ {
+ for (final IMutation mutation : mutations)
+ {
+ for (final TableId tid : mutation.getTableIds())
+ {
+ if (!partitionDenylist.isKeyPermitted(tid,
mutation.key().getKey()))
+ {
+ denylistMetrics.incrementWritesRejected();
+ // While Schema.instance.getTableMetadata() can return
a null value, in this case the isKeyPermitted
+ // call above ensures that we cannot have a null
associated tid at this point.
+ final TableMetadata tmd =
Schema.instance.getTableMetadata(tid);
Review comment:
This variable is not used anymore.
I guess you might introduce it in order to print proper ks/table name in the
error message on the next line, but forgot to do it. At the moment. that
message looks incorrect though ("unknown Table") because the table is known in
fact. For an unknown table we would not be here.
--
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]