-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
reconciler.go
164 lines (152 loc) · 4.8 KB
/
reconciler.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
// Copyright 2020 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
// Package ptreconcile provides logic to reconcile protected timestamp records
// with state associated with their metadata.
package ptreconcile
import (
"context"
"math/rand"
"time"
"github.com/cockroachdb/cockroach/pkg/kv"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/protectedts"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/protectedts/ptpb"
"github.com/cockroachdb/cockroach/pkg/settings"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/stop"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/errors"
)
// ReconcileInterval is the interval between two reconciliations of protected
// timestamp records.
var ReconcileInterval = settings.RegisterDurationSetting(
settings.TenantReadOnly,
"kv.protectedts.reconciliation.interval",
"the frequency for reconciling jobs with protected timestamp records",
5*time.Minute,
settings.NonNegativeDuration,
).WithPublic()
// StatusFunc is used to check on the status of a Record based on its Meta
// field.
type StatusFunc func(
ctx context.Context, txn *kv.Txn, meta []byte,
) (shouldRemove bool, _ error)
// StatusFuncs maps from MetaType to a StatusFunc.
type StatusFuncs map[string]StatusFunc
// Reconciler runs a loop to reconcile the protected timestamps with external
// state. Each record's status is determined using the record's meta type and
// meta in conjunction with the configured StatusFunc.
type Reconciler struct {
settings *cluster.Settings
db *kv.DB
pts protectedts.Storage
metrics Metrics
statusFuncs StatusFuncs
}
// New constructs a Reconciler.
func New(
st *cluster.Settings, db *kv.DB, storage protectedts.Storage, statusFuncs StatusFuncs,
) *Reconciler {
return &Reconciler{
settings: st,
db: db,
pts: storage,
metrics: makeMetrics(),
statusFuncs: statusFuncs,
}
}
// StartReconciler implements the protectedts.Reconciler interface.
func (r *Reconciler) StartReconciler(ctx context.Context, stopper *stop.Stopper) error {
return stopper.RunAsyncTask(ctx, "protectedts-reconciliation", func(ctx context.Context) {
r.run(ctx, stopper)
})
}
// Metrics returns the reconciler's metrics.
func (r *Reconciler) Metrics() *Metrics {
return &r.metrics
}
func (r *Reconciler) run(ctx context.Context, stopper *stop.Stopper) {
reconcileIntervalChanged := make(chan struct{}, 1)
ReconcileInterval.SetOnChange(&r.settings.SV, func(ctx context.Context) {
select {
case reconcileIntervalChanged <- struct{}{}:
default:
}
})
lastReconciled := time.Time{}
getInterval := func() time.Duration {
interval := ReconcileInterval.Get(&r.settings.SV)
const jitterFrac = .1
return time.Duration(float64(interval) * (1 + (rand.Float64()-.5)*jitterFrac))
}
timer := timeutil.NewTimer()
for {
timer.Reset(timeutil.Until(lastReconciled.Add(getInterval())))
select {
case <-timer.C:
timer.Read = true
r.reconcile(ctx)
lastReconciled = timeutil.Now()
case <-reconcileIntervalChanged:
// Go back around again.
case <-stopper.ShouldQuiesce():
return
case <-ctx.Done():
return
}
}
}
func (r *Reconciler) reconcile(ctx context.Context) {
// Load protected timestamp records.
var state ptpb.State
if err := r.db.Txn(ctx, func(ctx context.Context, txn *kv.Txn) error {
var err error
state, err = r.pts.GetState(ctx, txn)
return err
}); err != nil {
r.metrics.ReconciliationErrors.Inc(1)
log.Errorf(ctx, "failed to load protected timestamp records: %+v", err)
return
}
for _, rec := range state.Records {
task, ok := r.statusFuncs[rec.MetaType]
if !ok {
// NB: We don't expect to ever hit this case outside of testing.
continue
}
var didRemove bool
if err := r.db.Txn(ctx, func(ctx context.Context, txn *kv.Txn) (err error) {
didRemove = false // reset for retries
shouldRemove, err := task(ctx, txn, rec.Meta)
if err != nil {
return err
}
if !shouldRemove {
return nil
}
err = r.pts.Release(ctx, txn, rec.ID.GetUUID())
if err != nil && !errors.Is(err, protectedts.ErrNotExists) {
return err
}
didRemove = true
return nil
}); err != nil {
r.metrics.ReconciliationErrors.Inc(1)
log.Errorf(ctx, "failed to reconcile protected timestamp with id %s: %v",
rec.ID.String(), err)
} else {
r.metrics.RecordsProcessed.Inc(1)
if didRemove {
r.metrics.RecordsRemoved.Inc(1)
}
}
}
r.metrics.ReconcilationRuns.Inc(1)
}