Repository navigation
Expand file tree
/
Copy pathretention.go
More file actions
390 lines (315 loc) · 10.4 KB
/
Copy pathretention.go
File metadata and controls
390 lines (315 loc) · 10.4 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
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
382
383
384
385
386
387
388
389
390
package main
import (
"context"
"encoding/json"
"fmt"
"log/slog"
"path/filepath"
"strings"
"sync"
"time"
"github.com/google/uuid"
"github.com/fclairamb/dbbat/internal/api"
"github.com/fclairamb/dbbat/internal/archive"
"github.com/fclairamb/dbbat/internal/config"
"github.com/fclairamb/dbbat/internal/dump"
"github.com/fclairamb/dbbat/internal/safe"
"github.com/fclairamb/dbbat/internal/store"
)
const (
// retentionSweepInterval is the backstop pass: it covers what events do not
// (a replica that died before its timer fired, statements of sessions still
// open, a window set through another replica).
retentionSweepInterval = 1 * time.Hour
// retentionMinPassGap is the resolution of the windows: passes are at least
// this far apart, so a storm of closes costs one pass a second.
retentionMinPassGap = 1 * time.Second
// retentionTimerHorizon: a close arms a timer only when the nearest window of
// its server is under this; longer windows are served by the hourly pass.
retentionTimerHorizon = 1 * time.Hour
)
// goroutineNameRetentionSweep is what a panic in one pass is logged under.
const goroutineNameRetentionSweep = "retention sweep"
// retentionSweeper deletes (and archives) history past its retention windows.
//
// One per process. It runs a pass when it is woken: at startup, after a
// connection close, after a policy edit, and once an hour. A wake is a
// non-blocking send on a channel of size 1, so any number of closes during a
// pass collapse into one more pass and two passes never run at once.
//
// With no window set at any level it stays idle: no close wakes it and no pass
// reads a session. It still has to exist, because a window can be set from the
// UI while the process runs.
type retentionSweeper struct {
store *store.Store
cfg *config.Config
archiver *archive.Archiver
logger *slog.Logger
wakeCh chan struct{}
stop chan struct{}
stopOnce sync.Once
// active is whether any window is set anywhere (env, parameter, group,
// server). Refreshed at startup, after a policy edit and hourly.
activeMu sync.RWMutex
active bool
}
// startRetention opens the archive bucket (nil when DBB_ARCHIVE_URL is unset,
// the default: archival does not exist) and starts the one retention sweeper of
// the process. The sweeper always exists: a window can be set from the UI while
// the process runs.
func startRetention(
ctx context.Context,
cfg *config.Config,
dataStore *store.Store,
apiServer *api.Server,
logger *slog.Logger,
) (*retentionSweeper, error) {
archiver, err := archive.Open(ctx, cfg.ArchiveURL, dataStore, logger)
if err != nil {
return nil, fmt.Errorf("failed to open the archive: %w", err)
}
apiServer.SetArchive(archiver)
// After an hour of failing passes the archiver calls this once per outage.
if archiver != nil {
if notifier := apiServer.Notifier(); notifier != nil {
archiver.OnPersistentFailure = func(cause error) {
notifier.NotifyArchiveFailure(context.WithoutCancel(ctx), cause)
}
}
}
sweeper := startRetentionSweeper(ctx, cfg, dataStore, archiver, logger)
apiServer.SetRetentionWaker(sweeper.Wake)
return sweeper, nil
}
// startRetentionSweeper starts the sweeper. It always returns a sweeper.
func startRetentionSweeper(
ctx context.Context,
cfg *config.Config,
dataStore *store.Store,
archiver *archive.Archiver,
logger *slog.Logger,
) *retentionSweeper {
windows := cfg.RetentionWindows()
if windows.Misconfiguration != "" {
logger.WarnContext(ctx, "Invalid retention settings, nothing is deleted",
slog.String("problem", windows.Misconfiguration))
}
if cfg.Retention.Queries.ArchiveAfter != "" && cfg.ArchiveURL == "" {
logger.WarnContext(ctx, "DBB_RETENTION_QUERIES_ARCHIVE_AFTER is ignored: DBB_ARCHIVE_URL is not set")
}
s := &retentionSweeper{
store: dataStore,
cfg: cfg,
archiver: archiver,
logger: logger,
wakeCh: make(chan struct{}, 1),
stop: make(chan struct{}),
}
dataStore.SetConnectionClosedHook(s.onConnectionClosed)
go s.run(ctx)
logger.InfoContext(ctx, "Retention sweeper started",
slog.Duration("queries_delete_after", windows.Queries),
slog.Duration("queries_archive_after", windows.QueriesArchive),
slog.Duration("connections_delete_after", windows.Connections),
slog.Duration("dumps_delete_after", windows.Dumps),
slog.Bool("archive_enabled", archiver != nil))
return s
}
// Wake asks for a pass. Never blocks.
func (s *retentionSweeper) Wake() {
select {
case s.wakeCh <- struct{}{}:
default:
}
}
// onConnectionClosed wakes the sweeper after a close, and arms a timer at the
// nearest window of that session's server when it is under an hour: the session
// that just closed is due in archive_after, not now, and nothing else may close
// in between.
func (s *retentionSweeper) onConnectionClosed(serverUID uuid.UUID, _ time.Time) {
if !s.isActive() {
return
}
s.Wake()
go safe.RunMaintenance(context.Background(), s.logger, goroutineNameRetentionSweep, func() {
s.armTimer(context.Background(), serverUID)
})
}
func (s *retentionSweeper) armTimer(ctx context.Context, serverUID uuid.UUID) {
instance, err := s.store.InstanceRetention(ctx)
if err != nil {
return
}
resolved, err := s.store.ResolveRetentionByServers(ctx, []uuid.UUID{serverUID}, instance)
if err != nil {
return
}
r := resolved[serverUID]
nearest := time.Duration(0)
for _, d := range []time.Duration{r.Archive.Duration, r.Delete.Duration} {
if d > 0 && (nearest == 0 || d < nearest) {
nearest = d
}
}
if nearest > 0 && nearest < retentionTimerHorizon {
time.AfterFunc(nearest+retentionMinPassGap, s.Wake)
}
}
func (s *retentionSweeper) isActive() bool {
s.activeMu.RLock()
defer s.activeMu.RUnlock()
return s.active
}
// refreshActive re-reads whether any window is set anywhere.
func (s *retentionSweeper) refreshActive(ctx context.Context) bool {
w := s.cfg.RetentionWindows()
active := w.Queries > 0 || w.Connections > 0 || w.QueriesArchive > 0
if !active {
set, err := s.store.AnyRetentionPolicySet(ctx)
if err != nil {
s.logger.WarnContext(ctx, "failed to read the retention policy", slog.Any("error", err))
return s.isActive()
}
active = set
}
s.activeMu.Lock()
s.active = active
s.activeMu.Unlock()
return active
}
func (s *retentionSweeper) run(ctx context.Context) {
ticker := time.NewTicker(retentionSweepInterval)
defer ticker.Stop()
s.pass(ctx)
for {
select {
case <-s.wakeCh:
// Passes are at least a second apart.
select {
case <-time.After(retentionMinPassGap):
case <-s.stop:
return
}
s.pass(ctx)
case <-ticker.C:
s.pass(ctx)
case <-s.stop:
return
case <-ctx.Done():
return
}
}
}
// pass is one sweep, under safe.RunMaintenance: a panic must not take the
// process down and must not retire retention for its lifetime either.
func (s *retentionSweeper) pass(ctx context.Context) {
safe.RunMaintenance(ctx, s.logger, goroutineNameRetentionSweep, func() {
s.sweep(ctx)
})
}
func (s *retentionSweeper) sweep(ctx context.Context) {
windows := s.cfg.RetentionWindows()
s.sweepDumps(ctx, windows)
if !s.refreshActive(ctx) {
return
}
// 1. Archive due connections. 2. Delete due archives.
more := false
if s.archiver != nil {
more = s.sweepArchive(ctx)
}
// 3, 4. Delete due statements and ledger rows, per resolved window.
instance, err := s.store.InstanceRetention(ctx)
if err != nil {
s.logger.ErrorContext(ctx, "retention sweep failed", slog.Any("error", err))
return
}
if s.archiver == nil {
instance.Archive = store.RetentionWindow{}
}
result, err := s.store.CleanupRetention(ctx, store.SweepPolicy{
Instance: instance,
Connections: windows.Connections,
ArchiveEnabled: s.archiver != nil,
})
if err != nil {
s.logger.ErrorContext(ctx, "retention sweep failed",
slog.Any("error", err),
slog.Int64("queries_deleted", result.Queries),
slog.Int64("connections_deleted", result.Connections))
return
}
if result.Queries > 0 || result.Connections > 0 {
s.logger.InfoContext(ctx, "deleted history past retention",
slog.Int64("queries", result.Queries),
slog.Int64("connections", result.Connections))
}
if more {
s.Wake()
}
}
// sweepArchive runs the archive phases and reports whether a bound stopped them.
func (s *retentionSweeper) sweepArchive(ctx context.Context) bool {
res, err := s.archiver.Pass(ctx)
if err != nil {
s.logger.ErrorContext(ctx, "archive pass failed", slog.Any("error", err))
}
if res.Archived > 0 || res.Deleted > 0 {
s.logger.InfoContext(ctx, "archive pass",
slog.Int("archived", res.Archived),
slog.Int("archives_deleted", res.Deleted),
slog.Bool("bounded", res.Bounded))
}
return res.Bounded
}
// sweepDumps deletes local capture files past their window.
func (s *retentionSweeper) sweepDumps(ctx context.Context, w config.RetentionWindows) {
if s.cfg.Dump.Dir == "" || w.Dumps <= 0 {
return
}
removed, err := dump.CleanupOldFilesDetailed(s.cfg.Dump.Dir, w.Dumps)
if err != nil {
s.logger.ErrorContext(ctx, "dump cleanup failed", slog.Any("error", err))
return
}
if len(removed) == 0 {
return
}
s.logger.InfoContext(ctx, "dump cleanup", slog.Int("deleted", len(removed)))
if err := s.store.LogAuditEvents(ctx, dumpDeletedEvents(removed)...); err != nil {
s.logger.ErrorContext(ctx, "failed to record the expired captures", slog.Any("error", err))
}
}
// dumpDeletedEvents is one dump.deleted entry per expired capture, source
// "retention" and no actor. Files whose name is not a connection UID (legacy
// leftovers) are named by file rather than by UID.
func dumpDeletedEvents(removed []dump.RemovedFile) []*store.AuditEvent {
events := make([]*store.AuditEvent, 0, len(removed))
for _, f := range removed {
details := map[string]any{
"source": "retention",
"locations": []string{"local"},
"size_bytes": f.Size,
"file": f.Name,
}
base := strings.TrimSuffix(f.Name, filepath.Ext(f.Name))
if uid, err := uuid.Parse(base); err == nil {
details["connection_uid"] = uid
}
raw, err := json.Marshal(details)
if err != nil {
continue
}
events = append(events, &store.AuditEvent{EventType: store.AuditEventDumpDeleted, Details: raw})
}
return events
}
// Shutdown stops the sweeper. It never waits for an in-flight pass: the batched
// deletes make progress row by row, so an interrupted run resumes at the next
// start.
func (s *retentionSweeper) Shutdown(context.Context) error {
s.stopOnce.Do(func() {
close(s.stop)
})
return nil
}