Skip to content
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

Fix potential file leak in ES816BinaryQuantizedVectorsWriter #120014

Open
wants to merge 3 commits 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
6 changes: 6 additions & 0 deletions docs/changelog/120014.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pr: 120014
summary: Fix potential file leak in ES816BinaryQuantizedVectorsWriter
area: Search
type: bug
issues:
- 119981
3 changes: 0 additions & 3 deletions muted-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,6 @@ tests:
- class: org.elasticsearch.lucene.RollingUpgradeSearchableSnapshotIndexCompatibilityIT
method: testMountSearchableSnapshot {p0=[9.0.0, 9.0.0, 8.18.0]}
issue: https://github.com/elastic/elasticsearch/issues/119980
- class: org.elasticsearch.index.codec.vectors.es816.ES816HnswBinaryQuantizedVectorsFormatTests
method: testRandomExceptions
issue: https://github.com/elastic/elasticsearch/issues/119981
- class: org.elasticsearch.multi_cluster.MultiClusterYamlTestSuiteIT
issue: https://github.com/elastic/elasticsearch/issues/119983
- class: org.elasticsearch.lucene.RollingUpgradeSearchableSnapshotIndexCompatibilityIT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -437,32 +437,36 @@ private CloseableRandomVectorScorerSupplier mergeOneFieldToIndex(
float[] centroid,
float cDotC
) throws IOException {
long vectorDataOffset = binarizedVectorData.alignFilePointer(Float.BYTES);
final IndexOutput tempQuantizedVectorData = segmentWriteState.directory.createTempOutput(
binarizedVectorData.getName(),
"temp",
segmentWriteState.context
);
final IndexOutput tempScoreQuantizedVectorData = segmentWriteState.directory.createTempOutput(
binarizedVectorData.getName(),
"score_temp",
segmentWriteState.context
);
IndexInput binarizedDataInput = null;
IndexInput binarizedScoreDataInput = null;
boolean success = false;
int descritizedDimension = BQVectorUtils.discretize(fieldInfo.getVectorDimension(), 64);
BinaryQuantizer quantizer = new BinaryQuantizer(
final long vectorDataOffset = binarizedVectorData.alignFilePointer(Float.BYTES);
final int descritizedDimension = BQVectorUtils.discretize(fieldInfo.getVectorDimension(), 64);
final BinaryQuantizer quantizer = new BinaryQuantizer(
fieldInfo.getVectorDimension(),
descritizedDimension,
fieldInfo.getVectorSimilarityFunction()
);

IndexInput binarizedDataInput = null;
IndexInput binarizedScoreDataInput = null;
IndexOutput tempQuantizedVectorData = null;
IndexOutput tempScoreQuantizedVectorData = null;
boolean success = false;

try {
tempQuantizedVectorData = segmentWriteState.directory.createTempOutput(
binarizedVectorData.getName(),
"temp",
segmentWriteState.context
);
tempScoreQuantizedVectorData = segmentWriteState.directory.createTempOutput(
binarizedVectorData.getName(),
"score_temp",
segmentWriteState.context
);
FloatVectorValues floatVectorValues = KnnVectorsWriter.MergedVectorValues.mergeFloatVectorValues(fieldInfo, mergeState);
if (fieldInfo.getVectorSimilarityFunction() == COSINE) {
floatVectorValues = new NormalizedFloatVectorValues(floatVectorValues);
}
DocsWithFieldSet docsWithField = writeBinarizedVectorAndQueryData(
final DocsWithFieldSet docsWithField = writeBinarizedVectorAndQueryData(
tempQuantizedVectorData,
tempScoreQuantizedVectorData,
floatVectorValues,
Expand Down Expand Up @@ -490,10 +494,9 @@ private CloseableRandomVectorScorerSupplier mergeOneFieldToIndex(
cDotC,
docsWithField
);
success = true;
final IndexInput finalBinarizedDataInput = binarizedDataInput;
final IndexInput finalBinarizedScoreDataInput = binarizedScoreDataInput;
OffHeapBinarizedVectorValues vectorValues = new OffHeapBinarizedVectorValues.DenseOffHeapVectorValues(
final OffHeapBinarizedVectorValues vectorValues = new OffHeapBinarizedVectorValues.DenseOffHeapVectorValues(
fieldInfo.getVectorDimension(),
docsWithField.cardinality(),
centroid,
Expand All @@ -503,7 +506,7 @@ private CloseableRandomVectorScorerSupplier mergeOneFieldToIndex(
vectorsScorer,
finalBinarizedDataInput
);
RandomVectorScorerSupplier scorerSupplier = vectorsScorer.getRandomVectorScorerSupplier(
final RandomVectorScorerSupplier scorerSupplier = vectorsScorer.getRandomVectorScorerSupplier(
fieldInfo.getVectorSimilarityFunction(),
new OffHeapBinarizedQueryVectorValues(
finalBinarizedScoreDataInput,
Expand All @@ -513,12 +516,20 @@ private CloseableRandomVectorScorerSupplier mergeOneFieldToIndex(
),
vectorValues
);
final IndexOutput finalTempQuantizedVectorData = tempQuantizedVectorData;
final IndexOutput finalTempScoreQuantizedVectorData = tempScoreQuantizedVectorData;
success = true;
return new BinarizedCloseableRandomVectorScorerSupplier(scorerSupplier, vectorValues, () -> {
IOUtils.close(finalBinarizedDataInput, finalBinarizedScoreDataInput);
IOUtils.close(
finalBinarizedDataInput,
finalBinarizedScoreDataInput,
finalTempQuantizedVectorData,
finalTempScoreQuantizedVectorData
);
IOUtils.deleteFilesIgnoringExceptions(
segmentWriteState.directory,
tempQuantizedVectorData.getName(),
tempScoreQuantizedVectorData.getName()
finalTempQuantizedVectorData.getName(),
finalTempScoreQuantizedVectorData.getName()
);
});
} finally {
Expand All @@ -529,11 +540,12 @@ private CloseableRandomVectorScorerSupplier mergeOneFieldToIndex(
binarizedDataInput,
binarizedScoreDataInput
);
IOUtils.deleteFilesIgnoringExceptions(
segmentWriteState.directory,
tempQuantizedVectorData.getName(),
tempScoreQuantizedVectorData.getName()
);
if (tempQuantizedVectorData != null) {
IOUtils.deleteFilesIgnoringExceptions(segmentWriteState.directory, tempQuantizedVectorData.getName());
}
if (tempScoreQuantizedVectorData != null) {
IOUtils.deleteFilesIgnoringExceptions(segmentWriteState.directory, tempScoreQuantizedVectorData.getName());
}
}
}
}
Expand Down