Skip to content

CNDB-14207: Don't break the whole flush if SAI fails to build #1770

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ public void abort(Throwable accumulator, boolean fromIndex)

// For non-compaction, make any indexes involved in this transaction non-queryable, as they will likely not match the backing table.
// For compaction: the compaction task should be aborted and new sstables will not be added to tracker
if (fromIndex && opType != OperationType.COMPACTION)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this change means if we abort during a flush we should just abort the index creation and not fail the index? Please update the comment above as well.

Copy link
Member

@JeremiahDJordan JeremiahDJordan Jun 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When abort happens during flush, does it also mean the sstable was aborted? Just want to understand that we are not creating a case where we have data which can be queried from a direct query, but would be missing from an index.

if (fromIndex && opType != OperationType.COMPACTION && opType != OperationType.FLUSH)
indices.forEach(StorageAttachedIndex::makeIndexNonQueryable);

for (PerIndexWriter perIndexWriter : perIndexWriters)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,19 @@
import org.junit.Test;

import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.exceptions.ReadFailureException;
import org.apache.cassandra.db.marshal.Int32Type;
import org.apache.cassandra.index.sai.IndexContext;
import org.apache.cassandra.index.sai.SAITester;
import org.apache.cassandra.index.sai.disk.v1.kdtree.NumericIndexWriter;
import org.apache.cassandra.inject.ActionBuilder;
import org.apache.cassandra.inject.Expression;
import org.apache.cassandra.inject.Injection;
import org.apache.cassandra.inject.Injections;
import org.apache.cassandra.inject.InvokePointBuilder;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;

public class FlushingTest extends SAITester
{
Expand Down Expand Up @@ -84,4 +91,38 @@ public void testFlushingOverwriteDelete() throws Throwable

assertIndexFilesInToc(indexFiles());
}

@Test
public void testMemtableIndexFlushFailure() throws Throwable
{
Injection failMemtableComplete = Injections.newCustom("FailMemtableIndexWriterComplete")
.add(InvokePointBuilder.newInvokePoint()
.onClass("org.apache.cassandra.index.sai.disk.v1.MemtableIndexWriter")
.onMethod("complete", "com.google.common.base.Stopwatch")
)
.add(ActionBuilder.newActionBuilder().actions()
.doThrow(java.io.IOException.class, Expression.quote("Byteman-injected fault in MemtableIndexWriter.complete"))
)
.build();
Injections.inject(failMemtableComplete);

createTable(CREATE_TABLE_TEMPLATE);
createIndex(String.format(CREATE_INDEX_TEMPLATE, "v1"));

String pkValue = "key_bm_flush_fail";
int indexedValue = 456;

execute("INSERT INTO %s (id1, v1) VALUES (?, ?)", pkValue, indexedValue);

// The Byteman rule will cause the SAI part of the flush to fail
assertThrows(RuntimeException.class, this::flush);

// Assert that the index is still queryable for the inserted data despite the injected fault.
ResultSet indexQueryResults = executeNet("SELECT id1 FROM %s WHERE v1 = ?", indexedValue);
assertEquals("The index should be still usable despite flush failure", 1, indexQueryResults.all().size());

// Assert that the table is still queryable by primary key.
ResultSet pkQueryResults = executeNet("SELECT v1 FROM %s WHERE id1 = ?", pkValue);
assertEquals("Table should still be queryable by primary key", 1, pkQueryResults.all().size());
}
}