Skip to content

Commit a2676b1

Browse files
authored
Use ReadAdvice.RANDOM by default. (#13244)
This switches the default `ReadAdvice` from `NORMAL` to `RANDOM`, which is a better fit for the kind of access pattern that Lucene has. This is expected to reduce page cache trashing and contention on the page table. `NORMAL` is still available, but never used by any of the file formats.
1 parent 54a2e11 commit a2676b1

File tree

3 files changed

+16
-12
lines changed

3 files changed

+16
-12
lines changed

lucene/CHANGES.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,14 @@ Bug Fixes
157157

158158
* GITHUB#12878: Fix the declared Exceptions of Expression#evaluate() to match those
159159
of DoubleValues#doubleValue(). (Uwe Schindler)
160-
160+
161+
Changes in Runtime Behavior
162+
---------------------
163+
164+
* GITHUB#13244: IOContext now uses ReadAdvice#RANDOM by default for read
165+
operations. An implication is that `MMapDirectory` will use POSIX_MADV_RANDOM
166+
on POSIX systems. (Adrien Grand)
167+
161168
Changes in Backwards Compatibility Policy
162169
-----------------------------------------
163170

lucene/core/src/java/org/apache/lucene/store/IOContext.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public enum Context {
4949
* A default context for normal reads/writes. Use {@link #withReadAdvice(ReadAdvice)} to specify
5050
* another {@link ReadAdvice}.
5151
*/
52-
public static final IOContext DEFAULT = new IOContext(ReadAdvice.NORMAL);
52+
public static final IOContext DEFAULT = new IOContext(ReadAdvice.RANDOM);
5353

5454
/** A default context for reads with {@link ReadAdvice#SEQUENTIAL}. */
5555
public static final IOContext READONCE = new IOContext(ReadAdvice.SEQUENTIAL);
@@ -67,13 +67,10 @@ public enum Context {
6767
case FLUSH -> Objects.requireNonNull(
6868
flushInfo, "flushInfo must not be null if context is FLUSH");
6969
}
70-
if (context == Context.MERGE && readAdvice != ReadAdvice.SEQUENTIAL) {
70+
if ((context == Context.FLUSH || context == Context.MERGE)
71+
&& readAdvice != ReadAdvice.SEQUENTIAL) {
7172
throw new IllegalArgumentException(
72-
"The MERGE context must use the SEQUENTIAL read access advice");
73-
}
74-
if (context == Context.FLUSH && readAdvice != ReadAdvice.NORMAL) {
75-
throw new IllegalArgumentException(
76-
"The FLUSH context must use the NORMAL read access advice");
73+
"The FLUSH and MERGE contexts must use the SEQUENTIAL read access advice");
7774
}
7875
}
7976

@@ -84,7 +81,7 @@ private IOContext(ReadAdvice accessAdvice) {
8481

8582
/** Creates an {@link IOContext} for flushing. */
8683
public IOContext(FlushInfo flushInfo) {
87-
this(Context.FLUSH, null, flushInfo, ReadAdvice.NORMAL);
84+
this(Context.FLUSH, null, flushInfo, ReadAdvice.SEQUENTIAL);
8885
}
8986

9087
/** Creates an {@link IOContext} for merging. */

lucene/core/src/test/org/apache/lucene/store/TestMMapDirectory.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ public void testMadviseAvail() throws Exception {
9898
MMapDirectory.supportsMadvise());
9999
}
100100

101-
// Opens the input with IOContext.RANDOM to ensure basic code path coverage for POSIX_MADV_RANDOM.
102-
public void testWithRandom() throws Exception {
101+
// Opens the input with ReadAdvice.NORMAL to ensure basic code path coverage.
102+
public void testWithNormal() throws Exception {
103103
final int size = 8 * 1024;
104104
byte[] bytes = new byte[size];
105105
random().nextBytes(bytes);
@@ -110,7 +110,7 @@ public void testWithRandom() throws Exception {
110110
}
111111

112112
try (final IndexInput in =
113-
dir.openInput("test", IOContext.DEFAULT.withReadAdvice(ReadAdvice.RANDOM))) {
113+
dir.openInput("test", IOContext.DEFAULT.withReadAdvice(ReadAdvice.NORMAL))) {
114114
final byte[] readBytes = new byte[size];
115115
in.readBytes(readBytes, 0, readBytes.length);
116116
assertArrayEquals(bytes, readBytes);

0 commit comments

Comments
 (0)