This is an automated email from the ASF dual-hosted git repository.

jojochuang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ozone.git


The following commit(s) were added to refs/heads/master by this push:
     new 39dbf5bdf2e HDDS-16834. COMPOSITE_CRC key checksum fails or is wrong 
when a block contributes no composite CRC bytes (#12046)
39dbf5bdf2e is described below

commit 39dbf5bdf2eddc2226e6621cf5609a1e4724a732
Author: Andrey Yarovoy <[email protected]>
AuthorDate: Mon Nov 16 02:01:45 2026 -0500

    HDDS-16834. COMPOSITE_CRC key checksum fails or is wrong when a block 
contributes no composite CRC bytes (#12046)
    
    Co-authored-by: Claude Opus 5.5 <[email protected]>
---
 .../client/checksum/BaseFileChecksumHelper.java    |  43 ++++++--
 .../client/checksum/TestFileChecksumHelper.java    | 111 +++++++++++++++++++++
 2 files changed, 146 insertions(+), 8 deletions(-)

diff --git 
a/hadoop-ozone/client/src/main/java/org/apache/hadoop/ozone/client/checksum/BaseFileChecksumHelper.java
 
b/hadoop-ozone/client/src/main/java/org/apache/hadoop/ozone/client/checksum/BaseFileChecksumHelper.java
index 7c38b2b5049..00e0a26b679 100644
--- 
a/hadoop-ozone/client/src/main/java/org/apache/hadoop/ozone/client/checksum/BaseFileChecksumHelper.java
+++ 
b/hadoop-ozone/client/src/main/java/org/apache/hadoop/ozone/client/checksum/BaseFileChecksumHelper.java
@@ -46,6 +46,9 @@
 public abstract class BaseFileChecksumHelper {
   static final Logger LOG =
       LoggerFactory.getLogger(BaseFileChecksumHelper.class);
+  // MD5 of a 32 byte zero-padded byte array, the checksum of an empty key. 
Explicitly sized here in case the default
+  // DataOutputBuffer buffer length value is changed in future.
+  private static final byte[] EMPTY_KEY_MD5 = MD5Hash.digest(new 
byte[32]).getDigest();
   private OmKeyInfo keyInfo;
 
   private String volumeName;
@@ -198,9 +201,15 @@ protected void checksumBlocks() throws IOException {
          blockIdx++) {
       OmKeyLocationInfo keyLocationInfo =
           getKeyLocationInfoList().get(blockIdx);
-      if (currentLength > getLength()) {
+      if (currentLength >= getLength()) {
+        // The blocks so far cover the requested length.
         return;
       }
+      if (keyLocationInfo.getLength() == 0) {
+        // An empty block adds no bytes to the key, so it adds nothing to the 
checksum.
+        LOG.debug("Skipped empty block index {}", blockIdx);
+        continue;
+      }
 
       if (!checksumBlock(keyLocationInfo)) {
         throw new PathIOException(getSrc(),
@@ -312,23 +321,30 @@ public void compute() throws IOException {
      * magic entry that matches the md5 of a 32 byte zero-padded byte array.
      */
     if (keyLocationInfos == null || keyLocationInfos.isEmpty()) {
-      // Explicitly specified here in case the default DataOutputBuffer
-      // buffer length value is changed in future.
-      final int lenOfZeroBytes = 32;
-      byte[] emptyBlockMd5 = new byte[lenOfZeroBytes];
-      MD5Hash fileMD5 = MD5Hash.digest(emptyBlockMd5);
-      fileChecksum = new MD5MD5CRC32GzipFileChecksum(0, 0, fileMD5);
+      fileChecksum = makeEmptyFileChecksum();
     } else {
       checksumBlocks();
       fileChecksum = makeFinalResult();
     }
   }
 
+  /**
+   * @return the checksum of an empty key, the magic entry that matches the 
md5 of a 32 byte zero-padded byte array.
+   */
+  private static FileChecksum makeEmptyFileChecksum() {
+    // A new instance per call: FileChecksum and MD5Hash are mutable Writables.
+    return new MD5MD5CRC32GzipFileChecksum(0, 0, new 
MD5Hash(EMPTY_KEY_MD5.clone()));
+  }
+
   /**
    * Make final file checksum result given the per-block or per-block-group
    * checksums collected into getBlockChecksumBuf().
    */
   private FileChecksum makeFinalResult() throws IOException {
+    if (getBlockChecksumBuf().getLength() == 0) {
+      // Every block is empty, e.g. a zero-length key whose writer died after 
allocating a block.
+      return makeEmptyFileChecksum();
+    }
     switch (getCombineMode()) {
     case MD5MD5CRC:
       return makeMd5CrcResult();
@@ -377,14 +393,25 @@ FileChecksum makeCompositeCrcResult() throws IOException {
     }
     CrcComposer crcComposer =
         CrcComposer.newCrcComposer(toHadoopChecksumType(), blockSizeHint);
+    // NB: blockChecksumBytes.length may be much longer than actual bytes 
written into the DataOutput.
     byte[] blockChecksumBytes = blockChecksumBuf.getData();
+    int checksumOffset = 0;
 
     for (int i = 0; i < keyLocationInfos.size(); ++i) {
       OmKeyLocationInfo block = keyLocationInfos.get(i);
+      if (block.getLength() == 0) {
+        // checksumBlocks() skipped the empty block, there is no CRC to read.
+        continue;
+      }
+      if (checksumOffset >= blockChecksumBuf.getLength()) {
+        // checksumBlocks() stopped before this block because of the requested 
length.
+        break;
+      }
       // For every LocatedBlock, we expect getBlockSize()
       // to accurately reflect the number of file bytes digested in the block
       // checksum.
-      int blockCrc = CrcUtil.readInt(blockChecksumBytes, i * 4);
+      int blockCrc = CrcUtil.readInt(blockChecksumBytes, checksumOffset);
+      checksumOffset += Integer.BYTES;
 
       crcComposer.update(blockCrc, block.getLength());
       LOG.debug(
diff --git 
a/hadoop-ozone/client/src/test/java/org/apache/hadoop/ozone/client/checksum/TestFileChecksumHelper.java
 
b/hadoop-ozone/client/src/test/java/org/apache/hadoop/ozone/client/checksum/TestFileChecksumHelper.java
index fa02ebb91b6..9e07a9d534f 100644
--- 
a/hadoop-ozone/client/src/test/java/org/apache/hadoop/ozone/client/checksum/TestFileChecksumHelper.java
+++ 
b/hadoop-ozone/client/src/test/java/org/apache/hadoop/ozone/client/checksum/TestFileChecksumHelper.java
@@ -20,8 +20,11 @@
 import static java.nio.charset.StandardCharsets.UTF_8;
 import static org.apache.hadoop.hdds.client.ReplicationFactor.ONE;
 import static 
org.apache.hadoop.hdds.protocol.datanode.proto.ContainerProtos.ChecksumType.CRC32;
+import static 
org.apache.hadoop.hdds.scm.OzoneClientConfig.ChecksumCombineMode.COMPOSITE_CRC;
+import static 
org.apache.hadoop.hdds.scm.OzoneClientConfig.ChecksumCombineMode.MD5MD5CRC;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
 import static org.junit.jupiter.api.Assertions.assertNull;
 import static org.mockito.Mockito.any;
 import static org.mockito.Mockito.mock;
@@ -35,6 +38,8 @@
 import java.util.List;
 import java.util.UUID;
 import java.util.concurrent.CompletableFuture;
+import org.apache.commons.lang3.RandomUtils;
+import org.apache.hadoop.fs.CompositeCrcFileChecksum;
 import org.apache.hadoop.fs.FileChecksum;
 import org.apache.hadoop.fs.MD5MD5CRC32FileChecksum;
 import org.apache.hadoop.fs.MD5MD5CRC32GzipFileChecksum;
@@ -49,11 +54,13 @@
 import org.apache.hadoop.hdds.protocol.datanode.proto.ContainerProtos;
 import org.apache.hadoop.hdds.protocol.proto.HddsProtos;
 import org.apache.hadoop.hdds.scm.OzoneClientConfig;
+import org.apache.hadoop.hdds.scm.OzoneClientConfig.ChecksumCombineMode;
 import org.apache.hadoop.hdds.scm.XceiverClientFactory;
 import org.apache.hadoop.hdds.scm.XceiverClientGrpc;
 import org.apache.hadoop.hdds.scm.XceiverClientReply;
 import org.apache.hadoop.hdds.scm.pipeline.Pipeline;
 import org.apache.hadoop.hdds.scm.pipeline.PipelineID;
+import org.apache.hadoop.io.MD5Hash;
 import org.apache.hadoop.ozone.client.MockOmTransport;
 import org.apache.hadoop.ozone.client.MockXceiverClientFactory;
 import org.apache.hadoop.ozone.client.ObjectStore;
@@ -69,6 +76,7 @@
 import org.apache.hadoop.ozone.om.protocol.OzoneManagerProtocol;
 import org.apache.hadoop.ozone.om.protocolPB.OmTransport;
 import org.apache.hadoop.util.DataChecksum;
+import org.apache.hadoop.util.PureJavaCrc32C;
 import org.apache.hadoop.util.Time;
 import org.apache.ratis.thirdparty.com.google.protobuf.ByteString;
 import org.junit.jupiter.api.AfterEach;
@@ -81,6 +89,7 @@
  * Unit tests for Replicated and EC FileChecksumHelper class.
  */
 public class TestFileChecksumHelper {
+  private static final int BYTES_PER_CRC = 512;
   private final FileChecksum noCachedChecksum = null;
   private OzoneClient client;
   private ObjectStore store;
@@ -205,6 +214,7 @@ public void testOneBlock(ReplicationType helperType) throws 
IOException {
         new OmKeyLocationInfo.Builder()
             .setPipeline(pipeline)
             .setBlockID(blockID)
+            .setLength(10)
             .build();
     List<OmKeyLocationInfo> omKeyLocationInfoList =
         Collections.singletonList(omKeyLocationInfo);
@@ -252,6 +262,107 @@ public XceiverClientReply sendCommandAsync(
     assertEquals(1, helper.getKeyLocationInfoList().size());
   }
 
+  @ParameterizedTest
+  @EnumSource(ChecksumCombineMode.class)
+  public void testZeroLengthKeyWithEmptyBlock(ChecksumCombineMode combineMode) 
throws IOException {
+    assertEquals(checksumOfBlocks(combineMode, Long.MAX_VALUE),
+        checksumOfBlocks(combineMode, Long.MAX_VALUE, new byte[0]));
+  }
+
+  @ParameterizedTest
+  @EnumSource(ChecksumCombineMode.class)
+  public void testEmptyBlockInTheMiddleIsSkipped(ChecksumCombineMode 
combineMode) throws IOException {
+    byte[] first = RandomUtils.secure().randomBytes(1000);
+    byte[] second = RandomUtils.secure().randomBytes(700);
+    assertEquals(checksumOfBlocks(combineMode, Long.MAX_VALUE, first, second),
+        checksumOfBlocks(combineMode, Long.MAX_VALUE, first, new byte[0], 
second));
+  }
+
+  @ParameterizedTest
+  @EnumSource(ChecksumCombineMode.class)
+  public void testTrailingEmptyBlockIsSkipped(ChecksumCombineMode combineMode) 
throws IOException {
+    byte[] first = RandomUtils.secure().randomBytes(1000);
+    assertEquals(checksumOfBlocks(combineMode, Long.MAX_VALUE, first),
+        checksumOfBlocks(combineMode, Long.MAX_VALUE, first, new byte[0]));
+  }
+
+  @Test
+  public void testCompositeCrcOfBlocksMatchesCrcOfKeyData() throws IOException 
{
+    byte[] first = RandomUtils.secure().randomBytes(1000);
+    byte[] second = RandomUtils.secure().randomBytes(700);
+    PureJavaCrc32C crc = new PureJavaCrc32C();
+    crc.update(first, 0, first.length);
+    crc.update(second, 0, second.length);
+    assertEquals(new CompositeCrcFileChecksum((int) crc.getValue(), 
DataChecksum.Type.CRC32C, BYTES_PER_CRC),
+        checksumOfBlocks(COMPOSITE_CRC, Long.MAX_VALUE, first, new byte[0], 
second));
+  }
+
+  @ParameterizedTest
+  @EnumSource(ChecksumCombineMode.class)
+  public void testBlocksBeyondRequestedLengthAreIgnored(ChecksumCombineMode 
combineMode) throws IOException {
+    byte[] first = RandomUtils.secure().randomBytes(1000);
+    byte[] second = RandomUtils.secure().randomBytes(700);
+    FileChecksum firstBlockOnly = checksumOfBlocks(combineMode, 
Long.MAX_VALUE, first);
+    assertEquals(firstBlockOnly, checksumOfBlocks(combineMode, 500, first, 
second));
+    // the requested length ends exactly at the block boundary
+    assertEquals(firstBlockOnly, checksumOfBlocks(combineMode, first.length, 
first, second));
+  }
+
+  /**
+   * Computes the checksum of a RATIS key whose blocks hold the given data. 
Each block checksum is derived from the
+   * block data, so no datanode is needed. An empty block must not be read.
+   */
+  private FileChecksum checksumOfBlocks(ChecksumCombineMode combineMode, long 
length, byte[]... blocks)
+      throws IOException {
+    List<OmKeyLocationInfo> locations = new ArrayList<>();
+    for (int i = 0; i < blocks.length; i++) {
+      locations.add(new OmKeyLocationInfo.Builder()
+          .setBlockID(new BlockID(1, i))
+          .setLength(blocks[i].length)
+          .build());
+    }
+    OmKeyInfo keyInfo = omKeyInfo(ReplicationType.RATIS, noCachedChecksum, 
locations);
+    BaseFileChecksumHelper helper = new ReplicatedFileChecksumHelper(
+        "vol1", "bucket1", "dummy", length, combineMode, 
mock(RpcClient.class), keyInfo) {
+      private byte[] blockData;
+
+      @Override
+      protected List<ContainerProtos.ChunkInfo> 
getChunkInfos(OmKeyLocationInfo keyLocationInfo) {
+        blockData = blocks[(int) keyLocationInfo.getBlockID().getLocalID()];
+        assertNotEquals(0, blockData.length, "an empty block must not be 
read");
+        return Collections.singletonList(ContainerProtos.ChunkInfo.newBuilder()
+            .setChunkName("dummy_chunk")
+            .setOffset(0)
+            .setLen(blockData.length)
+            .setChecksumData(ContainerProtos.ChecksumData.newBuilder()
+                .setType(ContainerProtos.ChecksumType.CRC32C)
+                .setBytesPerChecksum(BYTES_PER_CRC))
+            .build());
+      }
+
+      @Override
+      protected AbstractBlockChecksumComputer 
getBlockChecksumComputer(List<ContainerProtos.ChunkInfo> chunkInfos,
+          long blockLength) {
+        final byte[] blockChecksum;
+        if (combineMode == MD5MD5CRC) {
+          blockChecksum = MD5Hash.digest(blockData).getDigest();
+        } else {
+          PureJavaCrc32C crc = new PureJavaCrc32C();
+          crc.update(blockData, 0, blockData.length);
+          blockChecksum = CrcUtil.intToBytes((int) crc.getValue());
+        }
+        return new AbstractBlockChecksumComputer() {
+          @Override
+          public void compute(ChecksumCombineMode mode) {
+            setOutBytes(blockChecksum);
+          }
+        };
+      }
+    };
+    helper.compute();
+    return helper.getFileChecksum();
+  }
+
   private XceiverClientReply buildValidResponse(ReplicationType type) {
     // return a GetBlockResponse message of a block and its chunk checksums.
     ContainerProtos.DatanodeBlockID blockID =


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to