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 a1305eb4cec HDDS-16974. EC file checksum is not bounded by the 
committed block group length when a replica lists a stripe beyond it (#12045)
a1305eb4cec is described below

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

    HDDS-16974. EC file checksum is not bounded by the committed block group 
length when a replica lists a stripe beyond it (#12045)
    
    Co-authored-by: Claude Opus 5.5 <[email protected]>
---
 .../client/checksum/ECBlockChecksumComputer.java   | 25 +++++-
 .../checksum/TestECBlockChecksumComputer.java      | 95 +++++++++++++++++++---
 2 files changed, 108 insertions(+), 12 deletions(-)

diff --git 
a/hadoop-ozone/client/src/main/java/org/apache/hadoop/ozone/client/checksum/ECBlockChecksumComputer.java
 
b/hadoop-ozone/client/src/main/java/org/apache/hadoop/ozone/client/checksum/ECBlockChecksumComputer.java
index 7b56d88af07..1c3b34dd030 100644
--- 
a/hadoop-ozone/client/src/main/java/org/apache/hadoop/ozone/client/checksum/ECBlockChecksumComputer.java
+++ 
b/hadoop-ozone/client/src/main/java/org/apache/hadoop/ozone/client/checksum/ECBlockChecksumComputer.java
@@ -73,8 +73,15 @@ private void computeMd5Crc() {
     Preconditions.checkArgument(!chunkInfoList.isEmpty());
 
     final MessageDigest digester = MD5Hash.getDigester();
+    final int numData = ((ECReplicationConfig) 
keyInfo.getReplicationConfig()).getData();
+    // A replica can list a stripe beyond the block length: a stripe whose 
write failed on another replica is written
+    // again into the next block group, but the replicas that stored it keep 
it.
+    long remaining = blockLength;
 
     for (ContainerProtos.ChunkInfo chunkInfo : chunkInfoList) {
+      if (remaining <= 0) {
+        break;
+      }
       long chunkSize = chunkInfo.getLen();
       long bytesPerCrc = chunkInfo.getChecksumData().getBytesPerChecksum();
       // Total parity checksum bytes per stripe to remove
@@ -87,8 +94,11 @@ private void computeMd5Crc() {
           "Checksum Bytes size does not match");
 
       final ByteBuffer byteWrap = stripeChecksum.asReadOnlyByteBuffer();
-      byteWrap.limit(checksumSize - parityBytes);
+      // Like computeCompositeCrc, digest only the data checksums of the bytes 
within the block length.
+      long committedChecksumBytes = getDataChecksumBytes(remaining, chunkSize, 
bytesPerCrc);
+      byteWrap.limit((int) Math.min(checksumSize - parityBytes, 
committedChecksumBytes));
       digester.update(byteWrap);
+      remaining -= chunkSize * numData;
     }
 
     final byte[] fileMD5 = digester.digest();
@@ -140,7 +150,8 @@ private void computeCompositeCrc() throws IOException {
       final ByteBuffer byteWrap = stripeChecksum.asReadOnlyByteBuffer();
       byteWrap.limit(checksumSize - parityBytes);
 
-      while (byteWrap.hasRemaining()) {
+      // Stop at the block length, the replica can list a stripe beyond it 
(see computeMd5Crc).
+      while (byteWrap.hasRemaining() && blockSize > 0) {
         // Here Math.min in mainly required for last stripe's last chunk. The 
last chunk of the last stripe can be
         // less than the chunkSize, chunkSize is only calculated from each 
stripe's first chunk. This would be fine
         // for rest of the stripe because all the chunks are of the same size. 
But for the last stripe we don't know
@@ -165,6 +176,16 @@ private void computeCompositeCrc() throws IOException {
         compositeCrcChunkChecksum);
   }
 
+  /**
+   * Get the number of data checksum bytes covering the first {@code length} 
bytes of a stripe whose data chunks are
+   * {@code chunkSize} long, the same CRCs computeCompositeCrc reads for them.
+   */
+  private static long getDataChecksumBytes(long length, long chunkSize, long 
bytesPerCrc) {
+    long crcsPerChunk = (chunkSize + bytesPerCrc - 1) / bytesPerCrc;
+    long crcsInLastChunk = (length % chunkSize + bytesPerCrc - 1) / 
bytesPerCrc;
+    return ((length / chunkSize) * crcsPerChunk + crcsInLastChunk) * 4L;
+  }
+
   /**
    * Get the number of parity checksum bytes per stripe.
    * For Example, EC chunk size 2MB and repConfig rs-3-2-2048k
diff --git 
a/hadoop-ozone/client/src/test/java/org/apache/hadoop/ozone/client/checksum/TestECBlockChecksumComputer.java
 
b/hadoop-ozone/client/src/test/java/org/apache/hadoop/ozone/client/checksum/TestECBlockChecksumComputer.java
index 6288a4effae..03980d372bc 100644
--- 
a/hadoop-ozone/client/src/test/java/org/apache/hadoop/ozone/client/checksum/TestECBlockChecksumComputer.java
+++ 
b/hadoop-ozone/client/src/test/java/org/apache/hadoop/ozone/client/checksum/TestECBlockChecksumComputer.java
@@ -24,16 +24,21 @@
 
 import java.io.IOException;
 import java.nio.ByteBuffer;
+import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
 import org.apache.commons.lang3.RandomUtils;
 import org.apache.hadoop.hdds.client.ECReplicationConfig;
 import org.apache.hadoop.hdds.protocol.datanode.proto.ContainerProtos;
+import org.apache.hadoop.hdds.scm.OzoneClientConfig.ChecksumCombineMode;
 import org.apache.hadoop.io.MD5Hash;
 import org.apache.hadoop.ozone.om.helpers.OmKeyInfo;
 import org.apache.hadoop.util.DataChecksum;
 import org.apache.ratis.thirdparty.com.google.protobuf.ByteString;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.Timeout;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.EnumSource;
 
 /**
  * Unit tests for ECBlockChecksumComputer class.
@@ -69,6 +74,69 @@ public void testComputeCompositeCrc() throws IOException {
     assertArrayEquals(expectedCompositeCrc, 
computer.getOutByteBuffer().array());
   }
 
+  /**
+   * A replica can list a stripe that the block group never committed: its 
write failed on another replica and the
+   * client wrote it again into the next block group. The checksum must stop 
at the committed block length.
+   */
+  @ParameterizedTest
+  @EnumSource(ChecksumCombineMode.class)
+  @Timeout(value = 30, threadMode = Timeout.ThreadMode.SEPARATE_THREAD)
+  public void testStripeBeyondBlockLengthIsIgnored(ChecksumCombineMode 
combineMode) throws IOException {
+    // With BYTES_PER_CRC == 4 and 4-byte CRCs, a stripe's data checksum bytes 
equal its data length.
+    int stripeLen = EC_CONFIG.getData() * CHUNK_LEN;
+    byte[] committedChecksumBytes = RandomUtils.secure().randomBytes(2 * 
stripeLen);
+    List<ContainerProtos.ChunkInfo> chunkInfoList = Arrays.asList(
+        buildChunkInfo(Arrays.copyOfRange(committedChecksumBytes, 0, 
stripeLen), CHUNK_LEN,
+            ContainerProtos.ChecksumType.CRC32C),
+        buildChunkInfo(Arrays.copyOfRange(committedChecksumBytes, stripeLen, 2 
* stripeLen), CHUNK_LEN,
+            ContainerProtos.ChecksumType.CRC32C),
+        // Uncommitted stripe beyond the block length.
+        buildChunkInfo(RandomUtils.secure().randomBytes(CHUNK_LEN / 2), 
CHUNK_LEN / 2,
+            ContainerProtos.ChecksumType.CRC32C));
+
+    ECBlockChecksumComputer computer = 
buildBlockChecksumComputer(chunkInfoList, 2 * stripeLen);
+    computer.compute(combineMode);
+    assertArrayEquals(expectedChecksum(combineMode, committedChecksumBytes), 
computer.getOutByteBuffer().array());
+  }
+
+  /**
+   * The block length ends inside the last listed stripe while the replica 
lists checksums for the whole stripe, and
+   * a stripe beyond it. Both modes must stop at the same byte.
+   */
+  @ParameterizedTest
+  @EnumSource(ChecksumCombineMode.class)
+  @Timeout(value = 30, threadMode = Timeout.ThreadMode.SEPARATE_THREAD)
+  public void testPartialLastStripeIsBoundedByBlockLength(ChecksumCombineMode 
combineMode) throws IOException {
+    int stripeLen = EC_CONFIG.getData() * CHUNK_LEN;
+    int partialLen = CHUNK_LEN + CHUNK_LEN / 2;
+    byte[] listedChecksumBytes = RandomUtils.secure().randomBytes(2 * 
stripeLen);
+    List<ContainerProtos.ChunkInfo> chunkInfoList = Arrays.asList(
+        buildChunkInfo(Arrays.copyOfRange(listedChecksumBytes, 0, stripeLen), 
CHUNK_LEN,
+            ContainerProtos.ChecksumType.CRC32C),
+        buildChunkInfo(Arrays.copyOfRange(listedChecksumBytes, stripeLen, 2 * 
stripeLen), CHUNK_LEN,
+            ContainerProtos.ChecksumType.CRC32C),
+        buildChunkInfo(RandomUtils.secure().randomBytes(stripeLen), CHUNK_LEN, 
ContainerProtos.ChecksumType.CRC32C));
+
+    ECBlockChecksumComputer computer = 
buildBlockChecksumComputer(chunkInfoList, stripeLen + partialLen);
+    computer.compute(combineMode);
+    // With BYTES_PER_CRC == 4, the committed data checksum bytes equal the 
committed length.
+    byte[] committedChecksumBytes = Arrays.copyOf(listedChecksumBytes, 
stripeLen + partialLen);
+    assertArrayEquals(expectedChecksum(combineMode, committedChecksumBytes), 
computer.getOutByteBuffer().array());
+  }
+
+  /** The checksum of a block whose data checksums are {@code checksumBytes}, 
each CRC covering BYTES_PER_CRC. */
+  private static byte[] expectedChecksum(ChecksumCombineMode combineMode, 
byte[] checksumBytes) throws IOException {
+    if (combineMode == MD5MD5CRC) {
+      return MD5Hash.digest(checksumBytes).getDigest();
+    }
+    CrcComposer composer = 
CrcComposer.newCrcComposer(DataChecksum.Type.CRC32C, BYTES_PER_CRC);
+    ByteBuffer crcs = ByteBuffer.wrap(checksumBytes);
+    while (crcs.hasRemaining()) {
+      composer.update(crcs.getInt(), BYTES_PER_CRC);
+    }
+    return composer.digest();
+  }
+
   private static byte[] computeExpectedCompositeCrc(byte[] dataChecksumBytes, 
long blockLength)
       throws IOException {
     CrcComposer blockCrcComposer =
@@ -87,6 +155,12 @@ private static byte[] computeExpectedCompositeCrc(byte[] 
dataChecksumBytes, long
 
   private static ECBlockChecksumComputer buildBlockChecksumComputer(
       byte[] dataChecksumBytes, int chunkLen, ContainerProtos.ChecksumType 
checksumType) {
+    return buildBlockChecksumComputer(
+        Collections.singletonList(buildChunkInfo(dataChecksumBytes, chunkLen, 
checksumType)), chunkLen);
+  }
+
+  private static ContainerProtos.ChunkInfo buildChunkInfo(
+      byte[] dataChecksumBytes, int chunkLen, ContainerProtos.ChecksumType 
checksumType) {
     int parityBytes = getParityBytes(chunkLen, BYTES_PER_CRC, 
EC_CONFIG.getParity());
     byte[] stripeChecksumBytes = new byte[dataChecksumBytes.length + 
parityBytes];
     System.arraycopy(dataChecksumBytes, 0, stripeChecksumBytes, 0, 
dataChecksumBytes.length);
@@ -96,16 +170,17 @@ private static ECBlockChecksumComputer 
buildBlockChecksumComputer(
             .setBytesPerChecksum((int) BYTES_PER_CRC)
             .setType(checksumType)
             .build();
-    ContainerProtos.ChunkInfo chunkInfo =
-        ContainerProtos.ChunkInfo.newBuilder()
-            .setChecksumData(checksumData)
-            .setChunkName("dummy_chunk")
-            .setOffset(0)
-            .setLen(chunkLen)
-            .setStripeChecksum(ByteString.copyFrom(stripeChecksumBytes))
-            .build();
-    List<ContainerProtos.ChunkInfo> chunkInfoList = 
Collections.singletonList(chunkInfo);
+    return ContainerProtos.ChunkInfo.newBuilder()
+        .setChecksumData(checksumData)
+        .setChunkName("dummy_chunk")
+        .setOffset(0)
+        .setLen(chunkLen)
+        .setStripeChecksum(ByteString.copyFrom(stripeChecksumBytes))
+        .build();
+  }
 
+  private static ECBlockChecksumComputer buildBlockChecksumComputer(
+      List<ContainerProtos.ChunkInfo> chunkInfoList, long blockLength) {
     OmKeyInfo keyInfo = new OmKeyInfo.Builder()
         .setVolumeName("vol1")
         .setBucketName("bucket1")
@@ -113,7 +188,7 @@ private static ECBlockChecksumComputer 
buildBlockChecksumComputer(
         .setReplicationConfig(EC_CONFIG)
         .build();
 
-    return new ECBlockChecksumComputer(chunkInfoList, keyInfo, chunkLen);
+    return new ECBlockChecksumComputer(chunkInfoList, keyInfo, blockLength);
   }
 
   private static int getParityBytes(long chunkSize, long bytesPerCrc, int 
numParity) {


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

Reply via email to