zhijiangW 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_r353582369
########## 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) { Review comment: It is only used in tests which should be tagged as `@VisibleForTesting`. And I also doubt whether it is necessary to have this for tests. I will double confirm it when reviewing the tests. ---------------------------------------------------------------- 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: [email protected] With regards, Apache Git Services
