Skip to content

Commit 8edcdd4

Browse files
authored
Fix bbq_hnsw merge file cleanup on random IO exceptions (#119691)
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 #119392
1 parent 59e9391 commit 8edcdd4

File tree

2 files changed

+30
-17
lines changed

2 files changed

+30
-17
lines changed

docs/changelog/119691.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pr: 119691
2+
summary: Fix `bbq_hnsw` merge file cleanup on random IO exceptions
3+
area: Vector Search
4+
type: bug
5+
issues:
6+
- 119392

server/src/main/java/org/elasticsearch/index/codec/vectors/es818/ES818BinaryQuantizedVectorsWriter.java

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -443,21 +443,27 @@ private CloseableRandomVectorScorerSupplier mergeOneFieldToIndex(
443443
float cDotC
444444
) throws IOException {
445445
long vectorDataOffset = binarizedVectorData.alignFilePointer(Float.BYTES);
446-
final IndexOutput tempQuantizedVectorData = segmentWriteState.directory.createTempOutput(
447-
binarizedVectorData.getName(),
448-
"temp",
449-
segmentWriteState.context
450-
);
451-
final IndexOutput tempScoreQuantizedVectorData = segmentWriteState.directory.createTempOutput(
452-
binarizedVectorData.getName(),
453-
"score_temp",
454-
segmentWriteState.context
455-
);
456446
IndexInput binarizedDataInput = null;
457447
IndexInput binarizedScoreDataInput = null;
448+
IndexOutput tempQuantizedVectorData = null;
449+
IndexOutput tempScoreQuantizedVectorData = null;
458450
boolean success = false;
459451
OptimizedScalarQuantizer quantizer = new OptimizedScalarQuantizer(fieldInfo.getVectorSimilarityFunction());
460452
try {
453+
// Since we are opening two files, it's possible that one or the other fails to open
454+
// we open them within the try to ensure they are cleaned
455+
tempQuantizedVectorData = segmentWriteState.directory.createTempOutput(
456+
binarizedVectorData.getName(),
457+
"temp",
458+
segmentWriteState.context
459+
);
460+
tempScoreQuantizedVectorData = segmentWriteState.directory.createTempOutput(
461+
binarizedVectorData.getName(),
462+
"score_temp",
463+
segmentWriteState.context
464+
);
465+
final String tempQuantizedVectorDataName = tempQuantizedVectorData.getName();
466+
final String tempScoreQuantizedVectorDataName = tempScoreQuantizedVectorData.getName();
461467
FloatVectorValues floatVectorValues = KnnVectorsWriter.MergedVectorValues.mergeFloatVectorValues(fieldInfo, mergeState);
462468
if (fieldInfo.getVectorSimilarityFunction() == COSINE) {
463469
floatVectorValues = new NormalizedFloatVectorValues(floatVectorValues);
@@ -516,8 +522,8 @@ private CloseableRandomVectorScorerSupplier mergeOneFieldToIndex(
516522
IOUtils.close(finalBinarizedDataInput, finalBinarizedScoreDataInput);
517523
IOUtils.deleteFilesIgnoringExceptions(
518524
segmentWriteState.directory,
519-
tempQuantizedVectorData.getName(),
520-
tempScoreQuantizedVectorData.getName()
525+
tempQuantizedVectorDataName,
526+
tempScoreQuantizedVectorDataName
521527
);
522528
});
523529
} finally {
@@ -528,11 +534,12 @@ private CloseableRandomVectorScorerSupplier mergeOneFieldToIndex(
528534
binarizedDataInput,
529535
binarizedScoreDataInput
530536
);
531-
IOUtils.deleteFilesIgnoringExceptions(
532-
segmentWriteState.directory,
533-
tempQuantizedVectorData.getName(),
534-
tempScoreQuantizedVectorData.getName()
535-
);
537+
if (tempQuantizedVectorData != null) {
538+
IOUtils.deleteFilesIgnoringExceptions(segmentWriteState.directory, tempQuantizedVectorData.getName());
539+
}
540+
if (tempScoreQuantizedVectorData != null) {
541+
IOUtils.deleteFilesIgnoringExceptions(segmentWriteState.directory, tempScoreQuantizedVectorData.getName());
542+
}
536543
}
537544
}
538545
}

0 commit comments

Comments
 (0)