-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Add SharedBytes.copyToCacheFileAligned without length method #106193
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1006,7 +1006,8 @@ public int populateAndRead( | |
| final RangeAvailableHandler reader, | ||
| final RangeMissingHandler writer | ||
| ) throws Exception { | ||
| assert assertOffsetsWithinFileLength(rangeToWrite.start(), rangeToWrite.length(), length); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be good to add
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might also be great to comment on why we allow
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Both comments make sense, I pushed fe84866 |
||
| // some cache files can grow after being created, so rangeToWrite can be larger than the initial {@code length} | ||
| assert rangeToWrite.start() >= 0 : rangeToWrite; | ||
| assert assertOffsetsWithinFileLength(rangeToRead.start(), rangeToRead.length(), length); | ||
| // We are interested in the total time that the system spends when fetching a result (including time spent queuing), so we start | ||
| // our measurement here. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,7 @@ | |
| import org.elasticsearch.common.unit.ByteSizeValue; | ||
| import org.elasticsearch.core.AbstractRefCounted; | ||
| import org.elasticsearch.core.IOUtils; | ||
| import org.elasticsearch.core.Streams; | ||
| import org.elasticsearch.core.SuppressForbidden; | ||
| import org.elasticsearch.env.Environment; | ||
| import org.elasticsearch.env.NodeEnvironment; | ||
|
|
@@ -184,6 +185,37 @@ public static void copyToCacheFileAligned( | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Copy all bytes from {@code input} to {@code fc}, only doing writes aligned along {@link #PAGE_SIZE}. | ||
| * | ||
| * @param fc output cache file reference | ||
| * @param input stream to read from | ||
| * @param fileChannelPos position in {@code fc} to write to | ||
| * @param progressUpdater callback to invoke with the number of copied bytes as they are copied | ||
| * @param buffer bytebuffer to use for writing | ||
| * @return the number of bytes copied | ||
| * @throws IOException on failure | ||
| */ | ||
| public static int copyToCacheFileAligned(IO fc, InputStream input, int fileChannelPos, IntConsumer progressUpdater, ByteBuffer buffer) | ||
| throws IOException { | ||
| int bytesCopied = 0; | ||
| while (true) { | ||
| final int bytesRead = Streams.read(input, buffer, buffer.remaining()); | ||
| if (bytesRead <= 0) { | ||
| break; | ||
| } | ||
| if (buffer.hasRemaining()) { | ||
| // ensure that last write is aligned on 4k boundaries (= page size) | ||
| final int remainder = buffer.position() % PAGE_SIZE; | ||
| final int adjustment = remainder == 0 ? 0 : PAGE_SIZE - remainder; | ||
| buffer.position(buffer.position() + adjustment); | ||
| } | ||
| bytesCopied += positionalWrite(fc, fileChannelPos + bytesCopied, buffer); | ||
| progressUpdater.accept(bytesCopied); | ||
|
Comment on lines
+207
to
+214
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mostly for my education: With the existing version of this method, we adjust We don't need the same adjustment here for the callback because
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes, we cannot update progress to a value that would exceed the pending range in the SparseFileTracker.
Yes 👍 |
||
| } | ||
| return bytesCopied; | ||
| } | ||
|
|
||
| private static int positionalWrite(IO fc, int start, ByteBuffer byteBuffer) throws IOException { | ||
| byteBuffer.flip(); | ||
| int written = fc.write(byteBuffer, start); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it make sense to reuse
computeRange(rangeSize, position, size, Long.MAX_VALUE)instead?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose it would be OK, but I prefer to have a dedicated method.