-
Notifications
You must be signed in to change notification settings - Fork 25.8k
Add POSIX madvise support and a closeable MappableByteBuffer API to improve mmap-based blob cache access with explicit prefetching #140371
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
9b0b8ed
Expose POSIX madvise via NativeAccess for blob cache usage
jimczi f7f6951
Replace MemorySegment with an explicit long address to avoid foreign …
jimczi 017490b
unused import
jimczi e3e5d3f
Expose POSIX madvise via NativeAccess for blob cache usage
jimczi bdad38c
Replace MemorySegment with an explicit long address to avoid foreign …
jimczi 515761f
unused import
jimczi 5c40e53
Revert "Replace MemorySegment with an explicit long address to avoid …
ChrisHegarty 5221318
add closeable mapped byte buffer
ChrisHegarty 7157483
Plug prefetch in shared bytes and frozen index input
jimczi ddaf755
[CI] Auto commit changes from spotless
717571e
fix uts
jimczi 68e7132
itr
ChrisHegarty 5c160df
Merge branch 'mmaped_buffer' into posix_madvise_access
ChrisHegarty f3d96c0
revert accidental
ChrisHegarty a2f6abc
Merge branch 'main' into posix_madvise_access
ChrisHegarty fe3202f
Merge branch 'main' into posix_madvise_access
ChrisHegarty 5d51e54
adapt uts
jimczi 6322ddd
[CI] Auto commit changes from spotless
3ecf8a1
fix test
ChrisHegarty c7ac00e
improve bounds checks
ChrisHegarty bef4fb8
mapped tests
ChrisHegarty 66d79f5
Merge branch 'main' into posix_madvise_access
ChrisHegarty 286b579
use a shared arena
ChrisHegarty 440c3f7
test itr
ChrisHegarty dd52ab0
Merge branch 'main' into posix_madvise_access
ChrisHegarty e5e8e16
test itr
ChrisHegarty 79f5261
Merge branch 'main' into posix_madvise_access
ChrisHegarty 32c5712
fix PosixCLib test and remove test accessor
ChrisHegarty 0176c2a
check error
ChrisHegarty 9e0cddf
remove unused ofAuto
ChrisHegarty 94209c8
add comment and mode descriptive failure message
ChrisHegarty 34c4b90
man page link
ChrisHegarty 8a70006
Merge branch 'main' into posix_madvise_access
ChrisHegarty 7c2a1f1
typo
ChrisHegarty d4561a9
itr
ChrisHegarty 4afc8cb
Merge branch 'main' into posix_madvise_access
ChrisHegarty 1140aa0
refactor - prefetch is Posix only - no-op on Windows
ChrisHegarty 9ee6e44
fix test
ChrisHegarty File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
libs/native/src/main/java/org/elasticsearch/nativeaccess/CloseableMappedByteBuffer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the "Elastic License | ||
| * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
| * Public License v 1"; you may not use this file except in compliance with, at | ||
| * your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
|
|
||
| package org.elasticsearch.nativeaccess; | ||
|
|
||
| /** A closeable buffer backed by a mapped file. */ | ||
| public interface CloseableMappedByteBuffer extends CloseableByteBuffer { | ||
|
|
||
| /** | ||
| * Returns a slice of this buffer. Closing a slice does not close it's parent. | ||
| */ | ||
| CloseableMappedByteBuffer slice(long index, long length); | ||
|
|
||
| /** | ||
| * Prefetches the given offset and length. | ||
| */ | ||
| void prefetch(long offset, long length); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
...native/src/main/java/org/elasticsearch/nativeaccess/jdk/JdkCloseableMappedByteBuffer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the "Elastic License | ||
| * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
| * Public License v 1"; you may not use this file except in compliance with, at | ||
| * your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
|
|
||
| package org.elasticsearch.nativeaccess.jdk; | ||
|
|
||
| import org.elasticsearch.nativeaccess.CloseableMappedByteBuffer; | ||
|
|
||
| import java.io.IOException; | ||
| import java.lang.foreign.Arena; | ||
| import java.lang.foreign.MemorySegment; | ||
| import java.nio.MappedByteBuffer; | ||
| import java.nio.channels.FileChannel; | ||
| import java.nio.channels.FileChannel.MapMode; | ||
| import java.util.Objects; | ||
|
|
||
| public class JdkCloseableMappedByteBuffer implements CloseableMappedByteBuffer { | ||
|
|
||
| private final Arena arena; | ||
| protected final MemorySegment segment; | ||
| private final MappedByteBuffer bufferView; | ||
|
|
||
| public static JdkCloseableMappedByteBuffer ofShared(FileChannel fileChannel, MapMode mode, long position, long size) | ||
| throws IOException { | ||
| var arena = Arena.ofShared(); | ||
| var seg = fileChannel.map(mode, position, size, arena); | ||
| return new JdkCloseableMappedByteBuffer(seg, arena); | ||
| } | ||
|
|
||
| protected JdkCloseableMappedByteBuffer(MemorySegment seg, Arena arena) { | ||
| this.arena = arena; | ||
| this.segment = seg; | ||
| this.bufferView = (MappedByteBuffer) seg.asByteBuffer(); | ||
| } | ||
|
|
||
| @Override | ||
| public MappedByteBuffer buffer() { | ||
| return bufferView; | ||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
| if (arena != null) { | ||
| arena.close(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public CloseableMappedByteBuffer slice(long index, long length) { | ||
| var slice = segment.asSlice(index, length); | ||
| return new JdkCloseableMappedByteBuffer(slice, null); // closing a slice does not close the parent. | ||
| } | ||
|
|
||
| @Override | ||
| public void prefetch(long offset, long length) { | ||
| Objects.checkFromIndexSize(offset, length, segment.byteSize()); | ||
| // no explicit action, override in subclass if needed. | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...tive/src/main/java/org/elasticsearch/nativeaccess/jdk/PosixCloseableMappedByteBuffer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the "Elastic License | ||
| * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
| * Public License v 1"; you may not use this file except in compliance with, at | ||
| * your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
|
|
||
| package org.elasticsearch.nativeaccess.jdk; | ||
|
|
||
| import org.elasticsearch.nativeaccess.lib.NativeLibraryProvider; | ||
| import org.elasticsearch.nativeaccess.lib.PosixCLibrary; | ||
|
|
||
| import java.io.IOException; | ||
| import java.lang.foreign.Arena; | ||
| import java.lang.foreign.MemorySegment; | ||
| import java.nio.channels.FileChannel; | ||
| import java.nio.channels.FileChannel.MapMode; | ||
| import java.util.Objects; | ||
|
|
||
| public class PosixCloseableMappedByteBuffer extends JdkCloseableMappedByteBuffer { | ||
|
|
||
| static final PosixCLibrary LIB = NativeLibraryProvider.instance().getLibrary(PosixCLibrary.class); | ||
| static final int PAGE_SIZE = LIB.getPageSize(); | ||
|
|
||
| public static PosixCloseableMappedByteBuffer ofShared(FileChannel fileChannel, MapMode mode, long position, long size) | ||
| throws IOException { | ||
| var arena = Arena.ofShared(); | ||
| var seg = fileChannel.map(mode, position, size, arena); | ||
| return new PosixCloseableMappedByteBuffer(seg, arena); | ||
| } | ||
|
|
||
| protected PosixCloseableMappedByteBuffer(MemorySegment seg, Arena arena) { | ||
| super(seg, arena); | ||
| } | ||
|
|
||
| @Override | ||
| public void prefetch(long offset, long length) { | ||
| Objects.checkFromIndexSize(offset, length, segment.byteSize()); | ||
| // Align offset with the page size, this is required for madvise. | ||
| // Compute the offset of the current position in the OS's page. | ||
| final long offsetInPage = (segment.address() + offset) % PAGE_SIZE; | ||
| offset -= offsetInPage; | ||
| length += offsetInPage; | ||
| if (offset < 0) { | ||
| // start of the page is before the start of this segment, ignore the first page. | ||
| offset += PAGE_SIZE; | ||
| length -= PAGE_SIZE; | ||
| if (length <= 0) { | ||
| // This segment has no data beyond the first page. | ||
| return; | ||
| } | ||
| } | ||
| int ret = LIB.madvise(segment, offset, length, PosixCLibrary.POSIX_MADV_WILLNEED); | ||
| if (ret != 0) { | ||
| int errno = LIB.errno(); | ||
| throw new RuntimeException("madvise failed with (error=" + errno + "): " + LIB.strerror(errno)); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.