-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathofflocksync_test.go
More file actions
162 lines (157 loc) · 4.23 KB
/
Copy pathofflocksync_test.go
File metadata and controls
162 lines (157 loc) · 4.23 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
package diskqueue
import (
"errors"
"sync"
"testing"
"time"
)
// TestSyncKeepsOpenFilesUnderCap: an off-lock flush pins the whole dirty set,
// and a pinned file is exactly the one evictOpen may not close — so opening
// them all at once made MaxOpenFiles unenforceable for the duration of the
// flush, and (nothing re-evicts on the way out) after it as well. NoSync is the
// unbounded shape: nothing is flushed until an explicit Sync, so every segment
// written since the last one is dirty and the descriptor count tracked the
// backlog rather than the cap. Sixteen dirty segments against a floor cap of 3
// used to leave 16 handles open; EMFILE is what it looks like at scale.
func TestSyncKeepsOpenFilesUnderCap(t *testing.T) {
const openCap = 3
m, u := bytesCodec()
w, err := New[[]byte](t.TempDir(), m, u, Options{
NoSync: true, SegmentSize: 4096, MaxSegments: -1, MaxOpenFiles: openCap,
})
if err != nil {
t.Fatal(err)
}
defer func() { _ = w.Close() }()
payload := make([]byte, 1024)
const records = 48
for i := 0; i < records; i++ {
if err := w.Add(payload); err != nil {
t.Fatal(err)
}
}
w.mu.Lock()
dirty := 0
for _, df := range w.st.files {
if df.dirty {
dirty++
}
}
w.mu.Unlock()
if dirty <= openCap {
t.Fatalf("only %d dirty segments against a cap of %d: the flush never has to chunk, so this test proves nothing", dirty, openCap)
}
if err := w.Sync(); err != nil {
t.Fatal(err)
}
w.mu.Lock()
open, stillDirty := w.st.nOpen, 0
for _, df := range w.st.files {
if df.dirty {
stillDirty++
}
}
w.mu.Unlock()
if open > openCap {
t.Errorf("%d segment handles open after a Sync over %d dirty segments, cap is %d", open, dirty, openCap)
}
// The cap must not have been bought by flushing less: everything dirty at
// the call is durable when it returns, which is the whole promise.
if stillDirty != 0 {
t.Errorf("%d segments still dirty after Sync, want 0", stillDirty)
}
if got := w.Stats().UnsyncedBytes; got != 0 {
t.Errorf("UnsyncedBytes=%d after Sync, want 0", got)
}
r := w.NewReader()
for i := 0; i < records; i++ {
v, ok, err := r.TryTake()
if err != nil || !ok {
t.Fatalf("record %d: ok=%v err=%v", i, ok, err)
}
if len(v) != len(payload) {
t.Fatalf("record %d: %d bytes, want %d", i, len(v), len(payload))
}
}
}
// The deterministic proofs for the off-lock flush live in
// offlocksync_faults_test.go, behind the injection seam. This file is the
// default build's share: a short hammer that runs Sync — foreground and the
// SyncInterval backstop both — against producers, a committing consumer and
// Close, under small segments and a floor MaxOpenFiles so eviction and
// reclamation keep crossing the pinned files. It asserts the invariants that
// survive scheduling (Close returns clean, a closed queue refuses Sync) and
// leaves the rest to the race detector, which is what `make test` runs it
// under.
func TestSyncCloseAddRace(t *testing.T) {
m, u := bytesCodec()
payload := make([]byte, 512)
for it := 0; it < 25; it++ {
w, err := New[[]byte](t.TempDir(), m, u, Options{
SyncEvery: 1 << 30,
SyncInterval: time.Millisecond,
SegmentSize: 4096,
MaxSegments: -1,
MaxOpenFiles: 3,
})
if err != nil {
t.Fatal(err)
}
var wg sync.WaitGroup
stop := make(chan struct{})
for p := 0; p < 2; p++ {
wg.Add(1)
go func() {
defer wg.Done()
for {
select {
case <-stop:
return
default:
}
if err := w.Add(payload); errors.Is(err, ErrClosed) {
return
}
}
}()
}
wg.Add(1)
go func() {
defer wg.Done()
for {
if err := w.Sync(); errors.Is(err, ErrClosed) {
return
}
select {
case <-stop:
return
default:
}
}
}()
wg.Add(1)
go func() {
defer wg.Done()
r := w.NewReader()
for {
if _, _, err := r.TryTake(); errors.Is(err, ErrClosed) {
return
}
select {
case <-stop:
return
default:
}
}
}()
time.Sleep(4 * time.Millisecond) // let the interval backstop fire mid-traffic
if err := w.Close(); err != nil {
t.Fatalf("iteration %d: Close: %v", it, err)
}
close(stop)
wg.Wait()
if err := w.Sync(); !errors.Is(err, ErrClosed) {
t.Fatalf("iteration %d: Sync after Close: %v, want ErrClosed", it, err)
}
}
}