-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_ownership_test.go
More file actions
278 lines (237 loc) · 7.9 KB
/
process_ownership_test.go
File metadata and controls
278 lines (237 loc) · 7.9 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
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
package comet
import (
"context"
"fmt"
"sync"
"testing"
)
func TestProcessExclusiveShardOwnership(t *testing.T) {
dir := t.TempDir()
// Test 1: Process 0 should own shards 0, 2, 4...
t.Run("Process0Ownership", func(t *testing.T) {
config := DefaultCometConfig()
// Multi-process mode is enabled by setting ProcessCount > 1
config.Concurrency.ProcessID = 0
config.Concurrency.ProcessCount = 2
client, err := NewClient(dir, config)
if err != nil {
t.Fatalf("failed to create client: %v", err)
}
defer client.Close()
ctx := context.Background()
// Should succeed - process 0 owns shard 0
_, err = client.Append(ctx, "test:v1:shard:0000", [][]byte{[]byte("test")})
if err != nil {
t.Errorf("Process 0 should be able to write to shard 0: %v", err)
}
// Should succeed - process 0 owns shard 2
_, err = client.Append(ctx, "test:v1:shard:0002", [][]byte{[]byte("test")})
if err != nil {
t.Errorf("Process 0 should be able to write to shard 2: %v", err)
}
// Should fail - process 0 doesn't own shard 1
_, err = client.Append(ctx, "test:v1:shard:0001", [][]byte{[]byte("test")})
if err == nil {
t.Error("Process 0 should NOT be able to write to shard 1")
}
// Should fail - process 0 doesn't own shard 3
_, err = client.Append(ctx, "test:v1:shard:0003", [][]byte{[]byte("test")})
if err == nil {
t.Error("Process 0 should NOT be able to write to shard 3")
}
})
// Test 2: Process 1 should own shards 1, 3, 5...
t.Run("Process1Ownership", func(t *testing.T) {
config := DefaultCometConfig()
// Multi-process mode is enabled by setting ProcessCount > 1
config.Concurrency.ProcessID = 1
config.Concurrency.ProcessCount = 2
client, err := NewClient(dir, config)
if err != nil {
t.Fatalf("failed to create client: %v", err)
}
defer client.Close()
ctx := context.Background()
// Should succeed - process 1 owns shard 1
_, err = client.Append(ctx, "test:v1:shard:0001", [][]byte{[]byte("test")})
if err != nil {
t.Errorf("Process 1 should be able to write to shard 1: %v", err)
}
// Should succeed - process 1 owns shard 3
_, err = client.Append(ctx, "test:v1:shard:0003", [][]byte{[]byte("test")})
if err != nil {
t.Errorf("Process 1 should be able to write to shard 3: %v", err)
}
// Should fail - process 1 doesn't own shard 0
_, err = client.Append(ctx, "test:v1:shard:0000", [][]byte{[]byte("test")})
if err == nil {
t.Error("Process 1 should NOT be able to write to shard 0")
}
// Should fail - process 1 doesn't own shard 2
_, err = client.Append(ctx, "test:v1:shard:0002", [][]byte{[]byte("test")})
if err == nil {
t.Error("Process 1 should NOT be able to write to shard 2")
}
})
// Test 3: Reads should work from any process
t.Run("CrossProcessReads", func(t *testing.T) {
// First, write data with process 0
config0 := DefaultCometConfig()
config0.Concurrency.ProcessID = 0
config0.Concurrency.ProcessCount = 2
client0, err := NewClient(dir, config0)
if err != nil {
t.Fatalf("failed to create client0: %v", err)
}
ctx := context.Background()
_, err = client0.Append(ctx, "test:v1:shard:0000", [][]byte{[]byte("from process 0")})
if err != nil {
t.Fatalf("failed to write from process 0: %v", err)
}
// Sync to ensure data is visible
if err := client0.Sync(ctx); err != nil {
t.Fatalf("failed to sync process 0: %v", err)
}
client0.Close()
// Write data with process 1
config1 := DefaultCometConfig()
config1.Concurrency.ProcessID = 1
config1.Concurrency.ProcessCount = 2
client1, err := NewClient(dir, config1)
if err != nil {
t.Fatalf("failed to create client1: %v", err)
}
_, err = client1.Append(ctx, "test:v1:shard:0001", [][]byte{[]byte("from process 1")})
if err != nil {
t.Fatalf("failed to write from process 1: %v", err)
}
// Sync to ensure data is visible
if err := client1.Sync(ctx); err != nil {
t.Fatalf("failed to sync process 1: %v", err)
}
// Now process 1 should be able to read from shard 0 (owned by process 0)
consumer := NewConsumer(client1, ConsumerOptions{Group: "test-cross-read"})
messages, err := consumer.Read(ctx, []uint32{0}, 10)
if err != nil {
t.Fatalf("Process 1 should be able to read from shard 0: %v", err)
}
// Find the message we wrote
found := false
for _, msg := range messages {
if string(msg.Data) == "from process 0" {
found = true
break
}
}
if !found {
t.Errorf("Expected to find 'from process 0' in messages, got %v", messages)
}
// Process 1 should also be able to read from its own shard
messages, err = consumer.Read(ctx, []uint32{1}, 10)
if err != nil {
t.Fatalf("Process 1 should be able to read from shard 1: %v", err)
}
// Find the message we wrote
found = false
for _, msg := range messages {
if string(msg.Data) == "from process 1" {
found = true
break
}
}
if !found {
t.Errorf("Expected to find 'from process 1' in messages, got %v", messages)
}
consumer.Close()
client1.Close()
})
// Test 4: Verify no lock contention for owned shards
t.Run("NoLockContentionForOwnedShards", func(t *testing.T) {
config := DefaultCometConfig()
// Multi-process mode is enabled by setting ProcessCount > 1
config.Concurrency.ProcessID = 0
config.Concurrency.ProcessCount = 2
client, err := NewClient(dir, config)
if err != nil {
t.Fatalf("failed to create client: %v", err)
}
defer client.Close()
ctx := context.Background()
// Write many entries concurrently to owned shards - should be fast with no lock contention
var wg sync.WaitGroup
errCh := make(chan error, 100)
for i := 0; i < 100; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
// Write to shard 0 (owned by process 0)
data := fmt.Sprintf("entry %d", i)
_, err := client.Append(ctx, "test:v1:shard:0000", [][]byte{[]byte(data)})
if err != nil {
errCh <- err
}
}(i)
}
wg.Wait()
close(errCh)
// Check for errors
for err := range errCh {
t.Errorf("Concurrent write to owned shard failed: %v", err)
}
// Verify all entries were written
length, err := client.Len(ctx, "test:v1:shard:0000")
if err != nil {
t.Fatalf("Failed to get length: %v", err)
}
// Should have at least 100 entries (plus any from previous tests)
if length < 100 {
t.Errorf("Expected at least 100 entries, got %d", length)
}
})
}
func TestProcessOwnershipWithThreeProcesses(t *testing.T) {
dir := t.TempDir()
// Test with 3 processes
// Process 0 owns: 0, 3, 6, 9...
// Process 1 owns: 1, 4, 7, 10...
// Process 2 owns: 2, 5, 8, 11...
testCases := []struct {
processID int
ownedShards []uint32
notOwned []uint32
}{
{0, []uint32{0, 3, 6, 9}, []uint32{1, 2, 4, 5}},
{1, []uint32{1, 4, 7, 10}, []uint32{0, 2, 3, 5}},
{2, []uint32{2, 5, 8, 11}, []uint32{0, 1, 3, 4}},
}
for _, tc := range testCases {
t.Run(fmt.Sprintf("Process%d", tc.processID), func(t *testing.T) {
config := DefaultCometConfig()
// Multi-process mode is enabled by setting ProcessCount > 1
config.Concurrency.ProcessID = tc.processID
config.Concurrency.ProcessCount = 3
client, err := NewClient(dir, config)
if err != nil {
t.Fatalf("failed to create client: %v", err)
}
defer client.Close()
ctx := context.Background()
// Test owned shards - writes should succeed
for _, shardID := range tc.ownedShards {
stream := fmt.Sprintf("test:v1:shard:%04d", shardID)
_, err := client.Append(ctx, stream, [][]byte{[]byte(fmt.Sprintf("from process %d", tc.processID))})
if err != nil {
t.Errorf("Process %d should own shard %d but write failed: %v", tc.processID, shardID, err)
}
}
// Test not owned shards - writes should fail
for _, shardID := range tc.notOwned {
stream := fmt.Sprintf("test:v1:shard:%04d", shardID)
_, err := client.Append(ctx, stream, [][]byte{[]byte("should fail")})
if err == nil {
t.Errorf("Process %d should NOT own shard %d but write succeeded", tc.processID, shardID)
}
}
})
}
}