Repository navigation
CASSANALYTICS-196: Skip CDC-irrelevant mutations on deserialization failure #241
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
bbotella
merged 2 commits into
apache:trunk
from
yifan-c:CASSANALYTICS-TBD/skip-cdc-irrelevant-mutation-on-deser-failure
Oct 7, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
...est/java/org/apache/cassandra/db/commitlog/BufferingCommitLogReaderTableContextTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| /* | ||
| * 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.commitlog; | ||
|
|
||
| import java.util.UUID; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import org.apache.cassandra.bridge.CassandraBridgeImplementation; | ||
| import org.apache.cassandra.cql3.CQLFragmentParser; | ||
| import org.apache.cassandra.cql3.CqlParser; | ||
| import org.apache.cassandra.db.Keyspace; | ||
| import org.apache.cassandra.io.util.DataOutputBuffer; | ||
| import org.apache.cassandra.schema.KeyspaceMetadata; | ||
| import org.apache.cassandra.schema.KeyspaceParams; | ||
| import org.apache.cassandra.schema.Schema; | ||
| import org.apache.cassandra.schema.TableId; | ||
| import org.apache.cassandra.schema.TableMetadata; | ||
| import org.apache.cassandra.schema.Types; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| public class BufferingCommitLogReaderTableContextTests | ||
| { | ||
| @Test | ||
| public void testFailedMutationMayInvolveCdcTableWhenNonCdcTableIdReadableButRestCorrupted() throws Exception | ||
| { | ||
| TableId tableId = registerTable("ks_non_cdc", "tbl", false); | ||
| byte[] corruptedMutation = buildCorruptedMutation(1, tableId); | ||
|
|
||
| boolean mayInvolveCdcTable = BufferingCommitLogReader.failedMutationMayInvolveCdcTable(corruptedMutation, | ||
| corruptedMutation.length); | ||
|
|
||
| assertThat(mayInvolveCdcTable).isFalse(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testFailedMutationMayInvolveCdcTableWhenCdcTableIdReadableButRestCorrupted() throws Exception | ||
| { | ||
| TableId tableId = registerTable("ks_cdc", "tbl", true); | ||
| byte[] corruptedMutation = buildCorruptedMutation(1, tableId); | ||
|
|
||
| boolean mayInvolveCdcTable = BufferingCommitLogReader.failedMutationMayInvolveCdcTable(corruptedMutation, | ||
| corruptedMutation.length); | ||
|
|
||
| assertThat(mayInvolveCdcTable).isTrue(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testFailedMutationMayInvolveCdcTableWhenTableIdUnresolvable() throws Exception | ||
| { | ||
| CassandraBridgeImplementation.setup(); | ||
| TableId unknownTableId = TableId.fromUUID(UUID.randomUUID()); | ||
|
Comment on lines
+69
to
+70
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Maybe move these two to a helper getUnregisteredTable method? |
||
| byte[] corruptedMutation = buildCorruptedMutation(1, unknownTableId); | ||
|
|
||
| boolean mayInvolveCdcTable = BufferingCommitLogReader.failedMutationMayInvolveCdcTable(corruptedMutation, | ||
| corruptedMutation.length); | ||
|
|
||
| // an unresolvable table is treated as not CDC-enabled | ||
| assertThat(mayInvolveCdcTable).isFalse(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testFailedMutationMayInvolveCdcTableWhenMultipleUpdatesEvenIfFirstIsNonCdc() throws Exception | ||
| { | ||
| TableId tableId = registerTable("ks_non_cdc_multi", "tbl", false); | ||
| byte[] corruptedMutation = buildCorruptedMutation(2, tableId); | ||
|
|
||
| boolean mayInvolveCdcTable = BufferingCommitLogReader.failedMutationMayInvolveCdcTable(corruptedMutation, | ||
| corruptedMutation.length); | ||
|
|
||
| // can't safely check the remaining update(s) without deserializing this one's body, so this is | ||
| // conservatively treated as possibly involving a CDC-enabled table | ||
| assertThat(mayInvolveCdcTable).isTrue(); | ||
| } | ||
|
|
||
| private TableId registerTable(String keyspaceName, String tableName, boolean cdc) | ||
| { | ||
| CassandraBridgeImplementation.setup(); | ||
| KeyspaceMetadata keyspaceMetadata = KeyspaceMetadata.create(keyspaceName, KeyspaceParams.simple(1)); | ||
| Schema.instance.load(keyspaceMetadata); | ||
| Keyspace.openWithoutSSTables(keyspaceName); | ||
|
|
||
| String createTableStatement = "CREATE TABLE " + keyspaceName + "." + tableName + " (pk int PRIMARY KEY)" | ||
| + (cdc ? " WITH cdc = true" : ""); | ||
| TableMetadata tableMetadata = CQLFragmentParser | ||
| .parseAny(CqlParser::createTableStatement, createTableStatement, "CREATE TABLE") | ||
| .keyspace(keyspaceName) | ||
| .prepare(null) | ||
| .builder(Types.none()) | ||
| .build(); | ||
| KeyspaceMetadata keyspace = Schema.instance.getKeyspaceMetadata(keyspaceName); | ||
| Schema.instance.load(keyspace.withSwapped(keyspace.tables.with(tableMetadata))); | ||
| return tableMetadata.id; | ||
| } | ||
|
|
||
| // Builds a mutation buffer by hand: a valid header (vint update count, followed by the real TableId of the | ||
| // first update) followed by garbage bytes standing in for a partition update body that fails to deserialize | ||
| // (e.g. due to a schema mismatch). The checksum in BufferingCommitLogReader already guarantees these bytes | ||
| // aren't randomly corrupted, so this reproduces a mutation whose first TableId is intact but whose body | ||
| // cannot be parsed. | ||
| private byte[] buildCorruptedMutation(int updateCount, TableId firstTableId) throws Exception | ||
| { | ||
| try (DataOutputBuffer out = new DataOutputBuffer()) | ||
| { | ||
| out.writeUnsignedVInt(updateCount); | ||
| firstTableId.serialize(out); | ||
| out.write(new byte[]{(byte) 0xDE, (byte) 0xAD, (byte) 0xBE, (byte) 0xEF}); | ||
| return out.toByteArray(); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't we check this before we need to extract the tableId and the metadata? (just at line 638)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It sounds good.