-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathwal.go
381 lines (329 loc) · 9.48 KB
/
wal.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
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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package prometheusremotewriteexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/prometheusremotewriteexporter"
import (
"context"
"errors"
"fmt"
"path/filepath"
"sync"
"sync/atomic"
"time"
"github.com/gogo/protobuf/proto"
"github.com/prometheus/prometheus/prompb"
"github.com/tidwall/wal"
"go.uber.org/multierr"
"go.uber.org/zap"
)
type prweWAL struct {
wg sync.WaitGroup // wg waits for the go routines to finish.
mu sync.Mutex // mu protects the fields below.
wal *wal.Log
walConfig *WALConfig
walPath string
exportSink func(ctx context.Context, reqL []*prompb.WriteRequest) error
stopOnce sync.Once
stopChan chan struct{}
rNotify chan struct{}
rWALIndex *atomic.Uint64
wWALIndex *atomic.Uint64
}
const (
defaultWALBufferSize = 300
defaultWALTruncateFrequency = 1 * time.Minute
)
type WALConfig struct {
Directory string `mapstructure:"directory"`
BufferSize int `mapstructure:"buffer_size"`
TruncateFrequency time.Duration `mapstructure:"truncate_frequency"`
}
func (wc *WALConfig) bufferSize() int {
if wc.BufferSize > 0 {
return wc.BufferSize
}
return defaultWALBufferSize
}
func (wc *WALConfig) truncateFrequency() time.Duration {
if wc.TruncateFrequency > 0 {
return wc.TruncateFrequency
}
return defaultWALTruncateFrequency
}
func newWAL(walConfig *WALConfig, exportSink func(context.Context, []*prompb.WriteRequest) error) *prweWAL {
if walConfig == nil {
// There are cases for which the WAL can be disabled.
// TODO: Perhaps log that the WAL wasn't enabled.
return nil
}
return &prweWAL{
exportSink: exportSink,
walConfig: walConfig,
stopChan: make(chan struct{}),
rNotify: make(chan struct{}),
rWALIndex: &atomic.Uint64{},
wWALIndex: &atomic.Uint64{},
}
}
func (wc *WALConfig) createWAL() (*wal.Log, string, error) {
walPath := filepath.Join(wc.Directory, "prom_remotewrite")
log, err := wal.Open(walPath, &wal.Options{
SegmentCacheSize: wc.bufferSize(),
NoCopy: true,
})
if err != nil {
return nil, "", fmt.Errorf("prometheusremotewriteexporter: failed to open WAL: %w", err)
}
return log, walPath, nil
}
var (
errAlreadyClosed = errors.New("already closed")
errNilWAL = errors.New("wal is nil")
)
// retrieveWALIndices queries the WriteAheadLog for its current first and last indices.
func (prweWAL *prweWAL) retrieveWALIndices() (err error) {
prweWAL.mu.Lock()
defer prweWAL.mu.Unlock()
err = prweWAL.closeWAL()
if err != nil {
return err
}
log, walPath, err := prweWAL.walConfig.createWAL()
if err != nil {
return err
}
prweWAL.wal = log
prweWAL.walPath = walPath
rIndex, err := prweWAL.wal.FirstIndex()
if err != nil {
return fmt.Errorf("prometheusremotewriteexporter: failed to retrieve the first WAL index: %w", err)
}
prweWAL.rWALIndex.Store(rIndex)
wIndex, err := prweWAL.wal.LastIndex()
if err != nil {
return fmt.Errorf("prometheusremotewriteexporter: failed to retrieve the last WAL index: %w", err)
}
prweWAL.wWALIndex.Store(wIndex)
return nil
}
func (prweWAL *prweWAL) stop() error {
err := errAlreadyClosed
prweWAL.stopOnce.Do(func() {
close(prweWAL.stopChan)
prweWAL.wg.Wait()
err = prweWAL.closeWAL()
})
return err
}
// run begins reading from the WAL until prwe.stopChan is closed.
func (prweWAL *prweWAL) run(ctx context.Context) (err error) {
var logger *zap.Logger
logger, err = loggerFromContext(ctx)
if err != nil {
return
}
if err = prweWAL.retrieveWALIndices(); err != nil {
logger.Error("unable to start write-ahead log", zap.Error(err))
return
}
runCtx, cancel := context.WithCancel(ctx)
// Start the process of exporting but wait until the exporting has started.
waitUntilStartedCh := make(chan bool)
prweWAL.wg.Add(1)
go func() {
defer prweWAL.wg.Done()
defer cancel()
signalStart := func() { close(waitUntilStartedCh) }
for {
select {
case <-runCtx.Done():
return
case <-prweWAL.stopChan:
return
default:
err := prweWAL.continuallyPopWALThenExport(runCtx, signalStart)
signalStart = func() {}
if err != nil {
// log err
logger.Error("error processing WAL entries", zap.Error(err))
// Restart WAL
if errS := prweWAL.retrieveWALIndices(); errS != nil {
logger.Error("unable to re-start write-ahead log after error", zap.Error(errS))
return
}
}
}
}
}()
<-waitUntilStartedCh
return nil
}
// continuallyPopWALThenExport reads a prompb.WriteRequest proto encoded blob from the WAL, and moves
// the WAL's front index forward until either the read buffer period expires or the maximum
// buffer size is exceeded. When either of the two conditions are matched, it then exports
// the requests to the Remote-Write endpoint, and then truncates the head of the WAL to where
// it last read from.
func (prweWAL *prweWAL) continuallyPopWALThenExport(ctx context.Context, signalStart func()) (err error) {
var reqL []*prompb.WriteRequest
defer func() {
// Keeping it within a closure to ensure that the later
// updated value of reqL is always flushed to disk.
if errL := prweWAL.exportSink(ctx, reqL); errL != nil {
err = multierr.Append(err, errL)
}
}()
freshTimer := func() *time.Timer {
return time.NewTimer(prweWAL.walConfig.truncateFrequency())
}
timer := freshTimer()
defer func() {
// Added in a closure to ensure we capture the later
// updated value of timer when changed in the loop below.
timer.Stop()
}()
signalStart()
maxCountPerUpload := prweWAL.walConfig.bufferSize()
for {
select {
case <-ctx.Done():
return ctx.Err()
case <-prweWAL.stopChan:
return nil
default:
}
var req *prompb.WriteRequest
req, err = prweWAL.readPrompbFromWAL(ctx, prweWAL.rWALIndex.Load())
if err != nil {
return err
}
reqL = append(reqL, req)
var shouldExport bool
select {
case <-timer.C:
shouldExport = true
default:
shouldExport = len(reqL) >= maxCountPerUpload
}
if !shouldExport {
continue
}
// Otherwise, it is time to export, flush and then truncate the WAL, but also to kill the timer!
timer.Stop()
timer = freshTimer()
if err = prweWAL.exportThenFrontTruncateWAL(ctx, reqL); err != nil {
return err
}
// Reset but reuse the write requests slice.
reqL = reqL[:0]
}
}
func (prweWAL *prweWAL) closeWAL() error {
if prweWAL.wal != nil {
err := prweWAL.wal.Close()
prweWAL.wal = nil
return err
}
return nil
}
func (prweWAL *prweWAL) syncAndTruncateFront() error {
prweWAL.mu.Lock()
defer prweWAL.mu.Unlock()
if prweWAL.wal == nil {
return errNilWAL
}
// Save all the entries that aren't yet committed, to the tail of the WAL.
if err := prweWAL.wal.Sync(); err != nil {
return err
}
// Truncate the WAL from the front for the entries that we already
// read from the WAL and had already exported.
if err := prweWAL.wal.TruncateFront(prweWAL.rWALIndex.Load()); err != nil && !errors.Is(err, wal.ErrOutOfRange) {
return err
}
return nil
}
func (prweWAL *prweWAL) exportThenFrontTruncateWAL(ctx context.Context, reqL []*prompb.WriteRequest) error {
if len(reqL) == 0 {
return nil
}
if cErr := ctx.Err(); cErr != nil {
return nil
}
if errL := prweWAL.exportSink(ctx, reqL); errL != nil {
return errL
}
if err := prweWAL.syncAndTruncateFront(); err != nil {
return err
}
// Reset by retrieving the respective read and write WAL indices.
return prweWAL.retrieveWALIndices()
}
// persistToWAL is the routine that'll be hooked into the exporter's receiving side and it'll
// write them to the Write-Ahead-Log so that shutdowns won't lose data, and that the routine that
// reads from the WAL can then process the previously serialized requests.
func (prweWAL *prweWAL) persistToWAL(requests []*prompb.WriteRequest) error {
prweWAL.mu.Lock()
defer prweWAL.mu.Unlock()
// Write all the requests to the WAL in a batch.
batch := new(wal.Batch)
for _, req := range requests {
protoBlob, err := proto.Marshal(req)
if err != nil {
return err
}
wIndex := prweWAL.wWALIndex.Add(1)
batch.Write(wIndex, protoBlob)
}
// Notify reader go routine that is possibly waiting for writes.
select {
case prweWAL.rNotify <- struct{}{}:
default:
}
return prweWAL.wal.WriteBatch(batch)
}
func (prweWAL *prweWAL) readPrompbFromWAL(ctx context.Context, index uint64) (wreq *prompb.WriteRequest, err error) {
var protoBlob []byte
for i := 0; i < 12; i++ {
// Firstly check if we've been terminated, then exit if so.
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-prweWAL.stopChan:
return nil, fmt.Errorf("attempt to read from WAL after stopped")
default:
}
if index <= 0 {
index = 1
}
prweWAL.mu.Lock()
if prweWAL.wal == nil {
return nil, fmt.Errorf("attempt to read from closed WAL")
}
protoBlob, err = prweWAL.wal.Read(index)
if err == nil { // The read succeeded.
req := new(prompb.WriteRequest)
if err = proto.Unmarshal(protoBlob, req); err != nil {
return nil, err
}
// Now increment the WAL's read index.
prweWAL.rWALIndex.Add(1)
prweWAL.mu.Unlock()
return req, nil
}
prweWAL.mu.Unlock()
// If WAL was empty, let's wait for a notification from
// the writer go routine.
if errors.Is(err, wal.ErrNotFound) {
select {
case <-prweWAL.rNotify:
case <-ctx.Done():
return nil, ctx.Err()
case <-prweWAL.stopChan:
return nil, fmt.Errorf("attempt to read from WAL after stopped")
}
}
if !errors.Is(err, wal.ErrNotFound) {
return nil, err
}
}
return nil, err
}