-
Notifications
You must be signed in to change notification settings - Fork 25.8k
Add CompressibleBytesOutputStream for compression #24927
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 10 commits
3829a84
5e73e97
467585a
85d42dc
54cee2f
d571f7c
a84e7e3
43bd03d
d121346
fdb58f0
79c0f3d
7f1af8d
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 |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| /* | ||
| * Licensed to Elasticsearch under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch 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.elasticsearch.transport; | ||
|
|
||
| import org.apache.lucene.util.IOUtils; | ||
| import org.elasticsearch.common.bytes.BytesReference; | ||
| import org.elasticsearch.common.compress.CompressorFactory; | ||
| import org.elasticsearch.common.io.Streams; | ||
| import org.elasticsearch.common.io.stream.BytesStream; | ||
| import org.elasticsearch.common.io.stream.StreamOutput; | ||
| import org.elasticsearch.common.lease.Releasable; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.zip.DeflaterOutputStream; | ||
|
|
||
| /** | ||
| * This class exists to provide a stream with optional compression. This is useful as using compression | ||
| * requires that the underlying {@link DeflaterOutputStream} be closed to write EOS bytes. However, the | ||
| * {@link BytesStream} should not be closed yet, as we have not used the bytes. This class handles these | ||
| * intricacies. | ||
| * | ||
| * {@link CompressibleBytesOutputStream#materializeBytes()} should be called when all the bytes have been | ||
| * written to this stream. If compression is enabled, the proper EOS bytes will be written at that point. | ||
| * The underlying {@link BytesReference} will be returned. | ||
| * | ||
| * {@link CompressibleBytesOutputStream#close()} should be called when the bytes are no longer needed and | ||
| * can be safely released. | ||
| */ | ||
| final class CompressibleBytesOutputStream extends StreamOutput implements Releasable { | ||
|
|
||
| private final StreamOutput stream; | ||
| private final BytesStream bytesStreamOutput; | ||
| private final boolean shouldCompress; | ||
|
|
||
| CompressibleBytesOutputStream(BytesStream bytesStreamOutput, boolean shouldCompress) throws IOException { | ||
| this.bytesStreamOutput = bytesStreamOutput; | ||
| this.shouldCompress = shouldCompress; | ||
| if (shouldCompress) { | ||
| this.stream = CompressorFactory.COMPRESSOR.streamOutput(Streams.flushOnCloseStream(bytesStreamOutput)); | ||
|
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. Personally I don't like mixing |
||
| } else { | ||
| this.stream = bytesStreamOutput; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * This method ensures that compression is complete and returns the underlying bytes. | ||
| * | ||
| * @return bytes underlying the stream | ||
| * @throws IOException if an exception occurs when writing or flushing | ||
| */ | ||
| BytesReference materializeBytes() throws IOException { | ||
| // If we are using compression the stream needs to be closed to ensure that EOS marker bytes are written. | ||
| // The actual ReleasableBytesStreamOutput will not be closed yet as it is wrapped in flushOnCloseStream when | ||
| // passed to the deflater stream. | ||
| if (shouldCompress) { | ||
| stream.close(); | ||
| } | ||
|
|
||
| return bytesStreamOutput.bytes(); | ||
|
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. should we also close the
Contributor
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. I don't think so. The bytesStreamOutput is still being used at that point. That's why this is separating calling the compressing stream and the bytes stream. Must close the compressing stream to write EOS bytes. But wait until we are done using the underlying bytes to close the byte stream. |
||
| } | ||
|
|
||
| @Override | ||
| public void writeByte(byte b) throws IOException { | ||
| stream.write(b); | ||
| } | ||
|
|
||
| @Override | ||
| public void writeBytes(byte[] b, int offset, int length) throws IOException { | ||
| stream.writeBytes(b, offset, length); | ||
| } | ||
|
|
||
| @Override | ||
| public void flush() throws IOException { | ||
| stream.flush(); | ||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
| // If we are not using compression stream == bytesStreamOutput | ||
|
||
| if (shouldCompress == false) { | ||
| IOUtils.closeWhileHandlingException(stream); | ||
| } else { | ||
| IOUtils.closeWhileHandlingException(stream, bytesStreamOutput); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void reset() throws IOException { | ||
| stream.reset(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| /* | ||
| * Licensed to Elasticsearch under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch 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.elasticsearch.transport; | ||
|
|
||
| import org.elasticsearch.common.bytes.BytesReference; | ||
| import org.elasticsearch.common.compress.CompressorFactory; | ||
| import org.elasticsearch.common.io.stream.BytesStream; | ||
| import org.elasticsearch.common.io.stream.BytesStreamOutput; | ||
| import org.elasticsearch.common.io.stream.StreamInput; | ||
| import org.elasticsearch.test.ESTestCase; | ||
|
|
||
| import java.io.EOFException; | ||
| import java.io.IOException; | ||
|
|
||
| public class CompressibleBytesOutputStreamTests extends ESTestCase { | ||
|
|
||
| public void testStreamWithoutCompression() throws IOException { | ||
| BytesStream bStream = new ZeroOutOnCloseStream(); | ||
| CompressibleBytesOutputStream stream = new CompressibleBytesOutputStream(bStream, false); | ||
|
|
||
| byte[] expectedBytes = randomBytes(randomInt(30)); | ||
| stream.write(expectedBytes); | ||
|
|
||
| BytesReference bytesRef = stream.materializeBytes(); | ||
|
|
||
| assertFalse(CompressorFactory.COMPRESSOR.isCompressed(bytesRef)); | ||
|
|
||
| StreamInput streamInput = bytesRef.streamInput(); | ||
| byte[] actualBytes = new byte[expectedBytes.length]; | ||
| streamInput.readBytes(actualBytes, 0, expectedBytes.length); | ||
|
|
||
| assertEquals(-1, streamInput.read()); | ||
| assertArrayEquals(expectedBytes, actualBytes); | ||
| stream.close(); | ||
|
|
||
| // The bytes should be zeroed out on close | ||
| for (byte b : bytesRef.toBytesRef().bytes) { | ||
| assertEquals((byte) 0, b); | ||
| } | ||
| } | ||
|
|
||
| public void testStreamWithCompression() throws IOException { | ||
| BytesStream bStream = new ZeroOutOnCloseStream(); | ||
| CompressibleBytesOutputStream stream = new CompressibleBytesOutputStream(bStream, true); | ||
|
|
||
| byte[] expectedBytes = randomBytes(randomInt(30)); | ||
| stream.write(expectedBytes); | ||
|
|
||
| BytesReference bytesRef = stream.materializeBytes(); | ||
|
|
||
| assertTrue(CompressorFactory.COMPRESSOR.isCompressed(bytesRef)); | ||
|
|
||
| StreamInput streamInput = CompressorFactory.COMPRESSOR.streamInput(bytesRef.streamInput()); | ||
| byte[] actualBytes = new byte[expectedBytes.length]; | ||
| streamInput.readBytes(actualBytes, 0, expectedBytes.length); | ||
|
|
||
| assertEquals(-1, streamInput.read()); | ||
| assertArrayEquals(expectedBytes, actualBytes); | ||
| stream.close(); | ||
|
|
||
| // The bytes should be zeroed out on close | ||
| for (byte b : bytesRef.toBytesRef().bytes) { | ||
| assertEquals((byte) 0, b); | ||
| } | ||
| } | ||
|
|
||
| public void testCompressionWithCallingMaterializeFails() throws IOException { | ||
| BytesStream bStream = new ZeroOutOnCloseStream(); | ||
| CompressibleBytesOutputStream stream = new CompressibleBytesOutputStream(bStream, true); | ||
|
|
||
| byte[] expectedBytes = randomBytes(randomInt(30)); | ||
| stream.write(expectedBytes); | ||
|
|
||
|
|
||
| StreamInput streamInput = CompressorFactory.COMPRESSOR.streamInput(bStream.bytes().streamInput()); | ||
| byte[] actualBytes = new byte[expectedBytes.length]; | ||
| EOFException e = expectThrows(EOFException.class, () -> streamInput.readBytes(actualBytes, 0, expectedBytes.length)); | ||
| assertEquals("Unexpected end of ZLIB input stream", e.getMessage()); | ||
|
|
||
| stream.close(); | ||
| } | ||
|
|
||
| private static byte[] randomBytes(int length) { | ||
| byte[] bytes = new byte[length]; | ||
| for (int i = 0; i < bytes.length; ++i) { | ||
| bytes[i] = randomByte(); | ||
| } | ||
| return bytes; | ||
| } | ||
|
|
||
| private static class ZeroOutOnCloseStream extends BytesStreamOutput { | ||
|
|
||
| @Override | ||
| public void close() { | ||
| int size = (int) bytes.size(); | ||
| bytes.set(0, new byte[size], 0, size); | ||
| } | ||
| } | ||
| } |
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.
can you add a class level javadoc about the class and why/what it is used for