Skip to content

Commit e99a3a2

Browse files
authored
Use a hint to indicate reading file footers only (#128816)
1 parent c34de40 commit e99a3a2

File tree

4 files changed

+15
-16
lines changed

4 files changed

+15
-16
lines changed

server/src/main/java/org/elasticsearch/index/store/FsDirectoryFactory.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,6 @@ public IndexInput openInput(String name, IOContext context) throws IOException {
163163
// we need to do these checks on the outer directory since the inner doesn't know about pending deletes
164164
ensureOpen();
165165
ensureCanRead(name);
166-
// we switch the context here since mmap checks for the READONCE context by identity
167-
context = context == Store.READONCE_CHECKSUM ? IOContext.READONCE : context;
168166
// we only use the mmap to open inputs. Everything else is managed by the NIOFSDirectory otherwise
169167
// we might run into trouble with files that are pendingDelete in one directory but still
170168
// listed in listAll() from the other. We on the other hand don't want to list files from both dirs
@@ -191,7 +189,7 @@ private static String getExtension(String name) {
191189
}
192190

193191
static boolean useDelegate(String name, IOContext ioContext) {
194-
if (ioContext == Store.READONCE_CHECKSUM) {
192+
if (ioContext.hints().contains(Store.FileFooterOnly.INSTANCE)) {
195193
// If we're just reading the footer for the checksum then mmap() isn't really necessary, and it's desperately inefficient
196194
// if pre-loading is enabled on this file.
197195
return false;

server/src/main/java/org/elasticsearch/index/store/Store.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -146,19 +146,22 @@ public class Store extends AbstractIndexShardComponent implements Closeable, Ref
146146
Property.IndexScope
147147
);
148148

149+
/**
150+
* A {@link org.apache.lucene.store.IOContext.FileOpenHint} that we will only read the Lucene file footer
151+
*/
152+
public enum FileFooterOnly implements IOContext.FileOpenHint {
153+
INSTANCE
154+
}
155+
149156
/**
150157
* Specific {@link IOContext} indicating that we will read only the Lucene file footer (containing the file checksum)
151158
* See {@link MetadataSnapshot#checksumFromLuceneFile}.
152159
*/
153-
public static final IOContext READONCE_CHECKSUM = createReadOnceContext();
154-
155-
// while equivalent, these different read once contexts are checked by identity in directory implementations
156-
private static IOContext createReadOnceContext() {
157-
var context = IOContext.READONCE.withHints(DataAccessHint.SEQUENTIAL, ReadOnceHint.INSTANCE);
158-
assert context != IOContext.READONCE;
159-
assert context.equals(IOContext.READONCE);
160-
return context;
161-
}
160+
public static final IOContext READONCE_CHECKSUM = IOContext.READONCE.withHints(
161+
DataAccessHint.SEQUENTIAL,
162+
ReadOnceHint.INSTANCE,
163+
FileFooterOnly.INSTANCE
164+
);
162165

163166
private final AtomicBoolean isClosed = new AtomicBoolean(false);
164167
private final StoreDirectory directory;
@@ -935,8 +938,6 @@ private static void checksumFromLuceneFile(
935938
boolean readFileAsHash,
936939
BytesRef writerUuid
937940
) throws IOException {
938-
// We select the read once context carefully here since these constants, while equivalent are
939-
// checked by identity in the different directory implementations.
940941
var context = file.startsWith(IndexFileNames.SEGMENTS) ? IOContext.READONCE : READONCE_CHECKSUM;
941942
try (IndexInput in = directory.openInput(file, context)) {
942943
final long length = in.length();

x-pack/plugin/searchable-snapshots/src/main/java/org/elasticsearch/xpack/searchablesnapshots/store/SearchableSnapshotDirectory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ public IndexInput openInput(final String name, final IOContext context) throws I
377377
final BytesRef content = fileInfo.metadata().hash();
378378
return new ByteArrayIndexInput("ByteArrayIndexInput(" + name + ')', content.bytes, content.offset, content.length);
379379
}
380-
if (context == Store.READONCE_CHECKSUM) {
380+
if (context.hints().contains(Store.FileFooterOnly.INSTANCE)) {
381381
return ChecksumBlobContainerIndexInput.create(fileInfo.physicalName(), fileInfo.length(), fileInfo.checksum(), context);
382382
}
383383

x-pack/plugin/searchable-snapshots/src/main/java/org/elasticsearch/xpack/searchablesnapshots/store/input/ChecksumBlobContainerIndexInput.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ private int checksumPositionOrThrow(long pos) {
115115
}
116116

117117
private static void ensureReadOnceChecksumContext(IOContext context) {
118-
if (context != Store.READONCE_CHECKSUM) {
118+
if (context.hints().contains(Store.FileFooterOnly.INSTANCE) == false) {
119119
assert false : "expected READONCE_CHECKSUM but got " + context;
120120
throw new IllegalArgumentException("ChecksumBlobContainerIndexInput should only be used with READONCE_CHECKSUM context");
121121
}

0 commit comments

Comments
 (0)