You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kafka.apache.org by jy...@apache.org on 2026/09/10 15:39:38 UTC

(kafka) branch trunk updated: KAFKA-20745: extend OffsetMapTest to verify different hash algorithms (#23391)

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

m1a2st pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/kafka.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 7a09196f933 KAFKA-20745: extend OffsetMapTest to verify different hash algorithms (#23391)
7a09196f933 is described below

commit 7a09196f9334d87e7470c80f986325955f2d60da
Author: Ilan Karasik <il...@gmail.com>
AuthorDate: Thu Sep 10 11:39:30 2026 -0400

    KAFKA-20745: extend OffsetMapTest to verify different hash algorithms (#23391)
    
    `SkimpyOffsetMap` supports configurable hash algorithms, but the tests
    only covered the default (`MD5`).
    - Parameterize the tests to also run against `MD2`, `SHA-1`, `SHA-256`,
    `SHA-384`, and `SHA-512`.
    - Add tests to check entry size based on each algorithm (`bytesPerEntry`
    is public already)
    - Add test for negative case to fail on unknown-algorithm.
    
    No production changes.
    
    Reviewers: Ken Huang <s7...@gmail.com>
---
 .../kafka/storage/internals/log/OffsetMapTest.java | 47 +++++++++++++++++-----
 1 file changed, 38 insertions(+), 9 deletions(-)

diff --git a/storage/src/test/java/org/apache/kafka/storage/internals/log/OffsetMapTest.java b/storage/src/test/java/org/apache/kafka/storage/internals/log/OffsetMapTest.java
index fd014ae3872..5b9a4ce3067 100644
--- a/storage/src/test/java/org/apache/kafka/storage/internals/log/OffsetMapTest.java
+++ b/storage/src/test/java/org/apache/kafka/storage/internals/log/OffsetMapTest.java
@@ -18,22 +18,37 @@ package org.apache.kafka.storage.internals.log;
 
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.params.ParameterizedTest;
-import org.junit.jupiter.params.provider.ValueSource;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
 
 import java.nio.ByteBuffer;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
 import java.util.stream.IntStream;
+import java.util.stream.Stream;
 
 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 
 public class OffsetMapTest {
 
     private static final int MEMORY_SIZE = 4096;
 
+    static Stream<String> hashAlgorithms() {
+        return Stream.of("MD2", "MD5", "SHA-1", "SHA-256", "SHA-384", "SHA-512");
+    }
+
+    static Stream<Arguments> algorithmsAndItems() {
+        return hashAlgorithms().flatMap(algorithm ->
+            IntStream.of(10, 100, 1000, 5000).mapToObj(items -> Arguments.of(algorithm, items)));
+    }
+
     @ParameterizedTest
-    @ValueSource(ints = {10, 100, 1000, 5000})
-    public void testBasicValidation(int items) throws Exception {
-        SkimpyOffsetMap map = new SkimpyOffsetMap(items * 48);
+    @MethodSource("algorithmsAndItems")
+    public void testBasicValidation(String algorithm, int items) throws Exception {
+        int bytesPerEntry = MessageDigest.getInstance(algorithm).getDigestLength() + 8;
+        SkimpyOffsetMap map = new SkimpyOffsetMap(items * bytesPerEntry * 2, algorithm);
         IntStream.range(0, items).forEach(i -> assertDoesNotThrow(() -> map.put(key(i), i)));
         for (int i = 0; i < items; i++) {
             assertEquals(i, map.get(key(i)));
@@ -53,9 +68,10 @@ public class OffsetMapTest {
         }
     }
 
-    @Test
-    public void testGetWhenFull() throws Exception {
-        SkimpyOffsetMap map = new SkimpyOffsetMap(MEMORY_SIZE);
+    @ParameterizedTest
+    @MethodSource("hashAlgorithms")
+    public void testGetWhenFull(String algorithm) throws Exception {
+        SkimpyOffsetMap map = new SkimpyOffsetMap(MEMORY_SIZE, algorithm);
         int i = 37;
         while (map.size() < map.slots()) {
             map.put(key(i), i);
@@ -89,7 +105,7 @@ public class OffsetMapTest {
         }
         assertEquals(i - 1, map.latestOffset());
     }
-    
+
     @Test
     public void testUtilization() throws Exception {
         SkimpyOffsetMap map = new SkimpyOffsetMap(MEMORY_SIZE);
@@ -102,7 +118,20 @@ public class OffsetMapTest {
         }
     }
 
+    @ParameterizedTest
+    @MethodSource("hashAlgorithms")
+    void testBytesPerEntryMatchesDigestLength(String algorithm) throws Exception {
+        int expected = MessageDigest.getInstance(algorithm).getDigestLength() + 8;
+        SkimpyOffsetMap map = new SkimpyOffsetMap(MEMORY_SIZE, algorithm);
+        assertEquals(expected, map.bytesPerEntry);
+    }
+
+    @Test
+    void testUnknownAlgorithmThrows() {
+        assertThrows(NoSuchAlgorithmException.class, () -> new SkimpyOffsetMap(MEMORY_SIZE, "NOT-A-REAL-HASH"));
+    }
+
     private ByteBuffer key(Integer key) {
         return ByteBuffer.wrap(key.toString().getBytes());
     }
-}
\ No newline at end of file
+}