Skip to content

Commit

Permalink
Fix bbq_hnsw merge file cleanup on random IO exceptions (elastic#119691)
Browse files Browse the repository at this point in the history
Since we open two temporary files during quantized vector merging, its
possible that the second file fails to be created. In that case, we
should ensure the previously created temporary files are removed.

closes elastic#119392
  • Loading branch information
benwtrent authored Jan 8, 2025
1 parent 59e9391 commit 8edcdd4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
6 changes: 6 additions & 0 deletions docs/changelog/119691.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pr: 119691
summary: Fix `bbq_hnsw` merge file cleanup on random IO exceptions
area: Vector Search
type: bug
issues:
- 119392
Original file line number Diff line number Diff line change
Expand Up @@ -443,21 +443,27 @@ private CloseableRandomVectorScorerSupplier mergeOneFieldToIndex(
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;
IndexOutput tempQuantizedVectorData = null;
IndexOutput tempScoreQuantizedVectorData = null;
boolean success = false;
OptimizedScalarQuantizer quantizer = new OptimizedScalarQuantizer(fieldInfo.getVectorSimilarityFunction());
try {
// Since we are opening two files, it's possible that one or the other fails to open
// we open them within the try to ensure they are cleaned
tempQuantizedVectorData = segmentWriteState.directory.createTempOutput(
binarizedVectorData.getName(),
"temp",
segmentWriteState.context
);
tempScoreQuantizedVectorData = segmentWriteState.directory.createTempOutput(
binarizedVectorData.getName(),
"score_temp",
segmentWriteState.context
);
final String tempQuantizedVectorDataName = tempQuantizedVectorData.getName();
final String tempScoreQuantizedVectorDataName = tempScoreQuantizedVectorData.getName();
FloatVectorValues floatVectorValues = KnnVectorsWriter.MergedVectorValues.mergeFloatVectorValues(fieldInfo, mergeState);
if (fieldInfo.getVectorSimilarityFunction() == COSINE) {
floatVectorValues = new NormalizedFloatVectorValues(floatVectorValues);
Expand Down Expand Up @@ -516,8 +522,8 @@ private CloseableRandomVectorScorerSupplier mergeOneFieldToIndex(
IOUtils.close(finalBinarizedDataInput, finalBinarizedScoreDataInput);
IOUtils.deleteFilesIgnoringExceptions(
segmentWriteState.directory,
tempQuantizedVectorData.getName(),
tempScoreQuantizedVectorData.getName()
tempQuantizedVectorDataName,
tempScoreQuantizedVectorDataName
);
});
} finally {
Expand All @@ -528,11 +534,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

0 comments on commit 8edcdd4

Please sign in to comment.