partitioner.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package sarama
  2. import (
  3. "hash"
  4. "hash/fnv"
  5. "math/rand"
  6. "time"
  7. )
  8. // Partitioner is anything that, given a Kafka message key and a number of partitions indexed [0...numPartitions-1],
  9. // decides to which partition to send the message. RandomPartitioner, RoundRobinPartitioner and HashPartitioner are provided
  10. // as simple default implementations.
  11. type Partitioner interface {
  12. Partition(key Encoder, numPartitions int32) int32
  13. }
  14. // PartitionerConstructor is the type for a function capable of constructing new Partitioners.
  15. type PartitionerConstructor func() Partitioner
  16. // RandomPartitioner implements the Partitioner interface by choosing a random partition each time.
  17. type RandomPartitioner struct {
  18. generator *rand.Rand
  19. }
  20. func NewRandomPartitioner() Partitioner {
  21. p := new(RandomPartitioner)
  22. p.generator = rand.New(rand.NewSource(time.Now().UTC().UnixNano()))
  23. return p
  24. }
  25. func (p *RandomPartitioner) Partition(key Encoder, numPartitions int32) int32 {
  26. return int32(p.generator.Intn(int(numPartitions)))
  27. }
  28. // RoundRobinPartitioner implements the Partitioner interface by walking through the available partitions one at a time.
  29. type RoundRobinPartitioner struct {
  30. partition int32
  31. }
  32. func NewRoundRobinPartitioner() Partitioner {
  33. return &RoundRobinPartitioner{}
  34. }
  35. func (p *RoundRobinPartitioner) Partition(key Encoder, numPartitions int32) int32 {
  36. if p.partition >= numPartitions {
  37. p.partition = 0
  38. }
  39. ret := p.partition
  40. p.partition++
  41. return ret
  42. }
  43. // HashPartitioner implements the Partitioner interface. If the key is nil, or fails to encode, then a random partition
  44. // is chosen. Otherwise the FNV-1a hash of the encoded bytes is used modulus the number of partitions. This ensures that messages
  45. // with the same key always end up on the same partition.
  46. type HashPartitioner struct {
  47. random Partitioner
  48. hasher hash.Hash32
  49. }
  50. func NewHashPartitioner() Partitioner {
  51. p := new(HashPartitioner)
  52. p.random = NewRandomPartitioner()
  53. p.hasher = fnv.New32a()
  54. return p
  55. }
  56. func (p *HashPartitioner) Partition(key Encoder, numPartitions int32) int32 {
  57. if key == nil {
  58. return p.random.Partition(key, numPartitions)
  59. }
  60. bytes, err := key.Encode()
  61. if err != nil {
  62. return p.random.Partition(key, numPartitions)
  63. }
  64. p.hasher.Reset()
  65. _, err = p.hasher.Write(bytes)
  66. if err != nil {
  67. return p.random.Partition(key, numPartitions)
  68. }
  69. hash := int32(p.hasher.Sum32())
  70. if hash < 0 {
  71. hash = -hash
  72. }
  73. return hash % numPartitions
  74. }
  75. // ConstantPartitioner implements the Partitioner interface by just returning a constant value.
  76. type ConstantPartitioner struct {
  77. Constant int32
  78. }
  79. func (p *ConstantPartitioner) Partition(key Encoder, numPartitions int32) int32 {
  80. return p.Constant
  81. }