-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathincremental_rebalancing_test.go
More file actions
363 lines (300 loc) · 11.1 KB
/
incremental_rebalancing_test.go
File metadata and controls
363 lines (300 loc) · 11.1 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
// Copyright (c) 2025 SciGo HDF5 Library Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
package hdf5_test
import (
"fmt"
"os"
"runtime"
"sync/atomic"
"testing"
"time"
"github.com/scigolib/hdf5"
"github.com/scigolib/hdf5/internal/structures"
)
// TestIncrementalRebalancing_WithDefer demonstrates BEST PRACTICE usage.
//
// Pattern: Enable incremental mode, defer Stop() for explicit cleanup.
// This is the recommended approach for production code.
func TestIncrementalRebalancing_WithDefer(t *testing.T) {
filename := "test_incremental_defer.h5"
defer os.Remove(filename)
// Track goroutine count
initialGoroutines := runtime.NumGoroutine()
// Create file and enable lazy mode
fw, err := hdf5.CreateForWrite(filename, hdf5.CreateTruncate)
if err != nil {
t.Fatalf("Failed to create file: %v", err)
}
// Enable lazy rebalancing (prerequisite for incremental)
lazyConfig := structures.DefaultLazyConfig()
if err := fw.EnableLazyRebalancing(lazyConfig); err != nil {
t.Fatalf("Failed to enable lazy rebalancing: %v", err)
}
// Enable incremental rebalancing with progress callback
var callbackCalled atomic.Int32
config := structures.DefaultIncrementalConfig()
config.Budget = 50 * time.Millisecond // Fast for testing
config.Interval = 100 * time.Millisecond // Frequent for testing
config.ProgressCallback = func(p structures.RebalancingProgress) {
callbackCalled.Add(1)
t.Logf("Progress: rebalanced=%d, remaining=%d, complete=%v",
p.NodesRebalanced, p.NodesRemaining, p.IsComplete)
}
if err := fw.EnableIncrementalRebalancing(config); err != nil {
t.Fatalf("Failed to enable incremental rebalancing: %v", err)
}
// ✅ BEST PRACTICE: Explicit defer Stop()
defer func() {
if err := fw.StopIncrementalRebalancing(); err != nil {
t.Errorf("Failed to stop incremental rebalancing: %v", err)
}
}()
// Create dataset with attributes to trigger B-tree work
ds, err := fw.CreateDataset("/data", hdf5.Int32, []uint64{100})
if err != nil {
t.Fatalf("Failed to create dataset: %v", err)
}
// Write dataset data
data := make([]int32, 100)
for i := range data {
data[i] = int32(i)
}
if err := ds.Write(data); err != nil {
t.Fatalf("Failed to write data: %v", err)
}
// Write many attributes (would trigger B-tree operations in dense storage)
// Note: For MVP with compact storage, this won't trigger goroutine,
// but demonstrates proper usage pattern
for i := 0; i < 10; i++ {
if err := ds.WriteAttribute(fmt.Sprintf("attr_%d", i), int32(i)); err != nil {
t.Fatalf("Failed to write attribute: %v", err)
}
}
// Wait a bit to let goroutine run (if active)
time.Sleep(200 * time.Millisecond)
// Close file (which also calls Stop() as safety net)
if err := fw.Close(); err != nil {
t.Fatalf("Failed to close file: %v", err)
}
// Verify goroutines cleaned up
// Allow small increase for runtime background goroutines
finalGoroutines := runtime.NumGoroutine()
if finalGoroutines > initialGoroutines+2 {
t.Errorf("Goroutine leak detected: initial=%d, final=%d",
initialGoroutines, finalGoroutines)
}
t.Logf("✅ No goroutine leak: initial=%d, final=%d",
initialGoroutines, finalGoroutines)
// Note: Callback might not be called if no actual B-tree work happened (MVP)
t.Logf("Callback called %d times", callbackCalled.Load())
}
// TestIncrementalRebalancing_AutomaticCleanup demonstrates SAFETY NET.
//
// Pattern: Enable incremental mode WITHOUT defer Stop().
// Close() should automatically stop goroutines (foolproof).
func TestIncrementalRebalancing_AutomaticCleanup(t *testing.T) {
filename := "test_incremental_auto.h5"
defer os.Remove(filename)
// Track goroutine count
initialGoroutines := runtime.NumGoroutine()
// Create file
fw, err := hdf5.CreateForWrite(filename, hdf5.CreateTruncate)
if err != nil {
t.Fatalf("Failed to create file: %v", err)
}
// Enable lazy + incremental
lazyConfig := structures.DefaultLazyConfig()
if err := fw.EnableLazyRebalancing(lazyConfig); err != nil {
t.Fatalf("Failed to enable lazy rebalancing: %v", err)
}
config := structures.DefaultIncrementalConfig()
config.Budget = 50 * time.Millisecond
config.Interval = 100 * time.Millisecond
if err := fw.EnableIncrementalRebalancing(config); err != nil {
t.Fatalf("Failed to enable incremental rebalancing: %v", err)
}
// ⚠️ INTENTIONALLY NO defer Stop() - testing automatic cleanup!
// Create dataset
ds, err := fw.CreateDataset("/data", hdf5.Int32, []uint64{50})
if err != nil {
t.Fatalf("Failed to create dataset: %v", err)
}
data := make([]int32, 50)
if err := ds.Write(data); err != nil {
t.Fatalf("Failed to write data: %v", err)
}
// Just call Close() - should automatically stop goroutines
if err := fw.Close(); err != nil {
t.Fatalf("Failed to close file: %v", err)
}
// Verify no goroutine leak even without explicit Stop()
time.Sleep(100 * time.Millisecond) // Let goroutines finish
finalGoroutines := runtime.NumGoroutine()
if finalGoroutines > initialGoroutines+2 {
t.Errorf("Goroutine leak detected (no defer): initial=%d, final=%d",
initialGoroutines, finalGoroutines)
}
t.Logf("✅ Automatic cleanup works: initial=%d, final=%d",
initialGoroutines, finalGoroutines)
}
// TestIncrementalRebalancing_MultipleStopCalls verifies Stop() is idempotent.
//
// Pattern: Call Stop() multiple times should be safe.
func TestIncrementalRebalancing_MultipleStopCalls(t *testing.T) {
filename := "test_incremental_multiple.h5"
defer os.Remove(filename)
fw, err := hdf5.CreateForWrite(filename, hdf5.CreateTruncate)
if err != nil {
t.Fatalf("Failed to create file: %v", err)
}
defer fw.Close()
// Enable lazy + incremental
if err := fw.EnableLazyRebalancing(structures.DefaultLazyConfig()); err != nil {
t.Fatalf("Failed to enable lazy rebalancing: %v", err)
}
if err := fw.EnableIncrementalRebalancing(structures.DefaultIncrementalConfig()); err != nil {
t.Fatalf("Failed to enable incremental rebalancing: %v", err)
}
// Call Stop() multiple times
for i := 0; i < 3; i++ {
if err := fw.StopIncrementalRebalancing(); err != nil {
t.Errorf("Stop() call %d failed: %v", i+1, err)
}
}
// Close() will call Stop() again internally - should be safe
// (tested by defer fw.Close() above)
}
// TestIncrementalRebalancing_NotEnabled verifies Stop() on non-enabled file is safe.
func TestIncrementalRebalancing_NotEnabled(t *testing.T) {
filename := "test_incremental_not_enabled.h5"
defer os.Remove(filename)
fw, err := hdf5.CreateForWrite(filename, hdf5.CreateTruncate)
if err != nil {
t.Fatalf("Failed to create file: %v", err)
}
defer fw.Close()
// Call Stop() without enabling incremental - should be safe (no-op)
if err := fw.StopIncrementalRebalancing(); err != nil {
t.Errorf("Stop() on non-enabled file failed: %v", err)
}
// Close() will also call Stop() - should be safe
}
// TestIncrementalRebalancing_ErrorCases tests error conditions.
//
// Note: For MVP, FileWriter methods are NO-OPs (incremental mode is per-dataset).
// This test documents expected behavior for future global tracking.
func TestIncrementalRebalancing_ErrorCases(t *testing.T) {
filename := "test_incremental_errors.h5"
defer os.Remove(filename)
fw, err := hdf5.CreateForWrite(filename, hdf5.CreateTruncate)
if err != nil {
t.Fatalf("Failed to create file: %v", err)
}
defer fw.Close()
// Test 1: Enable incremental (MVP: no-op, returns nil)
config := structures.DefaultIncrementalConfig()
err = fw.EnableIncrementalRebalancing(config)
if err != nil {
t.Errorf("EnableIncrementalRebalancing failed: %v", err)
}
t.Logf("✅ EnableIncrementalRebalancing (MVP no-op): %v", err)
// Test 2: Enable lazy (MVP: no-op, returns nil)
if err := fw.EnableLazyRebalancing(structures.DefaultLazyConfig()); err != nil {
t.Errorf("EnableLazyRebalancing failed: %v", err)
}
// Test 3: Stop incremental (MVP: no-op, returns nil)
err = fw.StopIncrementalRebalancing()
if err != nil {
t.Errorf("StopIncrementalRebalancing failed: %v", err)
}
t.Logf("✅ StopIncrementalRebalancing (MVP no-op): %v", err)
// Future: When global BTree tracking implemented, test:
// - Enable incremental without lazy mode (should error)
// - Enable incremental twice (should error)
// - Stop when not enabled (should succeed)
}
// BenchmarkIncrementalRebalancing_WithVsWithout compares performance.
//
// This benchmark demonstrates the performance benefit of incremental mode.
//
//nolint:gocognit // Benchmark complexity is acceptable
func BenchmarkIncrementalRebalancing_WithVsWithout(b *testing.B) {
b.Run("Without_Incremental", func(b *testing.B) {
filenames := make([]string, 0, b.N)
defer func() {
for _, fn := range filenames {
os.Remove(fn)
}
}()
for i := 0; i < b.N; i++ {
filename := fmt.Sprintf("bench_no_inc_%d.h5", i)
filenames = append(filenames, filename)
fw, err := hdf5.CreateForWrite(filename, hdf5.CreateTruncate)
if err != nil {
b.Fatalf("Failed to create file: %v", err)
}
ds, err := fw.CreateDataset("/data", hdf5.Int32, []uint64{100})
if err != nil {
b.Fatalf("Failed to create dataset: %v", err)
}
data := make([]int32, 100)
if err := ds.Write(data); err != nil {
b.Fatalf("Failed to write data: %v", err)
}
// Write attributes (immediate rebalancing)
for j := 0; j < 50; j++ {
if err := ds.WriteAttribute(fmt.Sprintf("attr_%d", j), int32(j)); err != nil {
b.Fatalf("Failed to write attribute: %v", err)
}
}
if err := fw.Close(); err != nil {
b.Fatalf("Failed to close file: %v", err)
}
}
})
b.Run("With_Incremental", func(b *testing.B) {
filenames := make([]string, 0, b.N)
defer func() {
for _, fn := range filenames {
os.Remove(fn)
}
}()
for i := 0; i < b.N; i++ {
filename := fmt.Sprintf("bench_with_inc_%d.h5", i)
filenames = append(filenames, filename)
fw, err := hdf5.CreateForWrite(filename, hdf5.CreateTruncate)
if err != nil {
b.Fatalf("Failed to create file: %v", err)
}
// Enable lazy + incremental
if err := fw.EnableLazyRebalancing(structures.DefaultLazyConfig()); err != nil {
b.Fatalf("Failed to enable lazy rebalancing: %v", err)
}
config := structures.DefaultIncrementalConfig()
config.Budget = 10 * time.Millisecond
config.Interval = 1 * time.Second // Infrequent for benchmark
if err := fw.EnableIncrementalRebalancing(config); err != nil {
b.Fatalf("Failed to enable incremental rebalancing: %v", err)
}
// Note: Close() will automatically stop incremental rebalancing
// No defer needed in benchmark loop (automatic cleanup)
ds, err := fw.CreateDataset("/data", hdf5.Int32, []uint64{100})
if err != nil {
b.Fatalf("Failed to create dataset: %v", err)
}
data := make([]int32, 100)
if err := ds.Write(data); err != nil {
b.Fatalf("Failed to write data: %v", err)
}
// Write attributes (background rebalancing)
for j := 0; j < 50; j++ {
if err := ds.WriteAttribute(fmt.Sprintf("attr_%d", j), int32(j)); err != nil {
b.Fatalf("Failed to write attribute: %v", err)
}
}
if err := fw.Close(); err != nil {
b.Fatalf("Failed to close file: %v", err)
}
}
})
}