You are viewing a plain text version of this content. The canonical link for it is here.
Posted to pr@cassandra.apache.org by "SEPURI-SAI-KRISHNA (via GitHub)" <gi...@apache.org> on 2026/08/05 16:12:19 UTC

[PR] CASSANDRA-21549: Fix deserialization of column masks in cluster metadata [cassandra]

SEPURI-SAI-KRISHNA opened a new pull request, #4997:
URL: https://github.com/apache/cassandra/pull/4997

   [CASSANDRA-21549](https://issues.apache.org/jira/browse/CASSANDRA-21549)
   
   ### Problem
   
   `ColumnMask.Serializer.deserialize` has two independent defects:
   
   1. `new ArrayList<>(numArgs + 1)` sets the *capacity*, not the size, so the list is empty and
      `argTypes.set(0, columnType)` throws `IndexOutOfBoundsException`. This fires for every masked
      column, including `MASKED WITH DEFAULT`.
   2. The ternary reading each partial argument value is inverted
      (`valuePresent ? null : read(..)`). Besides losing the value, this desynchronises the input
      stream and corrupts every field deserialized afterwards.
   
   ### Impact
   
   `ColumnMask.serializer` is reached through
   `ColumnMetadata → TableMetadata → Tables → KeyspaceMetadata → DistributedSchema → ClusterMetadata`.
   Ordinary DDL never hits it, because schema changes propagate as transformations that each node
   replays locally. It is reached once the schema is embedded in a serialized `ClusterMetadata`, i.e.
   for cluster metadata snapshots, which are read back on startup and when a lagging peer catches up
   from the CMS.
   
   Restarting a node that has a masked column and a stored snapshot fails during startup:
   
   ```
   java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0
   	at java.base/java.util.ArrayList.set(ArrayList.java:470)
   	at org.apache.cassandra.cql3.functions.masking.ColumnMask$Serializer.deserialize(ColumnMask.java:318)
   	at org.apache.cassandra.schema.ColumnMetadata$Serializer.deserialize(ColumnMetadata.java:819)
   	...
   	at org.apache.cassandra.tcm.MetadataSnapshots$SystemKeyspaceMetadataSnapshots.getLatestSnapshot(MetadataSnapshots.java:131)
   	at org.apache.cassandra.tcm.log.SystemKeyspaceStorage.getPersistedLogState(SystemKeyspaceStorage.java:133)
   	at org.apache.cassandra.tcm.log.LocalLog.replayPersisted(LocalLog.java:584)
   	at org.apache.cassandra.tcm.Startup.initializeAsNonCmsNode(Startup.java:185)
   	at org.apache.cassandra.tcm.Startup.initialize(Startup.java:120)
   ```
   
   Note that `MetadataSnapshots.getSnapshot` only catches `IOException`, so this unchecked exception
   propagates rather than degrading to a null snapshot.
   
   ### Reproduction
   
   ```
   CREATE TABLE ks.t (k int PRIMARY KEY, v text MASKED WITH mask_inner(2, 1));
   nodetool cms snapshot
   # restart the node -> startup fails with the stack above
   ```
   
   ### Tests
   
   Existing DDM tests miss this because none of them force a metadata snapshot.
   
   - `SchemaMetadataSerializationTest` — three round-trip cases: a masking function with no partial
     arguments, with partial arguments, and with a null partial argument.
   - `ColumnMaskMetadataSnapshotTest` (new dtest) — snapshot read-back, and node restart after a
     snapshot.
   
   Verified on `cassandra-6.0`, unpatched vs patched:
   
   | Suite | Patched | Unpatched |
   |---|---|---|
   | `SchemaMetadataSerializationTest` | 11/11 pass | 3 errors |
   | `ColumnMaskMetadataSnapshotTest` | 2/2 pass | 2 errors |
   | `ColumnMaskTest` (unit) | 17/17 pass | — |
   | `ColumnMaskTest` (distributed) | 2/2 pass | — |
   | `ClusterMetadataSerializerTest` | 2/2 pass | — |
   
   Isolating defect 1 alone makes the same tests fail with
   `EOFException: EOF after 43 bytes out of 1024`, confirming defect 2 independently.
   
   `ant jar`, `ant checkstyle` and `ant checkstyle-test` are clean.
   
   ### Branch
   
   Based on `cassandra-6.0`. Not applicable to `cassandra-5.0`, which has no `ColumnMask.Serializer` —
   dynamic data masking shipped in 5.0, but the schema is only serialized into cluster metadata from
   6.0 onwards. Needs merging forward to `trunk`.
   
   ---
   
   Prepared with AI assistance (Claude Opus 5); the commit carries an `Assisted-by:` trailer.
   


-- 
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: pr-unsubscribe@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


Re: [PR] CASSANDRA-21549: Fix deserialization of column masks in cluster metadata [cassandra]

Posted by "smiklosovic (via GitHub)" <gi...@apache.org>.
smiklosovic commented on code in PR #4997:
URL: https://github.com/apache/cassandra/pull/4997#discussion_r3736326914


##########
test/distributed/org/apache/cassandra/distributed/test/ColumnMaskMetadataSnapshotTest.java:
##########
@@ -0,0 +1,160 @@
+/*
+ * 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;
+
+import java.io.IOException;
+
+import org.junit.Test;
+
+import org.apache.cassandra.cql3.functions.masking.ColumnMask;
+import org.apache.cassandra.distributed.Cluster;
+import org.apache.cassandra.distributed.api.IInvokableInstance;
+import org.apache.cassandra.distributed.api.IIsolatedExecutor.SerializableCallable;
+import org.apache.cassandra.schema.ColumnMetadata;
+import org.apache.cassandra.schema.TableMetadata;
+import org.apache.cassandra.tcm.ClusterMetadata;
+import org.apache.cassandra.tcm.ClusterMetadataService;
+import org.apache.cassandra.utils.ByteBufferUtil;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * Tests that columns with an attached dynamic data mask survive a cluster metadata snapshot.
+ * <p>
+ * Ordinary schema changes are propagated as transformations that every node replays locally, so they never exercise
+ * {@link ColumnMask.Serializer}. The serializer is only reached once the schema is embedded in a serialized
+ * {@link ClusterMetadata}, which happens for cluster metadata snapshots. Those snapshots are read back when a node
+ * replays its persisted log at startup, and when a lagging peer catches up from the CMS.
+ */
+public class ColumnMaskMetadataSnapshotTest extends TestBaseImpl
+{
+    private static final String TABLE = "masked";
+
+    private static final String CREATE_TABLE =
+    "CREATE TABLE %s." + TABLE + " (" +
+    "k int PRIMARY KEY, " +
+    "no_args text MASKED WITH DEFAULT, " +           // masking function without partial arguments
+    "with_args text MASKED WITH mask_inner(2, 1), " + // masking function with partial arguments
+    "with_null text MASKED WITH mask_inner(null, 1))"; // masking function with a null partial argument
+
+    /**
+     * Tests that the masks of a table can be read back from a cluster metadata snapshot.
+     */
+    @Test
+    public void testMasksSurviveMetadataSnapshot() throws IOException
+    {
+        try (Cluster cluster = init(build(1)))
+        {
+            cluster.schemaChange(withKeyspace(CREATE_TABLE));
+
+            IInvokableInstance node = cluster.get(1);
+            node.nodetoolResult("cms", "snapshot").asserts().success();
+
+            assertEquals("OK", node.callOnInstance(compareSnapshotMasksToCurrent(KEYSPACE)));

Review Comment:
   @SEPURI-SAI-KRISHNA base this on booleans, not string being OK



-- 
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: pr-unsubscribe@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


Re: [PR] CASSANDRA-21549: Fix deserialization of column masks in cluster metadata [cassandra]

Posted by "SEPURI-SAI-KRISHNA (via GitHub)" <gi...@apache.org>.
SEPURI-SAI-KRISHNA commented on code in PR #4997:
URL: https://github.com/apache/cassandra/pull/4997#discussion_r3736698068


##########
test/distributed/org/apache/cassandra/distributed/test/ColumnMaskMetadataSnapshotTest.java:
##########
@@ -0,0 +1,160 @@
+/*
+ * 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;
+
+import java.io.IOException;
+
+import org.junit.Test;
+
+import org.apache.cassandra.cql3.functions.masking.ColumnMask;
+import org.apache.cassandra.distributed.Cluster;
+import org.apache.cassandra.distributed.api.IInvokableInstance;
+import org.apache.cassandra.distributed.api.IIsolatedExecutor.SerializableCallable;
+import org.apache.cassandra.schema.ColumnMetadata;
+import org.apache.cassandra.schema.TableMetadata;
+import org.apache.cassandra.tcm.ClusterMetadata;
+import org.apache.cassandra.tcm.ClusterMetadataService;
+import org.apache.cassandra.utils.ByteBufferUtil;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * Tests that columns with an attached dynamic data mask survive a cluster metadata snapshot.
+ * <p>
+ * Ordinary schema changes are propagated as transformations that every node replays locally, so they never exercise
+ * {@link ColumnMask.Serializer}. The serializer is only reached once the schema is embedded in a serialized
+ * {@link ClusterMetadata}, which happens for cluster metadata snapshots. Those snapshots are read back when a node
+ * replays its persisted log at startup, and when a lagging peer catches up from the CMS.
+ */
+public class ColumnMaskMetadataSnapshotTest extends TestBaseImpl
+{
+    private static final String TABLE = "masked";
+
+    private static final String CREATE_TABLE =
+    "CREATE TABLE %s." + TABLE + " (" +
+    "k int PRIMARY KEY, " +
+    "no_args text MASKED WITH DEFAULT, " +           // masking function without partial arguments
+    "with_args text MASKED WITH mask_inner(2, 1), " + // masking function with partial arguments
+    "with_null text MASKED WITH mask_inner(null, 1))"; // masking function with a null partial argument
+
+    /**
+     * Tests that the masks of a table can be read back from a cluster metadata snapshot.
+     */
+    @Test
+    public void testMasksSurviveMetadataSnapshot() throws IOException
+    {
+        try (Cluster cluster = init(build(1)))
+        {
+            cluster.schemaChange(withKeyspace(CREATE_TABLE));
+
+            IInvokableInstance node = cluster.get(1);
+            node.nodetoolResult("cms", "snapshot").asserts().success();
+
+            assertEquals("OK", node.callOnInstance(compareSnapshotMasksToCurrent(KEYSPACE)));

Review Comment:
   Good catch, thanks. Both helpers now return booleans and the descriptive text moved into the assert messages:
   
       assertTrue("the masks in the cluster metadata snapshot should match the in-memory schema",
                  node.callOnInstance(snapshotMasksMatchCurrent(KEYSPACE)));
   
   Renamed them to read as predicates while I was in there (snapshotMasksMatchCurrent, currentColumnsAreMasked).
   
   Re-verified after the change: both tests pass with the fix and still fail without it with "Index 0 out of bounds for length 0", so the assertions haven't gone blind. ant jar and ant checkstyle-test are clean. Amended into the existing commit and force-pushed.



-- 
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: pr-unsubscribe@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


Re: [PR] CASSANDRA-21549: Fix deserialization of column masks in cluster metadata [cassandra]

Posted by "smiklosovic (via GitHub)" <gi...@apache.org>.
smiklosovic closed pull request #4997: CASSANDRA-21549: Fix deserialization of column masks in cluster metadata
URL: https://github.com/apache/cassandra/pull/4997


-- 
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: pr-unsubscribe@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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