partitioner.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 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(message *ProducerMessage, numPartitions int32) (int32, error) // Partition takes a message 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(message *ProducerMessage, 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(message *ProducerMessage, 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 message's key is nil, or fails to
  58. // encode, then a random partition is chosen. Otherwise the FNV-1a hash of the encoded bytes of the message key
  59. // is used, modulus the number of partitions. This ensures that messages with the same key always end up on the
  60. // same partition.
  61. func NewHashPartitioner() Partitioner {
  62. p := new(hashPartitioner)
  63. p.random = NewRandomPartitioner()
  64. p.hasher = fnv.New32a()
  65. return p
  66. }
  67. func (p *hashPartitioner) Partition(message *ProducerMessage, numPartitions int32) (int32, error) {
  68. if message.Key == nil {
  69. return p.random.Partition(message, numPartitions)
  70. }
  71. bytes, err := message.Key.Encode()
  72. if err != nil {
  73. return -1, err
  74. }
  75. p.hasher.Reset()
  76. _, err = p.hasher.Write(bytes)
  77. if err != nil {
  78. return -1, err
  79. }
  80. hash := int32(p.hasher.Sum32())
  81. if hash < 0 {
  82. hash = -hash
  83. }
  84. return hash % numPartitions, nil
  85. }
  86. func (p *hashPartitioner) RequiresConsistency() bool {
  87. return true
  88. }