Skip to content

Commit cb9c0a7

Browse files
authored
[DiskBBQ] Clean up life cycle of tmp with quantize vectors (#133378)
1 parent 4ede4ad commit cb9c0a7

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

server/src/main/java/org/elasticsearch/index/codec/vectors/DefaultIVFVectorsWriter.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.apache.lucene.util.hnsw.IntToIntFunction;
2323
import org.apache.lucene.util.packed.PackedInts;
2424
import org.apache.lucene.util.packed.PackedLongValues;
25+
import org.elasticsearch.core.SuppressForbidden;
2526
import org.elasticsearch.index.codec.vectors.cluster.HierarchicalKMeans;
2627
import org.elasticsearch.index.codec.vectors.cluster.KMeansResult;
2728
import org.elasticsearch.logging.LogManager;
@@ -154,6 +155,7 @@ CentroidOffsetAndLength buildAndWritePostingsLists(
154155
}
155156

156157
@Override
158+
@SuppressForbidden(reason = "require usage of Lucene's IOUtils#deleteFilesIgnoringExceptions(...)")
157159
CentroidOffsetAndLength buildAndWritePostingsLists(
158160
FieldInfo fieldInfo,
159161
CentroidSupplier centroidSupplier,
@@ -166,10 +168,14 @@ CentroidOffsetAndLength buildAndWritePostingsLists(
166168
) throws IOException {
167169
// first, quantize all the vectors into a temporary file
168170
String quantizedVectorsTempName = null;
169-
IndexOutput quantizedVectorsTemp = null;
170171
boolean success = false;
171-
try {
172-
quantizedVectorsTemp = mergeState.segmentInfo.dir.createTempOutput(mergeState.segmentInfo.name, "qvec_", IOContext.DEFAULT);
172+
try (
173+
IndexOutput quantizedVectorsTemp = mergeState.segmentInfo.dir.createTempOutput(
174+
mergeState.segmentInfo.name,
175+
"qvec_",
176+
IOContext.DEFAULT
177+
)
178+
) {
173179
quantizedVectorsTempName = quantizedVectorsTemp.getName();
174180
OptimizedScalarQuantizer quantizer = new OptimizedScalarQuantizer(fieldInfo.getVectorSimilarityFunction());
175181
int[] quantized = new int[fieldInfo.getVectorDimension()];
@@ -202,12 +208,10 @@ CentroidOffsetAndLength buildAndWritePostingsLists(
202208
writeQuantizedValue(quantizedVectorsTemp, binary, zeroResult);
203209
}
204210
}
205-
// close the temporary file so we can read it later
206-
quantizedVectorsTemp.close();
207211
success = true;
208212
} finally {
209-
if (success == false && quantizedVectorsTemp != null) {
210-
mergeState.segmentInfo.dir.deleteFile(quantizedVectorsTemp.getName());
213+
if (success == false && quantizedVectorsTempName != null) {
214+
org.apache.lucene.util.IOUtils.deleteFilesIgnoringExceptions(mergeState.segmentInfo.dir, quantizedVectorsTempName);
211215
}
212216
}
213217
int[] centroidVectorCount = new int[centroidSupplier.size()];
@@ -298,6 +302,8 @@ CentroidOffsetAndLength buildAndWritePostingsLists(
298302
printClusterQualityStatistics(assignmentsByCluster);
299303
}
300304
return new CentroidOffsetAndLength(offsets.build(), lengths.build());
305+
} finally {
306+
org.apache.lucene.util.IOUtils.deleteFilesIgnoringExceptions(mergeState.segmentInfo.dir, quantizedVectorsTempName);
301307
}
302308
}
303309

0 commit comments

Comments
 (0)