-
Notifications
You must be signed in to change notification settings - Fork 4.1k
CASSANDRA-21356: Fix same-timestamp tombstone/expiring-cell tie-break… #4992
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
base: trunk
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -828,8 +828,8 @@ private static CellResolution resolveRegular(LivenessInfo left, LivenessInfo rig | |
| // (i.e. before expiry, the pure tombstone; after expiry, whichever is more recent) | ||
| // this inconsistency has no user-visible distinction, as at this point they are both logically tombstones | ||
| // (the only possible difference is the time at which the cells become purgeable) | ||
| boolean leftIsTombstone = !left.isExpiring(); // !isExpiring() == isTombstone(), but does not need to consider localDeletionTime() | ||
| boolean rightIsTombstone = !right.isExpiring(); | ||
| boolean leftIsTombstone = left.ttl() == LivenessInfo.NO_TTL; // ttl=0 → tombstone; ttl>0 → expiring | ||
| boolean rightIsTombstone = right.ttl() == LivenessInfo.NO_TTL; | ||
|
Comment on lines
-831
to
+832
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. The code here is a mirror of |
||
| if (leftIsTombstone != rightIsTombstone) | ||
| return leftIsTombstone ? LEFT : RIGHT; | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| /* | ||
| * 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; | ||
|
|
||
| import org.junit.Test; | ||
|
|
||
| import static org.junit.Assert.assertFalse; | ||
| import static org.junit.Assert.assertTrue; | ||
|
|
||
| /** | ||
| * CASSANDRA-21356: ReusableLivenessInfo.isExpiring() checked {@code localExpirationTime != | ||
| * NO_EXPIRATION_TIME} instead of {@code ttl != NO_TTL}. A tombstone cell also has a non-default | ||
| * localExpirationTime (it stores the deletion timestamp there), so the old check returned true | ||
| * for tombstones as well as expiring cells — violating the LivenessInfo contract that | ||
| * IS_DELETED_MASK and IS_EXPIRING_MASK are mutually exclusive, and matching the canonical | ||
| * definition in AbstractCell.isExpiring() (ttl() != NO_TTL). | ||
| */ | ||
| public class ReusableLivenessInfoTest | ||
| { | ||
| @Test | ||
| public void tombstoneIsNotExpiring() | ||
| { | ||
| ReusableLivenessInfo info = new ReusableLivenessInfo(); | ||
| // A tombstone cell (e.g. from INSERT ... null or DELETE): ttl is NO_TTL, but | ||
| // localExpirationTime is still set — it stores the deletion timestamp. | ||
| info.reset(1L, LivenessInfo.NO_TTL, 12345L); | ||
| assertTrue("ttl=NO_TTL with a set localExpirationTime is a tombstone", info.isTombstone()); | ||
| assertFalse("isExpiring() must not fire for a tombstone cell: it and IS_DELETED_MASK " + | ||
| "are mutually exclusive in the SSTable format", | ||
| info.isExpiring()); | ||
| } | ||
|
|
||
| @Test | ||
| public void expiringCellIsExpiringNotTombstone() | ||
| { | ||
| ReusableLivenessInfo info = new ReusableLivenessInfo(); | ||
| info.reset(1L, 3600, 12345L); | ||
| assertTrue("A cell with a positive ttl is expiring", info.isExpiring()); | ||
| assertFalse("An expiring cell is not a tombstone", info.isTombstone()); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| /* | ||
| * 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.compaction; | ||
|
|
||
| import org.junit.After; | ||
| import org.junit.Before; | ||
| 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.db.ColumnFamilyStore; | ||
| import org.apache.cassandra.io.sstable.format.big.BigFormat; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertFalse; | ||
| import static org.junit.Assume.assumeTrue; | ||
|
|
||
| /** | ||
| * Validates the logical correctness of cursor compaction's resolveRegular() same-timestamp | ||
| * tie-break, the bug CASSANDRA-21356 owns: a tombstone cell must beat an expiring cell at an | ||
| * identical timestamp. | ||
| * | ||
| * Before the fix, ReusableLivenessInfo.isExpiring() checked {@code localExpirationTime != | ||
| * NO_EXPIRATION_TIME} instead of {@code ttl != NO_TTL}. A tombstone cell also has a non-default | ||
| * localExpirationTime (it stores the deletion timestamp there), so isExpiring() returned true for | ||
| * both tombstone and expiring cells. resolveRegular() used {@code !isExpiring()} to identify | ||
| * tombstones, so both cells looked identical to it and it fell through to comparing | ||
| * localExpirationTime values — an expiring cell's is a future timestamp, a tombstone's is a past | ||
| * deletion timestamp, so the expiring cell always won, resurrecting an explicitly deleted column. | ||
| * See ReusableLivenessInfoTest for direct unit coverage of the root-cause isExpiring() check | ||
| * itself (the general tombstone case, independent of any tie-break). | ||
| * | ||
| * This is asserted by querying the compacted table back rather than by comparing raw Data.db / | ||
| * Index.db bytes against the iterator compaction path. SSTableCursorWriter has other, unrelated | ||
| * byte-encoding gaps (tracked separately as CASSANDRA-21336, CASSANDRA-21357, and CASSANDRA-21358) | ||
| * that make cursor and iterator compaction produce different raw bytes for reasons that have | ||
| * nothing to do with this bug — a byte-for-byte comparison here would fail regardless of whether | ||
| * this specific bug is fixed, so this test checks queryable behavior instead. | ||
| */ | ||
| public class CursorCompactionEquivalenceTest extends CQLTester | ||
|
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. Can we not add this test to one of the |
||
| { | ||
| private boolean origCursorEnabled; | ||
|
|
||
| @Before | ||
| public void guardAndSave() | ||
| { | ||
| assumeTrue("Cursor compaction requires BIG SSTable format", BigFormat.isSelected()); | ||
| origCursorEnabled = DatabaseDescriptor.cursorCompactionEnabled(); | ||
| DatabaseDescriptor.setCursorCompactionEnabled(true); | ||
| } | ||
|
|
||
| @After | ||
| public void restore() | ||
| { | ||
| DatabaseDescriptor.setCursorCompactionEnabled(origCursorEnabled); | ||
| } | ||
|
|
||
| // ── same-timestamp tombstone vs expiring cell tie-break ────────────────────── | ||
| // Exercises resolveRegular(): tombstone must beat expiring cell at identical timestamp. | ||
| // Without the fix, ReusableLivenessInfo.isExpiring() returns true for tombstones, | ||
| // causing resolveRegular() to misidentify the tombstone and pick the expiring cell instead. | ||
|
|
||
| @Test | ||
| public void testSameTimestampTieBreak() throws Throwable | ||
| { | ||
| createTable("CREATE TABLE %s (pk int, ck int, v text, PRIMARY KEY (pk, ck))" + | ||
| " WITH compression = {'enabled': 'false'}"); | ||
| ColumnFamilyStore cfs = getCurrentColumnFamilyStore(); | ||
| cfs.disableAutoCompaction(); | ||
|
|
||
| // SSTable 1: tombstone cell for v at timestamp 100 | ||
| execute("INSERT INTO %s (pk, ck, v) VALUES (0, 0, null) USING TIMESTAMP 100"); | ||
| cfs.forceBlockingFlush(ColumnFamilyStore.FlushReason.UNIT_TESTS); | ||
|
|
||
| // SSTable 2: expiring cell for v at the SAME timestamp 100 — tombstone must win | ||
| execute("INSERT INTO %s (pk, ck, v) VALUES (0, 0, 'x') USING TIMESTAMP 100 AND TTL 3600"); | ||
| cfs.forceBlockingFlush(ColumnFamilyStore.FlushReason.UNIT_TESTS); | ||
|
|
||
| cfs.forceMajorCompaction(); | ||
|
|
||
| UntypedResultSet rs = execute("SELECT v FROM %s WHERE pk = 0 AND ck = 0"); | ||
| assertEquals(1, rs.size()); | ||
| assertFalse("Tombstone must beat the expiring cell at an identical timestamp", | ||
| rs.one().has("v")); | ||
| } | ||
| } | ||
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.
Good fix, I would add a
linktoAbstractCell.isExpiring()to the method javadoc, but remove the rest of the comment as it only explains the commit and not the code.