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
|
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.",
})
gcExpiredRelationshipsCounter = prometheus.NewCounter(prometheus.CounterOpts{
Namespace: "spicedb",
Subsystem: "datastore",
Name: "gc_expired_relationships_total",
Help: "The number of expired 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 returns true if a garbage collection run has been completed.
HasGCRun() bool
// MarkGCCompleted marks that a garbage collection run has been completed.
MarkGCCompleted()
// ResetGCCompleted resets the state of the garbage collection run.
ResetGCCompleted()
// LockForGCRun attempts to acquire a lock for garbage collection. This lock
// is typically done at the datastore level, to ensure that no other nodes are
// running garbage collection at the same time.
LockForGCRun(ctx context.Context) (bool, error)
// UnlockAfterGCRun releases the lock after a garbage collection run.
// NOTE: this method does not take a context, as the context used for the
// reset of the GC run can be canceled/timed out and the unlock will still need to happen.
UnlockAfterGCRun() error
// ReadyState returns the current state of the datastore.
ReadyState(context.Context) (datastore.ReadyState, error)
// Now returns the current time from the datastore.
Now(context.Context) (time.Time, error)
// TxIDBefore returns the highest transaction ID before the provided time.
TxIDBefore(context.Context, time.Time) (datastore.Revision, error)
// DeleteBeforeTx deletes all data before the provided transaction ID.
DeleteBeforeTx(ctx context.Context, txID datastore.Revision) (DeletionCounts, error)
// DeleteExpiredRels deletes all relationships that have expired.
DeleteExpiredRels(ctx context.Context) (int64, 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
backoffInterval.Reset()
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()
nextInterval = interval
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
}
ok, err := gc.LockForGCRun(ctx)
if err != nil {
return fmt.Errorf("error locking for gc run: %w", err)
}
if !ok {
log.Info().
Msg("datastore garbage collection already in progress on another node")
return nil
}
defer func() {
err := gc.UnlockAfterGCRun()
if err != nil {
log.Error().
Err(err).
Msg("error unlocking after gc run")
}
}()
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)
expiredRelationshipsCount, eerr := gc.DeleteExpiredRels(ctx)
// even if an error happened, garbage would have been collected. This makes sure these are reflected even if the
// worker eventually fails or times out.
gcRelationshipsCounter.Add(float64(collected.Relationships))
gcTransactionsCounter.Add(float64(collected.Transactions))
gcNamespacesCounter.Add(float64(collected.Namespaces))
gcExpiredRelationshipsCounter.Add(float64(expiredRelationshipsCount))
collectionDuration := time.Since(startTime)
gcDurationHistogram.Observe(collectionDuration.Seconds())
if err != nil {
return fmt.Errorf("error deleting in gc: %w", err)
}
if eerr != nil {
return fmt.Errorf("error deleting expired relationships in gc: %w", eerr)
}
log.Ctx(ctx).Info().
Stringer("highestTxID", watermark).
Dur("duration", collectionDuration).
Time("nowTime", now).
Interface("collected", collected).
Int64("expiredRelationships", expiredRelationshipsCount).
Msg("datastore garbage collection completed successfully")
gc.MarkGCCompleted()
return nil
}
|