/
wal.go
210 lines (178 loc) · 5.1 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
package ingester
import (
"flag"
"sync"
"time"
"github.com/cortexproject/cortex/pkg/util"
"github.com/go-kit/kit/log/level"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/prometheus/tsdb/wal"
"github.com/grafana/loki/pkg/logproto"
)
var (
// shared pool for WALRecords and []logproto.Entries
recordPool = newRecordPool()
)
const walSegmentSize = wal.DefaultSegmentSize * 4
type WALConfig struct {
Enabled bool `yaml:"enabled"`
Dir string `yaml:"dir"`
Recover bool `yaml:"recover"`
CheckpointDuration time.Duration `yaml:"checkpoint_duration"`
FlushOnShutdown bool `yaml:"flush_on_shutdown"`
}
func (cfg *WALConfig) Validate() error {
if cfg.Enabled && cfg.CheckpointDuration < 1 {
return errors.Errorf("invalid checkpoint duration: %v", cfg.CheckpointDuration)
}
return nil
}
// RegisterFlags adds the flags required to config this to the given FlagSet
func (cfg *WALConfig) RegisterFlags(f *flag.FlagSet) {
f.StringVar(&cfg.Dir, "ingester.wal-dir", "wal", "Directory to store the WAL and/or recover from WAL.")
f.BoolVar(&cfg.Enabled, "ingester.wal-enabled", false, "Enable writing of ingested data into WAL.")
f.BoolVar(&cfg.Recover, "ingester.recover-from-wal", false, "Recover data from existing WAL irrespective of WAL enabled/disabled.")
f.DurationVar(&cfg.CheckpointDuration, "ingester.checkpoint-duration", 5*time.Minute, "Interval at which checkpoints should be created.")
f.BoolVar(&cfg.FlushOnShutdown, "ingester.flush-on-shutdown", false, "When WAL is enabled, should chunks be flushed to long-term storage on shutdown.")
}
// WAL interface allows us to have a no-op WAL when the WAL is disabled.
type WAL interface {
// Log marshalls the records and writes it into the WAL.
Log(*WALRecord) error
// Stop stops all the WAL operations.
Stop() error
}
type noopWAL struct{}
func (noopWAL) Log(*WALRecord) error { return nil }
func (noopWAL) Stop() error { return nil }
type walWrapper struct {
cfg WALConfig
wal *wal.WAL
metrics *ingesterMetrics
seriesIter SeriesIter
wait sync.WaitGroup
quit chan struct{}
}
// newWAL creates a WAL object. If the WAL is disabled, then the returned WAL is a no-op WAL.
func newWAL(cfg WALConfig, registerer prometheus.Registerer, metrics *ingesterMetrics, seriesIter SeriesIter) (WAL, error) {
if !cfg.Enabled {
return noopWAL{}, nil
}
tsdbWAL, err := wal.NewSize(util.Logger, registerer, cfg.Dir, walSegmentSize, false)
if err != nil {
return nil, err
}
w := &walWrapper{
cfg: cfg,
quit: make(chan struct{}),
wal: tsdbWAL,
metrics: metrics,
seriesIter: seriesIter,
}
w.wait.Add(1)
go w.run()
return w, nil
}
func (w *walWrapper) Log(record *WALRecord) error {
if record == nil || (len(record.Series) == 0 && len(record.RefEntries) == 0) {
return nil
}
select {
case <-w.quit:
return nil
default:
buf := recordPool.GetBytes()[:0]
defer func() {
recordPool.PutBytes(buf)
}()
// Always write series then entries.
if len(record.Series) > 0 {
buf = record.encodeSeries(buf)
if err := w.wal.Log(buf); err != nil {
return err
}
w.metrics.walRecordsLogged.Inc()
w.metrics.walLoggedBytesTotal.Add(float64(len(buf)))
buf = buf[:0]
}
if len(record.RefEntries) > 0 {
buf = record.encodeEntries(buf)
if err := w.wal.Log(buf); err != nil {
return err
}
w.metrics.walRecordsLogged.Inc()
w.metrics.walLoggedBytesTotal.Add(float64(len(buf)))
}
return nil
}
}
func (w *walWrapper) Stop() error {
close(w.quit)
w.wait.Wait()
err := w.wal.Close()
level.Info(util.Logger).Log("msg", "stopped", "component", "wal")
return err
}
func (w *walWrapper) checkpointWriter() *WALCheckpointWriter {
return &WALCheckpointWriter{
metrics: w.metrics,
segmentWAL: w.wal,
}
}
func (w *walWrapper) run() {
level.Info(util.Logger).Log("msg", "started", "component", "wal")
defer w.wait.Done()
checkpointer := NewCheckpointer(
w.cfg.CheckpointDuration,
w.seriesIter,
w.checkpointWriter(),
w.metrics,
w.quit,
)
checkpointer.Run()
}
type resettingPool struct {
rPool *sync.Pool // records
ePool *sync.Pool // entries
bPool *sync.Pool // bytes
}
func (p *resettingPool) GetRecord() *WALRecord {
rec := p.rPool.Get().(*WALRecord)
rec.Reset()
return rec
}
func (p *resettingPool) PutRecord(r *WALRecord) {
p.rPool.Put(r)
}
func (p *resettingPool) GetEntries() []logproto.Entry {
return p.ePool.Get().([]logproto.Entry)
}
func (p *resettingPool) PutEntries(es []logproto.Entry) {
p.ePool.Put(es[:0]) // nolint:staticcheck
}
func (p *resettingPool) GetBytes() []byte {
return p.bPool.Get().([]byte)
}
func (p *resettingPool) PutBytes(b []byte) {
p.bPool.Put(b[:0]) // nolint:staticcheck
}
func newRecordPool() *resettingPool {
return &resettingPool{
rPool: &sync.Pool{
New: func() interface{} {
return &WALRecord{}
},
},
ePool: &sync.Pool{
New: func() interface{} {
return make([]logproto.Entry, 0, 512)
},
},
bPool: &sync.Pool{
New: func() interface{} {
return make([]byte, 0, 1<<10) // 1kb
},
},
}
}