partitioner.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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(topic string) Partitioner
  20. type manualPartitioner struct{}
  21. // NewManualPartitioner returns a Partitioner which uses the partition manually set in the provided
  22. // ProducerMessage's Partition field as the partition to produce to.
  23. func NewManualPartitioner(topic string) Partitioner {
  24. return new(manualPartitioner)
  25. }
  26. func (p *manualPartitioner) Partition(message *ProducerMessage, numPartitions int32) (int32, error) {
  27. return message.Partition, nil
  28. }
  29. func (p *manualPartitioner) RequiresConsistency() bool {
  30. return true
  31. }
  32. type randomPartitioner struct {
  33. generator *rand.Rand
  34. }
  35. // NewRandomPartitioner returns a Partitioner which chooses a random partition each time.
  36. func NewRandomPartitioner(topic string) Partitioner {
  37. p := new(randomPartitioner)
  38. p.generator = rand.New(rand.NewSource(time.Now().UTC().UnixNano()))
  39. return p
  40. }
  41. func (p *randomPartitioner) Partition(message *ProducerMessage, numPartitions int32) (int32, error) {
  42. return int32(p.generator.Intn(int(numPartitions))), nil
  43. }
  44. func (p *randomPartitioner) RequiresConsistency() bool {
  45. return false
  46. }
  47. type roundRobinPartitioner struct {
  48. partition int32
  49. }
  50. // NewRoundRobinPartitioner returns a Partitioner which walks through the available partitions one at a time.
  51. func NewRoundRobinPartitioner(topic string) Partitioner {
  52. return &roundRobinPartitioner{}
  53. }
  54. func (p *roundRobinPartitioner) Partition(message *ProducerMessage, numPartitions int32) (int32, error) {
  55. if p.partition >= numPartitions {
  56. p.partition = 0
  57. }
  58. ret := p.partition
  59. p.partition++
  60. return ret, nil
  61. }
  62. func (p *roundRobinPartitioner) RequiresConsistency() bool {
  63. return false
  64. }
  65. type hashPartitioner struct {
  66. random Partitioner
  67. hasher hash.Hash32
  68. }
  69. // NewHashPartitioner returns a Partitioner which behaves as follows. If the message's key is nil, or fails to
  70. // encode, then a random partition is chosen. Otherwise the FNV-1a hash of the encoded bytes of the message key
  71. // is used, modulus the number of partitions. This ensures that messages with the same key always end up on the
  72. // same partition.
  73. func NewHashPartitioner(topic string) Partitioner {
  74. p := new(hashPartitioner)
  75. p.random = NewRandomPartitioner(topic)
  76. p.hasher = fnv.New32a()
  77. return p
  78. }
  79. func (p *hashPartitioner) Partition(message *ProducerMessage, numPartitions int32) (int32, error) {
  80. if message.Key == nil {
  81. return p.random.Partition(message, numPartitions)
  82. }
  83. bytes, err := message.Key.Encode()
  84. if err != nil {
  85. return -1, err
  86. }
  87. p.hasher.Reset()
  88. _, err = p.hasher.Write(bytes)
  89. if err != nil {
  90. return -1, err
  91. }
  92. hash := int32(p.hasher.Sum32())
  93. if hash < 0 {
  94. hash = -hash
  95. }
  96. return hash % numPartitions, nil
  97. }
  98. func (p *hashPartitioner) RequiresConsistency() bool {
  99. return true
  100. }