partitioner.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 takes a message and partition count and chooses a partition
  13. Partition(message *ProducerMessage, numPartitions int32) (int32, error)
  14. // RequiresConsistency indicates to the user of the partitioner whether the
  15. // mapping of key->partition is consistent or not. Specifically, if a
  16. // partitioner requires consistency then it must be allowed to choose from all
  17. // partitions (even ones known to be unavailable), and its choice must be
  18. // respected by the caller. The obvious example is the HashPartitioner.
  19. RequiresConsistency() bool
  20. }
  21. // PartitionerConstructor is the type for a function capable of constructing new Partitioners.
  22. type PartitionerConstructor func(topic string) Partitioner
  23. type manualPartitioner struct{}
  24. // NewManualPartitioner returns a Partitioner which uses the partition manually set in the provided
  25. // ProducerMessage's Partition field as the partition to produce to.
  26. func NewManualPartitioner(topic string) Partitioner {
  27. return new(manualPartitioner)
  28. }
  29. func (p *manualPartitioner) Partition(message *ProducerMessage, numPartitions int32) (int32, error) {
  30. return message.Partition, nil
  31. }
  32. func (p *manualPartitioner) RequiresConsistency() bool {
  33. return true
  34. }
  35. type randomPartitioner struct {
  36. generator *rand.Rand
  37. }
  38. // NewRandomPartitioner returns a Partitioner which chooses a random partition each time.
  39. func NewRandomPartitioner(topic string) Partitioner {
  40. p := new(randomPartitioner)
  41. p.generator = rand.New(rand.NewSource(time.Now().UTC().UnixNano()))
  42. return p
  43. }
  44. func (p *randomPartitioner) Partition(message *ProducerMessage, numPartitions int32) (int32, error) {
  45. return int32(p.generator.Intn(int(numPartitions))), nil
  46. }
  47. func (p *randomPartitioner) RequiresConsistency() bool {
  48. return false
  49. }
  50. type roundRobinPartitioner struct {
  51. partition int32
  52. }
  53. // NewRoundRobinPartitioner returns a Partitioner which walks through the available partitions one at a time.
  54. func NewRoundRobinPartitioner(topic string) Partitioner {
  55. return &roundRobinPartitioner{}
  56. }
  57. func (p *roundRobinPartitioner) Partition(message *ProducerMessage, numPartitions int32) (int32, error) {
  58. if p.partition >= numPartitions {
  59. p.partition = 0
  60. }
  61. ret := p.partition
  62. p.partition++
  63. return ret, nil
  64. }
  65. func (p *roundRobinPartitioner) RequiresConsistency() bool {
  66. return false
  67. }
  68. type hashPartitioner struct {
  69. random Partitioner
  70. hasher hash.Hash32
  71. }
  72. // NewCustomHashPartitioner is a wrapper around NewHashPartitioner, allowing the use of custom hasher.
  73. // The argument is a function providing the instance, implementing the hash.Hash32 interface. This is to ensure that
  74. // each partition dispatcher gets its own hasher, to avoid concurrency issues by sharing an instance.
  75. func NewCustomHashPartitioner(hasher func() hash.Hash32) PartitionerConstructor {
  76. return func(topic string) Partitioner {
  77. p := new(hashPartitioner)
  78. p.random = NewRandomPartitioner(topic)
  79. p.hasher = hasher()
  80. return p
  81. }
  82. }
  83. // NewHashPartitioner returns a Partitioner which behaves as follows. If the message's key is nil then a
  84. // random partition is chosen. Otherwise the FNV-1a hash of the encoded bytes of the message key is used,
  85. // modulus the number of partitions. This ensures that messages with the same key always end up on the
  86. // same partition.
  87. func NewHashPartitioner(topic string) Partitioner {
  88. p := new(hashPartitioner)
  89. p.random = NewRandomPartitioner(topic)
  90. p.hasher = fnv.New32a()
  91. return p
  92. }
  93. func (p *hashPartitioner) Partition(message *ProducerMessage, numPartitions int32) (int32, error) {
  94. if message.Key == nil {
  95. return p.random.Partition(message, numPartitions)
  96. }
  97. bytes, err := message.Key.Encode()
  98. if err != nil {
  99. return -1, err
  100. }
  101. p.hasher.Reset()
  102. _, err = p.hasher.Write(bytes)
  103. if err != nil {
  104. return -1, err
  105. }
  106. partition := int32(p.hasher.Sum32()) % numPartitions
  107. if partition < 0 {
  108. partition = -partition
  109. }
  110. return partition, nil
  111. }
  112. func (p *hashPartitioner) RequiresConsistency() bool {
  113. return true
  114. }