Skip to content

Commit c68d2bf

Browse files
authored
CNDB-15135: use EmptyFactory to avoid loading SAI per-sstable files if SAI_INDEX_READS_DISABLED is true (#1958)
### What is the issue CNDB-15135 ### What does this PR fix and why was it fixed Avoid reading SAI per-sstable files if SAI_INDEX_READS_DISABLED is true
1 parent 1226594 commit c68d2bf

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

src/java/org/apache/cassandra/index/sai/IndexContext.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.slf4j.LoggerFactory;
3939

4040
import io.github.jbellis.jvector.vector.VectorSimilarityFunction;
41+
import org.apache.cassandra.config.CassandraRelevantProperties;
4142
import org.apache.cassandra.cql3.Operator;
4243
import org.apache.cassandra.cql3.statements.schema.IndexTarget;
4344
import org.apache.cassandra.db.ClusteringComparator;
@@ -91,6 +92,7 @@
9192
import org.apache.cassandra.utils.Pair;
9293
import org.apache.cassandra.utils.concurrent.OpOrder;
9394

95+
import static org.apache.cassandra.config.CassandraRelevantProperties.SAI_INDEX_READS_DISABLED;
9496
import static org.apache.cassandra.config.CassandraRelevantProperties.VALIDATE_MAX_TERM_SIZE_AT_COORDINATOR;
9597

9698
/**
@@ -934,8 +936,15 @@ public Pair<Set<SSTableIndex>, Set<SSTableContext>> getBuiltIndexes(Collection<S
934936
}
935937

936938
SSTableIndex index = new SSTableIndex(context, perIndexComponents);
937-
long count = context.primaryKeyMapFactory().count();
938-
logger.debug(logMessage("Successfully loaded index for SSTable {} with {} rows."), context.descriptor(), count);
939+
if (SAI_INDEX_READS_DISABLED.getBoolean())
940+
{
941+
logger.debug(logMessage("Skipped loading index for SSTable {} as it's disabled by {}"), context.descriptor(), SAI_INDEX_READS_DISABLED.getKey());
942+
}
943+
else
944+
{
945+
long count = context.primaryKeyMapFactory().count();
946+
logger.debug(logMessage("Successfully loaded index for SSTable {} with {} rows."), context.descriptor(), count);
947+
}
939948

940949
// Try to add new index to the set, if set already has such index, we'll simply release and move on.
941950
// This covers situation when SSTable collection has the same SSTable multiple

src/java/org/apache/cassandra/index/sai/SSTableContext.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import com.google.common.base.Objects;
2121

22+
import org.apache.cassandra.config.CassandraRelevantProperties;
2223
import org.apache.cassandra.index.sai.disk.PrimaryKeyMap;
2324
import org.apache.cassandra.index.sai.disk.format.IndexComponents;
2425
import org.apache.cassandra.index.sai.utils.PrimaryKey;
@@ -68,6 +69,7 @@ private SSTableContext(SSTableContext copy)
6869
public static SSTableContext create(SSTableReader sstable, IndexComponents.ForRead perSSTableComponents)
6970
{
7071
var onDiskFormat = perSSTableComponents.onDiskFormat();
72+
// no disk access thus no need to use EmptyFactory
7173
PrimaryKey.Factory primaryKeyFactory = onDiskFormat.newPrimaryKeyFactory(sstable.metadata().comparator);
7274

7375
Ref<? extends SSTableReader> sstableRef = null;
@@ -82,7 +84,10 @@ public static SSTableContext create(SSTableReader sstable, IndexComponents.ForRe
8284
throw new IllegalStateException("Couldn't acquire reference to the sstable: " + sstable);
8385
}
8486

85-
primaryKeyMapFactory = onDiskFormat.newPrimaryKeyMapFactory(perSSTableComponents, primaryKeyFactory, sstable);
87+
// avoid opening SAI metadata if reads are disabled
88+
primaryKeyMapFactory = CassandraRelevantProperties.SAI_INDEX_READS_DISABLED.getBoolean()
89+
? new PrimaryKeyMap.DummyThrowingFactory()
90+
: onDiskFormat.newPrimaryKeyMapFactory(perSSTableComponents, primaryKeyFactory, sstable);
8691

8792
Cleanup cleanup = new Cleanup(primaryKeyMapFactory, sstableRef);
8893

src/java/org/apache/cassandra/index/sai/disk/PrimaryKeyMap.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,28 @@ default void close() throws IOException
124124
default void close() throws IOException
125125
{
126126
}
127+
128+
/**
129+
* When SAI_INDEX_READS_DISABLED is true, this is used to avoid loading SAI files to reduce disk access and memory usage
130+
*/
131+
class DummyThrowingFactory implements Factory
132+
{
133+
@Override
134+
public PrimaryKeyMap newPerSSTablePrimaryKeyMap()
135+
{
136+
throw new UnsupportedOperationException("EmptyFactory doesn't support newPerSSTablePrimaryKeyMap()");
137+
}
138+
139+
@Override
140+
public long count()
141+
{
142+
throw new UnsupportedOperationException("EmptyFactory doesn't support count()");
143+
}
144+
145+
@Override
146+
public void close() throws IOException
147+
{
148+
// no-op
149+
}
150+
}
127151
}

0 commit comments

Comments
 (0)