/
group.go
319 lines (294 loc) · 9.88 KB
/
group.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
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
// Copyright 2024 The Cockroach Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
package checkpoint
import (
"context"
"time"
"github.com/cockroachdb/cdc-sink/internal/types"
"github.com/cockroachdb/cdc-sink/internal/util/hlc"
"github.com/cockroachdb/cdc-sink/internal/util/notify"
"github.com/cockroachdb/cdc-sink/internal/util/retry"
"github.com/cockroachdb/cdc-sink/internal/util/stopper"
"github.com/cockroachdb/cdc-sink/internal/util/stopvar"
"github.com/jackc/pgx/v5"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
)
// The secondary index allows us to find the last-known checkpoint
// timestamp for a target schema.
const schema = `
CREATE TABLE IF NOT EXISTS %[1]s (
target_schema STRING NOT NULL,
source_nanos INT NOT NULL,
source_logical INT NOT NULL,
first_seen TIMESTAMPTZ NOT NULL DEFAULT now(),
target_applied_at TIMESTAMPTZ,
source_wall_time TIMESTAMPTZ AS (to_timestamp(source_nanos::FLOAT8 / 1e9)) VIRTUAL,
PRIMARY KEY (target_schema, source_nanos, source_logical),
INDEX (target_schema, source_nanos DESC, source_logical DESC)
)`
// Used for v21.X and v22.1 that don't support to_timestamp().
const schemaNoTimestamp = `
CREATE TABLE IF NOT EXISTS %[1]s (
target_schema STRING NOT NULL,
source_nanos INT NOT NULL,
source_logical INT NOT NULL,
first_seen TIMESTAMPTZ NOT NULL DEFAULT now(),
target_applied_at TIMESTAMPTZ,
PRIMARY KEY (target_schema, source_nanos, source_logical),
INDEX (target_schema, source_nanos DESC, source_logical DESC)
)`
// Group provides durable storage of the checkpoint (FKA resolved)
// timestamps associated with a [types.TableGroup]. Timestamps to be
// processed are recorded with [Group.Advance] and their completion is
// recorded via [Group.Commit]. This Group type will collaborate with
// other components by driving a [notify.Var] containing an [hlc.Range].
// This range represents a window of eligible checkpoints that require
// processing, where the minimum is called "committed" and the maximum
// is called "proposed".
type Group struct {
bounds *notify.Var[hlc.Range]
pool *types.StagingPool
target *types.TableGroup
fastWakeup notify.Var[struct{}]
metrics struct {
advanceDuration prometheus.Observer
backwards prometheus.Counter
commitDuration prometheus.Observer
committedAge prometheus.Gauge
committedTime prometheus.Gauge
proposedAge prometheus.Gauge
proposedTime prometheus.Gauge
refreshDuration prometheus.Observer
}
sql struct {
mark string
record string
refresh string
}
}
// This query conditionally inserts a new mark for a target schema if
// there is no previous mark or if the proposed mark is equal to or
// after the latest-known mark for the target schema.
//
// $1 = target_schema
// $2 = source_nanos
// $3 = source_logical
const advanceTemplate = `
WITH
not_before AS (
SELECT source_nanos, source_logical FROM %[1]s
WHERE target_schema=$1
ORDER BY source_nanos desc, source_logical desc
FOR UPDATE
LIMIT 1),
to_insert AS (
SELECT $1::STRING, $2::INT, $3::INT
WHERE (SELECT count(*) FROM not_before) = 0
OR ($2::INT, $3::INT) >= (SELECT (source_nanos, source_logical) FROM not_before))
UPSERT INTO %[1]s (target_schema, source_nanos, source_logical)
SELECT * FROM to_insert`
// Advance extends the proposed checkpoint timestamp associated with the
// Group. It is an error if the timestamp does not advance beyond its
// current point, as this will indicate a violation of changefeed
// invariants. If successful, this method will asynchronously refresh
// the Group.
func (r *Group) Advance(ctx context.Context, ts hlc.Time) error {
start := time.Now()
tag, err := r.pool.Exec(ctx,
r.sql.mark,
r.target.Name.Canonical().Raw(),
ts.Nanos(),
ts.Logical(),
)
if err != nil {
return errors.WithStack(err)
}
if tag.RowsAffected() == 0 {
r.metrics.backwards.Inc()
return errors.Errorf(
"proposed checkpoint timestamp for %s is going backwards %s; "+
"verify changefeed cursor or remove already-applied "+
"checkpoint timestamp entries",
r.target, ts)
}
r.Refresh()
r.metrics.advanceDuration.Observe(time.Since(start).Seconds())
log.WithFields(log.Fields{
"checkpoint": ts,
"group": r.target,
}).Trace("advanced checkpoint timestamp")
return nil
}
const applyTemplate = `
UPDATE %s
SET target_applied_at = now()
WHERE target_schema = $1
AND (source_nanos, source_logical) >= ($2, $3) AND (source_nanos, source_logical) < ($4, $5)
AND target_applied_at IS NULL
`
// Commit updates the applied-at timestamp associated with the
// checkpoints in the open range [min,max). This will asynchronously
// refresh the Group.
func (r *Group) Commit(ctx context.Context, rng hlc.Range) error {
err := retry.Retry(ctx, r.pool, func(ctx context.Context) error {
start := time.Now()
_, err := r.pool.Exec(ctx,
r.sql.record,
r.target.Name.Canonical().Raw(),
rng.Min().Nanos(),
rng.Min().Logical(),
rng.Max().Nanos(),
rng.Max().Logical(),
)
if err == nil {
r.metrics.commitDuration.Observe(time.Since(start).Seconds())
}
return errors.WithStack(err)
})
if err == nil {
log.Tracef("recorded checkpoint timestamps for %s: %s", r.target, rng)
r.Refresh()
}
return err
}
// Refresh the Group asynchronously. This is intended for testing.
func (r *Group) Refresh() {
r.fastWakeup.Notify()
}
// TableGroup returns the [types.TableGroup] whose checkpoints are being
// persisted.
func (r *Group) TableGroup() *types.TableGroup {
return r.target
}
// This query computes the open checkpoint window. That is, it returns
// the newest applied and newest unapplied checkpoint times.
//
// Params:
// - $1: target schema
// - $2: last successful checkpoint nanos
// - $3: last successful checkpoint logical
//
// CTE components:
// - start: The newest applied timestamp
// - stop: The newest unapplied timestamp (i.e. the last row)
//
// The top-level query uses coalesce+sub-select to provide default
// values if either one of the CTE elements returns no rows. In the
// bootstrap or idle case, the ($2, $3) pair will be emitted.
const refreshTemplate = `
WITH
start AS (
SELECT source_nanos n, source_logical l
FROM %[1]s
WHERE target_schema = $1
AND (source_nanos, source_logical) >= ($2, $3)
AND target_applied_at IS NOT NULL
ORDER BY n DESC, l DESC
LIMIT 1
),
stop AS (
SELECT source_nanos n, source_logical l
FROM %[1]s
WHERE target_schema = $1
AND (source_nanos, source_logical) >= ($2, $3)
AND target_applied_at IS NULL
ORDER BY n DESC, l DESC
LIMIT 1
)
SELECT
COALESCE((SELECT start.n FROM start), $2),
COALESCE((SELECT start.l FROM start), $3),
COALESCE((SELECT stop.n FROM stop), $2),
COALESCE((SELECT stop.l FROM stop), $3)
`
// refreshBounds synchronizes the in-memory bounds with the database.
func (r *Group) refreshBounds(ctx context.Context) error {
return retry.Retry(ctx, r.pool, func(ctx context.Context) error {
_, _, err := r.bounds.Update(func(old hlc.Range) (hlc.Range, error) {
start := time.Now()
var next hlc.Range
var minNanos, maxNanos int64
var minLogical, maxLogical int
if err := r.pool.QueryRow(ctx, r.sql.refresh,
r.target.Name.Canonical().Raw(),
old.Min().Nanos(),
old.Min().Logical(),
).Scan(&minNanos, &minLogical, &maxNanos, &maxLogical); err == nil {
next = hlc.RangeIncluding(
hlc.New(minNanos, minLogical),
hlc.New(maxNanos, maxLogical),
)
} else if errors.Is(err, pgx.ErrNoRows) {
// If there's no data for this group, do nothing.
return old, notify.ErrNoUpdate
} else {
return hlc.RangeEmpty(), errors.WithStack(err)
}
if next == old {
log.Tracef("group %s: checkpoint range unchanged: %s", r.target, old)
return old, notify.ErrNoUpdate
}
log.Tracef("group %s: checkpoint range: %s -> %s", r.target, old, next)
r.metrics.refreshDuration.Observe(time.Since(start).Seconds())
return next, nil
})
return err
})
}
// refreshJob starts a goroutine to periodically synchronize the
// in-memory bounds with the database.
func (r *Group) refreshJob(ctx *stopper.Context) {
ctx.Go(func() error {
for {
_, fastWakeup := r.fastWakeup.Get()
// There's an immediate load when the Group is created, so
// it's reasonable in this case to add an initial delay.
select {
case <-ctx.Stopping():
return nil
case <-fastWakeup:
case <-time.After(time.Second):
}
if err := r.refreshBounds(ctx); err != nil {
log.WithError(err).Warnf("could not refresh checkpoint timestamp "+
"bounds for %s; will continue", r.target)
}
}
})
}
// reportMetrics starts a goroutine that will update the enclosed
// metrics as the group's bounds change. The refresh is also periodic to
// allow age metrics to tick.
func (r *Group) reportMetrics(ctx *stopper.Context) {
ctx.Go(func() error {
_, err := stopvar.DoWhenChangedOrInterval(ctx,
hlc.RangeEmpty(), r.bounds, 5*time.Second,
func(ctx *stopper.Context, _, bounds hlc.Range) error {
minTime := bounds.Min().Nanos()
maxTime := bounds.Max().Nanos()
r.metrics.committedAge.Set(
time.Since(time.Unix(0, minTime)).Seconds())
r.metrics.committedTime.Set(float64(minTime) / 1e9)
r.metrics.proposedAge.Set(
time.Since(time.Unix(0, maxTime)).Seconds())
r.metrics.proposedTime.Set(float64(maxTime) / 1e9)
return nil
})
return err
})
}