You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cassandra.apache.org by ad...@apache.org on 2022/05/06 20:08:59 UTC

[cassandra] branch trunk updated: Rename DisableFlag class to EnableFlag on guardrails

This is an automated email from the ASF dual-hosted git repository.

adelapena pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/cassandra.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 013acc641c Rename DisableFlag class to EnableFlag on guardrails
013acc641c is described below

commit 013acc641c5d487b07be5c082af1e85d26bd127f
Author: Bernardo Botella Corbi <co...@bernardobotella.com>
AuthorDate: Mon Apr 18 16:09:21 2022 -0700

    Rename DisableFlag class to EnableFlag on guardrails
    
    patch by Bernardo Botella Corbi; reviewed by Andrés de la Peña, Josh McKenzie and Yifan Cai for CASSANDRA-17544
---
 CHANGES.txt                                        |  1 +
 .../cassandra/db/guardrails/DisableFlag.java       | 86 ----------------------
 .../apache/cassandra/db/guardrails/EnableFlag.java | 86 ++++++++++++++++++++++
 .../apache/cassandra/db/guardrails/Guardrails.java | 64 ++++++++--------
 .../cassandra/db/guardrails/GuardrailsTest.java    | 32 ++++----
 5 files changed, 135 insertions(+), 134 deletions(-)

diff --git a/CHANGES.txt b/CHANGES.txt
index d39b8cbc31..306acc1ec4 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -1,4 +1,5 @@
 4.2
+ * Rename DisableFlag class to EnableFlag on guardrails (CASSANDRA-17544)
 Merged from 4.1:
 Merged from 4.0:
 Merged from 3.11:
diff --git a/src/java/org/apache/cassandra/db/guardrails/DisableFlag.java b/src/java/org/apache/cassandra/db/guardrails/DisableFlag.java
deleted file mode 100644
index 9ec1951d27..0000000000
--- a/src/java/org/apache/cassandra/db/guardrails/DisableFlag.java
+++ /dev/null
@@ -1,86 +0,0 @@
-/*
- * 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.guardrails;
-
-import java.util.function.Predicate;
-import javax.annotation.Nullable;
-
-import org.apache.cassandra.service.ClientState;
-
-/**
- * A guardrail that completely disables the use of a particular feature.
- *
- * <p>Note that this guardrail only aborts operations (if the feature is disabled) so is only meant for
- * query-based guardrails (we're happy to reject queries deemed dangerous, but we don't want to create a guardrail
- * that breaks compaction for instance).
- */
-public class DisableFlag extends Guardrail
-{
-    private final Predicate<ClientState> disabled;
-    private final String what;
-
-    /**
-     * Creates a new {@link DisableFlag} guardrail.
-     *
-     * @param name     the identifying name of the guardrail
-     * @param disabled a {@link ClientState}-based supplier of boolean indicating whether the feature guarded by this
-     *                 guardrail must be disabled.
-     * @param what     The feature that is guarded by this guardrail (for reporting in error messages),
-     *                 {@link DisableFlag#ensureEnabled(String, ClientState)} can specify a different {@code what}.
-     */
-    public DisableFlag(String name, Predicate<ClientState> disabled, String what)
-    {
-        super(name);
-        this.disabled = disabled;
-        this.what = what;
-    }
-
-    /**
-     * Aborts the operation if this guardrail is disabled.
-     *
-     * <p>This must be called when the feature guarded by this guardrail is used to ensure such use is in fact
-     * allowed.
-     *
-     * @param state The client state, used to skip the check if the query is internal or is done by a superuser.
-     *              A {@code null} value means that the check should be done regardless of the query.
-     */
-    public void ensureEnabled(@Nullable ClientState state)
-    {
-        ensureEnabled(what, state);
-    }
-
-    /**
-     * Aborts the operation if this guardrail is disabled.
-     *
-     * <p>This must be called when the feature guarded by this guardrail is used to ensure such use is in fact
-     * allowed.
-     *
-     * @param what  The feature that is guarded by this guardrail (for reporting in error messages).
-     * @param state The client state, used to skip the check if the query is internal or is done by a superuser.
-     *              A {@code null} value means that the check should be done regardless of the query, although it won't
-     *              throw any exception if the failure threshold is exceeded. This is so because checks without an
-     *              associated client come from asynchronous processes such as compaction, and we don't want to
-     *              interrupt such processes.
-     */
-    public void ensureEnabled(String what, @Nullable ClientState state)
-    {
-        if (enabled(state) && disabled.test(state))
-            fail(what + " is not allowed", state);
-    }
-}
diff --git a/src/java/org/apache/cassandra/db/guardrails/EnableFlag.java b/src/java/org/apache/cassandra/db/guardrails/EnableFlag.java
new file mode 100644
index 0000000000..aba013a65f
--- /dev/null
+++ b/src/java/org/apache/cassandra/db/guardrails/EnableFlag.java
@@ -0,0 +1,86 @@
+/*
+ * 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.guardrails;
+
+import java.util.function.Predicate;
+import javax.annotation.Nullable;
+
+import org.apache.cassandra.service.ClientState;
+
+/**
+ * A guardrail that enables the use of a particular feature.
+ *
+ * <p>Note that this guardrail only aborts operations (if the feature is not enabled) so is only meant for query-based
+ * guardrails (we're happy to reject queries deemed dangerous, but we don't want to create a guardrail that breaks
+ * compaction for instance).
+ */
+public class EnableFlag extends Guardrail
+{
+    private final Predicate<ClientState> enabled;
+    private final String featureName;
+
+    /**
+     * Creates a new {@link EnableFlag} guardrail.
+     *
+     * @param name        the identifying name of the guardrail
+     * @param enabled     a {@link ClientState}-based supplier of boolean indicating whether the feature guarded by this
+     *                    guardrail is enabled.
+     * @param featureName The feature that is guarded by this guardrail (for reporting in error messages), {@link
+     *                    EnableFlag#ensureEnabled(String, ClientState)} can specify a different {@code featureName}.
+     */
+    public EnableFlag(String name, Predicate<ClientState> enabled, String featureName)
+    {
+        super(name);
+        this.enabled = enabled;
+        this.featureName = featureName;
+    }
+
+    /**
+     * Aborts the operation if this guardrail is not enabled.
+     *
+     * <p>This must be called when the feature guarded by this guardrail is used to ensure such use is in fact
+     * allowed.
+     *
+     * @param state The client state, used to skip the check if the query is internal or is done by a superuser.
+     *              A {@code null} value means that the check should be done regardless of the query.
+     */
+    public void ensureEnabled(@Nullable ClientState state)
+    {
+        ensureEnabled(featureName, state);
+    }
+
+    /**
+     * Aborts the operation if this guardrail is not enabled.
+     *
+     * <p>This must be called when the feature guarded by this guardrail is used to ensure such use is in fact
+     * allowed.
+     *
+     * @param featureName The feature that is guarded by this guardrail (for reporting in error messages).
+     * @param state       The client state, used to skip the check if the query is internal or is done by a superuser. A
+     *                    {@code null} value means that the check should be done regardless of the query, although it
+     *                    won't throw any exception if the failure threshold is exceeded. This is so because checks
+     *                    without an associated client come from asynchronous processes such as compaction, and we don't
+     *                    want to interrupt such processes.
+     */
+    public void ensureEnabled(String featureName, @Nullable ClientState state)
+    {
+        if (enabled(state) && !enabled.test(state))
+            fail(featureName + " is not allowed", state);
+    }
+}
diff --git a/src/java/org/apache/cassandra/db/guardrails/Guardrails.java b/src/java/org/apache/cassandra/db/guardrails/Guardrails.java
index b670f87cd7..a4811ac7f6 100644
--- a/src/java/org/apache/cassandra/db/guardrails/Guardrails.java
+++ b/src/java/org/apache/cassandra/db/guardrails/Guardrails.java
@@ -104,10 +104,10 @@ public final class Guardrails implements GuardrailsMBean
     /**
      * Guardrail disabling user's ability to create secondary indexes
      */
-    public static final DisableFlag createSecondaryIndexesEnabled =
-    new DisableFlag("secondary_indexes",
-                    state -> !CONFIG_PROVIDER.getOrCreate(state).getSecondaryIndexesEnabled(),
-                    "User creation of secondary indexes");
+    public static final EnableFlag createSecondaryIndexesEnabled =
+    new EnableFlag("secondary_indexes",
+                   state -> CONFIG_PROVIDER.getOrCreate(state).getSecondaryIndexesEnabled(),
+                   "User creation of secondary indexes");
 
     /**
      * Guardrail on the number of materialized views per table.
@@ -135,36 +135,36 @@ public final class Guardrails implements GuardrailsMBean
     /**
      * Guardrail disabling user-provided timestamps.
      */
-    public static final DisableFlag userTimestampsEnabled =
-    new DisableFlag("user_timestamps",
-                    state -> !CONFIG_PROVIDER.getOrCreate(state).getUserTimestampsEnabled(),
-                    "User provided timestamps (USING TIMESTAMP)");
+    public static final EnableFlag userTimestampsEnabled =
+    new EnableFlag("user_timestamps",
+                   state -> CONFIG_PROVIDER.getOrCreate(state).getUserTimestampsEnabled(),
+                   "User provided timestamps (USING TIMESTAMP)");
 
-    public static final DisableFlag groupByEnabled =
-    new DisableFlag("group_by",
-                    state -> !CONFIG_PROVIDER.getOrCreate(state).getGroupByEnabled(),
-                    "GROUP BY functionality");
+    public static final EnableFlag groupByEnabled =
+    new EnableFlag("group_by",
+                   state -> CONFIG_PROVIDER.getOrCreate(state).getGroupByEnabled(),
+                   "GROUP BY functionality");
 
-    public static final DisableFlag dropTruncateTableEnabled =
-    new DisableFlag("drop_truncate_table_enabled",
-                    state -> !CONFIG_PROVIDER.getOrCreate(state).getDropTruncateTableEnabled(),
-                    "DROP and TRUNCATE TABLE functionality");
+    public static final EnableFlag dropTruncateTableEnabled =
+    new EnableFlag("drop_truncate_table_enabled",
+                   state -> CONFIG_PROVIDER.getOrCreate(state).getDropTruncateTableEnabled(),
+                   "DROP and TRUNCATE TABLE functionality");
 
     /**
      * Guardrail disabling user's ability to turn off compression
      */
-    public static final DisableFlag uncompressedTablesEnabled =
-    new DisableFlag("uncompressed_tables_enabled",
-                    state -> !CONFIG_PROVIDER.getOrCreate(state).getUncompressedTablesEnabled(),
-                    "Uncompressed table");
+    public static final EnableFlag uncompressedTablesEnabled =
+    new EnableFlag("uncompressed_tables_enabled",
+                   state -> CONFIG_PROVIDER.getOrCreate(state).getUncompressedTablesEnabled(),
+                   "Uncompressed table");
 
     /**
      * Guardrail disabling the creation of new COMPACT STORAGE tables
      */
-    public static final DisableFlag compactTablesEnabled =
-    new DisableFlag("compact_tables",
-                    state -> !CONFIG_PROVIDER.getOrCreate(state).getCompactTablesEnabled(),
-                    "Creation of new COMPACT STORAGE tables");
+    public static final EnableFlag compactTablesEnabled =
+    new EnableFlag("compact_tables",
+                   state -> CONFIG_PROVIDER.getOrCreate(state).getCompactTablesEnabled(),
+                   "Creation of new COMPACT STORAGE tables");
 
     /**
      * Guardrail on the number of elements returned within page.
@@ -197,18 +197,18 @@ public final class Guardrails implements GuardrailsMBean
     /**
      * Guardrail disabling operations on lists that require read before write.
      */
-    public static final DisableFlag readBeforeWriteListOperationsEnabled =
-    new DisableFlag("read_before_write_list_operations",
-                    state -> !CONFIG_PROVIDER.getOrCreate(state).getReadBeforeWriteListOperationsEnabled(),
-                    "List operation requiring read before write");
+    public static final EnableFlag readBeforeWriteListOperationsEnabled =
+    new EnableFlag("read_before_write_list_operations",
+                   state -> CONFIG_PROVIDER.getOrCreate(state).getReadBeforeWriteListOperationsEnabled(),
+                   "List operation requiring read before write");
 
     /**
      * Guardrail disabling ALLOW FILTERING statement within a query
      */
-    public static final DisableFlag allowFilteringEnabled =
-    new DisableFlag("allow_filtering",
-                    state -> !CONFIG_PROVIDER.getOrCreate(state).getAllowFilteringEnabled(),
-                    "Querying with ALLOW FILTERING");
+    public static final EnableFlag allowFilteringEnabled =
+    new EnableFlag("allow_filtering",
+                   state -> CONFIG_PROVIDER.getOrCreate(state).getAllowFilteringEnabled(),
+                   "Querying with ALLOW FILTERING");
 
     /**
      * Guardrail on the number of restrictions created by a cartesian product of a CQL's {@code IN} query.
diff --git a/test/unit/org/apache/cassandra/db/guardrails/GuardrailsTest.java b/test/unit/org/apache/cassandra/db/guardrails/GuardrailsTest.java
index a0a5823b01..5c7e724abb 100644
--- a/test/unit/org/apache/cassandra/db/guardrails/GuardrailsTest.java
+++ b/test/unit/org/apache/cassandra/db/guardrails/GuardrailsTest.java
@@ -67,8 +67,8 @@ public class GuardrailsTest extends GuardrailTester
         MaxThreshold guard = new MaxThreshold("x",
                                         state -> 10,
                                         state -> 100,
-                                        (isWarn, what, v, t) -> format("%s: for %s, %s > %s",
-                                                                       isWarn ? "Warning" : "Aborting", what, v, t));
+                                        (isWarn, featureName, v, t) -> format("%s: for %s, %s > %s",
+                                                                       isWarn ? "Warning" : "Aborting", featureName, v, t));
 
         assertTrue(guard.enabled(userClientState));
 
@@ -93,8 +93,8 @@ public class GuardrailsTest extends GuardrailTester
         MaxThreshold guard = new MaxThreshold("x",
                                         state -> 10,
                                         state -> DISABLED,
-                                        (isWarn, what, v, t) -> format("%s: for %s, %s > %s",
-                                                                       isWarn ? "Warning" : "Aborting", what, v, t));
+                                        (isWarn, featureName, v, t) -> format("%s: for %s, %s > %s",
+                                                                       isWarn ? "Warning" : "Aborting", featureName, v, t));
 
         assertTrue(guard.enabled(userClientState));
 
@@ -111,8 +111,8 @@ public class GuardrailsTest extends GuardrailTester
         MaxThreshold guard = new MaxThreshold("x",
                                         state -> DISABLED,
                                         state -> 10,
-                                        (isWarn, what, v, t) -> format("%s: for %s, %s > %s",
-                                                                       isWarn ? "Warning" : "Aborting", what, v, t));
+                                        (isWarn, featureName, v, t) -> format("%s: for %s, %s > %s",
+                                                                       isWarn ? "Warning" : "Aborting", featureName, v, t));
 
         assertTrue(guard.enabled(userClientState));
 
@@ -129,8 +129,8 @@ public class GuardrailsTest extends GuardrailTester
         MaxThreshold guard = new MaxThreshold("x",
                                         state -> 10,
                                         state -> 100,
-                                        (isWarn, what, v, t) -> format("%s: for %s, %s > %s",
-                                                                       isWarn ? "Warning" : "Failure", what, v, t));
+                                        (isWarn, featureName, v, t) -> format("%s: for %s, %s > %s",
+                                                                       isWarn ? "Warning" : "Failure", featureName, v, t));
 
         // value under both thresholds
         assertValid(() -> guard.guard(5, "x", false, null));
@@ -251,25 +251,25 @@ public class GuardrailsTest extends GuardrailTester
     }
 
     @Test
-    public void testDisableFlag() throws Throwable
+    public void testEnableFlag() throws Throwable
     {
-        assertFails(() -> new DisableFlag("x", state -> true, "X").ensureEnabled(userClientState), "X is not allowed");
-        assertValid(() -> new DisableFlag("x", state -> false, "X").ensureEnabled(userClientState));
+        assertFails(() -> new EnableFlag("x", state -> false, "X").ensureEnabled(userClientState), "X is not allowed");
+        assertValid(() -> new EnableFlag("x", state -> true, "X").ensureEnabled(userClientState));
 
-        assertFails(() -> new DisableFlag("x", state -> true, "X").ensureEnabled("Y", userClientState), "Y is not allowed");
-        assertValid(() -> new DisableFlag("x", state -> false, "X").ensureEnabled("Y", userClientState));
+        assertFails(() -> new EnableFlag("x", state -> false, "X").ensureEnabled("Y", userClientState), "Y is not allowed");
+        assertValid(() -> new EnableFlag("x", state -> true, "X").ensureEnabled("Y", userClientState));
     }
 
     @Test
-    public void testDisableFlagUsers() throws Throwable
+    public void testEnableFlagUsers() throws Throwable
     {
-        DisableFlag enabled = new DisableFlag("x", state -> false, "X");
+        EnableFlag enabled = new EnableFlag("x", state -> true, "X");
         assertValid(() -> enabled.ensureEnabled(null));
         assertValid(() -> enabled.ensureEnabled(userClientState));
         assertValid(() -> enabled.ensureEnabled(systemClientState));
         assertValid(() -> enabled.ensureEnabled(superClientState));
 
-        DisableFlag disabled = new DisableFlag("x", state -> true, "X");
+        EnableFlag disabled = new EnableFlag("x", state -> false, "X");
         assertFails(() -> disabled.ensureEnabled(userClientState), "X is not allowed");
         assertValid(() -> disabled.ensureEnabled(systemClientState));
         assertValid(() -> disabled.ensureEnabled(superClientState));


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cassandra.apache.org
For additional commands, e-mail: commits-help@cassandra.apache.org