-
Notifications
You must be signed in to change notification settings - Fork 246
/
gc.go
201 lines (171 loc) · 5.91 KB
/
gc.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
package common
import (
"context"
"fmt"
"time"
"github.com/cenkalti/backoff/v4"
"github.com/prometheus/client_golang/prometheus"
"github.com/rs/zerolog"
log "github.com/authzed/spicedb/internal/logging"
"github.com/authzed/spicedb/pkg/datastore"
)
var (
gcDurationHistogram = prometheus.NewHistogram(prometheus.HistogramOpts{
Namespace: "spicedb",
Subsystem: "datastore",
Name: "gc_duration_seconds",
Help: "The duration of datastore garbage collection.",
Buckets: []float64{0.01, 0.1, 0.5, 1, 5, 10, 25, 60, 120},
})
gcRelationshipsCounter = prometheus.NewCounter(prometheus.CounterOpts{
Namespace: "spicedb",
Subsystem: "datastore",
Name: "gc_relationships_total",
Help: "The number of stale relationships deleted by the datastore garbage collection.",
})
gcTransactionsCounter = prometheus.NewCounter(prometheus.CounterOpts{
Namespace: "spicedb",
Subsystem: "datastore",
Name: "gc_transactions_total",
Help: "The number of stale transactions deleted by the datastore garbage collection.",
})
gcNamespacesCounter = prometheus.NewCounter(prometheus.CounterOpts{
Namespace: "spicedb",
Subsystem: "datastore",
Name: "gc_namespaces_total",
Help: "The number of stale namespaces deleted by the datastore garbage collection.",
})
gcFailureCounterConfig = prometheus.CounterOpts{
Namespace: "spicedb",
Subsystem: "datastore",
Name: "gc_failure_total",
Help: "The number of failed runs of the datastore garbage collection.",
}
gcFailureCounter = prometheus.NewCounter(gcFailureCounterConfig)
)
// RegisterGCMetrics registers garbage collection metrics to the default
// registry.
func RegisterGCMetrics() error {
for _, metric := range []prometheus.Collector{
gcDurationHistogram,
gcRelationshipsCounter,
gcTransactionsCounter,
gcNamespacesCounter,
gcFailureCounter,
} {
if err := prometheus.Register(metric); err != nil {
return err
}
}
return nil
}
// GarbageCollector represents any datastore that supports external garbage
// collection.
type GarbageCollector interface {
HasGCRun() bool
MarkGCCompleted()
ResetGCCompleted()
ReadyState(context.Context) (datastore.ReadyState, error)
Now(context.Context) (time.Time, error)
TxIDBefore(context.Context, time.Time) (datastore.Revision, error)
DeleteBeforeTx(ctx context.Context, txID datastore.Revision) (DeletionCounts, error)
}
// DeletionCounts tracks the amount of deletions that occurred when calling
// DeleteBeforeTx.
type DeletionCounts struct {
Relationships int64
Transactions int64
Namespaces int64
}
func (g DeletionCounts) MarshalZerologObject(e *zerolog.Event) {
e.
Int64("relationships", g.Relationships).
Int64("transactions", g.Transactions).
Int64("namespaces", g.Namespaces)
}
var MaxGCInterval = 60 * time.Minute
// StartGarbageCollector loops forever until the context is canceled and
// performs garbage collection on the provided interval.
func StartGarbageCollector(ctx context.Context, gc GarbageCollector, interval, window, timeout time.Duration) error {
return startGarbageCollectorWithMaxElapsedTime(ctx, gc, interval, window, 0, timeout, gcFailureCounter)
}
func startGarbageCollectorWithMaxElapsedTime(ctx context.Context, gc GarbageCollector, interval, window, maxElapsedTime, timeout time.Duration, failureCounter prometheus.Counter) error {
backoffInterval := backoff.NewExponentialBackOff()
backoffInterval.InitialInterval = interval
backoffInterval.MaxInterval = max(MaxGCInterval, interval)
backoffInterval.MaxElapsedTime = maxElapsedTime
nextInterval := interval
log.Ctx(ctx).Info().
Dur("interval", nextInterval).
Msg("datastore garbage collection worker started")
for {
select {
case <-ctx.Done():
log.Ctx(ctx).Info().
Msg("shutting down datastore garbage collection worker")
return ctx.Err()
case <-time.After(nextInterval):
log.Ctx(ctx).Info().
Dur("interval", nextInterval).
Dur("window", window).
Dur("timeout", timeout).
Msg("running garbage collection worker")
err := RunGarbageCollection(gc, window, timeout)
if err != nil {
failureCounter.Inc()
nextInterval = backoffInterval.NextBackOff()
log.Ctx(ctx).Warn().Err(err).
Dur("next-attempt-in", nextInterval).
Msg("error attempting to perform garbage collection")
continue
}
backoffInterval.Reset()
log.Ctx(ctx).Debug().
Dur("next-run-in", interval).
Msg("datastore garbage collection scheduled for next run")
}
}
}
// RunGarbageCollection runs garbage collection for the datastore.
func RunGarbageCollection(gc GarbageCollector, window, timeout time.Duration) error {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
ctx, span := tracer.Start(ctx, "RunGarbageCollection")
defer span.End()
// Before attempting anything, check if the datastore is ready.
startTime := time.Now()
ready, err := gc.ReadyState(ctx)
if err != nil {
return err
}
if !ready.IsReady {
log.Ctx(ctx).Warn().
Msgf("datastore wasn't ready when attempting garbage collection: %s", ready.Message)
return nil
}
now, err := gc.Now(ctx)
if err != nil {
return fmt.Errorf("error retrieving now: %w", err)
}
watermark, err := gc.TxIDBefore(ctx, now.Add(-1*window))
if err != nil {
return fmt.Errorf("error retrieving watermark: %w", err)
}
collected, err := gc.DeleteBeforeTx(ctx, watermark)
if err != nil {
return fmt.Errorf("error deleting in gc: %w", err)
}
collectionDuration := time.Since(startTime)
log.Ctx(ctx).Info().
Stringer("highestTxID", watermark).
Dur("duration", collectionDuration).
Time("nowTime", now).
Interface("collected", collected).
Msg("datastore garbage collection completed successfully")
gcDurationHistogram.Observe(collectionDuration.Seconds())
gcRelationshipsCounter.Add(float64(collected.Relationships))
gcTransactionsCounter.Add(float64(collected.Transactions))
gcNamespacesCounter.Add(float64(collected.Namespaces))
gc.MarkGCCompleted()
return nil
}