You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@hbase.apache.org by GitBox <gi...@apache.org> on 2022/08/11 23:44:45 UTC

[GitHub] [hbase] ankitsinghal commented on a diff in pull request #4675: HBASE-27264 Add options to consider compressed size when delimiting blocks during hfile writes

ankitsinghal commented on code in PR #4675:
URL: https://github.com/apache/hbase/pull/4675#discussion_r943994935


##########
hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/HFileBlock.java:
##########
@@ -248,6 +254,8 @@ static class Header {
   static final byte[] DUMMY_HEADER_NO_CHECKSUM =
     new byte[HConstants.HFILEBLOCK_HEADER_SIZE_NO_CHECKSUM];
 
+  public static final String MAX_BLOCK_SIZE_UNCOMPRESSED = "hbase.block.max.size.uncompressed";

Review Comment:
   Can we move this and it's logic to the predicate itself?



##########
hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/PreviousBlockCompressionRatePredicator.java:
##########
@@ -0,0 +1,63 @@
+/*
+ * 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.hadoop.hbase.io.hfile;
+
+import org.apache.hadoop.hbase.io.ByteArrayOutputStream;
+import org.apache.hadoop.hbase.io.encoding.EncodedDataBlock;
+import org.apache.yetus.audience.InterfaceAudience;
+
+import java.io.IOException;
+
+/**
+ * This BlockCompressedSizePredicator implementation adjusts the block size limit based on the
+ * compression rate of the block contents read so far. For the first block, adjusted size would be
+ * zero, so it performs a compression of current block contents and calculate compression rate and
+ * adjusted size. For subsequent blocks, it only performs this calculation once the previous block
+ * adjusted size has been reached, and the block is about to be closed.
+ */
+@InterfaceAudience.Private
+public class PreviousBlockCompressionRatePredicator implements BlockCompressedSizePredicator {
+  
+  int adjustedBlockSize;
+
+  /**
+   * Calculates an adjusted block size limit based on the compression rate of current block
+   * contents. This calculation is only performed if this is the first block, otherwise, if the
+   * adjusted size from previous block has been reached by the current one.
+   * @param context the meta file information for the current file.
+   * @param uncompressedBlockSize the total uncompressed size read for the block so far.
+   * @param contents The byte array containing the block content so far.
+   * @return the adjusted block size limit based on block compression rate.
+   * @throws IOException
+   */
+  @Override 
+  public int calculateCompressionSizeLimit(HFileContext context, int uncompressedBlockSize, 
+      ByteArrayOutputStream contents) throws IOException {
+    // In order to avoid excessive compression size calculations, we do it only once when
+    // the uncompressed size has reached BLOCKSIZE. We then use this compression size to
+    // calculate the compression rate, and adjust the block size limit by this ratio.
+    if (adjustedBlockSize == 0 || uncompressedBlockSize >= adjustedBlockSize) {

Review Comment:
   We are recomputing compression size every time if we are even a little over the previously calculated adjusted block size, which is possible for all the blocks as no compression ratio is consistent and will result in computation all the time?
   
   
   



##########
hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/PreviousBlockCompressionRatePredicator.java:
##########
@@ -0,0 +1,63 @@
+/*
+ * 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.hadoop.hbase.io.hfile;
+
+import org.apache.hadoop.hbase.io.ByteArrayOutputStream;
+import org.apache.hadoop.hbase.io.encoding.EncodedDataBlock;
+import org.apache.yetus.audience.InterfaceAudience;
+
+import java.io.IOException;
+
+/**
+ * This BlockCompressedSizePredicator implementation adjusts the block size limit based on the
+ * compression rate of the block contents read so far. For the first block, adjusted size would be
+ * zero, so it performs a compression of current block contents and calculate compression rate and
+ * adjusted size. For subsequent blocks, it only performs this calculation once the previous block
+ * adjusted size has been reached, and the block is about to be closed.
+ */
+@InterfaceAudience.Private
+public class PreviousBlockCompressionRatePredicator implements BlockCompressedSizePredicator {
+  
+  int adjustedBlockSize;
+
+  /**
+   * Calculates an adjusted block size limit based on the compression rate of current block
+   * contents. This calculation is only performed if this is the first block, otherwise, if the
+   * adjusted size from previous block has been reached by the current one.
+   * @param context the meta file information for the current file.
+   * @param uncompressedBlockSize the total uncompressed size read for the block so far.
+   * @param contents The byte array containing the block content so far.
+   * @return the adjusted block size limit based on block compression rate.
+   * @throws IOException
+   */
+  @Override 
+  public int calculateCompressionSizeLimit(HFileContext context, int uncompressedBlockSize, 
+      ByteArrayOutputStream contents) throws IOException {
+    // In order to avoid excessive compression size calculations, we do it only once when
+    // the uncompressed size has reached BLOCKSIZE. We then use this compression size to
+    // calculate the compression rate, and adjust the block size limit by this ratio.
+    if (adjustedBlockSize == 0 || uncompressedBlockSize >= adjustedBlockSize) {
+      int compressedSize = EncodedDataBlock.getCompressedSize(context.getCompression(),
+        context.getCompression().getCompressor(), contents.getBuffer(), 0,
+        contents.size());
+      adjustedBlockSize = uncompressedBlockSize / compressedSize;
+      adjustedBlockSize *= context.getBlocksize();

Review Comment:
   I was expecting the algorithm to be
   * Let the first block get closed with the regular adjustedBlockSize == block_size
   * Have a method  in this predicate to be called on finishBlock and use the currently closed block statistics to determine the compression ratio and store the adjusted size.
   * Use the above compression ratio/adjusted size to determine the boundaries of the next block. 
   
   This way, we will keep on adjusting the ratio for the next block as per the previous block without doing any extra compression
   
   



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

To unsubscribe, e-mail: issues-unsubscribe@hbase.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org