You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by GitBox <gi...@apache.org> on 2022/12/22 21:41:40 UTC

[GitHub] [kafka] ijuma opened a new pull request, #13042: KAFKA-14476: Move OffsetMap and related to storage module

ijuma opened a new pull request, #13042:
URL: https://github.com/apache/kafka/pull/13042

   For broader context on this change, please check:
   
   * KAFKA-14470: Move log layer to storage module
   
   ### Committer Checklist (excluded from commit message)
   - [ ] Verify design and implementation 
   - [ ] Verify test coverage and CI build status
   - [ ] Verify documentation (including upgrade notes)
   


-- 
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: jira-unsubscribe@kafka.apache.org

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


[GitHub] [kafka] fvaleri commented on pull request #13042: KAFKA-14476: Move OffsetMap and related to storage module

Posted by GitBox <gi...@apache.org>.
fvaleri commented on PR #13042:
URL: https://github.com/apache/kafka/pull/13042#issuecomment-1363945518

   LGTM


-- 
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: jira-unsubscribe@kafka.apache.org

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


[GitHub] [kafka] satishd commented on a diff in pull request #13042: KAFKA-14476: Move OffsetMap and related to storage module

Posted by GitBox <gi...@apache.org>.
satishd commented on code in PR #13042:
URL: https://github.com/apache/kafka/pull/13042#discussion_r1055958939


##########
storage/src/main/java/org/apache/kafka/server/log/internals/SkimpyOffsetMap.java:
##########
@@ -0,0 +1,241 @@
+/*
+ * 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.kafka.server.log.internals;
+
+import org.apache.kafka.common.utils.ByteUtils;
+import org.apache.kafka.common.utils.Utils;
+
+import java.nio.ByteBuffer;
+import java.security.DigestException;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.util.Arrays;
+
+/**
+ * An hash table used for deduplicating the log. This hash table uses a cryptographicly secure hash of the key as a proxy for the key

Review Comment:
   minor existing typo in earlier code: "A" instead of "An"



-- 
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: jira-unsubscribe@kafka.apache.org

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


[GitHub] [kafka] ijuma merged pull request #13042: KAFKA-14476: Move OffsetMap and related to storage module

Posted by GitBox <gi...@apache.org>.
ijuma merged PR #13042:
URL: https://github.com/apache/kafka/pull/13042


-- 
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: jira-unsubscribe@kafka.apache.org

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


[GitHub] [kafka] satishd commented on a diff in pull request #13042: KAFKA-14476: Move OffsetMap and related to storage module

Posted by GitBox <gi...@apache.org>.
satishd commented on code in PR #13042:
URL: https://github.com/apache/kafka/pull/13042#discussion_r1055973548


##########
storage/src/main/java/org/apache/kafka/server/log/internals/SkimpyOffsetMap.java:
##########
@@ -0,0 +1,241 @@
+/*
+ * 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.kafka.server.log.internals;
+
+import org.apache.kafka.common.utils.ByteUtils;
+import org.apache.kafka.common.utils.Utils;
+
+import java.nio.ByteBuffer;
+import java.security.DigestException;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.util.Arrays;
+
+/**
+ * An hash table used for deduplicating the log. This hash table uses a cryptographicly secure hash of the key as a proxy for the key
+ * for comparisons and to save space on object overhead. Collisions are resolved by probing. This hash table does not support deletes.
+ */
+public class SkimpyOffsetMap implements OffsetMap {
+
+    /**
+     * The number of bytes of space each entry uses (the number of bytes in the hash plus an 8 byte offset)
+     */
+    public final int bytesPerEntry;
+
+    private final ByteBuffer bytes;
+
+    /* the hash algorithm instance to use */
+    private final MessageDigest digest;
+
+    /* the number of bytes for this hash algorithm */
+    private final int hashSize;
+
+    /**
+     * The maximum number of entries this map can contain
+     */
+    private final int slots;
+
+    /* cache some hash buffers to avoid reallocating each time */
+    private final byte[] hash1;
+    private final byte[] hash2;
+
+    /* number of entries put into the map */
+    private int entries = 0;
+
+    /* number of lookups on the map */
+    private long lookups = 0L;
+
+    /* the number of probes for all lookups */
+    private long probes = 0L;
+
+    /* the latest offset written into the map */
+    private long lastOffset = -1L;
+
+    /**
+     * Create an instance of SkimplyOffsetMap with the default hash algorithm (MD5).
+     *
+     * @param memory The amount of memory this map can use
+     */
+    public SkimpyOffsetMap(int memory) throws NoSuchAlgorithmException {
+        this(memory, "MD5");
+    }
+
+    /**
+     * Create an instance of SkimpyOffsetMap.
+     *
+     * @param memory The amount of memory this map can use
+     * @param hashAlgorithm The hash algorithm instance to use: MD2, MD5, SHA-1, SHA-256, SHA-384, SHA-512
+     */
+    public SkimpyOffsetMap(int memory, String hashAlgorithm) throws NoSuchAlgorithmException {
+        this.bytes = ByteBuffer.allocate(memory);
+
+        this.digest = MessageDigest.getInstance(hashAlgorithm);
+
+        this.hashSize = digest.getDigestLength();
+        this.bytesPerEntry = hashSize * 8;
+        this.slots = memory / bytesPerEntry;
+
+        this.hash1 = new byte[hashSize];
+        this.hash2 = new byte[hashSize];
+    }
+
+    @Override
+    public int slots() {
+        return slots;
+    }
+
+    /**
+     * Get the offset associated with this key.
+     * @param key The key
+     * @return The offset associated with this key or -1 if the key is not found
+     */
+    @Override
+    public long get(ByteBuffer key) throws DigestException {
+        ++lookups;
+        hashInto(key, hash1);
+        // search for the hash of this key by repeated probing until we find the hash we are looking for or we find an empty slot
+        int attempt = 0;
+        int pos = 0;
+        //we need to guard against attempt integer overflow if the map is full
+        //limit attempt to number of slots once positionOf(..) enters linear search mode
+        int maxAttempts = slots + hashSize - 4;
+        do {
+            if (attempt >= maxAttempts)
+                return -1L;
+            pos = positionOf(hash1, attempt);
+            bytes.position(pos);
+            if (isEmpty(pos))
+                return -1L;
+            bytes.get(hash2);
+            ++attempt;
+        } while (!Arrays.equals(hash1, hash2));
+        return bytes.getLong();
+    }
+
+    /**
+     * Associate this offset to the given key.
+     * @param key The key
+     * @param offset The offset
+     */
+    @Override
+    public void put(ByteBuffer key, long offset) throws DigestException {
+        if (entries < slots)

Review Comment:
   It should be `entries >= slots`.
   This condition was there in earlier scala code with require check. We ned to invert it here. 



##########
storage/src/main/java/org/apache/kafka/server/log/internals/SkimpyOffsetMap.java:
##########
@@ -0,0 +1,241 @@
+/*
+ * 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.kafka.server.log.internals;
+
+import org.apache.kafka.common.utils.ByteUtils;
+import org.apache.kafka.common.utils.Utils;
+
+import java.nio.ByteBuffer;
+import java.security.DigestException;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.util.Arrays;
+
+/**
+ * An hash table used for deduplicating the log. This hash table uses a cryptographicly secure hash of the key as a proxy for the key
+ * for comparisons and to save space on object overhead. Collisions are resolved by probing. This hash table does not support deletes.
+ */
+public class SkimpyOffsetMap implements OffsetMap {
+
+    /**
+     * The number of bytes of space each entry uses (the number of bytes in the hash plus an 8 byte offset)
+     */
+    public final int bytesPerEntry;
+
+    private final ByteBuffer bytes;
+
+    /* the hash algorithm instance to use */
+    private final MessageDigest digest;
+
+    /* the number of bytes for this hash algorithm */
+    private final int hashSize;
+
+    /**
+     * The maximum number of entries this map can contain
+     */
+    private final int slots;
+
+    /* cache some hash buffers to avoid reallocating each time */
+    private final byte[] hash1;
+    private final byte[] hash2;
+
+    /* number of entries put into the map */
+    private int entries = 0;
+
+    /* number of lookups on the map */
+    private long lookups = 0L;
+
+    /* the number of probes for all lookups */
+    private long probes = 0L;
+
+    /* the latest offset written into the map */
+    private long lastOffset = -1L;
+
+    /**
+     * Create an instance of SkimplyOffsetMap with the default hash algorithm (MD5).
+     *
+     * @param memory The amount of memory this map can use
+     */
+    public SkimpyOffsetMap(int memory) throws NoSuchAlgorithmException {
+        this(memory, "MD5");
+    }
+
+    /**
+     * Create an instance of SkimpyOffsetMap.
+     *
+     * @param memory The amount of memory this map can use
+     * @param hashAlgorithm The hash algorithm instance to use: MD2, MD5, SHA-1, SHA-256, SHA-384, SHA-512
+     */
+    public SkimpyOffsetMap(int memory, String hashAlgorithm) throws NoSuchAlgorithmException {
+        this.bytes = ByteBuffer.allocate(memory);
+
+        this.digest = MessageDigest.getInstance(hashAlgorithm);
+
+        this.hashSize = digest.getDigestLength();
+        this.bytesPerEntry = hashSize * 8;
+        this.slots = memory / bytesPerEntry;
+
+        this.hash1 = new byte[hashSize];
+        this.hash2 = new byte[hashSize];
+    }
+
+    @Override
+    public int slots() {
+        return slots;
+    }
+
+    /**
+     * Get the offset associated with this key.
+     * @param key The key
+     * @return The offset associated with this key or -1 if the key is not found
+     */
+    @Override
+    public long get(ByteBuffer key) throws DigestException {
+        ++lookups;
+        hashInto(key, hash1);
+        // search for the hash of this key by repeated probing until we find the hash we are looking for or we find an empty slot
+        int attempt = 0;
+        int pos = 0;
+        //we need to guard against attempt integer overflow if the map is full
+        //limit attempt to number of slots once positionOf(..) enters linear search mode
+        int maxAttempts = slots + hashSize - 4;
+        do {
+            if (attempt >= maxAttempts)
+                return -1L;
+            pos = positionOf(hash1, attempt);
+            bytes.position(pos);
+            if (isEmpty(pos))
+                return -1L;
+            bytes.get(hash2);
+            ++attempt;
+        } while (!Arrays.equals(hash1, hash2));
+        return bytes.getLong();
+    }
+
+    /**
+     * Associate this offset to the given key.
+     * @param key The key
+     * @param offset The offset
+     */
+    @Override
+    public void put(ByteBuffer key, long offset) throws DigestException {
+        if (entries < slots)

Review Comment:
   It should be `entries >= slots`.
   This condition was there in earlier scala code with require check. We need to invert it here. 



-- 
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: jira-unsubscribe@kafka.apache.org

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


[GitHub] [kafka] ijuma commented on pull request #13042: KAFKA-14476: Move OffsetMap and related to storage module

Posted by GitBox <gi...@apache.org>.
ijuma commented on PR #13042:
URL: https://github.com/apache/kafka/pull/13042#issuecomment-1363545216

   @satishd Addressed your comments and pushed an additional fix to address an `OffsetMapTest` failure. The CI build should be good now, but I'll wait for it to complete.


-- 
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: jira-unsubscribe@kafka.apache.org

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


[GitHub] [kafka] satishd commented on a diff in pull request #13042: KAFKA-14476: Move OffsetMap and related to storage module

Posted by GitBox <gi...@apache.org>.
satishd commented on code in PR #13042:
URL: https://github.com/apache/kafka/pull/13042#discussion_r1055973548


##########
storage/src/main/java/org/apache/kafka/server/log/internals/SkimpyOffsetMap.java:
##########
@@ -0,0 +1,241 @@
+/*
+ * 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.kafka.server.log.internals;
+
+import org.apache.kafka.common.utils.ByteUtils;
+import org.apache.kafka.common.utils.Utils;
+
+import java.nio.ByteBuffer;
+import java.security.DigestException;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.util.Arrays;
+
+/**
+ * An hash table used for deduplicating the log. This hash table uses a cryptographicly secure hash of the key as a proxy for the key
+ * for comparisons and to save space on object overhead. Collisions are resolved by probing. This hash table does not support deletes.
+ */
+public class SkimpyOffsetMap implements OffsetMap {
+
+    /**
+     * The number of bytes of space each entry uses (the number of bytes in the hash plus an 8 byte offset)
+     */
+    public final int bytesPerEntry;
+
+    private final ByteBuffer bytes;
+
+    /* the hash algorithm instance to use */
+    private final MessageDigest digest;
+
+    /* the number of bytes for this hash algorithm */
+    private final int hashSize;
+
+    /**
+     * The maximum number of entries this map can contain
+     */
+    private final int slots;
+
+    /* cache some hash buffers to avoid reallocating each time */
+    private final byte[] hash1;
+    private final byte[] hash2;
+
+    /* number of entries put into the map */
+    private int entries = 0;
+
+    /* number of lookups on the map */
+    private long lookups = 0L;
+
+    /* the number of probes for all lookups */
+    private long probes = 0L;
+
+    /* the latest offset written into the map */
+    private long lastOffset = -1L;
+
+    /**
+     * Create an instance of SkimplyOffsetMap with the default hash algorithm (MD5).
+     *
+     * @param memory The amount of memory this map can use
+     */
+    public SkimpyOffsetMap(int memory) throws NoSuchAlgorithmException {
+        this(memory, "MD5");
+    }
+
+    /**
+     * Create an instance of SkimpyOffsetMap.
+     *
+     * @param memory The amount of memory this map can use
+     * @param hashAlgorithm The hash algorithm instance to use: MD2, MD5, SHA-1, SHA-256, SHA-384, SHA-512
+     */
+    public SkimpyOffsetMap(int memory, String hashAlgorithm) throws NoSuchAlgorithmException {
+        this.bytes = ByteBuffer.allocate(memory);
+
+        this.digest = MessageDigest.getInstance(hashAlgorithm);
+
+        this.hashSize = digest.getDigestLength();
+        this.bytesPerEntry = hashSize * 8;
+        this.slots = memory / bytesPerEntry;
+
+        this.hash1 = new byte[hashSize];
+        this.hash2 = new byte[hashSize];
+    }
+
+    @Override
+    public int slots() {
+        return slots;
+    }
+
+    /**
+     * Get the offset associated with this key.
+     * @param key The key
+     * @return The offset associated with this key or -1 if the key is not found
+     */
+    @Override
+    public long get(ByteBuffer key) throws DigestException {
+        ++lookups;
+        hashInto(key, hash1);
+        // search for the hash of this key by repeated probing until we find the hash we are looking for or we find an empty slot
+        int attempt = 0;
+        int pos = 0;
+        //we need to guard against attempt integer overflow if the map is full
+        //limit attempt to number of slots once positionOf(..) enters linear search mode
+        int maxAttempts = slots + hashSize - 4;
+        do {
+            if (attempt >= maxAttempts)
+                return -1L;
+            pos = positionOf(hash1, attempt);
+            bytes.position(pos);
+            if (isEmpty(pos))
+                return -1L;
+            bytes.get(hash2);
+            ++attempt;
+        } while (!Arrays.equals(hash1, hash2));
+        return bytes.getLong();
+    }
+
+    /**
+     * Associate this offset to the given key.
+     * @param key The key
+     * @param offset The offset
+     */
+    @Override
+    public void put(ByteBuffer key, long offset) throws DigestException {
+        if (entries < slots)

Review Comment:
   This condition does not look to be right. It should be `entries >= slots`.



-- 
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: jira-unsubscribe@kafka.apache.org

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


[GitHub] [kafka] ijuma commented on a diff in pull request #13042: KAFKA-14476: Move OffsetMap and related to storage module

Posted by GitBox <gi...@apache.org>.
ijuma commented on code in PR #13042:
URL: https://github.com/apache/kafka/pull/13042#discussion_r1055981406


##########
storage/src/main/java/org/apache/kafka/server/log/internals/SkimpyOffsetMap.java:
##########
@@ -0,0 +1,241 @@
+/*
+ * 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.kafka.server.log.internals;
+
+import org.apache.kafka.common.utils.ByteUtils;
+import org.apache.kafka.common.utils.Utils;
+
+import java.nio.ByteBuffer;
+import java.security.DigestException;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.util.Arrays;
+
+/**
+ * An hash table used for deduplicating the log. This hash table uses a cryptographicly secure hash of the key as a proxy for the key
+ * for comparisons and to save space on object overhead. Collisions are resolved by probing. This hash table does not support deletes.
+ */
+public class SkimpyOffsetMap implements OffsetMap {
+
+    /**
+     * The number of bytes of space each entry uses (the number of bytes in the hash plus an 8 byte offset)
+     */
+    public final int bytesPerEntry;
+
+    private final ByteBuffer bytes;
+
+    /* the hash algorithm instance to use */
+    private final MessageDigest digest;
+
+    /* the number of bytes for this hash algorithm */
+    private final int hashSize;
+
+    /**
+     * The maximum number of entries this map can contain
+     */
+    private final int slots;
+
+    /* cache some hash buffers to avoid reallocating each time */
+    private final byte[] hash1;
+    private final byte[] hash2;
+
+    /* number of entries put into the map */
+    private int entries = 0;
+
+    /* number of lookups on the map */
+    private long lookups = 0L;
+
+    /* the number of probes for all lookups */
+    private long probes = 0L;
+
+    /* the latest offset written into the map */
+    private long lastOffset = -1L;
+
+    /**
+     * Create an instance of SkimplyOffsetMap with the default hash algorithm (MD5).
+     *
+     * @param memory The amount of memory this map can use
+     */
+    public SkimpyOffsetMap(int memory) throws NoSuchAlgorithmException {
+        this(memory, "MD5");
+    }
+
+    /**
+     * Create an instance of SkimpyOffsetMap.
+     *
+     * @param memory The amount of memory this map can use
+     * @param hashAlgorithm The hash algorithm instance to use: MD2, MD5, SHA-1, SHA-256, SHA-384, SHA-512
+     */
+    public SkimpyOffsetMap(int memory, String hashAlgorithm) throws NoSuchAlgorithmException {
+        this.bytes = ByteBuffer.allocate(memory);
+
+        this.digest = MessageDigest.getInstance(hashAlgorithm);
+
+        this.hashSize = digest.getDigestLength();
+        this.bytesPerEntry = hashSize * 8;
+        this.slots = memory / bytesPerEntry;
+
+        this.hash1 = new byte[hashSize];
+        this.hash2 = new byte[hashSize];
+    }
+
+    @Override
+    public int slots() {
+        return slots;
+    }
+
+    /**
+     * Get the offset associated with this key.
+     * @param key The key
+     * @return The offset associated with this key or -1 if the key is not found
+     */
+    @Override
+    public long get(ByteBuffer key) throws DigestException {
+        ++lookups;
+        hashInto(key, hash1);
+        // search for the hash of this key by repeated probing until we find the hash we are looking for or we find an empty slot
+        int attempt = 0;
+        int pos = 0;
+        //we need to guard against attempt integer overflow if the map is full
+        //limit attempt to number of slots once positionOf(..) enters linear search mode
+        int maxAttempts = slots + hashSize - 4;
+        do {
+            if (attempt >= maxAttempts)
+                return -1L;
+            pos = positionOf(hash1, attempt);
+            bytes.position(pos);
+            if (isEmpty(pos))
+                return -1L;
+            bytes.get(hash2);
+            ++attempt;
+        } while (!Arrays.equals(hash1, hash2));
+        return bytes.getLong();
+    }
+
+    /**
+     * Associate this offset to the given key.
+     * @param key The key
+     * @param offset The offset
+     */
+    @Override
+    public void put(ByteBuffer key, long offset) throws DigestException {
+        if (entries < slots)

Review Comment:
   Good catch, the check needs to be reversed since I switched Scala's `require` to an `if`/`throw`. Fixed.



-- 
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: jira-unsubscribe@kafka.apache.org

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


[GitHub] [kafka] ijuma commented on a diff in pull request #13042: KAFKA-14476: Move OffsetMap and related to storage module

Posted by GitBox <gi...@apache.org>.
ijuma commented on code in PR #13042:
URL: https://github.com/apache/kafka/pull/13042#discussion_r1055980846


##########
storage/src/main/java/org/apache/kafka/server/log/internals/SkimpyOffsetMap.java:
##########
@@ -0,0 +1,241 @@
+/*
+ * 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.kafka.server.log.internals;
+
+import org.apache.kafka.common.utils.ByteUtils;
+import org.apache.kafka.common.utils.Utils;
+
+import java.nio.ByteBuffer;
+import java.security.DigestException;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.util.Arrays;
+
+/**
+ * An hash table used for deduplicating the log. This hash table uses a cryptographicly secure hash of the key as a proxy for the key

Review Comment:
   Fixed.



-- 
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: jira-unsubscribe@kafka.apache.org

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


[GitHub] [kafka] ijuma commented on pull request #13042: KAFKA-14476: Move OffsetMap and related to storage module

Posted by GitBox <gi...@apache.org>.
ijuma commented on PR #13042:
URL: https://github.com/apache/kafka/pull/13042#issuecomment-1363545617

   @dengziming Fyi, I pushed a small fix right after your review.


-- 
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: jira-unsubscribe@kafka.apache.org

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


[GitHub] [kafka] ijuma commented on a diff in pull request #13042: KAFKA-14476: Move OffsetMap and related to storage module

Posted by GitBox <gi...@apache.org>.
ijuma commented on code in PR #13042:
URL: https://github.com/apache/kafka/pull/13042#discussion_r1055981406


##########
storage/src/main/java/org/apache/kafka/server/log/internals/SkimpyOffsetMap.java:
##########
@@ -0,0 +1,241 @@
+/*
+ * 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.kafka.server.log.internals;
+
+import org.apache.kafka.common.utils.ByteUtils;
+import org.apache.kafka.common.utils.Utils;
+
+import java.nio.ByteBuffer;
+import java.security.DigestException;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.util.Arrays;
+
+/**
+ * An hash table used for deduplicating the log. This hash table uses a cryptographicly secure hash of the key as a proxy for the key
+ * for comparisons and to save space on object overhead. Collisions are resolved by probing. This hash table does not support deletes.
+ */
+public class SkimpyOffsetMap implements OffsetMap {
+
+    /**
+     * The number of bytes of space each entry uses (the number of bytes in the hash plus an 8 byte offset)
+     */
+    public final int bytesPerEntry;
+
+    private final ByteBuffer bytes;
+
+    /* the hash algorithm instance to use */
+    private final MessageDigest digest;
+
+    /* the number of bytes for this hash algorithm */
+    private final int hashSize;
+
+    /**
+     * The maximum number of entries this map can contain
+     */
+    private final int slots;
+
+    /* cache some hash buffers to avoid reallocating each time */
+    private final byte[] hash1;
+    private final byte[] hash2;
+
+    /* number of entries put into the map */
+    private int entries = 0;
+
+    /* number of lookups on the map */
+    private long lookups = 0L;
+
+    /* the number of probes for all lookups */
+    private long probes = 0L;
+
+    /* the latest offset written into the map */
+    private long lastOffset = -1L;
+
+    /**
+     * Create an instance of SkimplyOffsetMap with the default hash algorithm (MD5).
+     *
+     * @param memory The amount of memory this map can use
+     */
+    public SkimpyOffsetMap(int memory) throws NoSuchAlgorithmException {
+        this(memory, "MD5");
+    }
+
+    /**
+     * Create an instance of SkimpyOffsetMap.
+     *
+     * @param memory The amount of memory this map can use
+     * @param hashAlgorithm The hash algorithm instance to use: MD2, MD5, SHA-1, SHA-256, SHA-384, SHA-512
+     */
+    public SkimpyOffsetMap(int memory, String hashAlgorithm) throws NoSuchAlgorithmException {
+        this.bytes = ByteBuffer.allocate(memory);
+
+        this.digest = MessageDigest.getInstance(hashAlgorithm);
+
+        this.hashSize = digest.getDigestLength();
+        this.bytesPerEntry = hashSize * 8;
+        this.slots = memory / bytesPerEntry;
+
+        this.hash1 = new byte[hashSize];
+        this.hash2 = new byte[hashSize];
+    }
+
+    @Override
+    public int slots() {
+        return slots;
+    }
+
+    /**
+     * Get the offset associated with this key.
+     * @param key The key
+     * @return The offset associated with this key or -1 if the key is not found
+     */
+    @Override
+    public long get(ByteBuffer key) throws DigestException {
+        ++lookups;
+        hashInto(key, hash1);
+        // search for the hash of this key by repeated probing until we find the hash we are looking for or we find an empty slot
+        int attempt = 0;
+        int pos = 0;
+        //we need to guard against attempt integer overflow if the map is full
+        //limit attempt to number of slots once positionOf(..) enters linear search mode
+        int maxAttempts = slots + hashSize - 4;
+        do {
+            if (attempt >= maxAttempts)
+                return -1L;
+            pos = positionOf(hash1, attempt);
+            bytes.position(pos);
+            if (isEmpty(pos))
+                return -1L;
+            bytes.get(hash2);
+            ++attempt;
+        } while (!Arrays.equals(hash1, hash2));
+        return bytes.getLong();
+    }
+
+    /**
+     * Associate this offset to the given key.
+     * @param key The key
+     * @param offset The offset
+     */
+    @Override
+    public void put(ByteBuffer key, long offset) throws DigestException {
+        if (entries < slots)

Review Comment:
   Good catch, the check needs to be reversed since I switched Scala's from `require` to an `if`/`throw`. Fixed.



-- 
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: jira-unsubscribe@kafka.apache.org

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


[GitHub] [kafka] satishd commented on pull request #13042: KAFKA-14476: Move OffsetMap and related to storage module

Posted by GitBox <gi...@apache.org>.
satishd commented on PR #13042:
URL: https://github.com/apache/kafka/pull/13042#issuecomment-1363493292

   @ijuma There are test failures occurring in LogCleanerLagIntegrationTest, OffsetMapTest, and LogCleanerParameterizedIntegrationTest


-- 
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: jira-unsubscribe@kafka.apache.org

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


[GitHub] [kafka] ijuma commented on pull request #13042: KAFKA-14476: Move OffsetMap and related to storage module

Posted by GitBox <gi...@apache.org>.
ijuma commented on PR #13042:
URL: https://github.com/apache/kafka/pull/13042#issuecomment-1363376725

   cc @satishd 


-- 
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: jira-unsubscribe@kafka.apache.org

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


[GitHub] [kafka] ijuma commented on pull request #13042: KAFKA-14476: Move OffsetMap and related to storage module

Posted by GitBox <gi...@apache.org>.
ijuma commented on PR #13042:
URL: https://github.com/apache/kafka/pull/13042#issuecomment-1364051704

   JDK 17 build passed and the other build failures are unrelated:
   
   > Build / JDK 8 and Scala 2.12 / org.apache.kafka.connect.mirror.integration.MirrorConnectorsIntegrationBaseTest.testOffsetSyncsTopicsOnTarget()
   > Build / JDK 8 and Scala 2.12 / org.apache.kafka.streams.processor.internals.DefaultStateUpdaterTest.shouldPauseActiveTaskAndTransitToUpdateStandby()
   > Build / JDK 11 and Scala 2.13 / org.apache.kafka.clients.consumer.internals.AbstractCoordinatorTest.testWakeupAfterSyncGroupReceivedExternalCompletion()
   > 


-- 
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: jira-unsubscribe@kafka.apache.org

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