balancer.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. package kafka
  2. import (
  3. "hash"
  4. "hash/crc32"
  5. "hash/fnv"
  6. "math/rand"
  7. "sort"
  8. "sync"
  9. "sync/atomic"
  10. )
  11. // The Balancer interface provides an abstraction of the message distribution
  12. // logic used by Writer instances to route messages to the partitions available
  13. // on a kafka cluster.
  14. //
  15. // Balancers must be safe to use concurrently from multiple goroutines.
  16. type Balancer interface {
  17. // Balance receives a message and a set of available partitions and
  18. // returns the partition number that the message should be routed to.
  19. //
  20. // An application should refrain from using a balancer to manage multiple
  21. // sets of partitions (from different topics for examples), use one balancer
  22. // instance for each partition set, so the balancer can detect when the
  23. // partitions change and assume that the kafka topic has been rebalanced.
  24. Balance(msg Message, partitions ...int) (partition int)
  25. }
  26. // BalancerFunc is an implementation of the Balancer interface that makes it
  27. // possible to use regular functions to distribute messages across partitions.
  28. type BalancerFunc func(Message, ...int) int
  29. // Balance calls f, satisfies the Balancer interface.
  30. func (f BalancerFunc) Balance(msg Message, partitions ...int) int {
  31. return f(msg, partitions...)
  32. }
  33. // RoundRobin is an Balancer implementation that equally distributes messages
  34. // across all available partitions.
  35. type RoundRobin struct {
  36. // Use a 32 bits integer so RoundRobin values don't need to be aligned to
  37. // apply atomic increments.
  38. offset uint32
  39. }
  40. // Balance satisfies the Balancer interface.
  41. func (rr *RoundRobin) Balance(msg Message, partitions ...int) int {
  42. return rr.balance(partitions)
  43. }
  44. func (rr *RoundRobin) balance(partitions []int) int {
  45. length := uint32(len(partitions))
  46. offset := atomic.AddUint32(&rr.offset, 1) - 1
  47. return partitions[offset%length]
  48. }
  49. // LeastBytes is a Balancer implementation that routes messages to the partition
  50. // that has received the least amount of data.
  51. //
  52. // Note that no coordination is done between multiple producers, having good
  53. // balancing relies on the fact that each producer using a LeastBytes balancer
  54. // should produce well balanced messages.
  55. type LeastBytes struct {
  56. mutex sync.Mutex
  57. counters []leastBytesCounter
  58. }
  59. type leastBytesCounter struct {
  60. partition int
  61. bytes uint64
  62. }
  63. // Balance satisfies the Balancer interface.
  64. func (lb *LeastBytes) Balance(msg Message, partitions ...int) int {
  65. // partitions change
  66. if len(partitions) != len(lb.counters) {
  67. lb.counters = lb.makeCounters(partitions...)
  68. }
  69. minBytes := lb.counters[0].bytes
  70. minIndex := 0
  71. for i, c := range lb.counters[1:] {
  72. if c.bytes < minBytes {
  73. minIndex = i + 1
  74. minBytes = c.bytes
  75. }
  76. }
  77. c := &lb.counters[minIndex]
  78. c.bytes += uint64(len(msg.Key)) + uint64(len(msg.Value))
  79. return c.partition
  80. }
  81. func (lb *LeastBytes) makeCounters(partitions ...int) (counters []leastBytesCounter) {
  82. counters = make([]leastBytesCounter, len(partitions))
  83. for i, p := range partitions {
  84. counters[i].partition = p
  85. }
  86. sort.Slice(counters, func(i int, j int) bool {
  87. return counters[i].partition < counters[j].partition
  88. })
  89. return
  90. }
  91. var (
  92. fnv1aPool = &sync.Pool{
  93. New: func() interface{} {
  94. return fnv.New32a()
  95. },
  96. }
  97. )
  98. // Hash is a Balancer that uses the provided hash function to determine which
  99. // partition to route messages to. This ensures that messages with the same key
  100. // are routed to the same partition.
  101. //
  102. // The logic to calculate the partition is:
  103. //
  104. // hasher.Sum32() % len(partitions) => partition
  105. //
  106. // By default, Hash uses the FNV-1a algorithm. This is the same algorithm used
  107. // by the Sarama Producer and ensures that messages produced by kafka-go will
  108. // be delivered to the same topics that the Sarama producer would be delivered to
  109. type Hash struct {
  110. rr RoundRobin
  111. Hasher hash.Hash32
  112. // lock protects Hasher while calculating the hash code. It is assumed that
  113. // the Hasher field is read-only once the Balancer is created, so as a
  114. // performance optimization, reads of the field are not protected.
  115. lock sync.Mutex
  116. }
  117. func (h *Hash) Balance(msg Message, partitions ...int) int {
  118. if msg.Key == nil {
  119. return h.rr.Balance(msg, partitions...)
  120. }
  121. hasher := h.Hasher
  122. if hasher != nil {
  123. h.lock.Lock()
  124. defer h.lock.Unlock()
  125. } else {
  126. hasher = fnv1aPool.Get().(hash.Hash32)
  127. defer fnv1aPool.Put(hasher)
  128. }
  129. hasher.Reset()
  130. if _, err := hasher.Write(msg.Key); err != nil {
  131. panic(err)
  132. }
  133. // uses same algorithm that Sarama's hashPartitioner uses
  134. // note the type conversions here. if the uint32 hash code is not cast to
  135. // an int32, we do not get the same result as sarama.
  136. partition := int32(hasher.Sum32()) % int32(len(partitions))
  137. if partition < 0 {
  138. partition = -partition
  139. }
  140. return int(partition)
  141. }
  142. type randomBalancer struct {
  143. mock int // mocked return value, used for testing
  144. }
  145. func (b randomBalancer) Balance(msg Message, partitions ...int) (partition int) {
  146. if b.mock != 0 {
  147. return b.mock
  148. }
  149. return partitions[rand.Int()%len(partitions)]
  150. }
  151. // CRC32Balancer is a Balancer that uses the CRC32 hash function to determine
  152. // which partition to route messages to. This ensures that messages with the
  153. // same key are routed to the same partition. This balancer is compatible with
  154. // the built-in hash partitioners in librdkafka and the language bindings that
  155. // are built on top of it, including the
  156. // github.com/confluentinc/confluent-kafka-go Go package.
  157. //
  158. // With the Consistent field false (default), this partitioner is equivalent to
  159. // the "consistent_random" setting in librdkafka. When Consistent is true, this
  160. // partitioner is equivalent to the "consistent" setting. The latter will hash
  161. // empty or nil keys into the same partition.
  162. //
  163. // Unless you are absolutely certain that all your messages will have keys, it's
  164. // best to leave the Consistent flag off. Otherwise, you run the risk of
  165. // creating a very hot partition.
  166. type CRC32Balancer struct {
  167. Consistent bool
  168. random randomBalancer
  169. }
  170. func (b CRC32Balancer) Balance(msg Message, partitions ...int) (partition int) {
  171. // NOTE: the crc32 balancers in librdkafka don't differentiate between nil
  172. // and empty keys. both cases are treated as unset.
  173. if len(msg.Key) == 0 && !b.Consistent {
  174. return b.random.Balance(msg, partitions...)
  175. }
  176. idx := crc32.ChecksumIEEE(msg.Key) % uint32(len(partitions))
  177. return partitions[idx]
  178. }
  179. // Murmur2Balancer is a Balancer that uses the Murmur2 hash function to
  180. // determine which partition to route messages to. This ensures that messages
  181. // with the same key are routed to the same partition. This balancer is
  182. // compatible with the partitioner used by the Java library and by librdkafka's
  183. // "murmur2" and "murmur2_random" partitioners. /
  184. //
  185. // With the Consistent field false (default), this partitioner is equivalent to
  186. // the "murmur2_random" setting in librdkafka. When Consistent is true, this
  187. // partitioner is equivalent to the "murmur2" setting. The latter will hash
  188. // nil keys into the same partition. Empty, non-nil keys are always hashed to
  189. // the same partition regardless of configuration.
  190. //
  191. // Unless you are absolutely certain that all your messages will have keys, it's
  192. // best to leave the Consistent flag off. Otherwise, you run the risk of
  193. // creating a very hot partition.
  194. //
  195. // Note that the librdkafka documentation states that the "murmur2_random" is
  196. // functionally equivalent to the default Java partitioner. That's because the
  197. // Java partitioner will use a round robin balancer instead of random on nil
  198. // keys. We choose librdkafka's implementation because it arguably has a larger
  199. // install base.
  200. type Murmur2Balancer struct {
  201. Consistent bool
  202. random randomBalancer
  203. }
  204. func (b Murmur2Balancer) Balance(msg Message, partitions ...int) (partition int) {
  205. // NOTE: the murmur2 balancers in java and librdkafka treat a nil key as
  206. // non-existent while treating an empty slice as a defined value.
  207. if msg.Key == nil && !b.Consistent {
  208. return b.random.Balance(msg, partitions...)
  209. }
  210. idx := (murmur2(msg.Key) & 0x7fffffff) % uint32(len(partitions))
  211. return partitions[idx]
  212. }
  213. // Go port of the Java library's murmur2 function.
  214. // https://github.com/apache/kafka/blob/1.0/clients/src/main/java/org/apache/kafka/common/utils/Utils.java#L353
  215. func murmur2(data []byte) uint32 {
  216. length := len(data)
  217. const (
  218. seed uint32 = 0x9747b28c
  219. // 'm' and 'r' are mixing constants generated offline.
  220. // They're not really 'magic', they just happen to work well.
  221. m = 0x5bd1e995
  222. r = 24
  223. )
  224. // Initialize the hash to a random value
  225. h := seed ^ uint32(length)
  226. length4 := length / 4
  227. for i := 0; i < length4; i++ {
  228. i4 := i * 4
  229. k := (uint32(data[i4+0]) & 0xff) + ((uint32(data[i4+1]) & 0xff) << 8) + ((uint32(data[i4+2]) & 0xff) << 16) + ((uint32(data[i4+3]) & 0xff) << 24)
  230. k *= m
  231. k ^= k >> r
  232. k *= m
  233. h *= m
  234. h ^= k
  235. }
  236. // Handle the last few bytes of the input array
  237. extra := length % 4
  238. if extra >= 3 {
  239. h ^= (uint32(data[(length & ^3)+2]) & 0xff) << 16
  240. }
  241. if extra >= 2 {
  242. h ^= (uint32(data[(length & ^3)+1]) & 0xff) << 8
  243. }
  244. if extra >= 1 {
  245. h ^= uint32(data[length & ^3]) & 0xff
  246. h *= m
  247. }
  248. h ^= h >> 13
  249. h *= m
  250. h ^= h >> 15
  251. return h
  252. }