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_r354824544
 
 

 ##########
 File path: 
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/buffer/BufferCompressor.java
 ##########
 @@ -0,0 +1,116 @@
+/*
+ * 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.BlockCompressor;
+
+import static org.apache.flink.util.Preconditions.checkArgument;
+import static org.apache.flink.util.Preconditions.checkNotNull;
+import static org.apache.flink.util.Preconditions.checkState;
+
+/**
+ * Compressor for {@link Buffer}.
+ */
+public class BufferCompressor {
+
+       /** The backing block compressor for data compression. */
+       private final BlockCompressor blockCompressor;
+
+       /** The intermediate buffer for the compressed data. */
+       private final NetworkBuffer internalBuffer;
+
+       public BufferCompressor(int bufferSize, String factoryName) {
+               checkArgument(bufferSize > 0);
+               checkNotNull(factoryName);
+               // the size of this intermediate heap buffer will be gotten 
from the
+               // plugin configuration in the future, and currently, double 
size of
+               // the input buffer is enough for lz4-java compression library.
+               final byte[] heapBuffer = new byte[2 * bufferSize];
+               this.internalBuffer = new 
NetworkBuffer(MemorySegmentFactory.wrap(heapBuffer), 
FreeingBufferRecycler.INSTANCE);
+               this.blockCompressor = 
BlockCompressionFactory.createBlockCompressionFactory(factoryName).getCompressor();
+       }
+
+       /**
+        * Compresses the given {@link Buffer} using {@link BlockCompressor}. 
The compressed data will be stored in the
+        * intermediate buffer of this {@link BufferCompressor} and returned to 
the caller. The caller must guarantee
+        * that the returned {@link Buffer} has been freed when calling the 
method next time.
+        *
+        * <p>Notes that the compression will always start from offset 0 to the 
size of the input {@link Buffer}.
+        */
+       public Buffer compressToIntermediateBuffer(Buffer buffer) {
+               int compressedLen;
+               if ((compressedLen = compress(buffer)) == 0) {
+                       return buffer;
+               }
+
+               internalBuffer.setCompressed(true);
+               internalBuffer.setSize(compressedLen);
+               return internalBuffer.retainBuffer();
+       }
+
+       /**
+        * The difference between this method and {@link 
#compressToIntermediateBuffer(Buffer)} is that this method will
+        * copy the compressed data back 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 compressToOriginalBuffer(Buffer buffer) {
+               int compressedLen;
+               if ((compressedLen = compress(buffer)) == 0) {
+                       return buffer;
+               }
+
+               // copy the compressed data back
+               int memorySegmentOffset = buffer.getMemorySegmentOffset();
+               MemorySegment segment = buffer.getMemorySegment();
 
 Review comment:
   totally agree.

----------------------------------------------------------------
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