partitioner_test.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 TestHashPartitionerConsistency(t *testing.T) {
  134. partitioner := NewHashPartitioner("mytopic")
  135. ep, ok := partitioner.(DynamicConsistencyPartitioner)
  136. if !ok {
  137. t.Error("Hash partitioner does not implement DynamicConsistencyPartitioner")
  138. }
  139. consistency := ep.MessageRequiresConsistency(&ProducerMessage{Key: StringEncoder("hi")})
  140. if !consistency {
  141. t.Error("Messages with keys should require consistency")
  142. }
  143. consistency = ep.MessageRequiresConsistency(&ProducerMessage{})
  144. if consistency {
  145. t.Error("Messages without keys should require consistency")
  146. }
  147. }
  148. func TestHashPartitionerMinInt32(t *testing.T) {
  149. partitioner := NewHashPartitioner("mytopic")
  150. msg := ProducerMessage{}
  151. // "1468509572224" generates 2147483648 (uint32) result from Sum32 function
  152. // which is -2147483648 or int32's min value
  153. msg.Key = StringEncoder("1468509572224")
  154. choice, err := partitioner.Partition(&msg, 50)
  155. if err != nil {
  156. t.Error(partitioner, err)
  157. }
  158. if choice < 0 || choice >= 50 {
  159. t.Error("Returned partition", choice, "outside of range for nil key.")
  160. }
  161. }
  162. func TestManualPartitioner(t *testing.T) {
  163. partitioner := NewManualPartitioner("mytopic")
  164. choice, err := partitioner.Partition(&ProducerMessage{}, 1)
  165. if err != nil {
  166. t.Error(partitioner, err)
  167. }
  168. if choice != 0 {
  169. t.Error("Returned non-zero partition when only one available.")
  170. }
  171. for i := int32(1); i < 50; i++ {
  172. choice, err := partitioner.Partition(&ProducerMessage{Partition: i}, 50)
  173. if err != nil {
  174. t.Error(partitioner, err)
  175. }
  176. if choice != i {
  177. t.Error("Returned partition not the same as the input partition")
  178. }
  179. }
  180. }
  181. // By default, Sarama uses the message's key to consistently assign a partition to
  182. // a message using hashing. If no key is set, a random partition will be chosen.
  183. // This example shows how you can partition messages randomly, even when a key is set,
  184. // by overriding Config.Producer.Partitioner.
  185. func ExamplePartitioner_random() {
  186. config := NewConfig()
  187. config.Producer.Partitioner = NewRandomPartitioner
  188. producer, err := NewSyncProducer([]string{"localhost:9092"}, config)
  189. if err != nil {
  190. log.Fatal(err)
  191. }
  192. defer func() {
  193. if err := producer.Close(); err != nil {
  194. log.Println("Failed to close producer:", err)
  195. }
  196. }()
  197. msg := &ProducerMessage{Topic: "test", Key: StringEncoder("key is set"), Value: StringEncoder("test")}
  198. partition, offset, err := producer.SendMessage(msg)
  199. if err != nil {
  200. log.Fatalln("Failed to produce message to kafka cluster.")
  201. }
  202. log.Printf("Produced message to partition %d with offset %d", partition, offset)
  203. }
  204. // This example shows how to assign partitions to your messages manually.
  205. func ExamplePartitioner_manual() {
  206. config := NewConfig()
  207. // First, we tell the producer that we are going to partition ourselves.
  208. config.Producer.Partitioner = NewManualPartitioner
  209. producer, err := NewSyncProducer([]string{"localhost:9092"}, config)
  210. if err != nil {
  211. log.Fatal(err)
  212. }
  213. defer func() {
  214. if err := producer.Close(); err != nil {
  215. log.Println("Failed to close producer:", err)
  216. }
  217. }()
  218. // Now, we set the Partition field of the ProducerMessage struct.
  219. msg := &ProducerMessage{Topic: "test", Partition: 6, Value: StringEncoder("test")}
  220. partition, offset, err := producer.SendMessage(msg)
  221. if err != nil {
  222. log.Fatalln("Failed to produce message to kafka cluster.")
  223. }
  224. if partition != 6 {
  225. log.Fatal("Message should have been produced to partition 6!")
  226. }
  227. log.Printf("Produced message to partition %d with offset %d", partition, offset)
  228. }
  229. // This example shows how to set a different partitioner depending on the topic.
  230. func ExamplePartitioner_per_topic() {
  231. config := NewConfig()
  232. config.Producer.Partitioner = func(topic string) Partitioner {
  233. switch topic {
  234. case "access_log", "error_log":
  235. return NewRandomPartitioner(topic)
  236. default:
  237. return NewHashPartitioner(topic)
  238. }
  239. }
  240. // ...
  241. }