Skip to content

Commit 95c87fd

Browse files
authored
Fix bbq_hnsw merge file cleanup on random IO exceptions (#119691) (#119763)
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 ecc6c3f commit 95c87fd

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
@@ -441,21 +441,27 @@ private CloseableRandomVectorScorerSupplier mergeOneFieldToIndex(
441441
float cDotC
442442
) throws IOException {
443443
long vectorDataOffset = binarizedVectorData.alignFilePointer(Float.BYTES);
444-
final IndexOutput tempQuantizedVectorData = segmentWriteState.directory.createTempOutput(
445-
binarizedVectorData.getName(),
446-
"temp",
447-
segmentWriteState.context
448-
);
449-
final IndexOutput tempScoreQuantizedVectorData = segmentWriteState.directory.createTempOutput(
450-
binarizedVectorData.getName(),
451-
"score_temp",
452-
segmentWriteState.context
453-
);
454444
IndexInput binarizedDataInput = null;
455445
IndexInput binarizedScoreDataInput = null;
446+
IndexOutput tempQuantizedVectorData = null;
447+
IndexOutput tempScoreQuantizedVectorData = null;
456448
boolean success = false;
457449
OptimizedScalarQuantizer quantizer = new OptimizedScalarQuantizer(fieldInfo.getVectorSimilarityFunction());
458450
try {
451+
// Since we are opening two files, it's possible that one or the other fails to open
452+
// we open them within the try to ensure they are cleaned
453+
tempQuantizedVectorData = segmentWriteState.directory.createTempOutput(
454+
binarizedVectorData.getName(),
455+
"temp",
456+
segmentWriteState.context
457+
);
458+
tempScoreQuantizedVectorData = segmentWriteState.directory.createTempOutput(
459+
binarizedVectorData.getName(),
460+
"score_temp",
461+
segmentWriteState.context
462+
);
463+
final String tempQuantizedVectorDataName = tempQuantizedVectorData.getName();
464+
final String tempScoreQuantizedVectorDataName = tempScoreQuantizedVectorData.getName();
459465
FloatVectorValues floatVectorValues = KnnVectorsWriter.MergedVectorValues.mergeFloatVectorValues(fieldInfo, mergeState);
460466
if (fieldInfo.getVectorSimilarityFunction() == COSINE) {
461467
floatVectorValues = new NormalizedFloatVectorValues(floatVectorValues);
@@ -514,8 +520,8 @@ private CloseableRandomVectorScorerSupplier mergeOneFieldToIndex(
514520
IOUtils.close(finalBinarizedDataInput, finalBinarizedScoreDataInput);
515521
IOUtils.deleteFilesIgnoringExceptions(
516522
segmentWriteState.directory,
517-
tempQuantizedVectorData.getName(),
518-
tempScoreQuantizedVectorData.getName()
523+
tempQuantizedVectorDataName,
524+
tempScoreQuantizedVectorDataName
519525
);
520526
});
521527
} finally {
@@ -526,11 +532,12 @@ private CloseableRandomVectorScorerSupplier mergeOneFieldToIndex(
526532
binarizedDataInput,
527533
binarizedScoreDataInput
528534
);
529-
IOUtils.deleteFilesIgnoringExceptions(
530-
segmentWriteState.directory,
531-
tempQuantizedVectorData.getName(),
532-
tempScoreQuantizedVectorData.getName()
533-
);
535+
if (tempQuantizedVectorData != null) {
536+
IOUtils.deleteFilesIgnoringExceptions(segmentWriteState.directory, tempQuantizedVectorData.getName());
537+
}
538+
if (tempScoreQuantizedVectorData != null) {
539+
IOUtils.deleteFilesIgnoringExceptions(segmentWriteState.directory, tempScoreQuantizedVectorData.getName());
540+
}
534541
}
535542
}
536543
}

0 commit comments

Comments
 (0)