|
|
|
@ -5,14 +5,14 @@ import ( |
|
|
|
) |
|
|
|
|
|
|
|
func TestCooperativeStickyAssignmentStrategy_Name(t *testing.T) { |
|
|
|
strategy := &CooperativeStickyAssignmentStrategy{} |
|
|
|
if strategy.Name() != "cooperative-sticky" { |
|
|
|
t.Errorf("Expected strategy name 'cooperative-sticky', got '%s'", strategy.Name()) |
|
|
|
strategy := NewIncrementalCooperativeAssignmentStrategy() |
|
|
|
if strategy.Name() != ProtocolNameCooperativeSticky { |
|
|
|
t.Errorf("Expected strategy name '%s', got '%s'", ProtocolNameCooperativeSticky, strategy.Name()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
func TestCooperativeStickyAssignmentStrategy_InitialAssignment(t *testing.T) { |
|
|
|
strategy := &CooperativeStickyAssignmentStrategy{} |
|
|
|
strategy := NewIncrementalCooperativeAssignmentStrategy() |
|
|
|
|
|
|
|
members := []*GroupMember{ |
|
|
|
{ID: "member1", Subscription: []string{"topic1"}, Assignment: []PartitionAssignment{}}, |
|
|
|
@ -55,12 +55,12 @@ func TestCooperativeStickyAssignmentStrategy_InitialAssignment(t *testing.T) { |
|
|
|
} |
|
|
|
|
|
|
|
func TestCooperativeStickyAssignmentStrategy_StickyBehavior(t *testing.T) { |
|
|
|
strategy := &CooperativeStickyAssignmentStrategy{} |
|
|
|
strategy := NewIncrementalCooperativeAssignmentStrategy() |
|
|
|
|
|
|
|
// Initial state: member1 has partitions 0,1 and member2 has partitions 2,3
|
|
|
|
members := []*GroupMember{ |
|
|
|
{ |
|
|
|
ID: "member1", |
|
|
|
ID: "member1", |
|
|
|
Subscription: []string{"topic1"}, |
|
|
|
Assignment: []PartitionAssignment{ |
|
|
|
{Topic: "topic1", Partition: 0}, |
|
|
|
@ -68,7 +68,7 @@ func TestCooperativeStickyAssignmentStrategy_StickyBehavior(t *testing.T) { |
|
|
|
}, |
|
|
|
}, |
|
|
|
{ |
|
|
|
ID: "member2", |
|
|
|
ID: "member2", |
|
|
|
Subscription: []string{"topic1"}, |
|
|
|
Assignment: []PartitionAssignment{ |
|
|
|
{Topic: "topic1", Partition: 2}, |
|
|
|
@ -121,12 +121,12 @@ func TestCooperativeStickyAssignmentStrategy_StickyBehavior(t *testing.T) { |
|
|
|
} |
|
|
|
|
|
|
|
func TestCooperativeStickyAssignmentStrategy_NewMemberJoin(t *testing.T) { |
|
|
|
strategy := &CooperativeStickyAssignmentStrategy{} |
|
|
|
strategy := NewIncrementalCooperativeAssignmentStrategy() |
|
|
|
|
|
|
|
// Scenario: member1 has all partitions, member2 joins
|
|
|
|
members := []*GroupMember{ |
|
|
|
{ |
|
|
|
ID: "member1", |
|
|
|
ID: "member1", |
|
|
|
Subscription: []string{"topic1"}, |
|
|
|
Assignment: []PartitionAssignment{ |
|
|
|
{Topic: "topic1", Partition: 0}, |
|
|
|
@ -136,9 +136,9 @@ func TestCooperativeStickyAssignmentStrategy_NewMemberJoin(t *testing.T) { |
|
|
|
}, |
|
|
|
}, |
|
|
|
{ |
|
|
|
ID: "member2", |
|
|
|
ID: "member2", |
|
|
|
Subscription: []string{"topic1"}, |
|
|
|
Assignment: []PartitionAssignment{}, // New member, no existing assignment
|
|
|
|
Assignment: []PartitionAssignment{}, // New member, no existing assignment
|
|
|
|
}, |
|
|
|
} |
|
|
|
|
|
|
|
@ -146,6 +146,17 @@ func TestCooperativeStickyAssignmentStrategy_NewMemberJoin(t *testing.T) { |
|
|
|
"topic1": {0, 1, 2, 3}, |
|
|
|
} |
|
|
|
|
|
|
|
// First call: revocation phase
|
|
|
|
assignments1 := strategy.Assign(members, topicPartitions) |
|
|
|
|
|
|
|
// Update members with revocation results
|
|
|
|
members[0].Assignment = assignments1["member1"] |
|
|
|
members[1].Assignment = assignments1["member2"] |
|
|
|
|
|
|
|
// Force completion of revocation timeout
|
|
|
|
strategy.GetRebalanceState().RevocationTimeout = 0 |
|
|
|
|
|
|
|
// Second call: assignment phase
|
|
|
|
assignments := strategy.Assign(members, topicPartitions) |
|
|
|
|
|
|
|
// Verify fair redistribution (2 partitions each)
|
|
|
|
@ -177,12 +188,12 @@ func TestCooperativeStickyAssignmentStrategy_NewMemberJoin(t *testing.T) { |
|
|
|
} |
|
|
|
|
|
|
|
func TestCooperativeStickyAssignmentStrategy_MemberLeave(t *testing.T) { |
|
|
|
strategy := &CooperativeStickyAssignmentStrategy{} |
|
|
|
strategy := NewIncrementalCooperativeAssignmentStrategy() |
|
|
|
|
|
|
|
// Scenario: member2 leaves, member1 should get its partitions
|
|
|
|
members := []*GroupMember{ |
|
|
|
{ |
|
|
|
ID: "member1", |
|
|
|
ID: "member1", |
|
|
|
Subscription: []string{"topic1"}, |
|
|
|
Assignment: []PartitionAssignment{ |
|
|
|
{Topic: "topic1", Partition: 0}, |
|
|
|
@ -223,11 +234,11 @@ func TestCooperativeStickyAssignmentStrategy_MemberLeave(t *testing.T) { |
|
|
|
} |
|
|
|
|
|
|
|
func TestCooperativeStickyAssignmentStrategy_MultipleTopics(t *testing.T) { |
|
|
|
strategy := &CooperativeStickyAssignmentStrategy{} |
|
|
|
strategy := NewIncrementalCooperativeAssignmentStrategy() |
|
|
|
|
|
|
|
members := []*GroupMember{ |
|
|
|
{ |
|
|
|
ID: "member1", |
|
|
|
ID: "member1", |
|
|
|
Subscription: []string{"topic1", "topic2"}, |
|
|
|
Assignment: []PartitionAssignment{ |
|
|
|
{Topic: "topic1", Partition: 0}, |
|
|
|
@ -235,7 +246,7 @@ func TestCooperativeStickyAssignmentStrategy_MultipleTopics(t *testing.T) { |
|
|
|
}, |
|
|
|
}, |
|
|
|
{ |
|
|
|
ID: "member2", |
|
|
|
ID: "member2", |
|
|
|
Subscription: []string{"topic1", "topic2"}, |
|
|
|
Assignment: []PartitionAssignment{ |
|
|
|
{Topic: "topic1", Partition: 1}, |
|
|
|
@ -299,7 +310,7 @@ func TestCooperativeStickyAssignmentStrategy_MultipleTopics(t *testing.T) { |
|
|
|
} |
|
|
|
|
|
|
|
func TestCooperativeStickyAssignmentStrategy_UnevenPartitions(t *testing.T) { |
|
|
|
strategy := &CooperativeStickyAssignmentStrategy{} |
|
|
|
strategy := NewIncrementalCooperativeAssignmentStrategy() |
|
|
|
|
|
|
|
// 5 partitions, 2 members - should distribute 3:2 or 2:3
|
|
|
|
members := []*GroupMember{ |
|
|
|
@ -334,7 +345,7 @@ func TestCooperativeStickyAssignmentStrategy_UnevenPartitions(t *testing.T) { |
|
|
|
} |
|
|
|
|
|
|
|
func TestCooperativeStickyAssignmentStrategy_PartialSubscription(t *testing.T) { |
|
|
|
strategy := &CooperativeStickyAssignmentStrategy{} |
|
|
|
strategy := NewIncrementalCooperativeAssignmentStrategy() |
|
|
|
|
|
|
|
// member1 subscribes to both topics, member2 only to topic1
|
|
|
|
members := []*GroupMember{ |
|
|
|
@ -393,20 +404,20 @@ func TestCooperativeStickyAssignmentStrategy_PartialSubscription(t *testing.T) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if member1Topic1Count + member2Topic1Count != 2 { |
|
|
|
if member1Topic1Count+member2Topic1Count != 2 { |
|
|
|
t.Errorf("Expected all topic1 partitions to be assigned, got %d + %d = %d", |
|
|
|
member1Topic1Count, member2Topic1Count, member1Topic1Count + member2Topic1Count) |
|
|
|
member1Topic1Count, member2Topic1Count, member1Topic1Count+member2Topic1Count) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
func TestGetAssignmentStrategy_CooperativeSticky(t *testing.T) { |
|
|
|
strategy := GetAssignmentStrategy("cooperative-sticky") |
|
|
|
if strategy.Name() != "cooperative-sticky" { |
|
|
|
strategy := GetAssignmentStrategy(ProtocolNameCooperativeSticky) |
|
|
|
if strategy.Name() != ProtocolNameCooperativeSticky { |
|
|
|
t.Errorf("Expected cooperative-sticky strategy, got %s", strategy.Name()) |
|
|
|
} |
|
|
|
|
|
|
|
// Verify it's the correct type
|
|
|
|
if _, ok := strategy.(*CooperativeStickyAssignmentStrategy); !ok { |
|
|
|
t.Errorf("Expected CooperativeStickyAssignmentStrategy, got %T", strategy) |
|
|
|
if _, ok := strategy.(*IncrementalCooperativeAssignmentStrategy); !ok { |
|
|
|
t.Errorf("Expected IncrementalCooperativeAssignmentStrategy, got %T", strategy) |
|
|
|
} |
|
|
|
} |