aboutsummaryrefslogtreecommitdiff
path: root/weed/mq/kafka/consumer/assignment_test.go
blob: 14200366f9ccc9b2b9f0f57dfb9670c66cb20f75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
package consumer

import (
	"reflect"
	"sort"
	"testing"
)

func TestRangeAssignmentStrategy(t *testing.T) {
	strategy := &RangeAssignmentStrategy{}

	if strategy.Name() != ProtocolNameRange {
		t.Errorf("Expected strategy name '%s', got '%s'", ProtocolNameRange, strategy.Name())
	}

	// Test with 2 members, 4 partitions on one topic
	members := []*GroupMember{
		{
			ID:           "member1",
			Subscription: []string{"topic1"},
		},
		{
			ID:           "member2",
			Subscription: []string{"topic1"},
		},
	}

	topicPartitions := map[string][]int32{
		"topic1": {0, 1, 2, 3},
	}

	assignments := strategy.Assign(members, topicPartitions)

	// Verify all members have assignments
	if len(assignments) != 2 {
		t.Fatalf("Expected assignments for 2 members, got %d", len(assignments))
	}

	// Verify total partitions assigned
	totalAssigned := 0
	for _, assignment := range assignments {
		totalAssigned += len(assignment)
	}

	if totalAssigned != 4 {
		t.Errorf("Expected 4 total partitions assigned, got %d", totalAssigned)
	}

	// Range assignment should distribute evenly: 2 partitions each
	for memberID, assignment := range assignments {
		if len(assignment) != 2 {
			t.Errorf("Expected 2 partitions for member %s, got %d", memberID, len(assignment))
		}

		// Verify all assignments are for the subscribed topic
		for _, pa := range assignment {
			if pa.Topic != "topic1" {
				t.Errorf("Expected topic 'topic1', got '%s'", pa.Topic)
			}
		}
	}
}

func TestRangeAssignmentStrategy_UnevenPartitions(t *testing.T) {
	strategy := &RangeAssignmentStrategy{}

	// Test with 3 members, 4 partitions - should distribute 2,1,1
	members := []*GroupMember{
		{ID: "member1", Subscription: []string{"topic1"}},
		{ID: "member2", Subscription: []string{"topic1"}},
		{ID: "member3", Subscription: []string{"topic1"}},
	}

	topicPartitions := map[string][]int32{
		"topic1": {0, 1, 2, 3},
	}

	assignments := strategy.Assign(members, topicPartitions)

	// Get assignment counts
	counts := make([]int, 0, 3)
	for _, assignment := range assignments {
		counts = append(counts, len(assignment))
	}
	sort.Ints(counts)

	// Should be distributed as [1, 1, 2] (first member gets extra partition)
	expected := []int{1, 1, 2}
	if !reflect.DeepEqual(counts, expected) {
		t.Errorf("Expected partition distribution %v, got %v", expected, counts)
	}
}

func TestRangeAssignmentStrategy_MultipleTopics(t *testing.T) {
	strategy := &RangeAssignmentStrategy{}

	members := []*GroupMember{
		{ID: "member1", Subscription: []string{"topic1", "topic2"}},
		{ID: "member2", Subscription: []string{"topic1"}},
	}

	topicPartitions := map[string][]int32{
		"topic1": {0, 1},
		"topic2": {0, 1},
	}

	assignments := strategy.Assign(members, topicPartitions)

	// Member1 should get assignments from both topics
	member1Assignments := assignments["member1"]
	topicsAssigned := make(map[string]int)
	for _, pa := range member1Assignments {
		topicsAssigned[pa.Topic]++
	}

	if len(topicsAssigned) != 2 {
		t.Errorf("Expected member1 to be assigned to 2 topics, got %d", len(topicsAssigned))
	}

	// Member2 should only get topic1 assignments
	member2Assignments := assignments["member2"]
	for _, pa := range member2Assignments {
		if pa.Topic != "topic1" {
			t.Errorf("Expected member2 to only get topic1, but got %s", pa.Topic)
		}
	}
}

func TestRoundRobinAssignmentStrategy(t *testing.T) {
	strategy := &RoundRobinAssignmentStrategy{}

	if strategy.Name() != ProtocolNameRoundRobin {
		t.Errorf("Expected strategy name '%s', got '%s'", ProtocolNameRoundRobin, strategy.Name())
	}

	// Test with 2 members, 4 partitions on one topic
	members := []*GroupMember{
		{ID: "member1", Subscription: []string{"topic1"}},
		{ID: "member2", Subscription: []string{"topic1"}},
	}

	topicPartitions := map[string][]int32{
		"topic1": {0, 1, 2, 3},
	}

	assignments := strategy.Assign(members, topicPartitions)

	// Verify all members have assignments
	if len(assignments) != 2 {
		t.Fatalf("Expected assignments for 2 members, got %d", len(assignments))
	}

	// Verify total partitions assigned
	totalAssigned := 0
	for _, assignment := range assignments {
		totalAssigned += len(assignment)
	}

	if totalAssigned != 4 {
		t.Errorf("Expected 4 total partitions assigned, got %d", totalAssigned)
	}

	// Round robin should distribute evenly: 2 partitions each
	for memberID, assignment := range assignments {
		if len(assignment) != 2 {
			t.Errorf("Expected 2 partitions for member %s, got %d", memberID, len(assignment))
		}
	}
}

func TestRoundRobinAssignmentStrategy_MultipleTopics(t *testing.T) {
	strategy := &RoundRobinAssignmentStrategy{}

	members := []*GroupMember{
		{ID: "member1", Subscription: []string{"topic1", "topic2"}},
		{ID: "member2", Subscription: []string{"topic1", "topic2"}},
	}

	topicPartitions := map[string][]int32{
		"topic1": {0, 1},
		"topic2": {0, 1},
	}

	assignments := strategy.Assign(members, topicPartitions)

	// Each member should get 2 partitions (round robin across topics)
	for memberID, assignment := range assignments {
		if len(assignment) != 2 {
			t.Errorf("Expected 2 partitions for member %s, got %d", memberID, len(assignment))
		}
	}

	// Verify no partition is assigned twice
	assignedPartitions := make(map[string]map[int32]bool)
	for _, assignment := range assignments {
		for _, pa := range assignment {
			if assignedPartitions[pa.Topic] == nil {
				assignedPartitions[pa.Topic] = make(map[int32]bool)
			}
			if assignedPartitions[pa.Topic][pa.Partition] {
				t.Errorf("Partition %d of topic %s assigned multiple times", pa.Partition, pa.Topic)
			}
			assignedPartitions[pa.Topic][pa.Partition] = true
		}
	}
}

func TestGetAssignmentStrategy(t *testing.T) {
	rangeStrategy := GetAssignmentStrategy(ProtocolNameRange)
	if rangeStrategy.Name() != ProtocolNameRange {
		t.Errorf("Expected range strategy, got %s", rangeStrategy.Name())
	}

	rrStrategy := GetAssignmentStrategy(ProtocolNameRoundRobin)
	if rrStrategy.Name() != ProtocolNameRoundRobin {
		t.Errorf("Expected roundrobin strategy, got %s", rrStrategy.Name())
	}

	// Unknown strategy should default to range
	defaultStrategy := GetAssignmentStrategy("unknown")
	if defaultStrategy.Name() != ProtocolNameRange {
		t.Errorf("Expected default strategy to be range, got %s", defaultStrategy.Name())
	}
}

func TestConsumerGroup_AssignPartitions(t *testing.T) {
	group := &ConsumerGroup{
		ID:       "test-group",
		Protocol: ProtocolNameRange,
		Members: map[string]*GroupMember{
			"member1": {
				ID:           "member1",
				Subscription: []string{"topic1"},
				State:        MemberStateStable,
			},
			"member2": {
				ID:           "member2",
				Subscription: []string{"topic1"},
				State:        MemberStateStable,
			},
		},
	}

	topicPartitions := map[string][]int32{
		"topic1": {0, 1, 2, 3},
	}

	group.AssignPartitions(topicPartitions)

	// Verify assignments were created
	for memberID, member := range group.Members {
		if len(member.Assignment) == 0 {
			t.Errorf("Expected member %s to have partition assignments", memberID)
		}

		// Verify all assignments are valid
		for _, pa := range member.Assignment {
			if pa.Topic != "topic1" {
				t.Errorf("Unexpected topic assignment: %s", pa.Topic)
			}
			if pa.Partition < 0 || pa.Partition >= 4 {
				t.Errorf("Unexpected partition assignment: %d", pa.Partition)
			}
		}
	}
}

func TestConsumerGroup_GetMemberAssignments(t *testing.T) {
	group := &ConsumerGroup{
		Members: map[string]*GroupMember{
			"member1": {
				ID: "member1",
				Assignment: []PartitionAssignment{
					{Topic: "topic1", Partition: 0},
					{Topic: "topic1", Partition: 1},
				},
			},
		},
	}

	assignments := group.GetMemberAssignments()

	if len(assignments) != 1 {
		t.Fatalf("Expected 1 member assignment, got %d", len(assignments))
	}

	member1Assignments := assignments["member1"]
	if len(member1Assignments) != 2 {
		t.Errorf("Expected 2 partition assignments for member1, got %d", len(member1Assignments))
	}

	// Verify assignment content
	expectedAssignments := []PartitionAssignment{
		{Topic: "topic1", Partition: 0},
		{Topic: "topic1", Partition: 1},
	}

	if !reflect.DeepEqual(member1Assignments, expectedAssignments) {
		t.Errorf("Expected assignments %v, got %v", expectedAssignments, member1Assignments)
	}
}

func TestConsumerGroup_UpdateMemberSubscription(t *testing.T) {
	group := &ConsumerGroup{
		Members: map[string]*GroupMember{
			"member1": {
				ID:           "member1",
				Subscription: []string{"topic1"},
			},
			"member2": {
				ID:           "member2",
				Subscription: []string{"topic2"},
			},
		},
		SubscribedTopics: map[string]bool{
			"topic1": true,
			"topic2": true,
		},
	}

	// Update member1's subscription
	group.UpdateMemberSubscription("member1", []string{"topic1", "topic3"})

	// Verify member subscription updated
	member1 := group.Members["member1"]
	expectedSubscription := []string{"topic1", "topic3"}
	if !reflect.DeepEqual(member1.Subscription, expectedSubscription) {
		t.Errorf("Expected subscription %v, got %v", expectedSubscription, member1.Subscription)
	}

	// Verify group subscribed topics updated
	expectedGroupTopics := []string{"topic1", "topic2", "topic3"}
	actualGroupTopics := group.GetSubscribedTopics()

	if !reflect.DeepEqual(actualGroupTopics, expectedGroupTopics) {
		t.Errorf("Expected group topics %v, got %v", expectedGroupTopics, actualGroupTopics)
	}
}

func TestAssignmentStrategy_EmptyMembers(t *testing.T) {
	rangeStrategy := &RangeAssignmentStrategy{}
	rrStrategy := &RoundRobinAssignmentStrategy{}

	topicPartitions := map[string][]int32{
		"topic1": {0, 1, 2, 3},
	}

	// Both strategies should handle empty members gracefully
	rangeAssignments := rangeStrategy.Assign([]*GroupMember{}, topicPartitions)
	rrAssignments := rrStrategy.Assign([]*GroupMember{}, topicPartitions)

	if len(rangeAssignments) != 0 {
		t.Error("Expected empty assignments for empty members list (range)")
	}

	if len(rrAssignments) != 0 {
		t.Error("Expected empty assignments for empty members list (round robin)")
	}
}