Skip to content

Commit

Permalink
Add guardrail to disallow creation of new COMPACT STORAGE tables
Browse files Browse the repository at this point in the history
Patch by Josh McKenzie; reviewed by Caleb Rackliffe for CASSANDRA-17522
  • Loading branch information
jmckenzie-dev committed Apr 7, 2022
1 parent 828d98e commit 01c4404
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
4.1
* Add guardrail to disallow creation of new COMPACT STORAGE tables (CASSANDRA-17522)
* repair vtables should expose a completed field due to lack of filtering options in CQL (CASSANDRA-17520)
* remove outdated code from cqlsh (CASSANDRA-17490)
* remove support for deprecated version specific TLS in Python 3.6 (CASSANDRA-17365)
Expand Down
1 change: 1 addition & 0 deletions src/java/org/apache/cassandra/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,7 @@ public static void setClientMode(boolean clientMode)
public volatile Set<ConsistencyLevel> write_consistency_levels_warned = Collections.emptySet();
public volatile Set<ConsistencyLevel> write_consistency_levels_disallowed = Collections.emptySet();
public volatile boolean user_timestamps_enabled = true;
public volatile boolean compact_tables_enabled = true;
public volatile boolean read_before_write_list_operations_enabled = true;
public volatile DataStorageSpec collection_size_warn_threshold = DISABLED_SIZE_GUARDRAIL;
public volatile DataStorageSpec collection_size_fail_threshold = DISABLED_SIZE_GUARDRAIL;
Expand Down
14 changes: 14 additions & 0 deletions src/java/org/apache/cassandra/config/GuardrailsOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,20 @@ public void setUserTimestampsEnabled(boolean enabled)
x -> config.user_timestamps_enabled = x);
}

@Override
public boolean getCompactTablesEnabled()
{
return config.compact_tables_enabled;
}

public void setCompactTablesEnabled(boolean enabled)
{
updatePropertyWithLogging("compact_tables_enabled",
enabled,
() -> config.compact_tables_enabled,
x -> config.compact_tables_enabled = x);
}

@Override
public boolean getReadBeforeWriteListOperationsEnabled()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ public void validate(ClientState state)
.sum();
Guardrails.tables.guard(totalUserTables + 1, tableName, false, state);
}

// Guardrail to check whether creation of new COMPACT STORAGE tables is allowed
if (useCompactStorage)
Guardrails.compactTablesEnabled.ensureEnabled(state);
}
}

Expand Down
20 changes: 20 additions & 0 deletions src/java/org/apache/cassandra/db/guardrails/Guardrails.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,14 @@ public final class Guardrails implements GuardrailsMBean
state -> !CONFIG_PROVIDER.getOrCreate(state).getUserTimestampsEnabled(),
"User provided timestamps (USING TIMESTAMP)");

/**
* 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");

/**
* Guardrail on the number of elements returned within page.
*/
Expand Down Expand Up @@ -456,6 +464,18 @@ public void setUserTimestampsEnabled(boolean enabled)
DEFAULT_CONFIG.setUserTimestampsEnabled(enabled);
}

@Override
public boolean getCompactTablesEnabled()
{
return DEFAULT_CONFIG.getCompactTablesEnabled();
}

@Override
public void setCompactTablesEnabled(boolean enabled)
{
DEFAULT_CONFIG.setCompactTablesEnabled(enabled);
}

@Override
public int getPageSizeWarnThreshold()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,13 @@ public interface GuardrailsConfig
*/
boolean getUserTimestampsEnabled();

/**
* Returns whether users can create new COMPACT STORAGE tables
*
* @return {@code true} if allowed, {@code false} otherwise.
*/
boolean getCompactTablesEnabled();

/**
* @return The threshold to warn when page size exceeds given size.
*/
Expand Down
14 changes: 14 additions & 0 deletions src/java/org/apache/cassandra/db/guardrails/GuardrailsMBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,20 @@ public interface GuardrailsMBean
*/
void setUserTimestampsEnabled(boolean enabled);

/**
* Returns whether users can create new COMPACT STORAGE tables
*
* @return {@code true} if allowed, {@code false} otherwise.
*/
boolean getCompactTablesEnabled();

/**
* Sets whether users can create new COMPACT STORAGE tables
*
* @param enabled {@code true} if allowed, {@code false} otherwise.
*/
void setCompactTablesEnabled(boolean enabled);

/**
* @return The threshold to warn when requested page size greater than threshold.
* -1 means disabled.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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 org.junit.Test;

public class GuardrailNewCompactStorageTest extends GuardrailTester
{
private void setGuardrail(boolean enabled)
{
Guardrails.instance.setCompactTablesEnabled(enabled);
}

@Test
public void testFeatureEnabled() throws Throwable
{
setGuardrail(true);
assertValid("CREATE TABLE " + KEYSPACE + ".tbl (pk int, ck int, v int, PRIMARY KEY (pk, ck)) WITH COMPACT STORAGE");
}

@Test
public void testFeatureDisabled() throws Throwable
{
setGuardrail(false);
assertFails("CREATE TABLE " + KEYSPACE + ".tbl (pk int, ck int, v int, PRIMARY KEY (pk, ck)) WITH COMPACT STORAGE",
"Creation of new COMPACT STORAGE tables");
}
}

0 comments on commit 01c4404

Please sign in to comment.