Skip to content

Commit

Permalink
Introduce IORunnable to fix test failure TestIndexWriterOnDiskFull.te…
Browse files Browse the repository at this point in the history
…stAddIndexOnDiskFull (#13172)

(cherry picked from commit 6ee19c0)
  • Loading branch information
easyice committed Mar 16, 2024
1 parent fb77357 commit 4d71cbd
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.apache.lucene.index.PointValues.Relation;
import org.apache.lucene.index.SegmentWriteState;
import org.apache.lucene.store.IndexOutput;
import org.apache.lucene.util.IORunnable;
import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.bkd.BKDConfig;
import org.apache.lucene.util.bkd.BKDWriter;
Expand Down Expand Up @@ -143,7 +144,7 @@ public void writeField(FieldInfo fieldInfo, PointsReader reader) throws IOExcept
values.size())) {

if (values instanceof MutablePointTree) {
Runnable finalizer =
IORunnable finalizer =
writer.writeField(
metaOut, indexOut, dataOut, fieldInfo.name, (MutablePointTree) values);
if (finalizer != null) {
Expand Down Expand Up @@ -172,7 +173,7 @@ public Relation compare(byte[] minPackedValue, byte[] maxPackedValue) {
});

// We could have 0 points on merge since all docs with dimensional fields may be deleted:
Runnable finalizer = writer.finish(metaOut, indexOut, dataOut);
IORunnable finalizer = writer.finish(metaOut, indexOut, dataOut);
if (finalizer != null) {
metaOut.writeInt(fieldInfo.number);
finalizer.run();
Expand Down Expand Up @@ -267,7 +268,7 @@ public void merge(MergeState mergeState) throws IOException {
}
}

Runnable finalizer = writer.merge(metaOut, indexOut, dataOut, docMaps, pointValues);
IORunnable finalizer = writer.merge(metaOut, indexOut, dataOut, docMaps, pointValues);
if (finalizer != null) {
metaOut.writeInt(fieldInfo.number);
finalizer.run();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.apache.lucene.index.PointValues.Relation;
import org.apache.lucene.index.SegmentWriteState;
import org.apache.lucene.store.IndexOutput;
import org.apache.lucene.util.IORunnable;
import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.bkd.BKDConfig;
import org.apache.lucene.util.bkd.BKDWriter;
Expand Down Expand Up @@ -137,7 +138,7 @@ public void writeField(FieldInfo fieldInfo, PointsReader reader) throws IOExcept
values.size())) {

if (values instanceof MutablePointTree) {
Runnable finalizer =
IORunnable finalizer =
writer.writeField(
metaOut, indexOut, dataOut, fieldInfo.name, (MutablePointTree) values);
if (finalizer != null) {
Expand Down Expand Up @@ -166,7 +167,7 @@ public Relation compare(byte[] minPackedValue, byte[] maxPackedValue) {
});

// We could have 0 points on merge since all docs with dimensional fields may be deleted:
Runnable finalizer = writer.finish(metaOut, indexOut, dataOut);
IORunnable finalizer = writer.finish(metaOut, indexOut, dataOut);
if (finalizer != null) {
metaOut.writeInt(fieldInfo.number);
finalizer.run();
Expand Down Expand Up @@ -261,7 +262,7 @@ public void merge(MergeState mergeState) throws IOException {
}
}

Runnable finalizer = writer.merge(metaOut, indexOut, dataOut, docMaps, pointValues);
IORunnable finalizer = writer.merge(metaOut, indexOut, dataOut, docMaps, pointValues);
if (finalizer != null) {
metaOut.writeInt(fieldInfo.number);
finalizer.run();
Expand Down
29 changes: 29 additions & 0 deletions lucene/core/src/java/org/apache/lucene/util/IORunnable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.lucene.util;

import java.io.IOException;

/**
* A Runnable that may throw an IOException
*
* @see java.lang.Runnable
*/
@FunctionalInterface
public interface IORunnable {
public abstract void run() throws IOException;
}
28 changes: 10 additions & 18 deletions lucene/core/src/java/org/apache/lucene/util/bkd/BKDWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import java.io.Closeable;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand All @@ -41,6 +40,7 @@
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.BytesRefBuilder;
import org.apache.lucene.util.FixedBitSet;
import org.apache.lucene.util.IORunnable;
import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.NumericUtils;
import org.apache.lucene.util.PriorityQueue;
Expand Down Expand Up @@ -424,7 +424,7 @@ private interface BKDTreeLeafNodes {
* before writing them to disk. This method does not use transient disk in order to reorder
* points.
*/
public Runnable writeField(
public IORunnable writeField(
IndexOutput metaOut,
IndexOutput indexOut,
IndexOutput dataOut,
Expand Down Expand Up @@ -493,7 +493,7 @@ private void computePackedValueBounds(

/* In the 2+D case, we recursively pick the split dimension, compute the
* median value and partition other values around it. */
private Runnable writeFieldNDims(
private IORunnable writeFieldNDims(
IndexOutput metaOut,
IndexOutput indexOut,
IndexOutput dataOut,
Expand Down Expand Up @@ -557,7 +557,7 @@ private Runnable writeFieldNDims(

/* In the 1D case, we can simply sort points in ascending order and use the
* same writing logic as we use at merge time. */
private Runnable writeField1Dim(
private IORunnable writeField1Dim(
IndexOutput metaOut,
IndexOutput indexOut,
IndexOutput dataOut,
Expand Down Expand Up @@ -596,7 +596,7 @@ public Relation compare(byte[] minPackedValue, byte[] maxPackedValue) {
* already sorted values and currently only works when numDims==1. This returns -1 if all
* documents containing dimensional values were deleted.
*/
public Runnable merge(
public IORunnable merge(
IndexOutput metaOut,
IndexOutput indexOut,
IndexOutput dataOut,
Expand Down Expand Up @@ -724,7 +724,7 @@ assert valueInOrder(
assert (lastDocID = docID) >= 0; // only assign when asserts are enabled
}

public Runnable finish() throws IOException {
public IORunnable finish() throws IOException {
if (leafCount > 0) {
writeLeafBlock(leafCardinality);
leafCardinality = 0;
Expand Down Expand Up @@ -764,11 +764,7 @@ public int numLeaves() {
}
};
return () -> {
try {
writeIndex(metaOut, indexOut, config.maxPointsInLeafNode, leafNodes, dataStartFP);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
writeIndex(metaOut, indexOut, config.maxPointsInLeafNode, leafNodes, dataStartFP);
};
}

Expand Down Expand Up @@ -880,7 +876,7 @@ private void checkMaxLeafNodeCount(int numLeaves) {
* Writes the BKD tree to the provided {@link IndexOutput}s and returns a {@link Runnable} that
* writes the index of the tree if at least one point has been added, or {@code null} otherwise.
*/
public Runnable finish(IndexOutput metaOut, IndexOutput indexOut, IndexOutput dataOut)
public IORunnable finish(IndexOutput metaOut, IndexOutput indexOut, IndexOutput dataOut)
throws IOException {
// System.out.println("\nBKDTreeWriter.finish pointCount=" + pointCount + " out=" + out + "
// heapWriter=" + heapPointWriter);
Expand Down Expand Up @@ -975,7 +971,7 @@ public Runnable finish(IndexOutput metaOut, IndexOutput indexOut, IndexOutput da
return makeWriter(metaOut, indexOut, splitDimensionValues, leafBlockFPs, dataStartFP);
}

private Runnable makeWriter(
private IORunnable makeWriter(
IndexOutput metaOut,
IndexOutput indexOut,
byte[] splitDimensionValues,
Expand Down Expand Up @@ -1007,11 +1003,7 @@ public int numLeaves() {

return () -> {
// Write index:
try {
writeIndex(metaOut, indexOut, config.maxPointsInLeafNode, leafNodes, dataStartFP);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
writeIndex(metaOut, indexOut, config.maxPointsInLeafNode, leafNodes, dataStartFP);
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.lucene.store.IndexOutput;
import org.apache.lucene.tests.util.LuceneTestCase;
import org.apache.lucene.tests.util.LuceneTestCase.Monster;
import org.apache.lucene.util.IORunnable;
import org.apache.lucene.util.NumericUtils;

// e.g. run like this: ant test -Dtestcase=Test4BBKDPoints -Dtests.nightly=true -Dtests.verbose=true
Expand Down Expand Up @@ -66,7 +67,7 @@ public void test1D() throws Exception {
}
}
IndexOutput out = dir.createOutput("1d.bkd", IOContext.DEFAULT);
Runnable finalizer = w.finish(out, out, out);
IORunnable finalizer = w.finish(out, out, out);
long indexFP = out.getFilePointer();
finalizer.run();
out.close();
Expand Down Expand Up @@ -115,7 +116,7 @@ public void test2D() throws Exception {
}
}
IndexOutput out = dir.createOutput("2d.bkd", IOContext.DEFAULT);
Runnable finalizer = w.finish(out, out, out);
IORunnable finalizer = w.finish(out, out, out);
long indexFP = out.getFilePointer();
finalizer.run();
out.close();
Expand Down
25 changes: 13 additions & 12 deletions lucene/core/src/test/org/apache/lucene/util/bkd/TestBKD.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.apache.lucene.tests.util.LuceneTestCase;
import org.apache.lucene.tests.util.TestUtil;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.IORunnable;
import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.NumericUtils;

Expand All @@ -62,7 +63,7 @@ public void testBasicInts1D() throws Exception {

long indexFP;
try (IndexOutput out = dir.createOutput("bkd", IOContext.DEFAULT)) {
Runnable finalizer = w.finish(out, out, out);
IORunnable finalizer = w.finish(out, out, out);
indexFP = out.getFilePointer();
finalizer.run();
}
Expand Down Expand Up @@ -133,7 +134,7 @@ public void testRandomIntsNDims() throws Exception {

long indexFP;
try (IndexOutput out = dir.createOutput("bkd", IOContext.DEFAULT)) {
Runnable finalizer = w.finish(out, out, out);
IORunnable finalizer = w.finish(out, out, out);
indexFP = out.getFilePointer();
finalizer.run();
}
Expand Down Expand Up @@ -228,7 +229,7 @@ public void testBigIntNDims() throws Exception {

long indexFP;
try (IndexOutput out = dir.createOutput("bkd", IOContext.DEFAULT)) {
Runnable finalizer = w.finish(out, out, out);
IORunnable finalizer = w.finish(out, out, out);
indexFP = out.getFilePointer();
finalizer.run();
}
Expand Down Expand Up @@ -735,7 +736,7 @@ private void verify(
}
final int curDocIDBase = lastDocIDBase;
docMaps.add(docID1 -> curDocIDBase + docID1);
Runnable finalizer = w.finish(out, out, out);
IORunnable finalizer = w.finish(out, out, out);
toMerge.add(out.getFilePointer());
finalizer.run();
valuesInThisSeg = TestUtil.nextInt(random(), numValues / 10, numValues / 2);
Expand All @@ -760,7 +761,7 @@ private void verify(

if (toMerge != null) {
if (segCount > 0) {
Runnable finalizer = w.finish(out, out, out);
IORunnable finalizer = w.finish(out, out, out);
toMerge.add(out.getFilePointer());
finalizer.run();
final int curDocIDBase = lastDocIDBase;
Expand All @@ -783,14 +784,14 @@ private void verify(
readers.add(getPointValues(in));
}
out = dir.createOutput("bkd2", IOContext.DEFAULT);
Runnable finalizer = w.merge(out, out, out, docMaps, readers);
IORunnable finalizer = w.merge(out, out, out, docMaps, readers);
indexFP = out.getFilePointer();
finalizer.run();
out.close();
in.close();
in = dir.openInput("bkd2", IOContext.DEFAULT);
} else {
Runnable finalizer = w.finish(out, out, out);
IORunnable finalizer = w.finish(out, out, out);
indexFP = out.getFilePointer();
finalizer.run();
out.close();
Expand Down Expand Up @@ -1204,7 +1205,7 @@ public void testTieBreakOrder() throws Exception {
}

IndexOutput out = dir.createOutput("bkd", IOContext.DEFAULT);
Runnable finalizer = w.finish(out, out, out);
IORunnable finalizer = w.finish(out, out, out);
long fp = out.getFilePointer();
finalizer.run();
out.close();
Expand Down Expand Up @@ -1270,7 +1271,7 @@ public void testCheckDataDimOptimalOrder() throws IOException {
}
final long indexFP;
try (IndexOutput out = dir.createOutput("bkd", IOContext.DEFAULT)) {
Runnable finalizer = w.finish(out, out, out);
IORunnable finalizer = w.finish(out, out, out);
indexFP = out.getFilePointer();
finalizer.run();
w.close();
Expand Down Expand Up @@ -1332,7 +1333,7 @@ public void test2DLongOrdsOffline() throws Exception {
}

IndexOutput out = dir.createOutput("bkd", IOContext.DEFAULT);
Runnable finalizer = w.finish(out, out, out);
IORunnable finalizer = w.finish(out, out, out);
long fp = out.getFilePointer();
finalizer.run();
out.close();
Expand Down Expand Up @@ -1399,7 +1400,7 @@ public void testWastedLeadingBytes() throws Exception {
}

IndexOutput out = dir.createOutput("bkd", IOContext.DEFAULT);
Runnable finalizer = w.finish(out, out, out);
IORunnable finalizer = w.finish(out, out, out);
long fp = out.getFilePointer();
finalizer.run();
out.close();
Expand Down Expand Up @@ -1467,7 +1468,7 @@ public void testEstimatePointCount() throws IOException {
}
final long indexFP;
try (IndexOutput out = dir.createOutput("bkd", IOContext.DEFAULT)) {
Runnable finalizer = w.finish(out, out, out);
IORunnable finalizer = w.finish(out, out, out);
indexFP = out.getFilePointer();
finalizer.run();
w.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import org.apache.lucene.tests.codecs.mockrandom.MockRandomPostingsFormat;
import org.apache.lucene.tests.util.LuceneTestCase;
import org.apache.lucene.tests.util.TestUtil;
import org.apache.lucene.util.IORunnable;
import org.apache.lucene.util.bkd.BKDConfig;
import org.apache.lucene.util.bkd.BKDWriter;

Expand Down Expand Up @@ -149,7 +150,7 @@ public PointValues.Relation compare(

// We could have 0 points on merge since all docs with dimensional fields may be
// deleted:
Runnable finalizer = writer.finish(metaOut, indexOut, dataOut);
IORunnable finalizer = writer.finish(metaOut, indexOut, dataOut);
if (finalizer != null) {
metaOut.writeInt(fieldInfo.number);
finalizer.run();
Expand Down

0 comments on commit 4d71cbd

Please sign in to comment.