aboutsummaryrefslogtreecommitdiff
path: root/weed/mq/offset/manager_test.go
blob: 0db301e8403f8a489183d3b86426f5b83cf8376d (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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
package offset

import (
	"testing"
	"time"

	"github.com/seaweedfs/seaweedfs/weed/pb/schema_pb"
)

func createTestPartition() *schema_pb.Partition {
	return &schema_pb.Partition{
		RingSize:   1024,
		RangeStart: 0,
		RangeStop:  31,
		UnixTimeNs: time.Now().UnixNano(),
	}
}

func TestPartitionOffsetManager_BasicAssignment(t *testing.T) {
	storage := NewInMemoryOffsetStorage()
	partition := createTestPartition()

	manager, err := NewPartitionOffsetManager("test-namespace", "test-topic", partition, storage)
	if err != nil {
		t.Fatalf("Failed to create offset manager: %v", err)
	}

	// Test sequential offset assignment
	for i := int64(0); i < 10; i++ {
		offset := manager.AssignOffset()
		if offset != i {
			t.Errorf("Expected offset %d, got %d", i, offset)
		}
	}

	// Test high water mark
	hwm := manager.GetHighWaterMark()
	if hwm != 10 {
		t.Errorf("Expected high water mark 10, got %d", hwm)
	}
}

func TestPartitionOffsetManager_BatchAssignment(t *testing.T) {
	storage := NewInMemoryOffsetStorage()
	partition := createTestPartition()

	manager, err := NewPartitionOffsetManager("test-namespace", "test-topic", partition, storage)
	if err != nil {
		t.Fatalf("Failed to create offset manager: %v", err)
	}

	// Assign batch of 5 offsets
	baseOffset, lastOffset := manager.AssignOffsets(5)
	if baseOffset != 0 {
		t.Errorf("Expected base offset 0, got %d", baseOffset)
	}
	if lastOffset != 4 {
		t.Errorf("Expected last offset 4, got %d", lastOffset)
	}

	// Assign another batch
	baseOffset, lastOffset = manager.AssignOffsets(3)
	if baseOffset != 5 {
		t.Errorf("Expected base offset 5, got %d", baseOffset)
	}
	if lastOffset != 7 {
		t.Errorf("Expected last offset 7, got %d", lastOffset)
	}

	// Check high water mark
	hwm := manager.GetHighWaterMark()
	if hwm != 8 {
		t.Errorf("Expected high water mark 8, got %d", hwm)
	}
}

func TestPartitionOffsetManager_Recovery(t *testing.T) {
	storage := NewInMemoryOffsetStorage()
	partition := createTestPartition()

	// Create manager and assign some offsets
	manager1, err := NewPartitionOffsetManager("test-namespace", "test-topic", partition, storage)
	if err != nil {
		t.Fatalf("Failed to create offset manager: %v", err)
	}

	// Assign offsets and simulate records
	for i := 0; i < 150; i++ { // More than checkpoint interval
		offset := manager1.AssignOffset()
		storage.AddRecord("test-namespace", "test-topic", partition, offset)
	}

	// Wait for checkpoint to complete
	time.Sleep(100 * time.Millisecond)

	// Create new manager (simulates restart)
	manager2, err := NewPartitionOffsetManager("test-namespace", "test-topic", partition, storage)
	if err != nil {
		t.Fatalf("Failed to create offset manager after recovery: %v", err)
	}

	// Next offset should continue from checkpoint + 1
	// With checkpoint interval 100, checkpoint happens at offset 100
	// So recovery should start from 101, but we assigned 150 offsets (0-149)
	// The checkpoint should be at 100, so next offset should be 101
	// But since we have records up to 149, it should recover from storage scan
	nextOffset := manager2.AssignOffset()
	if nextOffset != 150 {
		t.Errorf("Expected next offset 150 after recovery, got %d", nextOffset)
	}
}

func TestPartitionOffsetManager_RecoveryFromStorage(t *testing.T) {
	storage := NewInMemoryOffsetStorage()
	partition := createTestPartition()

	// Simulate existing records in storage without checkpoint
	for i := int64(0); i < 50; i++ {
		storage.AddRecord("test-namespace", "test-topic", partition, i)
	}

	// Create manager - should recover from storage scan
	manager, err := NewPartitionOffsetManager("test-namespace", "test-topic", partition, storage)
	if err != nil {
		t.Fatalf("Failed to create offset manager: %v", err)
	}

	// Next offset should be 50
	nextOffset := manager.AssignOffset()
	if nextOffset != 50 {
		t.Errorf("Expected next offset 50 after storage recovery, got %d", nextOffset)
	}
}

func TestPartitionOffsetRegistry_MultiplePartitions(t *testing.T) {
	storage := NewInMemoryOffsetStorage()
	registry := NewPartitionOffsetRegistry(storage)

	// Create different partitions
	partition1 := &schema_pb.Partition{
		RingSize:   1024,
		RangeStart: 0,
		RangeStop:  31,
		UnixTimeNs: time.Now().UnixNano(),
	}

	partition2 := &schema_pb.Partition{
		RingSize:   1024,
		RangeStart: 32,
		RangeStop:  63,
		UnixTimeNs: time.Now().UnixNano(),
	}

	// Assign offsets to different partitions
	offset1, err := registry.AssignOffset("test-namespace", "test-topic", partition1)
	if err != nil {
		t.Fatalf("Failed to assign offset to partition1: %v", err)
	}
	if offset1 != 0 {
		t.Errorf("Expected offset 0 for partition1, got %d", offset1)
	}

	offset2, err := registry.AssignOffset("test-namespace", "test-topic", partition2)
	if err != nil {
		t.Fatalf("Failed to assign offset to partition2: %v", err)
	}
	if offset2 != 0 {
		t.Errorf("Expected offset 0 for partition2, got %d", offset2)
	}

	// Assign more offsets to partition1
	offset1_2, err := registry.AssignOffset("test-namespace", "test-topic", partition1)
	if err != nil {
		t.Fatalf("Failed to assign second offset to partition1: %v", err)
	}
	if offset1_2 != 1 {
		t.Errorf("Expected offset 1 for partition1, got %d", offset1_2)
	}

	// Partition2 should still be at 0 for next assignment
	offset2_2, err := registry.AssignOffset("test-namespace", "test-topic", partition2)
	if err != nil {
		t.Fatalf("Failed to assign second offset to partition2: %v", err)
	}
	if offset2_2 != 1 {
		t.Errorf("Expected offset 1 for partition2, got %d", offset2_2)
	}
}

func TestPartitionOffsetRegistry_BatchAssignment(t *testing.T) {
	storage := NewInMemoryOffsetStorage()
	registry := NewPartitionOffsetRegistry(storage)
	partition := createTestPartition()

	// Assign batch of offsets
	baseOffset, lastOffset, err := registry.AssignOffsets("test-namespace", "test-topic", partition, 10)
	if err != nil {
		t.Fatalf("Failed to assign batch offsets: %v", err)
	}

	if baseOffset != 0 {
		t.Errorf("Expected base offset 0, got %d", baseOffset)
	}
	if lastOffset != 9 {
		t.Errorf("Expected last offset 9, got %d", lastOffset)
	}

	// Get high water mark
	hwm, err := registry.GetHighWaterMark("test-namespace", "test-topic", partition)
	if err != nil {
		t.Fatalf("Failed to get high water mark: %v", err)
	}
	if hwm != 10 {
		t.Errorf("Expected high water mark 10, got %d", hwm)
	}
}

func TestOffsetAssigner_SingleAssignment(t *testing.T) {
	storage := NewInMemoryOffsetStorage()
	assigner := NewOffsetAssigner(storage)
	partition := createTestPartition()

	// Assign single offset
	result := assigner.AssignSingleOffset("test-namespace", "test-topic", partition)
	if result.Error != nil {
		t.Fatalf("Failed to assign single offset: %v", result.Error)
	}

	if result.Assignment == nil {
		t.Fatal("Assignment result is nil")
	}

	if result.Assignment.Offset != 0 {
		t.Errorf("Expected offset 0, got %d", result.Assignment.Offset)
	}

	if result.Assignment.Partition != partition {
		t.Error("Partition mismatch in assignment")
	}

	if result.Assignment.Timestamp <= 0 {
		t.Error("Timestamp should be set")
	}
}

func TestOffsetAssigner_BatchAssignment(t *testing.T) {
	storage := NewInMemoryOffsetStorage()
	assigner := NewOffsetAssigner(storage)
	partition := createTestPartition()

	// Assign batch of offsets
	result := assigner.AssignBatchOffsets("test-namespace", "test-topic", partition, 5)
	if result.Error != nil {
		t.Fatalf("Failed to assign batch offsets: %v", result.Error)
	}

	if result.Batch == nil {
		t.Fatal("Batch result is nil")
	}

	if result.Batch.BaseOffset != 0 {
		t.Errorf("Expected base offset 0, got %d", result.Batch.BaseOffset)
	}

	if result.Batch.LastOffset != 4 {
		t.Errorf("Expected last offset 4, got %d", result.Batch.LastOffset)
	}

	if result.Batch.Count != 5 {
		t.Errorf("Expected count 5, got %d", result.Batch.Count)
	}

	if result.Batch.Timestamp <= 0 {
		t.Error("Timestamp should be set")
	}
}

func TestOffsetAssigner_HighWaterMark(t *testing.T) {
	storage := NewInMemoryOffsetStorage()
	assigner := NewOffsetAssigner(storage)
	partition := createTestPartition()

	// Initially should be 0
	hwm, err := assigner.GetHighWaterMark("test-namespace", "test-topic", partition)
	if err != nil {
		t.Fatalf("Failed to get initial high water mark: %v", err)
	}
	if hwm != 0 {
		t.Errorf("Expected initial high water mark 0, got %d", hwm)
	}

	// Assign some offsets
	assigner.AssignBatchOffsets("test-namespace", "test-topic", partition, 10)

	// High water mark should be updated
	hwm, err = assigner.GetHighWaterMark("test-namespace", "test-topic", partition)
	if err != nil {
		t.Fatalf("Failed to get high water mark after assignment: %v", err)
	}
	if hwm != 10 {
		t.Errorf("Expected high water mark 10, got %d", hwm)
	}
}

func TestPartitionKey(t *testing.T) {
	partition1 := &schema_pb.Partition{
		RingSize:   1024,
		RangeStart: 0,
		RangeStop:  31,
		UnixTimeNs: 1234567890,
	}

	partition2 := &schema_pb.Partition{
		RingSize:   1024,
		RangeStart: 0,
		RangeStop:  31,
		UnixTimeNs: 1234567890,
	}

	partition3 := &schema_pb.Partition{
		RingSize:   1024,
		RangeStart: 32,
		RangeStop:  63,
		UnixTimeNs: 1234567890,
	}

	key1 := partitionKey(partition1)
	key2 := partitionKey(partition2)
	key3 := partitionKey(partition3)

	// Same partitions should have same key
	if key1 != key2 {
		t.Errorf("Same partitions should have same key: %s vs %s", key1, key2)
	}

	// Different partitions should have different keys
	if key1 == key3 {
		t.Errorf("Different partitions should have different keys: %s vs %s", key1, key3)
	}
}

func TestConcurrentOffsetAssignment(t *testing.T) {
	storage := NewInMemoryOffsetStorage()
	registry := NewPartitionOffsetRegistry(storage)
	partition := createTestPartition()

	const numGoroutines = 10
	const offsetsPerGoroutine = 100

	results := make(chan int64, numGoroutines*offsetsPerGoroutine)

	// Start concurrent offset assignments
	for i := 0; i < numGoroutines; i++ {
		go func() {
			for j := 0; j < offsetsPerGoroutine; j++ {
				offset, err := registry.AssignOffset("test-namespace", "test-topic", partition)
				if err != nil {
					t.Errorf("Failed to assign offset: %v", err)
					return
				}
				results <- offset
			}
		}()
	}

	// Collect all results
	offsets := make(map[int64]bool)
	for i := 0; i < numGoroutines*offsetsPerGoroutine; i++ {
		offset := <-results
		if offsets[offset] {
			t.Errorf("Duplicate offset assigned: %d", offset)
		}
		offsets[offset] = true
	}

	// Verify we got all expected offsets
	expectedCount := numGoroutines * offsetsPerGoroutine
	if len(offsets) != expectedCount {
		t.Errorf("Expected %d unique offsets, got %d", expectedCount, len(offsets))
	}

	// Verify offsets are in expected range
	for offset := range offsets {
		if offset < 0 || offset >= int64(expectedCount) {
			t.Errorf("Offset %d is out of expected range [0, %d)", offset, expectedCount)
		}
	}
}