Add public API for computing partition assignment from a key - #489
Open
dskliarov wants to merge 1 commit into
Open
Add public API for computing partition assignment from a key#489dskliarov wants to merge 1 commit into
dskliarov wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Goka's own partition assignment logic (View.hash()/Processor.hash()) is unexported in goka. In case if the client required to detect partition id by the key, he has to create his own version of the consistent hashing. There is a high possibility, that the logic could be changed and hashing calculation would silently diverge.
Adds PartitionForKey(key []byte, numPartitions int32, hasher func() hash.Hash32) (int32, error), a new exported function that computes the partition a key would be assigned to, using the same hash-and-mod algorithm goka already uses internally in Processor.hash and View.hash.
This lets external callers predict partition assignment (e.g. for routing, sharding-aware batching, or debugging) without needing a running View or Processor instance.
Changes
sarama.NewCustomHashPartitioner, parity with the previous internal View.hash logic, and custom hasher usage.
Behavior notes
the internal call sites, not a behavior change for existing users.
old strings.
(e.g. wrong partition count, or DefaultHasher() when a custom hasher was configured) will silently produce a different partition than the real one.
Testing
counts.