partitioner.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. type randomPartitioner struct {
  21. generator *rand.Rand
  22. }
  23. // NewRandomPartitioner returns a Partitioner which chooses a random partition each time.
  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. type roundRobinPartitioner struct {
  36. partition int32
  37. }
  38. // NewRoundRobinPartitioner returns a Partitioner which walks through the available partitions one at a time.
  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. type hashPartitioner struct {
  54. random Partitioner
  55. hasher hash.Hash32
  56. }
  57. // NewHashPartitioner returns a Partitioner which behaves as follows. If the key is nil, or fails to encode, then a random partition
  58. // is chosen. Otherwise the FNV-1a hash of the encoded bytes is used modulus the number of partitions. This ensures that messages
  59. // with the same key always end up on the same partition.
  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. }