partition_consumer_mapping_test.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. package sub_coordinator
  2. import (
  3. "github.com/seaweedfs/seaweedfs/weed/mq/pub_balancer"
  4. "reflect"
  5. "testing"
  6. )
  7. func Test_doBalanceSticky(t *testing.T) {
  8. type args struct {
  9. partitions []*pub_balancer.PartitionSlotToBroker
  10. consumerInstanceIds []*ConsumerGroupInstance
  11. prevMapping *PartitionSlotToConsumerInstanceList
  12. }
  13. tests := []struct {
  14. name string
  15. args args
  16. wantPartitionSlots []*PartitionSlotToConsumerInstance
  17. }{
  18. {
  19. name: "1 consumer instance, 1 partition",
  20. args: args{
  21. partitions: []*pub_balancer.PartitionSlotToBroker{
  22. {
  23. RangeStart: 0,
  24. RangeStop: 100,
  25. },
  26. },
  27. consumerInstanceIds: []*ConsumerGroupInstance{
  28. {
  29. InstanceId: "consumer-instance-1",
  30. MaxPartitionCount: 1,
  31. },
  32. },
  33. prevMapping: nil,
  34. },
  35. wantPartitionSlots: []*PartitionSlotToConsumerInstance{
  36. {
  37. RangeStart: 0,
  38. RangeStop: 100,
  39. AssignedInstanceId: "consumer-instance-1",
  40. },
  41. },
  42. },
  43. {
  44. name: "2 consumer instances, 1 partition",
  45. args: args{
  46. partitions: []*pub_balancer.PartitionSlotToBroker{
  47. {
  48. RangeStart: 0,
  49. RangeStop: 100,
  50. },
  51. },
  52. consumerInstanceIds: []*ConsumerGroupInstance{
  53. {
  54. InstanceId: "consumer-instance-1",
  55. MaxPartitionCount: 1,
  56. },
  57. {
  58. InstanceId: "consumer-instance-2",
  59. MaxPartitionCount: 1,
  60. },
  61. },
  62. prevMapping: nil,
  63. },
  64. wantPartitionSlots: []*PartitionSlotToConsumerInstance{
  65. {
  66. RangeStart: 0,
  67. RangeStop: 100,
  68. AssignedInstanceId: "consumer-instance-1",
  69. },
  70. },
  71. },
  72. {
  73. name: "1 consumer instance, 2 partitions",
  74. args: args{
  75. partitions: []*pub_balancer.PartitionSlotToBroker{
  76. {
  77. RangeStart: 0,
  78. RangeStop: 50,
  79. },
  80. {
  81. RangeStart: 50,
  82. RangeStop: 100,
  83. },
  84. },
  85. consumerInstanceIds: []*ConsumerGroupInstance{
  86. {
  87. InstanceId: "consumer-instance-1",
  88. MaxPartitionCount: 1,
  89. },
  90. },
  91. prevMapping: nil,
  92. },
  93. wantPartitionSlots: []*PartitionSlotToConsumerInstance{
  94. {
  95. RangeStart: 0,
  96. RangeStop: 50,
  97. AssignedInstanceId: "consumer-instance-1",
  98. },
  99. {
  100. RangeStart: 50,
  101. RangeStop: 100,
  102. AssignedInstanceId: "consumer-instance-1",
  103. },
  104. },
  105. },
  106. {
  107. name: "2 consumer instances, 2 partitions",
  108. args: args{
  109. partitions: []*pub_balancer.PartitionSlotToBroker{
  110. {
  111. RangeStart: 0,
  112. RangeStop: 50,
  113. },
  114. {
  115. RangeStart: 50,
  116. RangeStop: 100,
  117. },
  118. },
  119. consumerInstanceIds: []*ConsumerGroupInstance{
  120. {
  121. InstanceId: "consumer-instance-1",
  122. MaxPartitionCount: 1,
  123. },
  124. {
  125. InstanceId: "consumer-instance-2",
  126. MaxPartitionCount: 1,
  127. },
  128. },
  129. prevMapping: nil,
  130. },
  131. wantPartitionSlots: []*PartitionSlotToConsumerInstance{
  132. {
  133. RangeStart: 0,
  134. RangeStop: 50,
  135. AssignedInstanceId: "consumer-instance-1",
  136. },
  137. {
  138. RangeStart: 50,
  139. RangeStop: 100,
  140. AssignedInstanceId: "consumer-instance-2",
  141. },
  142. },
  143. },
  144. {
  145. name: "2 consumer instances, 2 partitions, 1 deleted consumer instance",
  146. args: args{
  147. partitions: []*pub_balancer.PartitionSlotToBroker{
  148. {
  149. RangeStart: 0,
  150. RangeStop: 50,
  151. },
  152. {
  153. RangeStart: 50,
  154. RangeStop: 100,
  155. },
  156. },
  157. consumerInstanceIds: []*ConsumerGroupInstance{
  158. {
  159. InstanceId: "consumer-instance-1",
  160. MaxPartitionCount: 1,
  161. },
  162. {
  163. InstanceId: "consumer-instance-2",
  164. MaxPartitionCount: 1,
  165. },
  166. },
  167. prevMapping: &PartitionSlotToConsumerInstanceList{
  168. PartitionSlots: []*PartitionSlotToConsumerInstance{
  169. {
  170. RangeStart: 0,
  171. RangeStop: 50,
  172. AssignedInstanceId: "consumer-instance-3",
  173. },
  174. {
  175. RangeStart: 50,
  176. RangeStop: 100,
  177. AssignedInstanceId: "consumer-instance-2",
  178. },
  179. },
  180. },
  181. },
  182. wantPartitionSlots: []*PartitionSlotToConsumerInstance{
  183. {
  184. RangeStart: 0,
  185. RangeStop: 50,
  186. AssignedInstanceId: "consumer-instance-1",
  187. },
  188. {
  189. RangeStart: 50,
  190. RangeStop: 100,
  191. AssignedInstanceId: "consumer-instance-2",
  192. },
  193. },
  194. },
  195. {
  196. name: "2 consumer instances, 2 partitions, 1 new consumer instance",
  197. args: args{
  198. partitions: []*pub_balancer.PartitionSlotToBroker{
  199. {
  200. RangeStart: 0,
  201. RangeStop: 50,
  202. },
  203. {
  204. RangeStart: 50,
  205. RangeStop: 100,
  206. },
  207. },
  208. consumerInstanceIds: []*ConsumerGroupInstance{
  209. {
  210. InstanceId: "consumer-instance-1",
  211. MaxPartitionCount: 1,
  212. },
  213. {
  214. InstanceId: "consumer-instance-2",
  215. MaxPartitionCount: 1,
  216. },
  217. {
  218. InstanceId: "consumer-instance-3",
  219. MaxPartitionCount: 1,
  220. },
  221. },
  222. prevMapping: &PartitionSlotToConsumerInstanceList{
  223. PartitionSlots: []*PartitionSlotToConsumerInstance{
  224. {
  225. RangeStart: 0,
  226. RangeStop: 50,
  227. AssignedInstanceId: "consumer-instance-3",
  228. },
  229. {
  230. RangeStart: 50,
  231. RangeStop: 100,
  232. AssignedInstanceId: "consumer-instance-2",
  233. },
  234. },
  235. },
  236. },
  237. wantPartitionSlots: []*PartitionSlotToConsumerInstance{
  238. {
  239. RangeStart: 0,
  240. RangeStop: 50,
  241. AssignedInstanceId: "consumer-instance-3",
  242. },
  243. {
  244. RangeStart: 50,
  245. RangeStop: 100,
  246. AssignedInstanceId: "consumer-instance-2",
  247. },
  248. },
  249. },
  250. {
  251. name: "2 consumer instances, 2 partitions, 1 new partition",
  252. args: args{
  253. partitions: []*pub_balancer.PartitionSlotToBroker{
  254. {
  255. RangeStart: 0,
  256. RangeStop: 50,
  257. },
  258. {
  259. RangeStart: 50,
  260. RangeStop: 100,
  261. },
  262. {
  263. RangeStart: 100,
  264. RangeStop: 150,
  265. },
  266. },
  267. consumerInstanceIds: []*ConsumerGroupInstance{
  268. {
  269. InstanceId: "consumer-instance-1",
  270. MaxPartitionCount: 1,
  271. },
  272. {
  273. InstanceId: "consumer-instance-2",
  274. MaxPartitionCount: 1,
  275. },
  276. },
  277. prevMapping: &PartitionSlotToConsumerInstanceList{
  278. PartitionSlots: []*PartitionSlotToConsumerInstance{
  279. {
  280. RangeStart: 0,
  281. RangeStop: 50,
  282. AssignedInstanceId: "consumer-instance-1",
  283. },
  284. {
  285. RangeStart: 50,
  286. RangeStop: 100,
  287. AssignedInstanceId: "consumer-instance-2",
  288. },
  289. },
  290. },
  291. },
  292. wantPartitionSlots: []*PartitionSlotToConsumerInstance{
  293. {
  294. RangeStart: 0,
  295. RangeStop: 50,
  296. AssignedInstanceId: "consumer-instance-1",
  297. },
  298. {
  299. RangeStart: 50,
  300. RangeStop: 100,
  301. AssignedInstanceId: "consumer-instance-2",
  302. },
  303. {
  304. RangeStart: 100,
  305. RangeStop: 150,
  306. AssignedInstanceId: "consumer-instance-1",
  307. },
  308. },
  309. },
  310. {
  311. name: "2 consumer instances, 2 partitions, 1 new partition, 1 new consumer instance",
  312. args: args{
  313. partitions: []*pub_balancer.PartitionSlotToBroker{
  314. {
  315. RangeStart: 0,
  316. RangeStop: 50,
  317. },
  318. {
  319. RangeStart: 50,
  320. RangeStop: 100,
  321. },
  322. {
  323. RangeStart: 100,
  324. RangeStop: 150,
  325. },
  326. },
  327. consumerInstanceIds: []*ConsumerGroupInstance{
  328. {
  329. InstanceId: "consumer-instance-1",
  330. MaxPartitionCount: 1,
  331. },
  332. {
  333. InstanceId: "consumer-instance-2",
  334. MaxPartitionCount: 1,
  335. },
  336. {
  337. InstanceId: "consumer-instance-3",
  338. MaxPartitionCount: 1,
  339. },
  340. },
  341. prevMapping: &PartitionSlotToConsumerInstanceList{
  342. PartitionSlots: []*PartitionSlotToConsumerInstance{
  343. {
  344. RangeStart: 0,
  345. RangeStop: 50,
  346. AssignedInstanceId: "consumer-instance-1",
  347. },
  348. {
  349. RangeStart: 50,
  350. RangeStop: 100,
  351. AssignedInstanceId: "consumer-instance-2",
  352. },
  353. },
  354. },
  355. },
  356. wantPartitionSlots: []*PartitionSlotToConsumerInstance{
  357. {
  358. RangeStart: 0,
  359. RangeStop: 50,
  360. AssignedInstanceId: "consumer-instance-1",
  361. },
  362. {
  363. RangeStart: 50,
  364. RangeStop: 100,
  365. AssignedInstanceId: "consumer-instance-2",
  366. },
  367. {
  368. RangeStart: 100,
  369. RangeStop: 150,
  370. AssignedInstanceId: "consumer-instance-3",
  371. },
  372. },
  373. },
  374. }
  375. for _, tt := range tests {
  376. t.Run(tt.name, func(t *testing.T) {
  377. if gotPartitionSlots := doBalanceSticky(tt.args.partitions, tt.args.consumerInstanceIds, tt.args.prevMapping); !reflect.DeepEqual(gotPartitionSlots, tt.wantPartitionSlots) {
  378. t.Errorf("doBalanceSticky() = %v, want %v", gotPartitionSlots, tt.wantPartitionSlots)
  379. }
  380. })
  381. }
  382. }