Repository navigation
Expand file tree
/
Copy pathexample_test.go
More file actions
231 lines (213 loc) · 5.42 KB
/
Copy pathexample_test.go
File metadata and controls
231 lines (213 loc) · 5.42 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
// SPDX-License-Identifier: MIT
package diskqueue_test
import (
"bytes"
"context"
"encoding/binary"
"errors"
"fmt"
"log"
"os"
"path/filepath"
"github.com/JohanLindvall/diskqueue"
)
// tempDir gives each example its own queue directory. A directory holds one
// queue: New takes an advisory lock on it.
func tempDir() string {
d, err := os.MkdirTemp("", "diskqueue-example")
if err != nil {
log.Fatal(err)
}
return d
}
// A zero-allocation codec. MarshalFunc must APPEND to dst and return the
// extended slice — returning a fresh slice instead works, but costs the
// allocation the reused buffer exists to avoid.
func marshal(dst []byte, v uint64) ([]byte, error) {
return binary.LittleEndian.AppendUint64(dst, v), nil
}
func unmarshal(data []byte) (uint64, error) {
if len(data) != 8 {
return 0, errors.New("bad length")
}
return binary.LittleEndian.Uint64(data), nil
}
func Example() {
q, err := diskqueue.New[uint64](tempDir(), marshal, unmarshal)
if err != nil {
log.Fatal(err)
}
defer func() { _ = q.Close() }()
for i := uint64(1); i <= 3; i++ {
if err := q.Add(i); err != nil {
log.Fatal(err)
}
}
r := q.NewReader()
for {
v, ok, err := r.TryTake() // read and commit in one step
if err != nil {
log.Fatal(err)
}
if !ok {
break
}
fmt.Println(v)
}
// Output:
// 1
// 2
// 3
}
// Damage on disk is dropped and counted, never delivered as data and never left
// blocking the queue: flip one byte of a record in its segment file, as a bad
// sector would, and a reopened queue reports exactly that record lost and hands
// over the rest. The README's Go Playground link runs this program, so a change
// here wants a fresh share there.
func Example_recovery() {
dir := tempDir()
text := func(dst []byte, s string) ([]byte, error) { return append(dst, s...), nil }
untext := func(b []byte) (string, error) { return string(b), nil }
q, err := diskqueue.New[string](dir, text, untext)
if err != nil {
log.Fatal(err)
}
for _, s := range []string{"alpha", "bravo", "charlie"} {
if err := q.Add(s); err != nil {
log.Fatal(err)
}
}
if err := q.Close(); err != nil {
log.Fatal(err)
}
// Find "alpha" in the segment file and flip one of its bytes.
segs, err := filepath.Glob(filepath.Join(dir, "data.*"))
if err != nil || len(segs) != 1 {
log.Fatal("expected one segment: ", segs, err)
}
b, err := os.ReadFile(segs[0])
if err != nil {
log.Fatal(err)
}
i := bytes.Index(b, []byte("alpha"))
if i < 0 {
log.Fatal("record not found on disk")
}
f, err := os.OpenFile(segs[0], os.O_RDWR, 0)
if err != nil {
log.Fatal(err)
}
if _, err := f.WriteAt([]byte{b[i] ^ 0xff}, int64(i)); err != nil {
log.Fatal(err)
}
if err := f.Close(); err != nil {
log.Fatal(err)
}
q, err = diskqueue.New[string](dir, text, untext)
if err != nil {
log.Fatal(err)
}
defer func() { _ = q.Close() }()
r := q.NewReader()
for {
v, ok, err := r.TryTake()
switch {
case errors.Is(err, diskqueue.ErrCorrupt):
fmt.Println("dropped:", err)
continue
case err != nil:
log.Fatal(err)
case !ok:
fmt.Println("lost records:", q.Stats().LostRecords, "- still queued:", q.Count())
return
}
fmt.Println(v)
}
// Output:
// dropped: diskqueue: corrupt: record checksum
// bravo
// charlie
// lost records: 1 - still queued: 0
}
// Reserve/Ack is the at-least-once path: the record is not retired until you
// say so, so a crash between the two replays it.
func ExampleReader_Reserve() {
q, err := diskqueue.New[uint64](tempDir(), marshal, unmarshal)
if err != nil {
log.Fatal(err)
}
defer func() { _ = q.Close() }()
if err := q.Add(42); err != nil {
log.Fatal(err)
}
r := q.NewReader()
v, ok, offset, err := r.Reserve(context.Background())
if err != nil || !ok {
log.Fatal(err)
}
// ... process v; only acknowledge once it is safely handled.
if err := r.Ack(offset); err != nil {
log.Fatal(err)
}
fmt.Println(v, q.Count())
// Output: 42 0
}
// Corruption never stops the queue: damage is dropped, counted and reported as
// one ErrCorrupt, and the next call makes progress. This is the loop to write.
func ExampleReader_TryTake_corruption() {
q, err := diskqueue.New[uint64](tempDir(), marshal, unmarshal)
if err != nil {
log.Fatal(err)
}
defer func() { _ = q.Close() }()
for i := uint64(1); i <= 2; i++ {
if err := q.Add(i); err != nil {
log.Fatal(err)
}
}
rd := q.NewReader()
var lost int
for {
v, ok, err := rd.TryTake()
switch {
case errors.Is(err, diskqueue.ErrCodec):
// A codec error leaves the head in place, even if it wraps ErrCorrupt.
log.Fatal(err)
case errors.Is(err, diskqueue.ErrCorrupt):
// Already dropped and stepped past; count it and go round again.
lost++
continue
case err != nil:
log.Fatal(err)
case !ok:
fmt.Println("drained, lost", lost, "of", q.Stats().Added)
return
}
_ = v
}
// Output: drained, lost 0 of 2
}
// An iterator cannot carry an error per item, so check Err after the loop: a nil
// Err means it ended because the queue ran out, not because something failed.
func ExampleReader_Drain() {
q, err := diskqueue.New[uint64](tempDir(), marshal, unmarshal)
if err != nil {
log.Fatal(err)
}
defer func() { _ = q.Close() }()
for i := uint64(1); i <= 3; i++ {
if err := q.Add(i); err != nil {
log.Fatal(err)
}
}
rd := q.NewReader()
sum := uint64(0)
for v := range rd.Drain(context.Background()) {
sum += v
}
if err := rd.Err(); err != nil {
log.Fatal(err)
}
fmt.Println(sum)
// Output: 6
}