wsry commented on a change in pull request #10375: [FLINK-14845][runtime] 
Introduce data compression to reduce disk and network IO of shuffle.
URL: https://github.com/apache/flink/pull/10375#discussion_r354183500
 
 

 ##########
 File path: 
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/buffer/BufferDecompressor.java
 ##########
 @@ -0,0 +1,106 @@
+/*
+ * 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.flink.runtime.io.network.buffer;
+
+import org.apache.flink.core.memory.MemorySegment;
+import org.apache.flink.core.memory.MemorySegmentFactory;
+import org.apache.flink.runtime.io.compression.BlockCompressionFactory;
+import org.apache.flink.runtime.io.compression.BlockDecompressor;
+
+import java.nio.ByteBuffer;
+
+import static org.apache.flink.util.Preconditions.checkArgument;
+
+/**
+ * Decompressor for compressed {@link Buffer}.
+ */
+public class BufferDecompressor {
+
+       /** The intermediate heap buffer for the decompressed data. */
+       private final byte[] heapBuffer;
+
+       /** The backing block decompressor for data decompression. */
+       private final BlockDecompressor blockDecompressor;
+
+       public BufferDecompressor(int bufferSize, String factoryName) {
+               checkArgument(bufferSize > 0);
+               this.heapBuffer = new byte[bufferSize];
+               this.blockDecompressor = 
BlockCompressionFactory.createBlockCompressionFactory(factoryName).getDecompressor();
+       }
+
+       /**
+        * Decompresses the given {@link Buffer} using {@link 
BlockDecompressor}. The decompressed data will be stored
+        * in the internal heap buffer of this {@link BufferDecompressor} and 
returned to the caller. The caller must
+        * guarantee that the returned {@link Buffer} is freed when calling the 
method next time.
+        *
+        * <p>Notes that the decompression will always start from offset 0 to 
the size of the input {@link Buffer}.
+        */
+       public Buffer decompressToInternalBuffer(Buffer buffer) {
+               int decompressedLen = decompress(buffer);
+
+               // warp the internal heap buffer as Buffer
+               MemorySegment segment = MemorySegmentFactory.wrap(heapBuffer);
+               NetworkBuffer uncompressedBuffer = new NetworkBuffer(segment, 
FreeingBufferRecycler.INSTANCE);
+               uncompressedBuffer.setSize(decompressedLen);
+               uncompressedBuffer.setCompressed(false);
+
+               return uncompressedBuffer;
+       }
+
+       /**
+        * The difference between this method and {@link 
#decompressToInternalBuffer(Buffer)} is that this method copies
+        * the decompressed data to the input {@link Buffer} starting from 
offset 0.
+        *
+        * <p>The caller must guarantee that the input {@link Buffer} is 
writable and there's enough space left.
+        */
+       public Buffer decompressInPlace(Buffer buffer) {
+               int decompressedLen = decompress(buffer);
+
+               // copy the decompressed data back
+               MemorySegment segment = buffer.getMemorySegment();
+               segment.put(buffer.getMemorySegmentOffset(), heapBuffer, 0, 
decompressedLen);
+               buffer.setCompressed(false);
+               buffer.setSize(decompressedLen);
+
+               return buffer;
+       }
+
+       /**
+        * Decompresses the input {@link Buffer} into the internal heap buffer 
and returns the decompressed data size.
+        */
+       private int decompress(Buffer buffer) {
+               checkArgument(buffer != null, "The input buffer must not be 
null.");
+               checkArgument(buffer.isCompressed(), "Buffer not compressed.");
+               checkArgument(buffer.getReaderIndex() == 0, "Reader index of 
the input buffer must be 0.");
+               checkArgument(buffer.readableBytes() > 0, "No data to be 
decompressed.");
+
+               MemorySegment segment = buffer.getMemorySegment();
+               int length = buffer.getSize();
+               int decompressedLen;
+               // decompress the given buffer into the internal heap buffer
+               if (segment.isOffHeap()) {
+                       ByteBuffer src = buffer.getNioBuffer(0, length);
+                       decompressedLen = blockDecompressor.decompress(src, 0, 
length, ByteBuffer.wrap(heapBuffer), 0);
+               } else {
+                       byte[] src = segment.getArray();
+                       decompressedLen = blockDecompressor.decompress(src, 
buffer.getMemorySegmentOffset(), length, heapBuffer, 0);
 
 Review comment:
   should be aways 0 in the current use case, I don't make the assumption to 
make it more general

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to