Skip to content

Commit

Permalink
Fix TestIndexWriterOnError.testIOError failure.
Browse files Browse the repository at this point in the history
Pull request apache#13406 inadvertly broke Lucene's handling of tragic exceptions by
stopping after the first `DocValuesProducer` whose `close()` calls throws an
exception, instead of keeping calling `close()` on further producers in the
list.

This moves back to the previous behavior.

Closes apache#13434
  • Loading branch information
jpountz committed May 29, 2024
1 parent b3dc915 commit 584471f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
import org.apache.lucene.codecs.DocValuesFormat;
import org.apache.lucene.codecs.DocValuesProducer;
import org.apache.lucene.internal.hppc.LongArrayList;
import org.apache.lucene.internal.hppc.LongCursor;
import org.apache.lucene.internal.hppc.LongObjectHashMap;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.IOContext;
import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.RefCount;

/**
Expand Down Expand Up @@ -76,10 +76,12 @@ synchronized DocValuesProducer getDocValuesProducer(

/** Decrement the reference count of the given {@link DocValuesProducer} generations. */
synchronized void decRef(LongArrayList dvProducersGens) throws IOException {
for (LongCursor gen : dvProducersGens) {
RefCount<DocValuesProducer> dvp = genDVProducers.get(gen.value);
assert dvp != null : "gen=" + gen;
dvp.decRef();
}
IOUtils.applyToAll(
dvProducersGens.stream().mapToObj(Long::valueOf),
gen -> {
RefCount<DocValuesProducer> dvp = genDVProducers.get(gen);
assert dvp != null : "gen=" + gen;
dvp.decRef();
});
}
}
14 changes: 11 additions & 3 deletions lucene/core/src/java/org/apache/lucene/util/IOUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Stream;
import org.apache.lucene.store.Directory;

/**
Expand Down Expand Up @@ -460,11 +461,18 @@ public static <T extends Throwable> T useOrSuppress(T first, T second) {
* The first exception thrown by the consumer is re-thrown and subsequent exceptions are
* suppressed.
*/
@SuppressWarnings("StreamToIterable")
public static <T> void applyToAll(Collection<T> collection, IOConsumer<T> consumer)
throws IOException {
applyToAll(collection.stream(), consumer);
}

/**
* Applies the consumer to all non-null elements in the stream even if an exception is thrown. The
* first exception thrown by the consumer is re-thrown and subsequent exceptions are suppressed.
*/
@SuppressWarnings("StreamToIterable")
public static <T> void applyToAll(Stream<T> stream, IOConsumer<T> consumer) throws IOException {
IOUtils.close(
collection.stream().filter(Objects::nonNull).map(t -> (Closeable) () -> consumer.accept(t))
::iterator);
stream.filter(Objects::nonNull).map(t -> (Closeable) () -> consumer.accept(t))::iterator);
}
}

0 comments on commit 584471f

Please sign in to comment.