-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiskq.go
191 lines (175 loc) · 5.48 KB
/
diskq.go
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
package diskq
import (
"encoding/json"
"fmt"
"os"
"time"
)
// New opens or creates a diskq based on a given path and config.
//
// The `Diskq` type itself should be thought of as a producer with
// exclusive access to write to the data directory named in the config.
//
// The [path] should be the location on disk you want to hold all
// the data associated with the diskq; you cannot split the diskq up
// across multiple disparate paths.
//
// The [cfg] should be customized for what you'd like to use for
// this particular "session" of the diskq producer. You may, for example,
// change the partition count between sessions, or change the segment size
// and retention settings. If you'd like to reuse a previous session's options
// for the diskq, you can use the helper [MaybeReadOptions], and pass the returned
// options from that helper as the [cfg] argument.
//
// If the diskq exists on disk, existing partitions will be opened, and if
// the configured partition count is greater than the number of existing
// partitions, new empty partitions will be created. Existing "orphaned" partitions
// will be left in place until vacuuming potentially removes them.
func New(path string, cfg Options) (*Diskq, error) {
d := &Diskq{
id: UUIDv4(),
path: path,
cfg: cfg,
}
if _, err := os.Stat(path); err != nil {
if err := os.MkdirAll(path, 0755); err != nil {
return nil, fmt.Errorf("diskq; cannot ensure data directory: %w", err)
}
}
if err := d.writeSettings(); err != nil {
return nil, err
}
if err := d.writeSentinel(); err != nil {
return nil, err
}
for partitionIndex := 0; partitionIndex < int(cfg.PartitionCountOrDefault()); partitionIndex++ {
p, err := createOrOpenPartition(path, cfg, uint32(partitionIndex))
if err != nil {
return nil, err
}
d.partitions = append(d.partitions, p)
}
return d, nil
}
// Diskq is the root struct of the diskq.
//
// It could be thought of primarily as the "producer" in the
// streaming system; you will use this type to "Push" messages into the partitions.
//
// Close will release open file handles that are held by the partitions.
type Diskq struct {
id UUID
path string
cfg Options
partitions []*Partition
}
// ID returns the id of the diskq producer.
func (dq *Diskq) ID() UUID { return dq.id }
// Path returns the path of the diskq producer.
func (dq *Diskq) Path() string { return dq.path }
// Options returns the config of the diskq producer.
func (dq *Diskq) Options() Options { return dq.cfg }
// Push pushes a new message into the diskq, returning the partition it was written to,
// the offset it was written to, and any errors that were generated while writing the message.
func (dq *Diskq) Push(value Message) (partition uint32, offset uint64, err error) {
if value.PartitionKey == "" {
value.PartitionKey = UUIDv4().String()
}
if value.TimestampUTC.IsZero() {
value.TimestampUTC = time.Now().UTC()
}
p := dq.partitionForMessage(value)
if p == nil {
err = fmt.Errorf("diskq; partition couldn't be resolved for message")
return
}
offset, err = p.Write(value)
if err != nil {
return
}
partition = p.index
return
}
// Vacuum deletes old segments from all partitions if retention is configured.
//
// Vacuum will operate on the partitions as they're found on disk; specifically the currently
// configured partition count is ignored in lieu of the extant partition list.
func (dq *Diskq) Vacuum() (err error) {
if dq.cfg.RetentionMaxAge == 0 && dq.cfg.RetentionMaxBytes == 0 {
return
}
var partitionIndexes []uint32
partitionIndexes, err = GetPartitions(dq.path)
if err != nil {
return
}
var partition *Partition
for _, partitionIndex := range partitionIndexes {
partition, err = openPartition(dq.path, dq.cfg, partitionIndex)
if err != nil {
return
}
if err = partition.Vacuum(); err != nil {
return
}
}
return
}
// Sync calls `fsync` on each of the partition file handles.
//
// This has the effect of realizing any buffered data to disk.
//
// You shouldn't ever need to call this, but it's here if you do need to.
func (dq *Diskq) Sync() error {
for _, p := range dq.partitions {
if err := p.Sync(); err != nil {
return err
}
}
return nil
}
// Close releases any resources associated with the diskq and
// removes the sentinel file.
func (dq *Diskq) Close() error {
for _, p := range dq.partitions {
_ = p.Close()
}
return dq.releaseSentinel()
}
//
// internal methods
//
func (dq *Diskq) writeSentinel() error {
sentinelPath := FormatPathForSentinel(dq.path)
sf, err := os.OpenFile(sentinelPath, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0644)
if err != nil {
return fmt.Errorf("diskq; write sentinel; cannot open file in exclusive mode: %w", err)
}
defer func() { _ = sf.Close() }()
_, err = sf.Write(dq.id[:])
if err != nil {
return fmt.Errorf("diskq; write sentinel; cannot write id to file: %w", err)
}
return nil
}
func (dq *Diskq) releaseSentinel() error {
sentinelPath := FormatPathForSentinel(dq.path)
return os.Remove(sentinelPath)
}
func (dq *Diskq) partitionForMessage(m Message) *Partition {
hashIndex := hashIndexForMessage(m, len(dq.partitions))
return dq.partitions[hashIndex]
}
func (dq *Diskq) writeSettings() error {
f, err := os.Create(FormatPathForSettings(dq.path))
if err != nil {
return fmt.Errorf("diskq; cannot create settings file: %w", err)
}
defer func() { _ = f.Close() }()
enc := json.NewEncoder(f)
enc.SetIndent("", " ")
if err = enc.Encode(dq.cfg); err != nil {
return fmt.Errorf("diskq; cannot encode settings file: %w", err)
}
return nil
}