partitioner_test.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. package sarama
  2. import (
  3. "crypto/rand"
  4. "hash/fnv"
  5. "log"
  6. "testing"
  7. )
  8. func assertPartitioningConsistent(t *testing.T, partitioner Partitioner, message *ProducerMessage, numPartitions int32) {
  9. choice, err := partitioner.Partition(message, numPartitions)
  10. if err != nil {
  11. t.Error(partitioner, err)
  12. }
  13. if choice < 0 || choice >= numPartitions {
  14. t.Error(partitioner, "returned partition", choice, "outside of range for", message)
  15. }
  16. for i := 1; i < 50; i++ {
  17. newChoice, err := partitioner.Partition(message, numPartitions)
  18. if err != nil {
  19. t.Error(partitioner, err)
  20. }
  21. if newChoice != choice {
  22. t.Error(partitioner, "returned partition", newChoice, "inconsistent with", choice, ".")
  23. }
  24. }
  25. }
  26. func TestRandomPartitioner(t *testing.T) {
  27. partitioner := NewRandomPartitioner("mytopic")
  28. choice, err := partitioner.Partition(nil, 1)
  29. if err != nil {
  30. t.Error(partitioner, err)
  31. }
  32. if choice != 0 {
  33. t.Error("Returned non-zero partition when only one available.")
  34. }
  35. for i := 1; i < 50; i++ {
  36. choice, err := partitioner.Partition(nil, 50)
  37. if err != nil {
  38. t.Error(partitioner, err)
  39. }
  40. if choice < 0 || choice >= 50 {
  41. t.Error("Returned partition", choice, "outside of range.")
  42. }
  43. }
  44. }
  45. func TestRoundRobinPartitioner(t *testing.T) {
  46. partitioner := NewRoundRobinPartitioner("mytopic")
  47. choice, err := partitioner.Partition(nil, 1)
  48. if err != nil {
  49. t.Error(partitioner, err)
  50. }
  51. if choice != 0 {
  52. t.Error("Returned non-zero partition when only one available.")
  53. }
  54. var i int32
  55. for i = 1; i < 50; i++ {
  56. choice, err := partitioner.Partition(nil, 7)
  57. if err != nil {
  58. t.Error(partitioner, err)
  59. }
  60. if choice != i%7 {
  61. t.Error("Returned partition", choice, "expecting", i%7)
  62. }
  63. }
  64. }
  65. func TestNewHashPartitionerWithHasher(t *testing.T) {
  66. // use the current default hasher fnv.New32a()
  67. partitioner := NewCustomHashPartitioner(fnv.New32a)("mytopic")
  68. choice, err := partitioner.Partition(&ProducerMessage{}, 1)
  69. if err != nil {
  70. t.Error(partitioner, err)
  71. }
  72. if choice != 0 {
  73. t.Error("Returned non-zero partition when only one available.")
  74. }
  75. for i := 1; i < 50; i++ {
  76. choice, err := partitioner.Partition(&ProducerMessage{}, 50)
  77. if err != nil {
  78. t.Error(partitioner, err)
  79. }
  80. if choice < 0 || choice >= 50 {
  81. t.Error("Returned partition", choice, "outside of range for nil key.")
  82. }
  83. }
  84. buf := make([]byte, 256)
  85. for i := 1; i < 50; i++ {
  86. if _, err := rand.Read(buf); err != nil {
  87. t.Error(err)
  88. }
  89. assertPartitioningConsistent(t, partitioner, &ProducerMessage{Key: ByteEncoder(buf)}, 50)
  90. }
  91. }
  92. func TestHashPartitionerWithHasherMinInt32(t *testing.T) {
  93. // use the current default hasher fnv.New32a()
  94. partitioner := NewCustomHashPartitioner(fnv.New32a)("mytopic")
  95. msg := ProducerMessage{}
  96. // "1468509572224" generates 2147483648 (uint32) result from Sum32 function
  97. // which is -2147483648 or int32's min value
  98. msg.Key = StringEncoder("1468509572224")
  99. choice, err := partitioner.Partition(&msg, 50)
  100. if err != nil {
  101. t.Error(partitioner, err)
  102. }
  103. if choice < 0 || choice >= 50 {
  104. t.Error("Returned partition", choice, "outside of range for nil key.")
  105. }
  106. }
  107. func TestHashPartitioner(t *testing.T) {
  108. partitioner := NewHashPartitioner("mytopic")
  109. choice, err := partitioner.Partition(&ProducerMessage{}, 1)
  110. if err != nil {
  111. t.Error(partitioner, err)
  112. }
  113. if choice != 0 {
  114. t.Error("Returned non-zero partition when only one available.")
  115. }
  116. for i := 1; i < 50; i++ {
  117. choice, err := partitioner.Partition(&ProducerMessage{}, 50)
  118. if err != nil {
  119. t.Error(partitioner, err)
  120. }
  121. if choice < 0 || choice >= 50 {
  122. t.Error("Returned partition", choice, "outside of range for nil key.")
  123. }
  124. }
  125. buf := make([]byte, 256)
  126. for i := 1; i < 50; i++ {
  127. if _, err := rand.Read(buf); err != nil {
  128. t.Error(err)
  129. }
  130. assertPartitioningConsistent(t, partitioner, &ProducerMessage{Key: ByteEncoder(buf)}, 50)
  131. }
  132. }
  133. func TestHashPartitionerMinInt32(t *testing.T) {
  134. partitioner := NewHashPartitioner("mytopic")
  135. msg := ProducerMessage{}
  136. // "1468509572224" generates 2147483648 (uint32) result from Sum32 function
  137. // which is -2147483648 or int32's min value
  138. msg.Key = StringEncoder("1468509572224")
  139. choice, err := partitioner.Partition(&msg, 50)
  140. if err != nil {
  141. t.Error(partitioner, err)
  142. }
  143. if choice < 0 || choice >= 50 {
  144. t.Error("Returned partition", choice, "outside of range for nil key.")
  145. }
  146. }
  147. func TestManualPartitioner(t *testing.T) {
  148. partitioner := NewManualPartitioner("mytopic")
  149. choice, err := partitioner.Partition(&ProducerMessage{}, 1)
  150. if err != nil {
  151. t.Error(partitioner, err)
  152. }
  153. if choice != 0 {
  154. t.Error("Returned non-zero partition when only one available.")
  155. }
  156. for i := int32(1); i < 50; i++ {
  157. choice, err := partitioner.Partition(&ProducerMessage{Partition: i}, 50)
  158. if err != nil {
  159. t.Error(partitioner, err)
  160. }
  161. if choice != i {
  162. t.Error("Returned partition not the same as the input partition")
  163. }
  164. }
  165. }
  166. // By default, Sarama uses the message's key to consistently assign a partition to
  167. // a message using hashing. If no key is set, a random partition will be chosen.
  168. // This example shows how you can partition messages randomly, even when a key is set,
  169. // by overriding Config.Producer.Partitioner.
  170. func ExamplePartitioner_random() {
  171. config := NewConfig()
  172. config.Producer.Partitioner = NewRandomPartitioner
  173. producer, err := NewSyncProducer([]string{"localhost:9092"}, config)
  174. if err != nil {
  175. log.Fatal(err)
  176. }
  177. defer func() {
  178. if err := producer.Close(); err != nil {
  179. log.Println("Failed to close producer:", err)
  180. }
  181. }()
  182. msg := &ProducerMessage{Topic: "test", Key: StringEncoder("key is set"), Value: StringEncoder("test")}
  183. partition, offset, err := producer.SendMessage(msg)
  184. if err != nil {
  185. log.Fatalln("Failed to produce message to kafka cluster.")
  186. }
  187. log.Printf("Produced message to partition %d with offset %d", partition, offset)
  188. }
  189. // This example shows how to assign partitions to your messages manually.
  190. func ExamplePartitioner_manual() {
  191. config := NewConfig()
  192. // First, we tell the producer that we are going to partition ourselves.
  193. config.Producer.Partitioner = NewManualPartitioner
  194. producer, err := NewSyncProducer([]string{"localhost:9092"}, config)
  195. if err != nil {
  196. log.Fatal(err)
  197. }
  198. defer func() {
  199. if err := producer.Close(); err != nil {
  200. log.Println("Failed to close producer:", err)
  201. }
  202. }()
  203. // Now, we set the Partition field of the ProducerMessage struct.
  204. msg := &ProducerMessage{Topic: "test", Partition: 6, Value: StringEncoder("test")}
  205. partition, offset, err := producer.SendMessage(msg)
  206. if err != nil {
  207. log.Fatalln("Failed to produce message to kafka cluster.")
  208. }
  209. if partition != 6 {
  210. log.Fatal("Message should have been produced to partition 6!")
  211. }
  212. log.Printf("Produced message to partition %d with offset %d", partition, offset)
  213. }
  214. // This example shows how to set a different partitioner depending on the topic.
  215. func ExamplePartitioner_per_topic() {
  216. config := NewConfig()
  217. config.Producer.Partitioner = func(topic string) Partitioner {
  218. switch topic {
  219. case "access_log", "error_log":
  220. return NewRandomPartitioner(topic)
  221. default:
  222. return NewHashPartitioner(topic)
  223. }
  224. }
  225. // ...
  226. }