kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | edfbe16841d2f37c9d5ffe0ce9bedd54ab91ba5eeb3e1fc7285b85713937f1e4 |
|---|---|
| Author | Olivier Michallat <olim7t@users.noreply.github.com> · 2016-09-20 16:45 -07:00 |
| Committer | GitHub <noreply@github.com> · 2016-09-20 16:45 -07:00 |
| Parents | 9945f5df57d4, a8c1832766ee |
12 files changed
modified changelog/README.md +1 −0
| @@ -15,6 +15,7 @@ | ||
| 15 | 15 | - [bug] JAVA-1292: 'Adjusted frame length' error breaks driver's ability to read data. |
| 16 | 16 | - [improvement] JAVA-1293: Make DecoderForStreamIdSize.MAX_FRAME_LENGTH configurable. |
| 17 | 17 | - [improvement] JAVA-1053: Add a metric for authentication errors |
| 18 | +- [improvement] JAVA-1263: Eliminate unnecessary memory copies in FrameCompressor implementations. | |
| 18 | 19 | |
| 19 | 20 | |
| 20 | 21 | ### 3.0.3 |
modified clirr-ignores.xml +12 −0
| @@ -56,4 +56,16 @@ | ||
| 56 | 56 | <justification>This class is only present if the project was compiled with JDK 8+</justification> |
| 57 | 57 | </difference> |
| 58 | 58 | |
| 59 | + <difference> | |
| 60 | + <differenceType>8001</differenceType> <!-- class removed --> | |
| 61 | + <className>com/datastax/driver/core/FrameCompressor$SnappyCompressor</className> | |
| 62 | + <justification>False positive, the enclosing class is package-private so this was never exposed</justification> | |
| 63 | + </difference> | |
| 64 | + | |
| 65 | + <difference> | |
| 66 | + <differenceType>8001</differenceType> <!-- class removed --> | |
| 67 | + <className>com/datastax/driver/core/FrameCompressor$LZ4Compressor</className> | |
| 68 | + <justification>False positive, the enclosing class is package-private so this was never exposed</justification> | |
| 69 | + </difference> | |
| 70 | + | |
| 59 | 71 | </differences> |
modified driver-core/pom.xml +1 −0
| @@ -324,6 +324,7 @@ | ||
| 324 | 324 | <include>**/ControlConnectionTest.java</include> |
| 325 | 325 | <include>**/ExtendedPeerCheckDisabledTest.java</include> |
| 326 | 326 | <include>**/FrameLengthTest.java</include> |
| 327 | + <include>**/HeapCompressionTest.java</include> | |
| 327 | 328 | </includes> |
| 328 | 329 | </configuration> |
| 329 | 330 | </plugin> |
modified driver-core/src/main/java/com/datastax/driver/core/FrameCompressor.java +14 −124
| @@ -15,138 +15,28 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | -import com.datastax.driver.core.exceptions.DriverInternalError; | |
| 19 | -import io.netty.buffer.Unpooled; | |
| 20 | -import net.jpountz.lz4.LZ4Factory; | |
| 21 | -import org.slf4j.Logger; | |
| 22 | -import org.slf4j.LoggerFactory; | |
| 23 | -import org.xerial.snappy.Snappy; | |
| 18 | +import io.netty.buffer.ByteBuf; | |
| 24 | 19 | |
| 25 | 20 | import java.io.IOException; |
| 21 | +import java.nio.ByteBuffer; | |
| 26 | 22 | |
| 27 | 23 | abstract class FrameCompressor { |
| 28 | 24 | |
| 29 | - private static final Logger logger = LoggerFactory.getLogger(FrameCompressor.class); | |
| 25 | + abstract Frame compress(Frame frame) throws IOException; | |
| 30 | 26 | |
| 31 | - public abstract Frame compress(Frame frame) throws IOException; | |
| 27 | + abstract Frame decompress(Frame frame) throws IOException; | |
| 32 | 28 | |
| 33 | - public abstract Frame decompress(Frame frame) throws IOException; | |
| 34 | - | |
| 35 | - public static class SnappyCompressor extends FrameCompressor { | |
| 36 | - | |
| 37 | - public static final SnappyCompressor instance; | |
| 38 | - | |
| 39 | - static { | |
| 40 | - SnappyCompressor i; | |
| 41 | - try { | |
| 42 | - i = new SnappyCompressor(); | |
| 43 | - } catch (NoClassDefFoundError e) { | |
| 44 | - i = null; | |
| 45 | - logger.warn("Cannot find Snappy class, you should make sure the Snappy library is in the classpath if you intend to use it. Snappy compression will not be available for the protocol."); | |
| 46 | - } catch (Throwable e) { | |
| 47 | - i = null; | |
| 48 | - logger.warn("Error loading Snappy library ({}). Snappy compression will not be available for the protocol.", e.toString()); | |
| 49 | - } | |
| 50 | - instance = i; | |
| 51 | - } | |
| 52 | - | |
| 53 | - private SnappyCompressor() { | |
| 54 | - // this would throw java.lang.NoClassDefFoundError if Snappy class | |
| 55 | - // wasn't found at runtime which should be processed by the calling method | |
| 56 | - Snappy.getNativeLibraryVersion(); | |
| 57 | - } | |
| 58 | - | |
| 59 | - @Override | |
| 60 | - public Frame compress(Frame frame) throws IOException { | |
| 61 | - byte[] input = CBUtil.readRawBytes(frame.body); | |
| 62 | - byte[] output = new byte[Snappy.maxCompressedLength(input.length)]; | |
| 63 | - | |
| 64 | - int written = Snappy.compress(input, 0, input.length, output, 0); | |
| 65 | - return frame.with(Unpooled.wrappedBuffer(output, 0, written)); | |
| 66 | - } | |
| 67 | - | |
| 68 | - @Override | |
| 69 | - public Frame decompress(Frame frame) throws IOException { | |
| 70 | - byte[] input = CBUtil.readRawBytes(frame.body); | |
| 71 | - | |
| 72 | - if (!Snappy.isValidCompressedBuffer(input, 0, input.length)) | |
| 73 | - throw new DriverInternalError("Provided frame does not appear to be Snappy compressed"); | |
| 74 | - | |
| 75 | - byte[] output = new byte[Snappy.uncompressedLength(input)]; | |
| 76 | - int size = Snappy.uncompress(input, 0, input.length, output, 0); | |
| 77 | - return frame.with(Unpooled.wrappedBuffer(output, 0, size)); | |
| 78 | - } | |
| 29 | + protected static ByteBuffer inputNioBuffer(ByteBuf buf) { | |
| 30 | + // Using internalNioBuffer(...) as we only hold the reference in this method and so can | |
| 31 | + // reduce Object allocations. | |
| 32 | + int index = buf.readerIndex(); | |
| 33 | + int len = buf.readableBytes(); | |
| 34 | + return buf.nioBufferCount() == 1 ? buf.internalNioBuffer(index, len) : buf.nioBuffer(index, len); | |
| 79 | 35 | } |
| 80 | 36 | |
| 81 | - public static class LZ4Compressor extends FrameCompressor { | |
| 82 | - | |
| 83 | - public static final LZ4Compressor instance; | |
| 84 | - | |
| 85 | - static { | |
| 86 | - LZ4Compressor i; | |
| 87 | - try { | |
| 88 | - i = new LZ4Compressor(); | |
| 89 | - } catch (NoClassDefFoundError e) { | |
| 90 | - i = null; | |
| 91 | - logger.warn("Cannot find LZ4 class, you should make sure the LZ4 library is in the classpath if you intend to use it. LZ4 compression will not be available for the protocol."); | |
| 92 | - } catch (Throwable e) { | |
| 93 | - i = null; | |
| 94 | - logger.warn("Error loading LZ4 library ({}). LZ4 compression will not be available for the protocol.", e.toString()); | |
| 95 | - } | |
| 96 | - instance = i; | |
| 97 | - } | |
| 98 | - | |
| 99 | - private static final int INTEGER_BYTES = 4; | |
| 100 | - private final net.jpountz.lz4.LZ4Compressor compressor; | |
| 101 | - private final net.jpountz.lz4.LZ4FastDecompressor decompressor; | |
| 102 | - | |
| 103 | - private LZ4Compressor() { | |
| 104 | - final LZ4Factory lz4Factory = LZ4Factory.fastestInstance(); | |
| 105 | - logger.info("Using {}", lz4Factory.toString()); | |
| 106 | - compressor = lz4Factory.fastCompressor(); | |
| 107 | - decompressor = lz4Factory.fastDecompressor(); | |
| 108 | - } | |
| 109 | - | |
| 110 | - @Override | |
| 111 | - public Frame compress(Frame frame) throws IOException { | |
| 112 | - byte[] input = CBUtil.readRawBytes(frame.body); | |
| 113 | - | |
| 114 | - int maxCompressedLength = compressor.maxCompressedLength(input.length); | |
| 115 | - byte[] output = new byte[INTEGER_BYTES + maxCompressedLength]; | |
| 116 | - | |
| 117 | - output[0] = (byte) (input.length >>> 24); | |
| 118 | - output[1] = (byte) (input.length >>> 16); | |
| 119 | - output[2] = (byte) (input.length >>> 8); | |
| 120 | - output[3] = (byte) (input.length); | |
| 121 | - | |
| 122 | - try { | |
| 123 | - int written = compressor.compress(input, 0, input.length, output, INTEGER_BYTES, maxCompressedLength); | |
| 124 | - return frame.with(Unpooled.wrappedBuffer(output, 0, INTEGER_BYTES + written)); | |
| 125 | - } catch (Exception e) { | |
| 126 | - throw new IOException(e); | |
| 127 | - } | |
| 128 | - } | |
| 129 | - | |
| 130 | - @Override | |
| 131 | - public Frame decompress(Frame frame) throws IOException { | |
| 132 | - byte[] input = CBUtil.readRawBytes(frame.body); | |
| 133 | - | |
| 134 | - int uncompressedLength = ((input[0] & 0xFF) << 24) | |
| 135 | - | ((input[1] & 0xFF) << 16) | |
| 136 | - | ((input[2] & 0xFF) << 8) | |
| 137 | - | ((input[3] & 0xFF)); | |
| 138 | - | |
| 139 | - byte[] output = new byte[uncompressedLength]; | |
| 140 | - | |
| 141 | - try { | |
| 142 | - int read = decompressor.decompress(input, INTEGER_BYTES, output, 0, uncompressedLength); | |
| 143 | - if (read != input.length - INTEGER_BYTES) | |
| 144 | - throw new IOException("Compressed lengths mismatch"); | |
| 145 | - | |
| 146 | - return frame.with(Unpooled.wrappedBuffer(output)); | |
| 147 | - } catch (Exception e) { | |
| 148 | - throw new IOException(e); | |
| 149 | - } | |
| 150 | - } | |
| 37 | + protected static ByteBuffer outputNioBuffer(ByteBuf buf) { | |
| 38 | + int index = buf.writerIndex(); | |
| 39 | + int len = buf.writableBytes(); | |
| 40 | + return buf.nioBufferCount() == 1 ? buf.internalNioBuffer(index, len) : buf.nioBuffer(index, len); | |
| 151 | 41 | } |
| 152 | 42 | } |
added driver-core/src/main/java/com/datastax/driver/core/LZ4Compressor.java +182 −0
| @@ -0,0 +1,182 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (C) 2012-2015 DataStax Inc. | |
| 3 | + * | |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | + * you may not use this file except in compliance with the License. | |
| 6 | + * You may obtain a copy of the License at | |
| 7 | + * | |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | + * | |
| 10 | + * Unless required by applicable law or agreed to in writing, software | |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | + * See the License for the specific language governing permissions and | |
| 14 | + * limitations under the License. | |
| 15 | + */ | |
| 16 | +package com.datastax.driver.core; | |
| 17 | + | |
| 18 | +import io.netty.buffer.ByteBuf; | |
| 19 | +import net.jpountz.lz4.LZ4Factory; | |
| 20 | +import org.slf4j.Logger; | |
| 21 | +import org.slf4j.LoggerFactory; | |
| 22 | + | |
| 23 | +import java.io.IOException; | |
| 24 | +import java.nio.ByteBuffer; | |
| 25 | + | |
| 26 | +class LZ4Compressor extends FrameCompressor { | |
| 27 | + | |
| 28 | + private static final Logger logger = LoggerFactory.getLogger(LZ4Compressor.class); | |
| 29 | + | |
| 30 | + static final LZ4Compressor instance; | |
| 31 | + | |
| 32 | + static { | |
| 33 | + LZ4Compressor i; | |
| 34 | + try { | |
| 35 | + i = new LZ4Compressor(); | |
| 36 | + } catch (NoClassDefFoundError e) { | |
| 37 | + i = null; | |
| 38 | + logger.warn("Cannot find LZ4 class, you should make sure the LZ4 library is in the classpath if you intend to use it. LZ4 compression will not be available for the protocol."); | |
| 39 | + } catch (Throwable e) { | |
| 40 | + i = null; | |
| 41 | + logger.warn("Error loading LZ4 library ({}). LZ4 compression will not be available for the protocol.", e.toString()); | |
| 42 | + } | |
| 43 | + instance = i; | |
| 44 | + } | |
| 45 | + | |
| 46 | + private static final int INTEGER_BYTES = 4; | |
| 47 | + private final net.jpountz.lz4.LZ4Compressor compressor; | |
| 48 | + private final net.jpountz.lz4.LZ4FastDecompressor decompressor; | |
| 49 | + | |
| 50 | + private LZ4Compressor() { | |
| 51 | + final LZ4Factory lz4Factory = LZ4Factory.fastestInstance(); | |
| 52 | + logger.info("Using {}", lz4Factory.toString()); | |
| 53 | + compressor = lz4Factory.fastCompressor(); | |
| 54 | + decompressor = lz4Factory.fastDecompressor(); | |
| 55 | + } | |
| 56 | + | |
| 57 | + @Override | |
| 58 | + Frame compress(Frame frame) throws IOException { | |
| 59 | + ByteBuf input = frame.body; | |
| 60 | + | |
| 61 | + // TODO: JAVA-1306: Use the same API calls for direct and heap buffers when LZ4 updated. | |
| 62 | + ByteBuf frameBody = input.isDirect() ? compressDirect(input) : compressHeap(input); | |
| 63 | + return frame.with(frameBody); | |
| 64 | + } | |
| 65 | + | |
| 66 | + private ByteBuf compressDirect(ByteBuf input) throws IOException { | |
| 67 | + int maxCompressedLength = compressor.maxCompressedLength(input.readableBytes()); | |
| 68 | + // If the input is direct we will allocate a direct output buffer as well as this will allow us to use | |
| 69 | + // LZ4Compressor.compress and so eliminate memory copies. | |
| 70 | + ByteBuf output = input.alloc().directBuffer(INTEGER_BYTES + maxCompressedLength); | |
| 71 | + try { | |
| 72 | + ByteBuffer in = inputNioBuffer(input); | |
| 73 | + // Increase reader index. | |
| 74 | + input.readerIndex(input.writerIndex()); | |
| 75 | + | |
| 76 | + output.writeInt(in.remaining()); | |
| 77 | + | |
| 78 | + ByteBuffer out = outputNioBuffer(output); | |
| 79 | + int written = compressor.compress(in, in.position(), in.remaining(), out, out.position(), out.remaining()); | |
| 80 | + // Set the writer index so the amount of written bytes is reflected | |
| 81 | + output.writerIndex(output.writerIndex() + written); | |
| 82 | + } catch (Exception e) { | |
| 83 | + // release output buffer so we not leak and rethrow exception. | |
| 84 | + output.release(); | |
| 85 | + throw new IOException(e); | |
| 86 | + } | |
| 87 | + return output; | |
| 88 | + } | |
| 89 | + | |
| 90 | + private ByteBuf compressHeap(ByteBuf input) throws IOException { | |
| 91 | + int maxCompressedLength = compressor.maxCompressedLength(input.readableBytes()); | |
| 92 | + | |
| 93 | + // Not a direct buffer so use byte arrays... | |
| 94 | + int inOffset = input.arrayOffset() + input.readerIndex(); | |
| 95 | + byte[] in = input.array(); | |
| 96 | + int len = input.readableBytes(); | |
| 97 | + // Increase reader index. | |
| 98 | + input.readerIndex(input.writerIndex()); | |
| 99 | + | |
| 100 | + // Allocate a heap buffer from the ByteBufAllocator as we may use a PooledByteBufAllocator and so | |
| 101 | + // can eliminate the overhead of allocate a new byte[]. | |
| 102 | + ByteBuf output = input.alloc().heapBuffer(INTEGER_BYTES + maxCompressedLength); | |
| 103 | + try { | |
| 104 | + output.writeInt(len); | |
| 105 | + // calculate the correct offset. | |
| 106 | + int offset = output.arrayOffset() + output.writerIndex(); | |
| 107 | + byte[] out = output.array(); | |
| 108 | + int written = compressor.compress(in, inOffset, len, out, offset); | |
| 109 | + | |
| 110 | + // Set the writer index so the amount of written bytes is reflected | |
| 111 | + output.writerIndex(output.writerIndex() + written); | |
| 112 | + } catch (Exception e) { | |
| 113 | + // release output buffer so we not leak and rethrow exception. | |
| 114 | + output.release(); | |
| 115 | + throw new IOException(e); | |
| 116 | + } | |
| 117 | + return output; | |
| 118 | + } | |
| 119 | + | |
| 120 | + @Override | |
| 121 | + Frame decompress(Frame frame) throws IOException { | |
| 122 | + ByteBuf input = frame.body; | |
| 123 | + | |
| 124 | + // TODO: JAVA-1306: Use the same API calls for direct and heap buffers when LZ4 updated. | |
| 125 | + ByteBuf frameBody = input.isDirect() ? decompressDirect(input) : decompressHeap(input); | |
| 126 | + return frame.with(frameBody); | |
| 127 | + } | |
| 128 | + | |
| 129 | + private ByteBuf decompressDirect(ByteBuf input) throws IOException { | |
| 130 | + // If the input is direct we will allocate a direct output buffer as well as this will allow us to use | |
| 131 | + // LZ4Compressor.decompress and so eliminate memory copies. | |
| 132 | + int readable = input.readableBytes(); | |
| 133 | + int uncompressedLength = input.readInt(); | |
| 134 | + ByteBuffer in = inputNioBuffer(input); | |
| 135 | + // Increase reader index. | |
| 136 | + input.readerIndex(input.writerIndex()); | |
| 137 | + ByteBuf output = input.alloc().directBuffer(uncompressedLength); | |
| 138 | + try { | |
| 139 | + ByteBuffer out = outputNioBuffer(output); | |
| 140 | + int read = decompressor.decompress(in, in.position(), out, out.position(), out.remaining()); | |
| 141 | + if (read != readable - INTEGER_BYTES) | |
| 142 | + throw new IOException("Compressed lengths mismatch"); | |
| 143 | + | |
| 144 | + // Set the writer index so the amount of written bytes is reflected | |
| 145 | + output.writerIndex(output.writerIndex() + uncompressedLength); | |
| 146 | + } catch (Exception e) { | |
| 147 | + // release output buffer so we not leak and rethrow exception. | |
| 148 | + output.release(); | |
| 149 | + throw new IOException(e); | |
| 150 | + } | |
| 151 | + return output; | |
| 152 | + } | |
| 153 | + | |
| 154 | + private ByteBuf decompressHeap(ByteBuf input) throws IOException { | |
| 155 | + // Not a direct buffer so use byte arrays... | |
| 156 | + byte[] in = input.array(); | |
| 157 | + int len = input.readableBytes(); | |
| 158 | + int uncompressedLength = input.readInt(); | |
| 159 | + int inOffset = input.arrayOffset() + input.readerIndex(); | |
| 160 | + // Increase reader index. | |
| 161 | + input.readerIndex(input.writerIndex()); | |
| 162 | + | |
| 163 | + // Allocate a heap buffer from the ByteBufAllocator as we may use a PooledByteBufAllocator and so | |
| 164 | + // can eliminate the overhead of allocate a new byte[]. | |
| 165 | + ByteBuf output = input.alloc().heapBuffer(uncompressedLength); | |
| 166 | + try { | |
| 167 | + int offset = output.arrayOffset() + output.writerIndex(); | |
| 168 | + byte out[] = output.array(); | |
| 169 | + int read = decompressor.decompress(in, inOffset, out, offset, uncompressedLength); | |
| 170 | + if (read != len - INTEGER_BYTES) | |
| 171 | + throw new IOException("Compressed lengths mismatch"); | |
| 172 | + | |
| 173 | + // Set the writer index so the amount of written bytes is reflected | |
| 174 | + output.writerIndex(output.writerIndex() + uncompressedLength); | |
| 175 | + } catch (Exception e) { | |
| 176 | + // release output buffer so we not leak and rethrow exception. | |
| 177 | + output.release(); | |
| 178 | + throw new IOException(e); | |
| 179 | + } | |
| 180 | + return output; | |
| 181 | + } | |
| 182 | +} |
modified driver-core/src/main/java/com/datastax/driver/core/ProtocolOptions.java +2 −2
| @@ -41,7 +41,7 @@ | ||
| 41 | 41 | SNAPPY("snappy") { |
| 42 | 42 | @Override |
| 43 | 43 | FrameCompressor compressor() { |
| 44 | - return FrameCompressor.SnappyCompressor.instance; | |
| 44 | + return SnappyCompressor.instance; | |
| 45 | 45 | } |
| 46 | 46 | }, |
| 47 | 47 | /** |
| @@ -50,7 +50,7 @@ | ||
| 50 | 50 | LZ4("lz4") { |
| 51 | 51 | @Override |
| 52 | 52 | FrameCompressor compressor() { |
| 53 | - return FrameCompressor.LZ4Compressor.instance; | |
| 53 | + return LZ4Compressor.instance; | |
| 54 | 54 | } |
| 55 | 55 | }; |
| 56 | 56 |
added driver-core/src/main/java/com/datastax/driver/core/SnappyCompressor.java +170 −0
| @@ -0,0 +1,170 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (C) 2012-2015 DataStax Inc. | |
| 3 | + * | |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | + * you may not use this file except in compliance with the License. | |
| 6 | + * You may obtain a copy of the License at | |
| 7 | + * | |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | + * | |
| 10 | + * Unless required by applicable law or agreed to in writing, software | |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | + * See the License for the specific language governing permissions and | |
| 14 | + * limitations under the License. | |
| 15 | + */ | |
| 16 | +package com.datastax.driver.core; | |
| 17 | + | |
| 18 | +import com.datastax.driver.core.exceptions.DriverInternalError; | |
| 19 | +import io.netty.buffer.ByteBuf; | |
| 20 | +import org.slf4j.Logger; | |
| 21 | +import org.slf4j.LoggerFactory; | |
| 22 | +import org.xerial.snappy.Snappy; | |
| 23 | + | |
| 24 | +import java.io.IOException; | |
| 25 | +import java.nio.ByteBuffer; | |
| 26 | + | |
| 27 | +class SnappyCompressor extends FrameCompressor { | |
| 28 | + | |
| 29 | + private static final Logger logger = LoggerFactory.getLogger(SnappyCompressor.class); | |
| 30 | + | |
| 31 | + static final SnappyCompressor instance; | |
| 32 | + | |
| 33 | + static { | |
| 34 | + SnappyCompressor i; | |
| 35 | + try { | |
| 36 | + i = new SnappyCompressor(); | |
| 37 | + } catch (NoClassDefFoundError e) { | |
| 38 | + i = null; | |
| 39 | + logger.warn("Cannot find Snappy class, you should make sure the Snappy library is in the classpath if you intend to use it. Snappy compression will not be available for the protocol."); | |
| 40 | + } catch (Throwable e) { | |
| 41 | + i = null; | |
| 42 | + logger.warn("Error loading Snappy library ({}). Snappy compression will not be available for the protocol.", e.toString()); | |
| 43 | + } | |
| 44 | + instance = i; | |
| 45 | + } | |
| 46 | + | |
| 47 | + private SnappyCompressor() { | |
| 48 | + // this would throw java.lang.NoClassDefFoundError if Snappy class | |
| 49 | + // wasn't found at runtime which should be processed by the calling method | |
| 50 | + Snappy.getNativeLibraryVersion(); | |
| 51 | + } | |
| 52 | + | |
| 53 | + @Override | |
| 54 | + Frame compress(Frame frame) throws IOException { | |
| 55 | + ByteBuf input = frame.body; | |
| 56 | + ByteBuf frameBody = input.isDirect() ? compressDirect(input) : compressHeap(input); | |
| 57 | + return frame.with(frameBody); | |
| 58 | + } | |
| 59 | + | |
| 60 | + private ByteBuf compressDirect(ByteBuf input) throws IOException { | |
| 61 | + int maxCompressedLength = Snappy.maxCompressedLength(input.readableBytes()); | |
| 62 | + // If the input is direct we will allocate a direct output buffer as well as this will allow us to use | |
| 63 | + // Snappy.compress(ByteBuffer, ByteBuffer) and so eliminate memory copies. | |
| 64 | + ByteBuf output = input.alloc().directBuffer(maxCompressedLength); | |
| 65 | + try { | |
| 66 | + ByteBuffer in = inputNioBuffer(input); | |
| 67 | + // Increase reader index. | |
| 68 | + input.readerIndex(input.writerIndex()); | |
| 69 | + | |
| 70 | + ByteBuffer out = outputNioBuffer(output); | |
| 71 | + int written = Snappy.compress(in, out); | |
| 72 | + // Set the writer index so the amount of written bytes is reflected | |
| 73 | + output.writerIndex(output.writerIndex() + written); | |
| 74 | + } catch (IOException e) { | |
| 75 | + // release output buffer so we not leak and rethrow exception. | |
| 76 | + output.release(); | |
| 77 | + throw e; | |
| 78 | + } | |
| 79 | + return output; | |
| 80 | + } | |
| 81 | + | |
| 82 | + private ByteBuf compressHeap(ByteBuf input) throws IOException { | |
| 83 | + int maxCompressedLength = Snappy.maxCompressedLength(input.readableBytes()); | |
| 84 | + int inOffset = input.arrayOffset() + input.readerIndex(); | |
| 85 | + byte[] in = input.array(); | |
| 86 | + int len = input.readableBytes(); | |
| 87 | + // Increase reader index. | |
| 88 | + input.readerIndex(input.writerIndex()); | |
| 89 | + | |
| 90 | + // Allocate a heap buffer from the ByteBufAllocator as we may use a PooledByteBufAllocator and so | |
| 91 | + // can eliminate the overhead of allocate a new byte[]. | |
| 92 | + ByteBuf output = input.alloc().heapBuffer(maxCompressedLength); | |
| 93 | + try { | |
| 94 | + // Calculate the correct offset. | |
| 95 | + int offset = output.arrayOffset() + output.writerIndex(); | |
| 96 | + byte[] out = output.array(); | |
| 97 | + int written = Snappy.compress(in, inOffset, len, out, offset); | |
| 98 | + | |
| 99 | + // Increase the writerIndex with the written bytes. | |
| 100 | + output.writerIndex(output.writerIndex() + written); | |
| 101 | + } catch (IOException e) { | |
| 102 | + // release output buffer so we not leak and rethrow exception. | |
| 103 | + output.release(); | |
| 104 | + throw e; | |
| 105 | + } | |
| 106 | + return output; | |
| 107 | + } | |
| 108 | + | |
| 109 | + @Override | |
| 110 | + Frame decompress(Frame frame) throws IOException { | |
| 111 | + ByteBuf input = frame.body; | |
| 112 | + ByteBuf frameBody = input.isDirect() ? decompressDirect(input) : decompressHeap(input); | |
| 113 | + return frame.with(frameBody); | |
| 114 | + } | |
| 115 | + | |
| 116 | + private ByteBuf decompressDirect(ByteBuf input) throws IOException { | |
| 117 | + ByteBuffer in = inputNioBuffer(input); | |
| 118 | + // Increase reader index. | |
| 119 | + input.readerIndex(input.writerIndex()); | |
| 120 | + | |
| 121 | + if (!Snappy.isValidCompressedBuffer(in)) | |
| 122 | + throw new DriverInternalError("Provided frame does not appear to be Snappy compressed"); | |
| 123 | + | |
| 124 | + // If the input is direct we will allocate a direct output buffer as well as this will allow us to use | |
| 125 | + // Snappy.compress(ByteBuffer, ByteBuffer) and so eliminate memory copies. | |
| 126 | + ByteBuf output = input.alloc().directBuffer(Snappy.uncompressedLength(in)); | |
| 127 | + try { | |
| 128 | + ByteBuffer out = outputNioBuffer(output); | |
| 129 | + | |
| 130 | + int size = Snappy.uncompress(in, out); | |
| 131 | + // Set the writer index so the amount of written bytes is reflected | |
| 132 | + output.writerIndex(output.writerIndex() + size); | |
| 133 | + } catch (IOException e) { | |
| 134 | + // release output buffer so we not leak and rethrow exception. | |
| 135 | + output.release(); | |
| 136 | + throw e; | |
| 137 | + } | |
| 138 | + return output; | |
| 139 | + } | |
| 140 | + | |
| 141 | + private ByteBuf decompressHeap(ByteBuf input) throws IOException { | |
| 142 | + // Not a direct buffer so use byte arrays... | |
| 143 | + int inOffset = input.arrayOffset() + input.readerIndex(); | |
| 144 | + byte[] in = input.array(); | |
| 145 | + int len = input.readableBytes(); | |
| 146 | + // Increase reader index. | |
| 147 | + input.readerIndex(input.writerIndex()); | |
| 148 | + | |
| 149 | + if (!Snappy.isValidCompressedBuffer(in, inOffset, len)) | |
| 150 | + throw new DriverInternalError("Provided frame does not appear to be Snappy compressed"); | |
| 151 | + | |
| 152 | + // Allocate a heap buffer from the ByteBufAllocator as we may use a PooledByteBufAllocator and so | |
| 153 | + // can eliminate the overhead of allocate a new byte[]. | |
| 154 | + ByteBuf output = input.alloc().heapBuffer(Snappy.uncompressedLength(in, inOffset, len)); | |
| 155 | + try { | |
| 156 | + // Calculate the correct offset. | |
| 157 | + int offset = output.arrayOffset() + output.writerIndex(); | |
| 158 | + byte[] out = output.array(); | |
| 159 | + int written = Snappy.uncompress(in, inOffset, len, out, offset); | |
| 160 | + | |
| 161 | + // Increase the writerIndex with the written bytes. | |
| 162 | + output.writerIndex(output.writerIndex() + written); | |
| 163 | + } catch (IOException e) { | |
| 164 | + // release output buffer so we not leak and rethrow exception. | |
| 165 | + output.release(); | |
| 166 | + throw e; | |
| 167 | + } | |
| 168 | + return output; | |
| 169 | + } | |
| 170 | +} |
added driver-core/src/test/java/com/datastax/driver/core/CompressionTest.java +55 −0
| @@ -0,0 +1,55 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (C) 2012-2015 DataStax Inc. | |
| 3 | + * | |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | + * you may not use this file except in compliance with the License. | |
| 6 | + * You may obtain a copy of the License at | |
| 7 | + * | |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | + * | |
| 10 | + * Unless required by applicable law or agreed to in writing, software | |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | + * See the License for the specific language governing permissions and | |
| 14 | + * limitations under the License. | |
| 15 | + */ | |
| 16 | +package com.datastax.driver.core; | |
| 17 | + | |
| 18 | +import java.util.Locale; | |
| 19 | + | |
| 20 | +import static com.datastax.driver.core.SessionTest.checkExecuteResultSet; | |
| 21 | +import static org.assertj.core.api.Assertions.assertThat; | |
| 22 | + | |
| 23 | +public class CompressionTest extends CCMTestsSupport { | |
| 24 | + | |
| 25 | + private static String TABLE = "test"; | |
| 26 | + | |
| 27 | + public void onTestContextInitialized() { | |
| 28 | + execute(String.format("CREATE TABLE %s (k text PRIMARY KEY, t text, i int, f float)", TABLE)); | |
| 29 | + } | |
| 30 | + | |
| 31 | + void compressionTest(ProtocolOptions.Compression compression) { | |
| 32 | + cluster().getConfiguration().getProtocolOptions().setCompression(compression); | |
| 33 | + try { | |
| 34 | + Session compressedSession = cluster().connect(keyspace); | |
| 35 | + | |
| 36 | + // Simple calls to all versions of the execute/executeAsync methods | |
| 37 | + String key = "execute_compressed_test_" + compression; | |
| 38 | + ResultSet rs = compressedSession.execute(String.format(Locale.US, "INSERT INTO %s (k, t, i, f) VALUES ('%s', '%s', %d, %f)", TABLE, key, "foo", 42, 24.03f)); | |
| 39 | + assertThat(rs.isExhausted()).isTrue(); | |
| 40 | + | |
| 41 | + String SELECT_ALL = String.format(TestUtils.SELECT_ALL_FORMAT + " WHERE k = '%s'", TABLE, key); | |
| 42 | + | |
| 43 | + // execute | |
| 44 | + checkExecuteResultSet(compressedSession.execute(SELECT_ALL), key); | |
| 45 | + checkExecuteResultSet(compressedSession.execute(new SimpleStatement(SELECT_ALL).setConsistencyLevel(ConsistencyLevel.ONE)), key); | |
| 46 | + | |
| 47 | + // executeAsync | |
| 48 | + checkExecuteResultSet(compressedSession.executeAsync(SELECT_ALL).getUninterruptibly(), key); | |
| 49 | + checkExecuteResultSet(compressedSession.executeAsync(new SimpleStatement(SELECT_ALL).setConsistencyLevel(ConsistencyLevel.ONE)).getUninterruptibly(), key); | |
| 50 | + | |
| 51 | + } finally { | |
| 52 | + cluster().getConfiguration().getProtocolOptions().setCompression(ProtocolOptions.Compression.NONE); | |
| 53 | + } | |
| 54 | + } | |
| 55 | +} |
added driver-core/src/test/java/com/datastax/driver/core/DirectCompressionTest.java +47 −0
| @@ -0,0 +1,47 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (C) 2012-2015 DataStax Inc. | |
| 3 | + * | |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | + * you may not use this file except in compliance with the License. | |
| 6 | + * You may obtain a copy of the License at | |
| 7 | + * | |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | + * | |
| 10 | + * Unless required by applicable law or agreed to in writing, software | |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | + * See the License for the specific language governing permissions and | |
| 14 | + * limitations under the License. | |
| 15 | + */ | |
| 16 | +package com.datastax.driver.core; | |
| 17 | + | |
| 18 | +import com.datastax.driver.core.utils.CassandraVersion; | |
| 19 | +import org.testng.annotations.Test; | |
| 20 | + | |
| 21 | +public class DirectCompressionTest extends CompressionTest { | |
| 22 | + | |
| 23 | + /** | |
| 24 | + * Validates that a session can be established using snappy compression and executes some queries that inserts and | |
| 25 | + * retrieves data using that session(). | |
| 26 | + * | |
| 27 | + * @test_category connection:compression | |
| 28 | + * @expected_result session established and queries made successfully using it. | |
| 29 | + */ | |
| 30 | + @Test(groups = "short") | |
| 31 | + public void should_function_with_snappy_compression() throws Exception { | |
| 32 | + compressionTest(ProtocolOptions.Compression.SNAPPY); | |
| 33 | + } | |
| 34 | + | |
| 35 | + /** | |
| 36 | + * Validates that a session can be established using lz4 compression and executes some queries that inserts and | |
| 37 | + * retrieves data using that session(). | |
| 38 | + * | |
| 39 | + * @test_category connection:compression | |
| 40 | + * @expected_result session established and queries made successfully using it. | |
| 41 | + */ | |
| 42 | + @Test(groups = "short") | |
| 43 | + @CassandraVersion(major = 2.0) | |
| 44 | + public void should_function_with_lz4_compression() throws Exception { | |
| 45 | + compressionTest(ProtocolOptions.Compression.LZ4); | |
| 46 | + } | |
| 47 | +} |
added driver-core/src/test/java/com/datastax/driver/core/HeapCompressionTest.java +54 −0
| @@ -0,0 +1,54 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (C) 2012-2015 DataStax Inc. | |
| 3 | + * | |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | + * you may not use this file except in compliance with the License. | |
| 6 | + * You may obtain a copy of the License at | |
| 7 | + * | |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | + * | |
| 10 | + * Unless required by applicable law or agreed to in writing, software | |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | + * See the License for the specific language governing permissions and | |
| 14 | + * limitations under the License. | |
| 15 | + */ | |
| 16 | +package com.datastax.driver.core; | |
| 17 | + | |
| 18 | +import com.datastax.driver.core.utils.CassandraVersion; | |
| 19 | +import org.testng.annotations.BeforeClass; | |
| 20 | +import org.testng.annotations.Test; | |
| 21 | + | |
| 22 | +public class HeapCompressionTest extends CompressionTest { | |
| 23 | + | |
| 24 | + @BeforeClass(groups = "isolated") | |
| 25 | + public void beforeTestClass() throws Exception { | |
| 26 | + // Configure with noPreferDirect and noUnsafe to force heap buffers. | |
| 27 | + System.setProperty("io.netty.noPreferDirect", "true"); | |
| 28 | + System.setProperty("io.netty.noUnsafe", "true"); | |
| 29 | + super.beforeTestClass(); | |
| 30 | + } | |
| 31 | + | |
| 32 | + /** | |
| 33 | + * Validates that snappy compression still works when using heap buffers. | |
| 34 | + * | |
| 35 | + * @test_category connection:compression | |
| 36 | + * @expected_result session established and queries made successfully using it. | |
| 37 | + */ | |
| 38 | + @Test(groups = "isolated") | |
| 39 | + public void should_function_with_snappy_compression() throws Exception { | |
| 40 | + compressionTest(ProtocolOptions.Compression.SNAPPY); | |
| 41 | + } | |
| 42 | + | |
| 43 | + /** | |
| 44 | + * Validates that lz4 compression still works when using heap buffers. | |
| 45 | + * | |
| 46 | + * @test_category connection:compression | |
| 47 | + * @expected_result session established and queries made successfully using it. | |
| 48 | + */ | |
| 49 | + @Test(groups = "isolated") | |
| 50 | + @CassandraVersion(major = 2.0) | |
| 51 | + public void should_function_with_lz4_compression() throws Exception { | |
| 52 | + compressionTest(ProtocolOptions.Compression.LZ4); | |
| 53 | + } | |
| 54 | +} |
modified driver-core/src/test/java/com/datastax/driver/core/SessionTest.java +1 −52
| @@ -16,7 +16,6 @@ | ||
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | 18 | import com.datastax.driver.core.exceptions.SyntaxError; |
| 19 | -import com.datastax.driver.core.utils.CassandraVersion; | |
| 20 | 19 | import com.google.common.util.concurrent.ListenableFuture; |
| 21 | 20 | import org.testng.annotations.Test; |
| 22 | 21 | |
| @@ -81,7 +80,7 @@ | ||
| 81 | 80 | checkExecuteResultSet(session().executeAsync(bs.setConsistencyLevel(ConsistencyLevel.ONE)).getUninterruptibly(), key); |
| 82 | 81 | } |
| 83 | 82 | |
| 84 | - private static void checkExecuteResultSet(ResultSet rs, String key) { | |
| 83 | + static void checkExecuteResultSet(ResultSet rs, String key) { | |
| 85 | 84 | assertThat(rs.isExhausted()).isFalse(); |
| 86 | 85 | Row row = rs.one(); |
| 87 | 86 | assertThat(rs.isExhausted()).isTrue(); |
| @@ -104,56 +103,6 @@ | ||
| 104 | 103 | assertThat(rows.get(0).getLong("c")).isEqualTo(2L); |
| 105 | 104 | } |
| 106 | 105 | |
| 107 | - /** | |
| 108 | - * Validates that a session can be established using snappy compression and executes some queries that inserts and | |
| 109 | - * retrieves data using that session(). | |
| 110 | - * | |
| 111 | - * @test_category connection:compression | |
| 112 | - * @expected_result session established and queries made successfully using it. | |
| 113 | - */ | |
| 114 | - @Test(groups = "short") | |
| 115 | - public void should_function_with_snappy_compression() throws Exception { | |
| 116 | - compressionTest(ProtocolOptions.Compression.SNAPPY); | |
| 117 | - } | |
| 118 | - | |
| 119 | - /** | |
| 120 | - * Validates that a session can be established using lz4 compression and executes some queries that inserts and | |
| 121 | - * retrieves data using that session(). | |
| 122 | - * | |
| 123 | - * @test_category connection:compression | |
| 124 | - * @expected_result session established and queries made successfully using it. | |
| 125 | - */ | |
| 126 | - @Test(groups = "short") | |
| 127 | - @CassandraVersion(major = 2.0) | |
| 128 | - public void should_function_with_lz4_compression() throws Exception { | |
| 129 | - compressionTest(ProtocolOptions.Compression.LZ4); | |
| 130 | - } | |
| 131 | - | |
| 132 | - public void compressionTest(ProtocolOptions.Compression compression) { | |
| 133 | - cluster().getConfiguration().getProtocolOptions().setCompression(compression); | |
| 134 | - try { | |
| 135 | - Session compressedSession = cluster().connect(keyspace); | |
| 136 | - | |
| 137 | - // Simple calls to all versions of the execute/executeAsync methods | |
| 138 | - String key = "execute_compressed_test_" + compression; | |
| 139 | - ResultSet rs = compressedSession.execute(String.format(Locale.US, "INSERT INTO %s (k, t, i, f) VALUES ('%s', '%s', %d, %f)", TABLE3, key, "foo", 42, 24.03f)); | |
| 140 | - assertThat(rs.isExhausted()).isTrue(); | |
| 141 | - | |
| 142 | - String SELECT_ALL = String.format(TestUtils.SELECT_ALL_FORMAT + " WHERE k = '%s'", TABLE3, key); | |
| 143 | - | |
| 144 | - // execute | |
| 145 | - checkExecuteResultSet(compressedSession.execute(SELECT_ALL), key); | |
| 146 | - checkExecuteResultSet(compressedSession.execute(new SimpleStatement(SELECT_ALL).setConsistencyLevel(ConsistencyLevel.ONE)), key); | |
| 147 | - | |
| 148 | - // executeAsync | |
| 149 | - checkExecuteResultSet(compressedSession.executeAsync(SELECT_ALL).getUninterruptibly(), key); | |
| 150 | - checkExecuteResultSet(compressedSession.executeAsync(new SimpleStatement(SELECT_ALL).setConsistencyLevel(ConsistencyLevel.ONE)).getUninterruptibly(), key); | |
| 151 | - | |
| 152 | - } finally { | |
| 153 | - cluster().getConfiguration().getProtocolOptions().setCompression(ProtocolOptions.Compression.NONE); | |
| 154 | - } | |
| 155 | - } | |
| 156 | - | |
| 157 | 106 | /** |
| 158 | 107 | * Checks for deadlocks when a session shutdown races with the initialization of the cluster (JAVA-418). |
| 159 | 108 | */ |
modified pom.xml +1 −1
| @@ -285,7 +285,7 @@ | ||
| 285 | 285 | </execution> |
| 286 | 286 | </executions> |
| 287 | 287 | <configuration> |
| 288 | - <comparisonVersion>3.0.2</comparisonVersion> | |
| 288 | + <comparisonVersion>3.0.3</comparisonVersion> | |
| 289 | 289 | <ignoredDifferencesFile>../clirr-ignores.xml</ignoredDifferencesFile> |
| 290 | 290 | </configuration> |
| 291 | 291 | <!-- |