-
Notifications
You must be signed in to change notification settings - Fork 2
/
ingester.go
276 lines (230 loc) · 7.6 KB
/
ingester.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
package sqlog
import (
"errors"
"log/slog"
"sync/atomic"
"time"
)
// ErrIngesterClosed is returned by methods of the `Client` interface
// when they are called after the Ingester has already been closed.
var ErrIngesterClosed = errors.New("[sqlog] the ingester was already closed")
// IngesterConfig contains configuration parameters for the ingester.
type IngesterConfig struct {
// Chunks is the size of the chunk buffer (default 3).
Chunks uint8
// ChunkSize defines the maximum number of log records per chunk before
// being persisted in storage (default|max 900).
ChunkSize uint16
// MaxChunkSizeBytes sets the maximum desired chunk size in bytes.
// If this size is exceeded, the chunk will be sent to storage (default 0).
MaxChunkSizeBytes int64
// MaxDirtyChunks specifies the maximum number of chunks with data in memory (default 50).
// This prevents unlimited memory consumption in the event of a catastrophic failure
// in writing logs.
MaxDirtyChunks int
// MaxFlushRetry defines the number of retry attempts to persist a chunk in case of failure (default 3).
MaxFlushRetry int32
// FlushAfterSec defines how long a chunk can remain inactive before being sent to storage (default 3 seconds).
FlushAfterSec int
// IntervalCheckMs sets the interval for chunk maintenance in milliseconds (default 100 ms).
IntervalCheckMs int32
}
// Ingester is the interface that represents the behavior of the log ingester.
type Ingester interface {
// Close terminates the ingester and flushes any remaining log data.
Close() (err error)
// Ingest adds a new log entry with the given timestamp, level, and content.
Ingest(t time.Time, level int8, content []byte) error
}
// ingester is the implementation of the Ingester interface, responsible for managing
// log chunks and ensuring they are flushed to storage.
type ingester struct {
flushChunk *Chunk // The chunk that will be saved to the database
writeChunk *Chunk // The chunk currently receiving log entries
flushChunkId int32 // ID of the currently active flush chunk
writeChunkId int32 // ID of the currently active write chunk
config *IngesterConfig // Configuration options for the ingester
storage Storage // The storage backend used to persist chunks
quit chan struct{} // Channel used to signal termination
shutdown chan struct{} // Channel used to signal shutdown completion
}
// NewIngester creates a new ingester with the given configuration and storage.
// If no configuration is provided, default values are used.
func NewIngester(config *IngesterConfig, storage Storage) (*ingester, error) {
if config == nil {
config = &IngesterConfig{}
}
// Set default values for config if necessary
if config.Chunks <= 0 {
config.Chunks = 3
}
if config.MaxDirtyChunks <= int(config.Chunks) {
config.MaxDirtyChunks = 50
}
if config.FlushAfterSec <= 0 {
config.FlushAfterSec = 3
}
if config.MaxFlushRetry <= 0 {
config.MaxFlushRetry = 3
}
if config.ChunkSize <= 0 || config.ChunkSize > 900 {
config.ChunkSize = 900
}
if config.IntervalCheckMs <= 0 {
config.IntervalCheckMs = 100
}
root := NewChunk(int32(config.ChunkSize))
root.Init(config.Chunks)
i := &ingester{
config: config,
writeChunkId: root.id,
flushChunk: root,
writeChunk: root,
storage: storage,
quit: make(chan struct{}),
shutdown: make(chan struct{}),
}
// Start the routine to regularly check chunk states
go i.routineCheck()
return i, nil
}
// Ingest adds a new log entry to the active write chunk. If the chunk becomes full,
// the ingester switches to a new chunk.
func (i *ingester) Ingest(t time.Time, level int8, content []byte) error {
lastWriteId := i.writeChunkId
chunk, isFull := i.writeChunk.Put(&Entry{t, level, content})
if isFull && atomic.CompareAndSwapInt32(&i.writeChunkId, lastWriteId, chunk.id) {
// The chunk is full, switch to the next one
i.writeChunk = chunk
}
return nil
}
// routineCheck is responsible for periodically checking the status of chunks,
// flushing them if necessary, and managing shutdown procedures.
func (i *ingester) routineCheck() {
defer close(i.shutdown)
d := time.Duration(i.config.IntervalCheckMs)
tick := time.NewTicker(d)
defer tick.Stop()
for {
select {
case <-tick.C:
// Perform a routine check of chunk states
i.doRoutineCheck()
tick.Reset(d)
case <-i.quit:
// Flush all pending logs when termination is requested
tick.Stop()
chunk := i.flushChunk
chunk.Lock()
t := 0
// Attempt to flush all chunks
for {
if chunk.Empty() {
break
}
if chunk.Ready() {
// If the chunk is ready to be written to storage, flush it
if err := i.storage.Flush(chunk); err != nil {
retries := atomic.AddInt32(&chunk.retries, 1)
slog.Error("[sqlog] error writing chunk", slog.Any("error", err))
// If retries exceed the limit, move to the next chunk
if retries > i.config.MaxFlushRetry {
chunk = chunk.Next()
chunk.Lock()
} else {
time.Sleep(10 * time.Millisecond)
}
} else {
chunk = chunk.Next()
chunk.Lock()
}
} else {
// Unexpected state, continue checking next chunk
t++
if t > 3 {
chunk = chunk.Next()
chunk.Lock()
continue
}
t = 0
chunk.Lock()
time.Sleep(2 * time.Millisecond)
}
}
i.flushChunk = chunk
atomic.StoreInt32(&i.flushChunkId, chunk.id)
// Close the storage after flushing all logs
if err := i.storage.Close(); err != nil {
slog.Warn(
"[sqlog] error closing storage",
slog.Any("error", err),
)
}
return
}
}
}
func (i *ingester) getFlushChunk() {
}
// doRoutineCheck handles the periodic maintenance of chunks, flushing them if they
// meet the conditions for size or age, and ensuring memory usage stays within limits.
func (i *ingester) doRoutineCheck() {
for {
chunk := i.flushChunk
if chunk.Empty() {
break
}
// Flush the chunk if it's ready to be persisted
if chunk.Ready() {
if err := i.storage.Flush(chunk); err != nil {
retries := atomic.AddInt32(&chunk.retries, 1)
slog.Error("[sqlog] error writing chunk", slog.Any("error", err))
if retries > i.config.MaxFlushRetry {
chunk.Init(i.config.Chunks + 1)
} else {
break
}
} else {
chunk.Init(i.config.Chunks + 1)
}
} else {
// If the chunk is inactive for too long or exceeds the size limit, prepare it for flushing
if int(chunk.TTL().Seconds()) > i.config.FlushAfterSec {
chunk.Lock() // Lock the chunk for flushing in the next routine
chunk.Init(i.config.Chunks)
} else if i.config.MaxChunkSizeBytes > 0 && chunk.Size() > i.config.MaxChunkSizeBytes {
chunk.Lock() // Lock the chunk for flushing in the next routine
chunk.Init(i.config.Chunks)
}
break
}
i.flushChunk = i.flushChunk.Next()
atomic.StoreInt32(&i.flushChunkId, i.flushChunk.id)
}
// Limit memory consumption by discarding old chunks if necessary
if !i.flushChunk.Empty() && i.flushChunk.Depth() > i.config.MaxDirtyChunks {
for {
if i.flushChunk.Depth() > i.config.MaxDirtyChunks {
i.flushChunk = i.flushChunk.Next()
atomic.StoreInt32(&i.flushChunkId, i.flushChunk.id)
} else {
break
}
}
i.flushChunk.Init(i.config.Chunks)
}
}
// Close flushes any pending log data and closes the storage.
func (i *ingester) Close() (err error) {
defer func() {
// Always recover, as a panic could be raised if `i.quit` was closed,
// indicating the method was called more than once.
if rec := recover(); rec != nil {
err = ErrIngesterClosed
}
}()
close(i.quit)
<-i.shutdown
return nil
}