-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathretention_modes_test.go
More file actions
220 lines (183 loc) · 6.19 KB
/
retention_modes_test.go
File metadata and controls
220 lines (183 loc) · 6.19 KB
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
package comet
import (
"context"
"fmt"
"sync/atomic"
"testing"
"time"
)
// TestRetentionSingleProcess tests retention in single-process mode
func TestRetentionSingleProcess(t *testing.T) {
dir := t.TempDir()
// Create single-process config with retention
config := DefaultCometConfig()
config.Retention.MaxAge = 100 * time.Millisecond
config.Retention.CleanupInterval = 50 * time.Millisecond
config.Retention.MinFilesToKeep = 0 // Allow deletion of all old files
config.Storage.MaxFileSize = 1024
// Single-process mode is the default
client, err := NewClient(dir, config)
if err != nil {
t.Fatalf("failed to create client: %v", err)
}
defer client.Close()
ctx := context.Background()
streamName := "events:v1:shard:0000"
// Write data to create multiple files
for i := 0; i < 30; i++ {
data := [][]byte{[]byte(fmt.Sprintf(`{"id": %d, "mode": "single"}`, i))}
_, err := client.Append(ctx, streamName, data)
if err != nil {
t.Fatalf("failed to write data: %v", err)
}
}
// Get initial file count
shard, _ := client.getOrCreateShard(0)
shard.mu.RLock()
initialFiles := len(shard.index.Files)
shard.mu.RUnlock()
if initialFiles < 2 {
t.Skip("Need multiple files to test retention")
}
// Mark first files as old
shard.mu.Lock()
oldTime := time.Now().Add(-200 * time.Millisecond)
for i := 0; i < len(shard.index.Files)-1 && i < 2; i++ {
shard.index.Files[i].EndTime = oldTime
}
shard.mu.Unlock()
// Force retention cleanup
client.ForceRetentionCleanup()
// Verify files were deleted
shard.mu.RLock()
finalFiles := len(shard.index.Files)
shard.mu.RUnlock()
if finalFiles >= initialFiles {
t.Errorf("Single-process mode: expected files to be deleted, had %d initially, now have %d",
initialFiles, finalFiles)
}
t.Logf("Single-process retention: %d files -> %d files", initialFiles, finalFiles)
}
// TestRetentionWithActiveReaders tests that retention respects active readers
func TestRetentionWithActiveReaders(t *testing.T) {
dir := t.TempDir()
config := DefaultCometConfig()
config.Retention.MaxAge = 50 * time.Millisecond
config.Retention.CleanupInterval = 25 * time.Millisecond
config.Retention.MinFilesToKeep = 0 // Allow deletion of all old files
config.Storage.MaxFileSize = 512
client, err := NewClient(dir, config)
if err != nil {
t.Fatalf("failed to create client: %v", err)
}
defer client.Close()
ctx := context.Background()
streamName := "events:v1:shard:0000"
// Write data to create files
for i := 0; i < 20; i++ {
data := [][]byte{[]byte(fmt.Sprintf(`{"id": %d}`, i))}
_, err := client.Append(ctx, streamName, data)
if err != nil {
t.Fatalf("failed to write data: %v", err)
}
}
// Create an active reader by getting the shard and incrementing reader count
shard, _ := client.getOrCreateShard(0)
atomic.AddInt64(&shard.readerCount, 1)
defer atomic.AddInt64(&shard.readerCount, -1)
// Mark files as old
shard.mu.Lock()
oldTime := time.Now().Add(-100 * time.Millisecond)
for i := range shard.index.Files {
if shard.index.Files[i].Path != shard.index.CurrentFile {
shard.index.Files[i].EndTime = oldTime
}
}
initialFiles := len(shard.index.Files)
shard.mu.Unlock()
// Force retention cleanup while reader is active
client.ForceRetentionCleanup()
// Check that some files were protected due to active reader
shard.mu.RLock()
finalFiles := len(shard.index.Files)
readerCount := atomic.LoadInt64(&shard.readerCount)
shard.mu.RUnlock()
if readerCount == 0 {
t.Error("Expected active reader count > 0")
}
// We should still have deleted some files (middle ones), but kept first/last
if finalFiles == initialFiles {
t.Log("No files deleted with active reader - this is acceptable for safety")
} else if finalFiles < initialFiles {
t.Logf("Deleted %d files even with active reader (kept boundary files)", initialFiles-finalFiles)
}
}
// TestRetentionConsumerProtection tests that retention respects unconsumed data
func TestRetentionConsumerProtection(t *testing.T) {
dir := t.TempDir()
config := DefaultCometConfig()
config.Retention.MaxAge = 50 * time.Millisecond
config.Retention.CleanupInterval = 25 * time.Millisecond
config.Retention.MinFilesToKeep = 0 // Allow deletion of all old files
config.Retention.ProtectUnconsumed = true // Enable consumer protection
config.Storage.MaxFileSize = 512
client, err := NewClient(dir, config)
if err != nil {
t.Fatalf("failed to create client: %v", err)
}
defer client.Close()
ctx := context.Background()
streamName := "events:v1:shard:0000"
// Write data
for i := 0; i < 20; i++ {
data := [][]byte{[]byte(fmt.Sprintf(`{"id": %d}`, i))}
_, err := client.Append(ctx, streamName, data)
if err != nil {
t.Fatalf("failed to write data: %v", err)
}
}
// Set a consumer offset manually to simulate partial consumption
shard, _ := client.getOrCreateShard(0)
shard.mu.Lock()
// Simulate that consumer has read first 5 entries
shard.index.ConsumerOffsets = map[string]int64{
"test-group": 5,
}
shard.mu.Unlock()
// Mark all files as old
shard.mu.Lock()
oldTime := time.Now().Add(-100 * time.Millisecond)
for i := range shard.index.Files {
if shard.index.Files[i].Path != shard.index.CurrentFile {
shard.index.Files[i].EndTime = oldTime
}
}
initialFiles := len(shard.index.Files)
// Find which files contain unconsumed data
protectedFiles := 0
consumerOffset := shard.index.ConsumerOffsets["test-group"]
for _, file := range shard.index.Files {
fileLastEntry := file.StartEntry + file.Entries - 1
if fileLastEntry >= consumerOffset {
protectedFiles++
}
}
shard.mu.Unlock()
// Force retention cleanup
client.ForceRetentionCleanup()
// Check that files with unconsumed data were protected
shard.mu.RLock()
finalFiles := len(shard.index.Files)
shard.mu.RUnlock()
// We should have kept at least the files with unconsumed data
minExpectedFiles := protectedFiles
if minExpectedFiles < config.Retention.MinFilesToKeep {
minExpectedFiles = config.Retention.MinFilesToKeep
}
if finalFiles < minExpectedFiles {
t.Errorf("Consumer protection failed: have %d files, expected at least %d",
finalFiles, minExpectedFiles)
}
t.Logf("Consumer protection: %d files -> %d files (protected %d files with unconsumed data)",
initialFiles, finalFiles, protectedFiles)
}