Skip to content
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

fix: correct WithCustomFallbackPartitioner implementation #1988

Merged
merged 1 commit into from
Aug 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions partitioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ func WithCustomHashFunction(hasher func() hash.Hash32) HashPartitionerOption {
}

// WithCustomFallbackPartitioner lets you specify what HashPartitioner should be used in case a Distribution Key is empty
func WithCustomFallbackPartitioner(randomHP *hashPartitioner) HashPartitionerOption {
func WithCustomFallbackPartitioner(randomHP Partitioner) HashPartitionerOption {
return func(hp *hashPartitioner) {
hp.random = hp
hp.random = randomHP
}
}

Expand Down
30 changes: 30 additions & 0 deletions partitioner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,36 @@ func TestManualPartitioner(t *testing.T) {
}
}

func TestWithCustomFallbackPartitioner(t *testing.T) {
topic := "mytopic"

partitioner := NewCustomPartitioner(
// override default random partitioner with round robin
WithCustomFallbackPartitioner(NewRoundRobinPartitioner(topic)),
)(topic)

// Should use round robin implementation if there is no key
var i int32
for i = 0; i < 50; i++ {
choice, err := partitioner.Partition(&ProducerMessage{Key: nil}, 7)
if err != nil {
t.Error(partitioner, err)
}
if choice != i%7 {
t.Error("Returned partition", choice, "expecting", i%7)
}
}

// should use hash partitioner if key is specified
buf := make([]byte, 256)
for i := 0; i < 50; i++ {
if _, err := rand.Read(buf); err != nil {
t.Error(err)
}
assertPartitioningConsistent(t, partitioner, &ProducerMessage{Key: ByteEncoder(buf)}, 50)
}
}

// By default, Sarama uses the message's key to consistently assign a partition to
// a message using hashing. If no key is set, a random partition will be chosen.
// This example shows how you can partition messages randomly, even when a key is set,
Expand Down