-
Notifications
You must be signed in to change notification settings - Fork 15k
KAFKA-17747: [2/N] Add compute topic and group hash #19523
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
36cb999
f4c9749
ced64d7
7e00a14
77b99da
aff55ce
7f7649a
8fc5b33
d2427f9
b5eebbf
b6ccef3
a1be1f6
9b4294f
478dd98
9fc8e8e
167cd4a
6e219a9
08cdd37
527b387
cde92d8
b238375
a1b658a
ed7b450
44c1337
f1ea74b
bb1edb3
3382702
c031a29
ec7024c
cf35b7a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,11 +19,21 @@ | |
| import org.apache.kafka.common.KafkaException; | ||
| import org.apache.kafka.common.message.ListGroupsResponseData; | ||
| import org.apache.kafka.coordinator.common.runtime.CoordinatorRecord; | ||
| import org.apache.kafka.image.ClusterImage; | ||
| import org.apache.kafka.image.TopicImage; | ||
| import org.apache.kafka.metadata.BrokerRegistration; | ||
|
|
||
| import com.google.common.hash.HashCode; | ||
| import com.google.common.hash.HashFunction; | ||
| import com.google.common.hash.Hasher; | ||
| import com.google.common.hash.Hashing; | ||
|
|
||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.Locale; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
| import java.util.Optional; | ||
| import java.util.Set; | ||
| import java.util.function.Function; | ||
|
|
@@ -209,4 +219,50 @@ void validateOffsetFetch( | |
| default boolean shouldExpire() { | ||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Computes the hash of the topics in a group. | ||
| * | ||
| * @param topicHashes The map of topic hashes. Key is topic name and value is the topic hash. | ||
| * @return The hash of the group. | ||
| */ | ||
| static long computeGroupHash(Map<String, Long> topicHashes) { | ||
|
||
| return Hashing.combineOrdered( | ||
| topicHashes.entrySet() | ||
| .stream() | ||
| .sorted(Map.Entry.comparingByKey()) | ||
| .map(e -> HashCode.fromLong(e.getValue())) | ||
| .toList() | ||
| ).asLong(); | ||
| } | ||
|
|
||
| /** | ||
| * Computes the hash of the topic id, name, number of partitions, and partition racks by Murmur3. | ||
|
||
| * | ||
| * @param topicImage The topic image. | ||
| * @param clusterImage The cluster image. | ||
| * @return The hash of the topic. | ||
| */ | ||
| static long computeTopicHash(TopicImage topicImage, ClusterImage clusterImage) { | ||
| HashFunction hf = Hashing.murmur3_128(); | ||
| Hasher topicHasher = hf.newHasher() | ||
| .putByte((byte) 0) // magic byte | ||
|
||
| .putLong(topicImage.id().hashCode()) // topic Id | ||
| .putString(topicImage.name(), StandardCharsets.UTF_8) // topic name | ||
| .putInt(topicImage.partitions().size()); // number of partitions | ||
|
|
||
| topicImage.partitions().entrySet().stream().sorted(Map.Entry.comparingByKey()).forEach(entry -> { | ||
|
||
| topicHasher.putInt(entry.getKey()); // partition id | ||
| String racks = Arrays.stream(entry.getValue().replicas) | ||
| .mapToObj(clusterImage::broker) | ||
| .filter(Objects::nonNull) | ||
| .map(BrokerRegistration::rack) | ||
| .filter(Optional::isPresent) | ||
| .map(Optional::get) | ||
| .sorted() | ||
| .collect(Collectors.joining(";")); | ||
|
||
| topicHasher.putString(racks, StandardCharsets.UTF_8); // sorted racks with separator ";" | ||
| }); | ||
| return topicHasher.hash().asLong(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,189 @@ | ||
| /* | ||
| * 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.coordinator.group; | ||
|
|
||
| import org.apache.kafka.common.Uuid; | ||
| import org.apache.kafka.image.MetadataImage; | ||
|
|
||
| import com.google.common.hash.HashCode; | ||
| import com.google.common.hash.HashFunction; | ||
| import com.google.common.hash.Hasher; | ||
| import com.google.common.hash.Hashing; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.Arguments; | ||
| import org.junit.jupiter.params.provider.MethodSource; | ||
|
|
||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.stream.Stream; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNotEquals; | ||
|
|
||
| public class GroupTest { | ||
| private static final Uuid FOO_TOPIC_ID = Uuid.randomUuid(); | ||
| private static final String FOO_TOPIC_NAME = "foo"; | ||
| private static final String BAR_TOPIC_NAME = "bar"; | ||
| private static final int FOO_NUM_PARTITIONS = 2; | ||
| private static final MetadataImage FOO_METADATA_IMAGE = new MetadataImageBuilder() | ||
| .addTopic(FOO_TOPIC_ID, FOO_TOPIC_NAME, FOO_NUM_PARTITIONS) | ||
| .addRacks() | ||
| .build(); | ||
|
|
||
| @Test | ||
| void testComputeTopicHash() { | ||
| long result = Group.computeTopicHash(FOO_METADATA_IMAGE.topics().getTopic(FOO_TOPIC_ID), FOO_METADATA_IMAGE.cluster()); | ||
|
|
||
| HashFunction hf = Hashing.murmur3_128(); | ||
| Hasher topicHasher = hf.newHasher() | ||
| .putByte((byte) 0) // magic byte | ||
| .putLong(FOO_TOPIC_ID.hashCode()) // topic Id | ||
| .putString(FOO_TOPIC_NAME, StandardCharsets.UTF_8) // topic name | ||
| .putInt(FOO_NUM_PARTITIONS) // number of partitions | ||
| .putInt(0) // partition 0 | ||
| .putString("rack0;rack1", StandardCharsets.UTF_8) // rack of partition 0 | ||
| .putInt(1) // partition 1 | ||
| .putString("rack1;rack2", StandardCharsets.UTF_8); // rack of partition 1 | ||
| assertEquals(topicHasher.hash().asLong(), result); | ||
| } | ||
|
|
||
| @Test | ||
| void testComputeTopicHashWithDifferentMagicByte() { | ||
| long result = Group.computeTopicHash(FOO_METADATA_IMAGE.topics().getTopic(FOO_TOPIC_ID), FOO_METADATA_IMAGE.cluster()); | ||
|
|
||
| HashFunction hf = Hashing.murmur3_128(); | ||
| Hasher topicHasher = hf.newHasher() | ||
| .putByte((byte) 1) // different magic byte | ||
| .putLong(FOO_TOPIC_ID.hashCode()) // topic Id | ||
| .putString(FOO_TOPIC_NAME, StandardCharsets.UTF_8) // topic name | ||
| .putInt(FOO_NUM_PARTITIONS) // number of partitions | ||
| .putInt(0) // partition 0 | ||
| .putString("rack0;rack1", StandardCharsets.UTF_8) // rack of partition 0 | ||
| .putInt(1) // partition 1 | ||
| .putString("rack1;rack2", StandardCharsets.UTF_8); // rack of partition 1 | ||
| assertNotEquals(topicHasher.hash().asLong(), result); | ||
| } | ||
|
|
||
| @Test | ||
| void testComputeTopicHashWithDifferentPartitionOrder() { | ||
| long result = Group.computeTopicHash(FOO_METADATA_IMAGE.topics().getTopic(FOO_TOPIC_ID), FOO_METADATA_IMAGE.cluster()); | ||
|
|
||
| HashFunction hf = Hashing.murmur3_128(); | ||
| Hasher topicHasher = hf.newHasher() | ||
| .putByte((byte) 0) // magic byte | ||
| .putLong(FOO_TOPIC_ID.hashCode()) // topic Id | ||
| .putString(FOO_TOPIC_NAME, StandardCharsets.UTF_8) // topic name | ||
| .putInt(FOO_NUM_PARTITIONS) // number of partitions | ||
| // different partition order | ||
| .putInt(1) // partition 1 | ||
| .putString("rack1;rack2", StandardCharsets.UTF_8) // rack of partition 1 | ||
| .putInt(0) // partition 0 | ||
| .putString("rack0;rack1", StandardCharsets.UTF_8); // rack of partition 0 | ||
| assertNotEquals(topicHasher.hash().asLong(), result); | ||
| } | ||
|
|
||
| @Test | ||
| void testComputeTopicHashWithDifferentRackOrder() { | ||
| long result = Group.computeTopicHash(FOO_METADATA_IMAGE.topics().getTopic(FOO_TOPIC_ID), FOO_METADATA_IMAGE.cluster()); | ||
|
|
||
| HashFunction hf = Hashing.murmur3_128(); | ||
| Hasher topicHasher = hf.newHasher() | ||
| .putByte((byte) 0) // magic byte | ||
| .putLong(FOO_TOPIC_ID.hashCode()) // topic Id | ||
| .putString(FOO_TOPIC_NAME, StandardCharsets.UTF_8) // topic name | ||
| .putInt(FOO_NUM_PARTITIONS) // number of partitions | ||
| .putInt(0) // partition 0 | ||
| .putString("rack1;rack0", StandardCharsets.UTF_8) // different rack order of partition 0 | ||
| .putInt(1) // partition 1 | ||
| .putString("rack1;rack2", StandardCharsets.UTF_8); // rack of partition 1 | ||
| assertNotEquals(topicHasher.hash().asLong(), result); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @MethodSource("differentFieldGenerator") | ||
| void testComputeTopicHashWithDifferentField(MetadataImage differentImage, Uuid topicId) { | ||
| long result = Group.computeTopicHash(FOO_METADATA_IMAGE.topics().getTopic(FOO_TOPIC_ID), FOO_METADATA_IMAGE.cluster()); | ||
|
|
||
| assertNotEquals( | ||
| Group.computeTopicHash( | ||
| differentImage.topics().getTopic(topicId), | ||
| differentImage.cluster() | ||
| ), | ||
| result | ||
| ); | ||
| } | ||
|
|
||
| private static Stream<Arguments> differentFieldGenerator() { | ||
| Uuid differentTopicId = Uuid.randomUuid(); | ||
| return Stream.of( | ||
| Arguments.of(new MetadataImageBuilder() // different topic id | ||
| .addTopic(differentTopicId, FOO_TOPIC_NAME, FOO_NUM_PARTITIONS) | ||
| .addRacks() | ||
| .build(), | ||
| differentTopicId | ||
| ), | ||
| Arguments.of(new MetadataImageBuilder() // different topic name | ||
| .addTopic(FOO_TOPIC_ID, "bar", FOO_NUM_PARTITIONS) | ||
| .addRacks() | ||
| .build(), | ||
| FOO_TOPIC_ID | ||
| ), | ||
| Arguments.of(new MetadataImageBuilder() // different partitions | ||
| .addTopic(FOO_TOPIC_ID, FOO_TOPIC_NAME, 1) | ||
| .addRacks() | ||
| .build(), | ||
| FOO_TOPIC_ID | ||
| ), | ||
| Arguments.of(new MetadataImageBuilder() // different racks | ||
| .addTopic(FOO_TOPIC_ID, FOO_TOPIC_NAME, FOO_NUM_PARTITIONS) | ||
| .build(), | ||
| FOO_TOPIC_ID | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| void testComputeGroupHash() { | ||
| long result = Group.computeGroupHash(Map.of( | ||
| BAR_TOPIC_NAME, 123L, | ||
| FOO_TOPIC_NAME, 456L | ||
| )); | ||
|
|
||
| long expected = Hashing.combineOrdered(List.of( | ||
| HashCode.fromLong(123L), | ||
| HashCode.fromLong(456L) | ||
| )).asLong(); | ||
| assertEquals(expected, result); | ||
| } | ||
|
|
||
| @Test | ||
| void testComputeGroupHashWithDifferentOrder() { | ||
| long result = Group.computeGroupHash(Map.of( | ||
| BAR_TOPIC_NAME, 123L, | ||
| FOO_TOPIC_NAME, 456L | ||
| )); | ||
|
|
||
| long unexpected = Hashing.combineOrdered(List.of( | ||
| HashCode.fromLong(456L), | ||
| HashCode.fromLong(123L) | ||
| )).asLong(); | ||
| assertNotEquals(unexpected, result); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| Checker Framework qualifiers | ||
| Copyright 2004-present by the Checker Framework developers | ||
|
|
||
| MIT License: | ||
|
|
||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
|
|
||
| The above copyright notice and this permission notice shall be included in | ||
| all copies or substantial portions of the Software. | ||
|
|
||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| THE SOFTWARE. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is something that we haven't really discussed in the KIP because it is an implementation detail but we should discuss whether we really want to take a dependency on Guava.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I update PR to remove guava. I think we can put all data to a byte array and use Murmur3 to hash it, so we don't rely on guava.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
While talking to @ijuma about it, he has suggested to look into https://github.com/lz4/lz4-java/tree/master/src/java/net/jpountz/xxhash. We get it via lz4 and it is apparently much faster than Murmur3. It may be worth running a few benchmarks to compare then. What do you think?
I also wonder what is the impact of putting all the data to a byte array before hashing it. Do you have thoughts on this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIRC, the lz4-java is not maintained anymore, so not sure whether the code maintaining is a risk.
I suggest that
EventProcessorThreadcan leverageGrowableBufferSupplierto reuse buffer as much as possible. Additionally,Group#computeTopicHashinshould useByteBufferOutputStreamto generate the bytes array, asByteBufferOutputStream#buffer#arraycan avoid extra array copy likeByteArrayOutputStream#toByteArrayThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This hash function is used by zstd too. Its pretty safe to rely on it given that lz4 and zstd are the most popular compression algorithms. And we will be supporting them for the foreseeable future.
Which particular implementation we use is a fair question.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with using a BufferSupplier in order to reuse buffers. However, EventProcessorThread may be too low level to hold it. Having it in Shard may be enough.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the suggestion. Updated benchmark result.
TopicHashBenchmark.java
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can revisit this when the critical code are used by production :)
@FrankYang0529 thanks for updates. the result LGTM.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on the KIP-1101, it minimizes the calculation count of topic hash. The result can be shared between groups. I think we can keep this function simple currently.
With BufferSupplier, the hash function needs to be thread safe to reuse the buffer. We can revisit it in the future.
The ByteBufferOutputStream needs a ByteBuffer with capacity. I wonder whether we can calculate a accurate capacity. For example, rack string can contain any character. It presents 1 to 4 bytes in UTF-8.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The initialize capacity can be discussed later. In fact, it may be not a issue if we adopt the growable buffer. The buffer can be big enough for each hash computing eventually.