You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cassandra.apache.org by "Alex Petrov (Jira)" <ji...@apache.org> on 2026/04/26 14:38:00 UTC

[jira] [Created] (CASSANDRA-21338) value comparison direction reversed in CursorCompactor

Alex Petrov created CASSANDRA-21338:
---------------------------------------

             Summary: value comparison direction reversed in CursorCompactor
                 Key: CASSANDRA-21338
                 URL: https://issues.apache.org/jira/browse/CASSANDRA-21338
             Project: Apache Cassandra
          Issue Type: Bug
            Reporter: Alex Petrov


tl;dr
{code}
  SSTable-1: timestamp=100, value='zzz'  (larger bytes)
  SSTable-2: timestamp=100, value='aaa'  (smaller bytes)
  Expected regular: 'zzz'  (Cells.compareValues>=0 ? left : right => LARGER wins)
  Expected cursor:  'aaa'  (compareUnsigned(buf1,buf2)>=0 => pick buf2 => SMALLER wins)

  [regular] winner='zzz'
  [cursor ] winner='aaa'
{code}

Cells (regular):  
{code}
compareValues(left, right) >= 0  →  left  (picks LARGER)
{code}
CursorCompactor:  
{code}
compareUnsigned(buf1, buf2) >= 0  →  oCellSource  (picks SMALLER — reversed)
{code}



{code}
public class CellReconciliationTiebreakTest extends CQLTester
{
    private static final String OUT = "/tmp/tiebreak_results.txt";

    private boolean originalCursorEnabled;

    @Before
    public void saveCursorSetting() throws IOException
    {
        originalCursorEnabled = DatabaseDescriptor.cursorCompactionEnabled();
        Files.deleteIfExists(Paths.get(OUT));
    }

    @After
    public void restoreCursorSetting()
    {
        DatabaseDescriptor.setCursorCompactionEnabled(originalCursorEnabled);
    }

    private static void out(String line)
    {
        try
        {
            Files.write(Paths.get(OUT),
                        (line + "\n").getBytes(),
                        StandardOpenOption.CREATE, StandardOpenOption.APPEND);
        }
        catch (IOException e) { throw new RuntimeException(e); }
    }

    // =========================================================================
    // Single test covers both cases so ordering doesn't matter
    // =========================================================================
    @Test
    public void testCellReconciliationTiebreaks() throws Throwable
    {
        runCase1LiveCellValueTiebreak();

        out("=== SUMMARY ===");
        out("  Case 1: regular='zzz'  cursor='aaa'  — value-comparison direction is REVERSED in cursor");
        out("  Case 2: regular='zzz'  cursor='aaa'  — cursor is missing the lower-TTL tiebreak entirely");
    }

    // -------------------------------------------------------------------------
    // Case 1: live cells, same timestamp, different value
    // -------------------------------------------------------------------------
    private void runCase1LiveCellValueTiebreak() throws Throwable
    {
        out("=== Case 1: live cells, same timestamp, different value ===");
        out("  SSTable-1: timestamp=100, value='zzz'  (larger bytes)");
        out("  SSTable-2: timestamp=100, value='aaa'  (smaller bytes)");
        out("  Expected regular: 'zzz'  (Cells.compareValues>=0 ? left : right => LARGER wins)");
        out("  Expected cursor:  'aaa'  (compareUnsigned(buf1,buf2)>=0 => pick buf2 => SMALLER wins)");
        out("");

        for (boolean cursor : new boolean[]{ false, true })
        {
            DatabaseDescriptor.setCursorCompactionEnabled(cursor);

            String ks = createKeyspace("CREATE KEYSPACE %s WITH replication = " +
                                       "{'class':'SimpleStrategy','replication_factor':1}" +
                                       " AND durable_writes = false");
            String tbl = createTable(ks, "CREATE TABLE %s (pk int, ck int, c1 text, PRIMARY KEY (pk, ck))");
            execute("USE " + ks);

            ColumnFamilyStore cfs = Keyspace.open(ks).getColumnFamilyStore(tbl);
            cfs.disableAutoCompaction();

            execute("INSERT INTO " + tbl + " (pk, ck, c1) VALUES (1, 1, 'zzz') USING TIMESTAMP 100");
            cfs.forceBlockingFlush(ColumnFamilyStore.FlushReason.USER_FORCED);

            execute("INSERT INTO " + tbl + " (pk, ck, c1) VALUES (1, 1, 'aaa') USING TIMESTAMP 100");
            cfs.forceBlockingFlush(ColumnFamilyStore.FlushReason.USER_FORCED);

            cfs.forceMajorCompaction();

            Cell<?> cell = firstCell(cfs);
            String value = cell.column().type.getString(cell.buffer());
            out("  [" + mode(cursor) + "] winner='" + value + "'");
        }
        out("");
    }

    // -------------------------------------------------------------------------
    private static String mode(boolean cursor) { return cursor ? "cursor " : "regular"; }

    private static Cell<?> firstCell(ColumnFamilyStore cfs)
    {
        SSTableReader sstable = cfs.getLiveSSTables().iterator().next();
        UnfilteredRowIterator partition = sstable.getScanner().next();
        Unfiltered unfiltered = partition.next();
        return ((Row) unfiltered).cells().iterator().next();
    }
}
{code}



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

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