-
Notifications
You must be signed in to change notification settings - Fork 3.8k
WriteOutBytes improvements #16698
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
adarshsanjeev
merged 12 commits into
apache:master
from
adarshsanjeev:write-out-bytes-improvement
Aug 23, 2024
Merged
WriteOutBytes improvements #16698
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
aa5ac2f
WriteOutBytes improvements
adarshsanjeev 6479a5e
Add tests
adarshsanjeev b8bae76
Handle closing better
adarshsanjeev 8b1bc68
Fix tests
adarshsanjeev a938127
Add tests
adarshsanjeev 4bf8071
Fix checkstyle
adarshsanjeev 003387c
Add tests
adarshsanjeev 71d2a25
Fix tests
adarshsanjeev ef20cbc
Reintroduce old TmpFileSegmentWriteOutMedium
imply-cheddar c80aede
Merge pull request #1 from imply-cheddar/keep-legacy-write-out-bytes
adarshsanjeev d12b9b7
Fix checkstyle
adarshsanjeev f1b9bc3
Merge remote-tracking branch 'origin/master' into write-out-bytes-imp…
adarshsanjeev 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
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
259 changes: 259 additions & 0 deletions
259
...ng/src/main/java/org/apache/druid/segment/writeout/LazilyAllocatingHeapWriteOutBytes.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,259 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.apache.druid.segment.writeout; | ||
|
|
||
| import com.google.common.annotations.VisibleForTesting; | ||
| import org.apache.commons.io.input.NullInputStream; | ||
| import org.apache.druid.error.DruidException; | ||
| import org.apache.druid.io.ByteBufferInputStream; | ||
| import org.apache.druid.java.util.common.io.Closer; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.nio.BufferOverflowException; | ||
| import java.nio.BufferUnderflowException; | ||
| import java.nio.ByteBuffer; | ||
| import java.nio.channels.WritableByteChannel; | ||
| import java.util.function.Supplier; | ||
|
|
||
| /** | ||
| * Lazily decides to use a tmpBuffer to act as WriteOutBytes, till more than certain threshold is reached. Once this is | ||
| * met, it switches to delegating all calls to a {@link WriteOutBytes} created by delegateSupplier. | ||
| * <p> | ||
| * This is useful if the data stored in the {@link WriteOutBytes} is small enough that buffering the changes in memory | ||
| * would be faster than creating some {@link WriteOutBytes}. | ||
| */ | ||
| public class LazilyAllocatingHeapWriteOutBytes extends WriteOutBytes | ||
| { | ||
| private final Supplier<WriteOutBytes> delegateSupplier; | ||
|
|
||
| private ByteBuffer tmpBuffer = null; | ||
| private WriteOutBytes delegate = null; | ||
| private boolean open = true; | ||
|
|
||
| public LazilyAllocatingHeapWriteOutBytes(Supplier<WriteOutBytes> delegateSupplier, Closer closer) | ||
| { | ||
| this.delegateSupplier = delegateSupplier; | ||
| closer.register(() -> { | ||
| open = false; | ||
| tmpBuffer = null; | ||
| delegate = null; | ||
| }); | ||
| } | ||
|
|
||
| @Override | ||
| public void writeInt(int v) throws IOException | ||
| { | ||
| checkOpen(); | ||
| final boolean useBuffer = ensureBytes(Integer.BYTES); | ||
| if (useBuffer) { | ||
| tmpBuffer.putInt(v); | ||
| return; | ||
| } | ||
|
|
||
| delegate.writeInt(v); | ||
| } | ||
|
|
||
| @Override | ||
| public long size() | ||
| { | ||
| if (delegate == null) { | ||
| return tmpBuffer == null ? 0 : tmpBuffer.position(); | ||
| } else { | ||
| return delegate.size(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void writeTo(WritableByteChannel channel) throws IOException | ||
| { | ||
| checkOpen(); | ||
| if (delegate == null) { | ||
| if (tmpBuffer == null) { | ||
| return; | ||
| } | ||
|
|
||
| final ByteBuffer tmpBufCopy = tmpBuffer.asReadOnlyBuffer(); | ||
| tmpBufCopy.flip(); | ||
| channel.write(tmpBufCopy); | ||
| } else { | ||
| delegate.writeTo(channel); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public InputStream asInputStream() throws IOException | ||
| { | ||
| checkOpen(); | ||
| if (delegate == null) { | ||
| if (tmpBuffer == null) { | ||
| return new NullInputStream(); | ||
| } | ||
| final ByteBuffer tmpBufCopy = tmpBuffer.asReadOnlyBuffer(); | ||
| tmpBufCopy.flip(); | ||
| return new ByteBufferInputStream(tmpBufCopy); | ||
| } else { | ||
| return delegate.asInputStream(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void readFully(long pos, ByteBuffer buffer) throws IOException | ||
| { | ||
| checkOpen(); | ||
| if (delegate == null) { | ||
| if (tmpBuffer == null) { | ||
| return; | ||
| } | ||
|
|
||
| if (pos <= tmpBuffer.position()) { | ||
| final ByteBuffer tmpBufCopy = tmpBuffer.asReadOnlyBuffer(); | ||
| tmpBufCopy.flip().position((int) pos); | ||
| if (tmpBufCopy.remaining() < buffer.remaining()) { | ||
| throw new BufferUnderflowException(); | ||
| } | ||
| tmpBufCopy.limit(tmpBufCopy.position() + buffer.remaining()); | ||
| buffer.put(tmpBufCopy); | ||
| } else { | ||
| throw new BufferOverflowException(); | ||
| } | ||
| } else { | ||
| delegate.readFully(pos, buffer); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void write(int b) throws IOException | ||
| { | ||
| checkOpen(); | ||
| final boolean useBuffer = ensureBytes(1); | ||
| if (useBuffer) { | ||
| tmpBuffer.put((byte) b); | ||
| return; | ||
| } | ||
|
|
||
| delegate.write(b); | ||
| } | ||
|
|
||
| @Override | ||
| public void write(byte[] b, int off, int len) throws IOException | ||
| { | ||
| write(ByteBuffer.wrap(b, off, len)); | ||
| } | ||
|
|
||
| @Override | ||
| public int write(ByteBuffer src) throws IOException | ||
| { | ||
| checkOpen(); | ||
| final int numToWrite = src.remaining(); | ||
| final boolean useBuffer = ensureBytes(numToWrite); | ||
| if (useBuffer) { | ||
| tmpBuffer.put(src); | ||
| return numToWrite; | ||
| } | ||
|
|
||
| return delegate.write(src); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isOpen() | ||
| { | ||
| return open; | ||
| } | ||
|
|
||
| private void checkOpen() | ||
| { | ||
| if (!isOpen()) { | ||
| throw DruidException.defensive("WriteOutBytes is already closed."); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Ensures bytes are available, returns a boolean indicating if the buffer should be used or the delegate | ||
| * | ||
| * @param numBytes the number of bytes that need writing | ||
| * @return boolean true if the buffer should be used, false if the delegate should be used | ||
| * @throws IOException if an issue with IO occurs (can primarily happen when copying buffers) | ||
| */ | ||
| private boolean ensureBytes(int numBytes) throws IOException | ||
| { | ||
| if (tmpBuffer == null) { | ||
| if (delegate == null) { | ||
| if (numBytes < 128) { | ||
| tmpBuffer = ByteBuffer.allocate(128); | ||
| } else if (numBytes < 4096) { | ||
| tmpBuffer = ByteBuffer.allocate(4096); | ||
| } else { | ||
| // We are likely dealing with something that's already buffering stuff, so just switch delegate immediately | ||
| delegate = delegateSupplier.get(); | ||
| return false; | ||
| } | ||
| return true; | ||
| } else { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| if (numBytes < tmpBuffer.remaining()) { | ||
| return true; | ||
| } | ||
|
|
||
| final ByteBuffer newBuf; | ||
| switch (tmpBuffer.capacity()) { | ||
| case 128: | ||
| if (numBytes < 4096 - tmpBuffer.position()) { | ||
| newBuf = ByteBuffer.allocate(4096); | ||
| break; | ||
| } | ||
| case 4096: | ||
| if (numBytes < 16384 - tmpBuffer.position()) { | ||
| newBuf = ByteBuffer.allocate(16384); | ||
| break; | ||
| } | ||
| default: | ||
| newBuf = null; | ||
| } | ||
|
|
||
| if (newBuf == null) { | ||
| delegate = delegateSupplier.get(); | ||
| tmpBuffer.flip(); | ||
| delegate.write(tmpBuffer); | ||
| tmpBuffer = null; | ||
| return false; | ||
| } else { | ||
| tmpBuffer.flip(); | ||
| newBuf.put(tmpBuffer); | ||
| tmpBuffer = newBuf; | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
| ByteBuffer getTmpBuffer() | ||
| { | ||
| return tmpBuffer; | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
| WriteOutBytes getDelegate() | ||
| { | ||
| return delegate; | ||
| } | ||
| } |
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.