aboutsummaryrefslogtreecommitdiff
path: root/weed/mq/kafka/consumer/rebalance_timeout_test.go
blob: ac5f90aee6b446b125435edfb0a9df134c74a96b (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
package consumer

import (
	"testing"
	"time"
)

func TestRebalanceTimeoutManager_CheckRebalanceTimeouts(t *testing.T) {
	coordinator := NewGroupCoordinator()
	defer coordinator.Close()
	
	rtm := coordinator.rebalanceTimeoutManager
	
	// Create a group with a member that has a short rebalance timeout
	group := coordinator.GetOrCreateGroup("test-group")
	group.Mu.Lock()
	group.State = GroupStatePreparingRebalance
	
	member := &GroupMember{
		ID:               "member1",
		ClientID:         "client1",
		SessionTimeout:   30000, // 30 seconds
		RebalanceTimeout: 1000,  // 1 second (very short for testing)
		State:            MemberStatePending,
		LastHeartbeat:    time.Now(),
		JoinedAt:         time.Now().Add(-2 * time.Second), // Joined 2 seconds ago
	}
	group.Members["member1"] = member
	group.Mu.Unlock()
	
	// Check timeouts - member should be evicted
	rtm.CheckRebalanceTimeouts()
	
	group.Mu.RLock()
	if len(group.Members) != 0 {
		t.Errorf("Expected member to be evicted due to rebalance timeout, but %d members remain", len(group.Members))
	}
	
	if group.State != GroupStateEmpty {
		t.Errorf("Expected group state to be Empty after member eviction, got %s", group.State.String())
	}
	group.Mu.RUnlock()
}

func TestRebalanceTimeoutManager_SessionTimeoutFallback(t *testing.T) {
	coordinator := NewGroupCoordinator()
	defer coordinator.Close()
	
	rtm := coordinator.rebalanceTimeoutManager
	
	// Create a group with a member that has exceeded session timeout
	group := coordinator.GetOrCreateGroup("test-group")
	group.Mu.Lock()
	group.State = GroupStatePreparingRebalance
	
	member := &GroupMember{
		ID:               "member1",
		ClientID:         "client1",
		SessionTimeout:   1000, // 1 second
		RebalanceTimeout: 30000, // 30 seconds
		State:            MemberStatePending,
		LastHeartbeat:    time.Now().Add(-2 * time.Second), // Last heartbeat 2 seconds ago
		JoinedAt:         time.Now(),
	}
	group.Members["member1"] = member
	group.Mu.Unlock()
	
	// Check timeouts - member should be evicted due to session timeout
	rtm.CheckRebalanceTimeouts()
	
	group.Mu.RLock()
	if len(group.Members) != 0 {
		t.Errorf("Expected member to be evicted due to session timeout, but %d members remain", len(group.Members))
	}
	group.Mu.RUnlock()
}

func TestRebalanceTimeoutManager_LeaderEviction(t *testing.T) {
	coordinator := NewGroupCoordinator()
	defer coordinator.Close()
	
	rtm := coordinator.rebalanceTimeoutManager
	
	// Create a group with leader and another member
	group := coordinator.GetOrCreateGroup("test-group")
	group.Mu.Lock()
	group.State = GroupStatePreparingRebalance
	group.Leader = "member1"
	
	// Leader with expired rebalance timeout
	leader := &GroupMember{
		ID:               "member1",
		ClientID:         "client1",
		SessionTimeout:   30000,
		RebalanceTimeout: 1000,
		State:            MemberStatePending,
		LastHeartbeat:    time.Now(),
		JoinedAt:         time.Now().Add(-2 * time.Second),
	}
	group.Members["member1"] = leader
	
	// Another member that's still valid
	member2 := &GroupMember{
		ID:               "member2",
		ClientID:         "client2",
		SessionTimeout:   30000,
		RebalanceTimeout: 30000,
		State:            MemberStatePending,
		LastHeartbeat:    time.Now(),
		JoinedAt:         time.Now(),
	}
	group.Members["member2"] = member2
	group.Mu.Unlock()
	
	// Check timeouts - leader should be evicted, new leader selected
	rtm.CheckRebalanceTimeouts()
	
	group.Mu.RLock()
	if len(group.Members) != 1 {
		t.Errorf("Expected 1 member to remain after leader eviction, got %d", len(group.Members))
	}
	
	if group.Leader != "member2" {
		t.Errorf("Expected member2 to become new leader, got %s", group.Leader)
	}
	
	if group.State != GroupStatePreparingRebalance {
		t.Errorf("Expected group to restart rebalancing after leader eviction, got %s", group.State.String())
	}
	group.Mu.RUnlock()
}

func TestRebalanceTimeoutManager_IsRebalanceStuck(t *testing.T) {
	coordinator := NewGroupCoordinator()
	defer coordinator.Close()
	
	rtm := coordinator.rebalanceTimeoutManager
	
	// Create a group that's been rebalancing for a while
	group := coordinator.GetOrCreateGroup("test-group")
	group.Mu.Lock()
	group.State = GroupStatePreparingRebalance
	group.LastActivity = time.Now().Add(-15 * time.Minute) // 15 minutes ago
	group.Mu.Unlock()
	
	// Check if rebalance is stuck (max 10 minutes)
	maxDuration := 10 * time.Minute
	if !rtm.IsRebalanceStuck(group, maxDuration) {
		t.Error("Expected rebalance to be detected as stuck")
	}
	
	// Test with a group that's not stuck
	group.Mu.Lock()
	group.LastActivity = time.Now().Add(-5 * time.Minute) // 5 minutes ago
	group.Mu.Unlock()
	
	if rtm.IsRebalanceStuck(group, maxDuration) {
		t.Error("Expected rebalance to not be detected as stuck")
	}
	
	// Test with stable group (should not be stuck)
	group.Mu.Lock()
	group.State = GroupStateStable
	group.LastActivity = time.Now().Add(-15 * time.Minute)
	group.Mu.Unlock()
	
	if rtm.IsRebalanceStuck(group, maxDuration) {
		t.Error("Stable group should not be detected as stuck")
	}
}

func TestRebalanceTimeoutManager_ForceCompleteRebalance(t *testing.T) {
	coordinator := NewGroupCoordinator()
	defer coordinator.Close()
	
	rtm := coordinator.rebalanceTimeoutManager
	
	// Test forcing completion from PreparingRebalance
	group := coordinator.GetOrCreateGroup("test-group")
	group.Mu.Lock()
	group.State = GroupStatePreparingRebalance
	
	member := &GroupMember{
		ID:    "member1",
		State: MemberStatePending,
	}
	group.Members["member1"] = member
	group.Mu.Unlock()
	
	rtm.ForceCompleteRebalance(group)
	
	group.Mu.RLock()
	if group.State != GroupStateCompletingRebalance {
		t.Errorf("Expected group state to be CompletingRebalance, got %s", group.State.String())
	}
	group.Mu.RUnlock()
	
	// Test forcing completion from CompletingRebalance
	rtm.ForceCompleteRebalance(group)
	
	group.Mu.RLock()
	if group.State != GroupStateStable {
		t.Errorf("Expected group state to be Stable, got %s", group.State.String())
	}
	
	if member.State != MemberStateStable {
		t.Errorf("Expected member state to be Stable, got %s", member.State.String())
	}
	group.Mu.RUnlock()
}

func TestRebalanceTimeoutManager_GetRebalanceStatus(t *testing.T) {
	coordinator := NewGroupCoordinator()
	defer coordinator.Close()
	
	rtm := coordinator.rebalanceTimeoutManager
	
	// Test with non-existent group
	status := rtm.GetRebalanceStatus("non-existent")
	if status != nil {
		t.Error("Expected nil status for non-existent group")
	}
	
	// Create a group with members
	group := coordinator.GetOrCreateGroup("test-group")
	group.Mu.Lock()
	group.State = GroupStatePreparingRebalance
	group.Generation = 5
	group.Leader = "member1"
	group.LastActivity = time.Now().Add(-2 * time.Minute)
	
	member1 := &GroupMember{
		ID:               "member1",
		State:            MemberStatePending,
		LastHeartbeat:    time.Now().Add(-30 * time.Second),
		JoinedAt:         time.Now().Add(-2 * time.Minute),
		SessionTimeout:   30000,  // 30 seconds
		RebalanceTimeout: 300000, // 5 minutes
	}
	group.Members["member1"] = member1
	
	member2 := &GroupMember{
		ID:               "member2",
		State:            MemberStatePending,
		LastHeartbeat:    time.Now().Add(-10 * time.Second),
		JoinedAt:         time.Now().Add(-1 * time.Minute),
		SessionTimeout:   60000,  // 1 minute
		RebalanceTimeout: 180000, // 3 minutes
	}
	group.Members["member2"] = member2
	group.Mu.Unlock()
	
	// Get status
	status = rtm.GetRebalanceStatus("test-group")
	
	if status == nil {
		t.Fatal("Expected non-nil status")
	}
	
	if status.GroupID != "test-group" {
		t.Errorf("Expected group ID 'test-group', got %s", status.GroupID)
	}
	
	if status.State != GroupStatePreparingRebalance {
		t.Errorf("Expected state PreparingRebalance, got %s", status.State.String())
	}
	
	if status.Generation != 5 {
		t.Errorf("Expected generation 5, got %d", status.Generation)
	}
	
	if status.MemberCount != 2 {
		t.Errorf("Expected 2 members, got %d", status.MemberCount)
	}
	
	if status.Leader != "member1" {
		t.Errorf("Expected leader 'member1', got %s", status.Leader)
	}
	
	if !status.IsRebalancing {
		t.Error("Expected IsRebalancing to be true")
	}
	
	if len(status.Members) != 2 {
		t.Errorf("Expected 2 member statuses, got %d", len(status.Members))
	}
	
	// Check member timeout calculations
	for _, memberStatus := range status.Members {
		if memberStatus.SessionTimeRemaining < 0 {
			t.Errorf("Session time remaining should not be negative for member %s", memberStatus.MemberID)
		}
		
		if memberStatus.RebalanceTimeRemaining < 0 {
			t.Errorf("Rebalance time remaining should not be negative for member %s", memberStatus.MemberID)
		}
	}
}

func TestRebalanceTimeoutManager_DefaultRebalanceTimeout(t *testing.T) {
	coordinator := NewGroupCoordinator()
	defer coordinator.Close()
	
	rtm := coordinator.rebalanceTimeoutManager
	
	// Create a group with a member that has no rebalance timeout set (0)
	group := coordinator.GetOrCreateGroup("test-group")
	group.Mu.Lock()
	group.State = GroupStatePreparingRebalance
	
	member := &GroupMember{
		ID:               "member1",
		ClientID:         "client1",
		SessionTimeout:   30000, // 30 seconds
		RebalanceTimeout: 0,     // Not set, should use default
		State:            MemberStatePending,
		LastHeartbeat:    time.Now(),
		JoinedAt:         time.Now().Add(-6 * time.Minute), // Joined 6 minutes ago
	}
	group.Members["member1"] = member
	group.Mu.Unlock()
	
	// Default rebalance timeout is 5 minutes (300000ms), so member should be evicted
	rtm.CheckRebalanceTimeouts()
	
	group.Mu.RLock()
	if len(group.Members) != 0 {
		t.Errorf("Expected member to be evicted using default rebalance timeout, but %d members remain", len(group.Members))
	}
	group.Mu.RUnlock()
}