-
Notifications
You must be signed in to change notification settings - Fork 14.5k
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 all 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 |
---|---|---|
|
@@ -25,10 +25,15 @@ | |
import org.apache.kafka.common.protocol.ApiMessage; | ||
import org.apache.kafka.coordinator.group.generated.ConsumerGroupCurrentMemberAssignmentValue; | ||
import org.apache.kafka.coordinator.group.generated.ShareGroupCurrentMemberAssignmentValue; | ||
import org.apache.kafka.image.ClusterImage; | ||
import org.apache.kafka.image.MetadataImage; | ||
import org.apache.kafka.image.TopicImage; | ||
import org.apache.kafka.image.TopicsImage; | ||
import org.apache.kafka.metadata.BrokerRegistration; | ||
import org.apache.kafka.server.common.ApiMessageAndVersion; | ||
|
||
import com.dynatrace.hash4j.hashing.HashStream64; | ||
import com.dynatrace.hash4j.hashing.Hashing; | ||
import com.google.re2j.Pattern; | ||
import com.google.re2j.PatternSyntaxException; | ||
|
||
|
@@ -324,4 +329,99 @@ static void throwIfRegularExpressionIsInvalid( | |
regex, ex.getDescription())); | ||
} | ||
} | ||
|
||
/** | ||
* The magic byte used to identify the version of topic hash function. | ||
*/ | ||
static final byte TOPIC_HASH_MAGIC_BYTE = 0x00; | ||
|
||
/** | ||
* Computes the hash of the topics in a group. | ||
* <p> | ||
* The computed hash value is stored as the metadata hash in the *GroupMetadataValue. | ||
* <p> | ||
* If there is no topic, the hash value is set to 0. | ||
* The hashing process involves the following steps: | ||
* 1. Sort the topic hashes by topic name. | ||
* 2. Write each topic hash in order. | ||
* | ||
* @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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: public? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IIUC, this function will only be used in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok. It works for me. |
||
if (topicHashes.isEmpty()) { | ||
return 0; | ||
} | ||
|
||
// Sort entries by topic name | ||
List<Map.Entry<String, Long>> sortedEntries = new ArrayList<>(topicHashes.entrySet()); | ||
sortedEntries.sort(Map.Entry.comparingByKey()); | ||
|
||
HashStream64 hasher = Hashing.xxh3_64().hashStream(); | ||
for (Map.Entry<String, Long> entry : sortedEntries) { | ||
hasher.putLong(entry.getValue()); | ||
} | ||
|
||
return hasher.getAsLong(); | ||
} | ||
|
||
/** | ||
* Computes the hash of the topic id, name, number of partitions, and partition racks by streaming XXH3. | ||
* <p> | ||
* The computed hash value for the topic is utilized in conjunction with the {@link #computeGroupHash(Map)} | ||
* method and is stored as part of the metadata hash in the *GroupMetadataValue. | ||
* It is important to note that if the hash algorithm is changed, the magic byte must be updated to reflect the | ||
* new hash version. | ||
* <p> | ||
* For non-existent topics, the hash value is set to 0. | ||
* For existent topics, the hashing process involves the following steps: | ||
* 1. Write a magic byte to denote the version of the hash function. | ||
* 2. Write the hash code of the topic ID with mostSignificantBits and leastSignificantBits. | ||
* 3. Write the topic name. | ||
* 4. Write the number of partitions associated with the topic. | ||
* 5. For each partition, write the partition ID and a sorted list of rack identifiers. | ||
* - Rack identifiers are formatted as "<length1><value1><length2><value2>" to prevent issues with simple separators. | ||
* | ||
* @param topicName The topic image. | ||
* @param metadataImage The cluster image. | ||
* @return The hash of the topic. | ||
*/ | ||
static long computeTopicHash(String topicName, MetadataImage metadataImage) { | ||
TopicImage topicImage = metadataImage.topics().getTopic(topicName); | ||
if (topicImage == null) { | ||
return 0; | ||
} | ||
|
||
HashStream64 hasher = Hashing.xxh3_64().hashStream(); | ||
hasher = hasher | ||
.putByte(TOPIC_HASH_MAGIC_BYTE) | ||
.putLong(topicImage.id().getMostSignificantBits()) | ||
.putLong(topicImage.id().getLeastSignificantBits()) | ||
.putString(topicImage.name()) | ||
.putInt(topicImage.partitions().size()); | ||
|
||
ClusterImage clusterImage = metadataImage.cluster(); | ||
List<String> racks = new ArrayList<>(); | ||
for (int i = 0; i < topicImage.partitions().size(); i++) { | ||
hasher = hasher.putInt(i); | ||
racks.clear(); // Clear the list for reuse | ||
for (int replicaId : topicImage.partitions().get(i).replicas) { | ||
BrokerRegistration broker = clusterImage.broker(replicaId); | ||
if (broker != null) { | ||
broker.rack().ifPresent(racks::add); | ||
} | ||
} | ||
|
||
Collections.sort(racks); | ||
for (String rack : racks) { | ||
// Format: "<length><value>" | ||
// The rack string combination cannot use simple separator like ",", because there is no limitation for rack character. | ||
// If using simple separator like "," it may hit edge case like ",," and ",,," / ",,," and ",,". | ||
// Add length before the rack string to avoid the edge case. | ||
hasher = hasher.putInt(rack.length()).putString(rack); | ||
} | ||
} | ||
|
||
return hasher.getAsLong(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Should we put an empty line before the return? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated it. |
||
} | ||
} |
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 suppose that we need to update the license file too.
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.
Yes, I added it to LICENSE-binary and
python committer-tools/verify_license.py
can pass. Do I need to modify other files? Thanks.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 fine. Thanks.