partitioner.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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, error) // Partition takes the key and partition count and chooses a partition
  13. // RequiresConsistency indicates to the user of the partitioner whether the mapping of key->partition is consistent or not.
  14. // Specifically, if a partitioner requires consistency then it must be allowed to choose from all partitions (even ones known to
  15. // be unavailable), and its choice must be respected by the caller. The obvious example is the HashPartitioner.
  16. RequiresConsistency() bool
  17. }
  18. // PartitionerConstructor is the type for a function capable of constructing new Partitioners.
  19. type PartitionerConstructor func() Partitioner
  20. // RandomPartitioner implements the Partitioner interface by choosing a random partition each time.
  21. type RandomPartitioner struct {
  22. generator *rand.Rand
  23. }
  24. func NewRandomPartitioner() Partitioner {
  25. p := new(RandomPartitioner)
  26. p.generator = rand.New(rand.NewSource(time.Now().UTC().UnixNano()))
  27. return p
  28. }
  29. func (p *RandomPartitioner) Partition(key Encoder, numPartitions int32) (int32, error) {
  30. return int32(p.generator.Intn(int(numPartitions))), nil
  31. }
  32. func (p *RandomPartitioner) RequiresConsistency() bool {
  33. return false
  34. }
  35. // RoundRobinPartitioner implements the Partitioner interface by walking through the available partitions one at a time.
  36. type RoundRobinPartitioner struct {
  37. partition int32
  38. }
  39. func NewRoundRobinPartitioner() Partitioner {
  40. return &RoundRobinPartitioner{}
  41. }
  42. func (p *RoundRobinPartitioner) Partition(key Encoder, numPartitions int32) (int32, error) {
  43. if p.partition >= numPartitions {
  44. p.partition = 0
  45. }
  46. ret := p.partition
  47. p.partition++
  48. return ret, nil
  49. }
  50. func (p *RoundRobinPartitioner) RequiresConsistency() bool {
  51. return false
  52. }
  53. // HashPartitioner implements the Partitioner interface. If the key is nil, or fails to encode, then a random partition
  54. // is chosen. Otherwise the FNV-1a hash of the encoded bytes is used modulus the number of partitions. This ensures that messages
  55. // with the same key always end up on the same partition.
  56. type HashPartitioner struct {
  57. random Partitioner
  58. hasher hash.Hash32
  59. }
  60. func NewHashPartitioner() Partitioner {
  61. p := new(HashPartitioner)
  62. p.random = NewRandomPartitioner()
  63. p.hasher = fnv.New32a()
  64. return p
  65. }
  66. func (p *HashPartitioner) Partition(key Encoder, numPartitions int32) (int32, error) {
  67. if key == nil {
  68. return p.random.Partition(key, numPartitions)
  69. }
  70. bytes, err := key.Encode()
  71. if err != nil {
  72. return -1, err
  73. }
  74. p.hasher.Reset()
  75. _, err = p.hasher.Write(bytes)
  76. if err != nil {
  77. return -1, err
  78. }
  79. hash := int32(p.hasher.Sum32())
  80. if hash < 0 {
  81. hash = -hash
  82. }
  83. return hash % numPartitions, nil
  84. }
  85. func (p *HashPartitioner) RequiresConsistency() bool {
  86. return true
  87. }
  88. // ConstantPartitioner implements the Partitioner interface by just returning a constant value.
  89. type ConstantPartitioner struct {
  90. Constant int32
  91. }
  92. func (p *ConstantPartitioner) Partition(key Encoder, numPartitions int32) (int32, error) {
  93. return p.Constant, nil
  94. }
  95. func (p *ConstantPartitioner) RequiresConsistency() bool {
  96. return true
  97. }