partitioner.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. // NewHashPartitioner returns a Partitioner which behaves as follows. If the message's key is nil then a
  73. // random partition is chosen. Otherwise the FNV-1a hash of the encoded bytes of the message key is used,
  74. // modulus the number of partitions. This ensures that messages with the same key always end up on the
  75. // same partition.
  76. func NewHashPartitioner(topic string) Partitioner {
  77. p := new(hashPartitioner)
  78. p.random = NewRandomPartitioner(topic)
  79. p.hasher = fnv.New32a()
  80. return p
  81. }
  82. func (p *hashPartitioner) Partition(message *ProducerMessage, numPartitions int32) (int32, error) {
  83. if message.Key == nil {
  84. return p.random.Partition(message, numPartitions)
  85. }
  86. bytes, err := message.Key.Encode()
  87. if err != nil {
  88. return -1, err
  89. }
  90. p.hasher.Reset()
  91. _, err = p.hasher.Write(bytes)
  92. if err != nil {
  93. return -1, err
  94. }
  95. partition := int32(p.hasher.Sum32()) % numPartitions
  96. if partition < 0 {
  97. partition = -partition
  98. }
  99. return partition, nil
  100. }
  101. func (p *hashPartitioner) RequiresConsistency() bool {
  102. return true
  103. }