Repository navigation
TEZ-4760: IFile.Writer: buffer per-record writes before the compression and checksum streams #544
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
Open
deniskuzZ
wants to merge
2
commits into
apache:master
Choose a base branch
from
deniskuzZ:TEZ-ifile-write-perf
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
89 changes: 89 additions & 0 deletions
89
tez-common/src/main/java/org/apache/tez/common/io/NonSyncBufferedOutputStream.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,89 @@ | ||
| /* | ||
| * 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.tez.common.io; | ||
|
|
||
| import java.io.FilterOutputStream; | ||
| import java.io.IOException; | ||
| import java.io.OutputStream; | ||
|
|
||
| /** | ||
| * A thread-not-safe version of BufferedOutputStream, which removes all | ||
| * synchronized modifiers. Use it where a stream is confined to one thread and | ||
| * the writes are small and frequent: the lock is a measurable share of the cost | ||
| * when the work per call is tiny. | ||
| */ | ||
| public class NonSyncBufferedOutputStream extends FilterOutputStream { | ||
| private final byte[] buf; | ||
| private int count; | ||
|
|
||
| public NonSyncBufferedOutputStream(OutputStream stream, int size) { | ||
| super(stream); | ||
| if (size <= 0) { | ||
| throw new IllegalArgumentException("Buffer size <= 0"); | ||
| } | ||
| this.buf = new byte[size]; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| @Override | ||
| public void write(int b) throws IOException { | ||
| if (count == buf.length) { | ||
| drain(); | ||
| } | ||
| buf[count++] = (byte) b; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| * | ||
| * <p>A write at least as large as the buffer goes straight to the underlying | ||
| * stream, so a large record is not copied twice. | ||
| */ | ||
| @Override | ||
| public void write(byte[] b, int off, int len) throws IOException { | ||
| if (len >= buf.length) { | ||
| drain(); | ||
| out.write(b, off, len); | ||
| return; | ||
| } | ||
| if (len > buf.length - count) { | ||
| drain(); | ||
| } | ||
| System.arraycopy(b, off, buf, count, len); | ||
| count += len; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| @Override | ||
| public void flush() throws IOException { | ||
| drain(); | ||
| out.flush(); | ||
| } | ||
|
|
||
| private void drain() throws IOException { | ||
| if (count > 0) { | ||
| out.write(buf, 0, count); | ||
| count = 0; | ||
| } | ||
| } | ||
| } |
136 changes: 136 additions & 0 deletions
136
tez-common/src/test/java/org/apache/tez/common/io/TestNonSyncBufferedOutputStream.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,136 @@ | ||
| /* | ||
| * 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.tez.common.io; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertArrayEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
|
||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.IOException; | ||
| import java.io.OutputStream; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class TestNonSyncBufferedOutputStream { | ||
|
|
||
| private static final int SIZE = 64; | ||
|
|
||
| /** Records the size of every write that reaches the underlying stream. */ | ||
| private static final class RecordingStream extends OutputStream { | ||
| private final ByteArrayOutputStream bytes = new ByteArrayOutputStream(); | ||
| private final List<Integer> writes = new ArrayList<>(); | ||
|
|
||
| @Override | ||
| public void write(int b) { | ||
| bytes.write(b); | ||
| writes.add(1); | ||
| } | ||
|
|
||
| @Override | ||
| public void write(byte[] b, int off, int len) { | ||
| bytes.write(b, off, len); | ||
| writes.add(len); | ||
| } | ||
| } | ||
|
|
||
| private static byte[] pattern(int len, int seed) { | ||
| byte[] b = new byte[len]; | ||
| for (int i = 0; i < len; i++) { | ||
| b[i] = (byte) (seed + i); | ||
| } | ||
| return b; | ||
| } | ||
|
|
||
| @Test | ||
| public void testWriteOfExactlyBufferSizeBypassesTheBuffer() throws IOException { | ||
| RecordingStream sink = new RecordingStream(); | ||
| byte[] data = pattern(SIZE, 1); | ||
| try (NonSyncBufferedOutputStream out = new NonSyncBufferedOutputStream(sink, SIZE)) { | ||
| out.write(data, 0, data.length); | ||
| // len >= buffer: handed to the underlying stream as it is, before any flush | ||
| assertEquals(List.of(SIZE), sink.writes); | ||
| } | ||
| assertArrayEquals(data, sink.bytes.toByteArray()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testWriteLargerThanTheBufferBypassesTheBuffer() throws IOException { | ||
| RecordingStream sink = new RecordingStream(); | ||
| byte[] data = pattern(SIZE * 3 + 7, 2); | ||
| try (NonSyncBufferedOutputStream out = new NonSyncBufferedOutputStream(sink, SIZE)) { | ||
| out.write(data, 0, data.length); | ||
| assertEquals(List.of(data.length), sink.writes); | ||
| } | ||
| assertArrayEquals(data, sink.bytes.toByteArray()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testWritesThatCrossTheBufferAreDrainedOnce() throws IOException { | ||
| RecordingStream sink = new RecordingStream(); | ||
| byte[] first = pattern(SIZE - 1, 3); | ||
| byte[] second = pattern(2, 4); | ||
| try (NonSyncBufferedOutputStream out = new NonSyncBufferedOutputStream(sink, SIZE)) { | ||
| out.write(first, 0, first.length); | ||
| assertEquals(List.of(), sink.writes, "the first write fits, so nothing reaches the stream"); | ||
| out.write(second, 0, second.length); | ||
| // does not fit in what is left: the buffer is drained, then the new bytes are kept | ||
| assertEquals(List.of(SIZE - 1), sink.writes); | ||
| out.flush(); | ||
| assertEquals(List.of(SIZE - 1, 2), sink.writes); | ||
| } | ||
| byte[] expected = Arrays.copyOf(first, first.length + second.length); | ||
| System.arraycopy(second, 0, expected, first.length, second.length); | ||
| assertArrayEquals(expected, sink.bytes.toByteArray()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSingleByteWritesFillAndDrainTheBuffer() throws IOException { | ||
| RecordingStream sink = new RecordingStream(); | ||
| byte[] data = pattern(SIZE + 1, 5); | ||
| try (NonSyncBufferedOutputStream out = new NonSyncBufferedOutputStream(sink, SIZE)) { | ||
| for (byte b : data) { | ||
| out.write(b); | ||
| } | ||
| // the byte after the buffer is full drains the full buffer first | ||
| assertEquals(List.of(SIZE), sink.writes); | ||
| } | ||
| assertArrayEquals(data, sink.bytes.toByteArray()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCloseDrainsTheBuffer() throws IOException { | ||
| RecordingStream sink = new RecordingStream(); | ||
| byte[] data = pattern(3, 6); | ||
| NonSyncBufferedOutputStream out = new NonSyncBufferedOutputStream(sink, SIZE); | ||
| out.write(data, 0, data.length); | ||
| assertEquals(List.of(), sink.writes); | ||
| out.close(); | ||
| assertArrayEquals(data, sink.bytes.toByteArray()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testNonPositiveBufferSizeIsRejected() { | ||
| assertThrows(IllegalArgumentException.class, | ||
| () -> new NonSyncBufferedOutputStream(new ByteArrayOutputStream(), 0)); | ||
| } | ||
| } |
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.
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.
possible follow-up: use initial size of 4 * 1024 and grow until 64 * 1024
Uh oh!
There was an error while loading. Please reload this page.
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.
We could do more measurements in the follow-up