-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_id_integration_test.go
More file actions
413 lines (334 loc) · 11.5 KB
/
process_id_integration_test.go
File metadata and controls
413 lines (334 loc) · 11.5 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
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
//go:build integration
// +build integration
package comet
import (
"context"
"fmt"
"os"
"os/exec"
"runtime"
"strconv"
"sync"
"syscall"
"testing"
"time"
"unsafe"
)
// TestGetProcessID_MultiProcess tests actual multi-process coordination
func TestGetProcessID_MultiProcess(t *testing.T) {
if testing.Short() {
t.Skip("skipping process ID test in short mode")
}
// Check if we're the parent or child process
if workerID := os.Getenv("COMET_PROCESSID_WORKER"); workerID != "" {
// We're a child process - run the worker
runProcessIDWorker(t, workerID)
return
}
// Safety check - don't spawn if we're already in a subprocess
if os.Getenv("GO_TEST_SUBPROCESS") == "1" {
t.Skip("Skipping test in subprocess to prevent recursion")
return
}
shmFile := t.TempDir() + "/worker-slots-integration"
numProcesses := 4
maxSlots := runtime.NumCPU()
// In CI environments, we might have fewer CPU cores than processes
expectedSlots := numProcesses
if maxSlots < numProcesses {
expectedSlots = maxSlots
t.Logf("Note: System has %d CPU cores, but test wants %d processes. Only %d slots available.", maxSlots, numProcesses, maxSlots)
}
t.Logf("Starting process ID test with %d processes, %d expected slots, shared memory file: %s", numProcesses, expectedSlots, shmFile)
// Start multiple processes
var processes []*exec.Cmd
var wg sync.WaitGroup
for i := 0; i < numProcesses; i++ {
cmd := exec.Command(os.Args[0], "-test.run=TestGetProcessID_MultiProcess", "-test.v")
cmd.Env = append(os.Environ(),
fmt.Sprintf("COMET_PROCESSID_WORKER=%d", i),
fmt.Sprintf("COMET_SHM_FILE=%s", shmFile),
"GO_TEST_SUBPROCESS=1",
)
if err := cmd.Start(); err != nil {
t.Fatalf("Failed to start process %d: %v", i, err)
}
processes = append(processes, cmd)
}
// Wait for all processes with timeout
for i, cmd := range processes {
wg.Add(1)
go func(processIndex int, process *exec.Cmd) {
defer wg.Done()
done := make(chan error, 1)
go func() {
done <- process.Wait()
}()
select {
case err := <-done:
if err != nil {
// Check if it's exit code 42 (no slot available)
if exitError, ok := err.(*exec.ExitError); ok && exitError.ExitCode() == 42 {
// This is expected when there are more processes than slots
t.Logf("Process %d exited with code 42 (no slot available)", processIndex)
} else {
t.Errorf("Process %d failed: %v", processIndex, err)
}
}
case <-time.After(30 * time.Second):
t.Errorf("Process %d timed out", processIndex)
process.Process.Kill()
}
}(i, cmd)
}
wg.Wait()
// Verify that processes acquired unique IDs by checking the shared memory file
verifyUniqueSlotAssignment(t, shmFile, expectedSlots)
}
func runProcessIDWorker(t *testing.T, workerIDStr string) {
workerID, _ := strconv.Atoi(workerIDStr)
shmFile := os.Getenv("COMET_SHM_FILE")
t.Logf("Worker %d starting (PID: %d)", workerID, os.Getpid())
// Get process ID
processID := GetProcessID(shmFile)
t.Logf("Worker %d acquired process ID: %d", workerID, processID)
if processID < 0 {
t.Logf("Worker %d failed to acquire process ID (no available slots)", workerID)
// Exit with a specific code to indicate no slot available
os.Exit(42)
}
// Test that we can create a Comet client with this process ID
config := DeprecatedMultiProcessConfig(processID, runtime.NumCPU())
tempDir := t.TempDir()
ensureDirectoryCleanup(t, tempDir)
client, err := NewClient(tempDir, config)
if err != nil {
t.Fatalf("Worker %d failed to create Comet client: %v", workerID, err)
}
defer cleanupClient(t, client)
// Test basic operations
ctx := context.Background()
streamName := fmt.Sprintf("test:worker%d:shard:%04d", workerID, processID)
// Write some data
data := [][]byte{[]byte(fmt.Sprintf("Hello from worker %d", workerID))}
ids, err := client.Append(ctx, streamName, data)
if err != nil {
t.Fatalf("Worker %d failed to write data: %v", workerID, err)
}
if len(ids) != 1 {
t.Fatalf("Worker %d expected 1 ID, got %d", workerID, len(ids))
}
// Read it back
length, err := client.Len(ctx, streamName)
if err != nil {
t.Fatalf("Worker %d failed to get length: %v", workerID, err)
}
if length != 1 {
t.Fatalf("Worker %d expected length 1, got %d", workerID, length)
}
// Hold the slot briefly to test concurrent access
time.Sleep(200 * time.Millisecond)
t.Logf("Worker %d completed successfully with process ID %d", workerID, processID)
}
func verifyUniqueSlotAssignment(t *testing.T, shmFile string, expectedSlots int) {
maxWorkers := runtime.NumCPU()
slotSize := 8
file, err := os.OpenFile(shmFile, os.O_RDONLY, 0644)
if err != nil {
t.Fatalf("Failed to open shared memory file: %v", err)
}
defer file.Close()
data, err := syscall.Mmap(int(file.Fd()), 0, maxWorkers*slotSize,
syscall.PROT_READ, syscall.MAP_SHARED)
if err != nil {
t.Fatalf("Failed to mmap file: %v", err)
}
defer syscall.Munmap(data)
// Count occupied slots and verify uniqueness
occupiedSlots := 0
seenPIDs := make(map[uint32]int)
for i := 0; i < maxWorkers; i++ {
offset := i * slotSize
pid := *(*uint32)(unsafe.Pointer(&data[offset]))
if pid != 0 {
occupiedSlots++
seenPIDs[pid] = i
t.Logf("Slot %d: PID %d", i, pid)
}
}
if occupiedSlots == 0 {
t.Error("No processes acquired slots")
}
// Each PID should only appear once
if len(seenPIDs) != occupiedSlots {
t.Errorf("Expected %d unique PIDs, got %d", occupiedSlots, len(seenPIDs))
}
t.Logf("Successfully assigned %d unique process slots out of %d expected slots", occupiedSlots, expectedSlots)
// Verify we got the expected number of slots
if occupiedSlots != expectedSlots {
t.Errorf("Expected %d occupied slots, got %d", expectedSlots, occupiedSlots)
}
}
// TestGetProcessID_ProcessRestart tests process restart scenarios
func TestGetProcessID_ProcessRestart(t *testing.T) {
if testing.Short() {
t.Skip("skipping process restart test in short mode")
}
// Check if we're the parent or child process
if phase := os.Getenv("COMET_RESTART_PHASE"); phase != "" {
runRestartWorker(t, phase)
return
}
// Safety check
if os.Getenv("GO_TEST_SUBPROCESS") == "1" {
t.Skip("Skipping test in subprocess to prevent recursion")
return
}
shmFile := t.TempDir() + "/worker-slots-restart"
t.Logf("Testing process restart with shared memory file: %s", shmFile)
// Start first process
cmd1 := exec.Command(os.Args[0], "-test.run=TestGetProcessID_ProcessRestart", "-test.v")
cmd1.Env = append(os.Environ(),
"COMET_RESTART_PHASE=1",
fmt.Sprintf("COMET_SHM_FILE=%s", shmFile),
"GO_TEST_SUBPROCESS=1",
)
if err := cmd1.Start(); err != nil {
t.Fatalf("Failed to start first process: %v", err)
}
// Wait for it to complete
if err := cmd1.Wait(); err != nil {
t.Fatalf("First process failed: %v", err)
}
// Start second process (simulating restart)
cmd2 := exec.Command(os.Args[0], "-test.run=TestGetProcessID_ProcessRestart", "-test.v")
cmd2.Env = append(os.Environ(),
"COMET_RESTART_PHASE=2",
fmt.Sprintf("COMET_SHM_FILE=%s", shmFile),
"GO_TEST_SUBPROCESS=1",
)
if err := cmd2.Start(); err != nil {
t.Fatalf("Failed to start second process: %v", err)
}
// Wait for it to complete
if err := cmd2.Wait(); err != nil {
t.Fatalf("Second process failed: %v", err)
}
t.Log("Process restart test completed successfully")
}
func runRestartWorker(t *testing.T, phase string) {
shmFile := os.Getenv("COMET_SHM_FILE")
// Get process ID
processID := GetProcessID(shmFile)
t.Logf("Phase %s acquired process ID: %d (PID: %d)", phase, processID, os.Getpid())
if processID < 0 {
t.Fatalf("Failed to acquire process ID in phase %s", phase)
}
// Create Comet client
config := DeprecatedMultiProcessConfig(processID, runtime.NumCPU())
tempDir := t.TempDir()
ensureDirectoryCleanup(t, tempDir)
client, err := NewClient(tempDir, config)
if err != nil {
t.Fatalf("Failed to create Comet client in phase %s: %v", phase, err)
}
defer cleanupClient(t, client)
// Test operations
ctx := context.Background()
streamName := fmt.Sprintf("test:phase%s:shard:%04d", phase, processID)
data := [][]byte{[]byte(fmt.Sprintf("Data from phase %s", phase))}
_, err = client.Append(ctx, streamName, data)
if err != nil {
t.Fatalf("Failed to write data in phase %s: %v", phase, err)
}
// Explicitly release the process ID before exiting (simulate graceful shutdown)
if phase == "1" {
ReleaseProcessID(shmFile)
t.Logf("Phase %s released process ID %d", phase, processID)
}
t.Logf("Phase %s completed with process ID %d", phase, processID)
}
// TestGetProcessID_FailureRecovery tests recovery from process failures
func TestGetProcessID_FailureRecovery(t *testing.T) {
if testing.Short() {
t.Skip("skipping failure recovery test in short mode")
}
// Check if we're the parent or child process
if workerType := os.Getenv("COMET_FAILURE_WORKER"); workerType != "" {
runFailureWorker(t, workerType)
return
}
// Safety check
if os.Getenv("GO_TEST_SUBPROCESS") == "1" {
t.Skip("Skipping test in subprocess to prevent recursion")
return
}
shmFile := t.TempDir() + "/worker-slots-failure"
t.Logf("Testing failure recovery with shared memory file: %s", shmFile)
// Start a process that will be killed
cmd1 := exec.Command(os.Args[0], "-test.run=TestGetProcessID_FailureRecovery", "-test.v")
cmd1.Env = append(os.Environ(),
"COMET_FAILURE_WORKER=victim",
fmt.Sprintf("COMET_SHM_FILE=%s", shmFile),
"GO_TEST_SUBPROCESS=1",
)
if err := cmd1.Start(); err != nil {
t.Fatalf("Failed to start victim process: %v", err)
}
// Give it time to acquire a slot
time.Sleep(200 * time.Millisecond)
// Kill the process without cleanup
if err := cmd1.Process.Kill(); err != nil {
t.Fatalf("Failed to kill victim process: %v", err)
}
cmd1.Wait() // Clean up zombie
// Start a recovery process that should detect the dead process and reuse its slot
cmd2 := exec.Command(os.Args[0], "-test.run=TestGetProcessID_FailureRecovery", "-test.v")
cmd2.Env = append(os.Environ(),
"COMET_FAILURE_WORKER=recovery",
fmt.Sprintf("COMET_SHM_FILE=%s", shmFile),
"GO_TEST_SUBPROCESS=1",
)
if err := cmd2.Start(); err != nil {
t.Fatalf("Failed to start recovery process: %v", err)
}
if err := cmd2.Wait(); err != nil {
t.Fatalf("Recovery process failed: %v", err)
}
t.Log("Failure recovery test completed successfully")
}
func runFailureWorker(t *testing.T, workerType string) {
shmFile := os.Getenv("COMET_SHM_FILE")
processID := GetProcessID(shmFile)
t.Logf("%s worker acquired process ID: %d (PID: %d)", workerType, processID, os.Getpid())
if processID < 0 {
t.Fatalf("%s worker failed to acquire process ID", workerType)
}
if workerType == "victim" {
// This process will be killed, so just hold the slot
t.Logf("Victim worker holding slot %d", processID)
for {
time.Sleep(100 * time.Millisecond)
}
} else if workerType == "recovery" {
// This process should successfully reuse the dead process's slot
t.Logf("Recovery worker successfully acquired slot %d", processID)
// Test that we can use it
config := DeprecatedMultiProcessConfig(processID, runtime.NumCPU())
tempDir := t.TempDir()
ensureDirectoryCleanup(t, tempDir)
client, err := NewClient(tempDir, config)
if err != nil {
t.Fatalf("Recovery worker failed to create Comet client: %v", err)
}
defer cleanupClient(t, client)
ctx := context.Background()
streamName := fmt.Sprintf("test:recovery:shard:%04d", processID)
data := [][]byte{[]byte("Recovery test data")}
_, err = client.Append(ctx, streamName, data)
if err != nil {
t.Fatalf("Recovery worker failed to write data: %v", err)
}
t.Log("Recovery worker completed successfully")
}
}