-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
changefeed.go
355 lines (330 loc) · 11.7 KB
/
changefeed.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
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
// Copyright 2018 The Cockroach Authors.
//
// Licensed as a CockroachDB Enterprise file under the Cockroach Community
// License (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// https://github.com/cockroachdb/cockroach/blob/master/licenses/CCL.txt
package changefeedccl
import (
"context"
"time"
"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/jobs"
"github.com/cockroachdb/cockroach/pkg/jobs/jobspb"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/settings"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/sql"
"github.com/cockroachdb/cockroach/pkg/sql/row"
"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
"github.com/cockroachdb/cockroach/pkg/util/bufalloc"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
)
var changefeedPollInterval = func() *settings.DurationSetting {
s := settings.RegisterNonNegativeDurationSetting(
"changefeed.experimental_poll_interval",
"polling interval for the prototype changefeed implementation",
1*time.Second,
)
s.SetSensitive()
return s
}()
// PushEnabled is a cluster setting that triggers all subsequently
// created/unpaused changefeeds to receive kv changes via RangeFeed push
// (instead of ExportRequest polling).
var PushEnabled = settings.RegisterBoolSetting(
"changefeed.push.enabled",
"if set, changed are pushed instead of pulled. This requires the "+
"kv.rangefeed.enabled setting. See "+
base.DocsURL(`change-data-capture.html#enable-rangefeeds-to-reduce-latency`),
true,
)
const (
jsonMetaSentinel = `__crdb__`
)
type emitEntry struct {
// row, if not the zero value, represents a changed row to be emitted.
row encodeRow
// resolved, if non-nil, is a guarantee for the associated
// span that no previously unseen entries with a lower or equal updated
// timestamp will be emitted.
resolved *jobspb.ResolvedSpan
// bufferGetTimestamp is the time this entry came out of the buffer.
bufferGetTimestamp time.Time
}
// kvsToRows gets changed kvs from a closure and converts them into sql rows. It
// returns a closure that may be repeatedly called to advance the changefeed.
// The returned closure is not threadsafe.
func kvsToRows(
leaseMgr *sql.LeaseManager,
details jobspb.ChangefeedDetails,
inputFn func(context.Context) (bufferEntry, error),
) func(context.Context) ([]emitEntry, error) {
rfCache := newRowFetcherCache(leaseMgr)
var kvs row.SpanKVFetcher
appendEmitEntryForKV := func(
ctx context.Context, output []emitEntry, kv roachpb.KeyValue, schemaTimestamp hlc.Timestamp,
bufferGetTimestamp time.Time,
) ([]emitEntry, error) {
// Reuse kvs to save allocations.
kvs.KVs = kvs.KVs[:0]
desc, err := rfCache.TableDescForKey(ctx, kv.Key, schemaTimestamp)
if err != nil {
return nil, err
}
if _, ok := details.Targets[desc.ID]; !ok {
// This kv is for an interleaved table that we're not watching.
if log.V(3) {
log.Infof(ctx, `skipping key from unwatched table %s: %s`, desc.Name, kv.Key)
}
return nil, nil
}
rf, err := rfCache.RowFetcherForTableDesc(desc)
if err != nil {
return nil, err
}
// TODO(dan): Handle tables with multiple column families.
kvs.KVs = append(kvs.KVs, kv)
if err := rf.StartScanFrom(ctx, &kvs); err != nil {
return nil, err
}
for {
var r emitEntry
r.bufferGetTimestamp = bufferGetTimestamp
r.row.datums, r.row.tableDesc, _, err = rf.NextRow(ctx)
if err != nil {
return nil, err
}
if r.row.datums == nil {
break
}
r.row.datums = append(sqlbase.EncDatumRow(nil), r.row.datums...)
r.row.deleted = rf.RowIsDeleted()
// TODO(mrtracy): This should likely be set to schemaTimestamp instead of
// the value timestamp, if schema timestamp is set. However, doing so
// seems to break some of the assumptions of our existing tests in subtle
// ways, so this should be done as part of a dedicated PR.
r.row.updated = schemaTimestamp
output = append(output, r)
}
return output, nil
}
var output []emitEntry
return func(ctx context.Context) ([]emitEntry, error) {
// Reuse output to save allocations.
output = output[:0]
for {
input, err := inputFn(ctx)
if err != nil {
return nil, err
}
if input.kv.Key != nil {
if log.V(3) {
log.Infof(ctx, "changed key %s %s", input.kv.Key, input.kv.Value.Timestamp)
}
schemaTimestamp := input.kv.Value.Timestamp
if input.schemaTimestamp != (hlc.Timestamp{}) {
schemaTimestamp = input.schemaTimestamp
}
output, err = appendEmitEntryForKV(
ctx, output, input.kv, schemaTimestamp, input.bufferGetTimestamp)
if err != nil {
return nil, err
}
}
if input.resolved != nil {
output = append(output, emitEntry{
resolved: input.resolved,
bufferGetTimestamp: input.bufferGetTimestamp,
})
}
if output != nil {
return output, nil
}
}
}
}
// emitEntries connects to a sink, receives rows from a closure, and repeatedly
// emits them to the sink. It returns a closure that may be repeatedly called to
// advance the changefeed and which returns span-level resolved timestamp
// updates. The returned closure is not threadsafe. Note that rows read from
// `inputFn` which precede or equal the Frontier of `sf` will not be emitted
// because they're provably duplicates.
func emitEntries(
settings *cluster.Settings,
details jobspb.ChangefeedDetails,
sf *spanFrontier,
encoder Encoder,
sink Sink,
inputFn func(context.Context) ([]emitEntry, error),
knobs TestingKnobs,
metrics *Metrics,
) func(context.Context) ([]jobspb.ResolvedSpan, error) {
var scratch bufalloc.ByteAllocator
emitRowFn := func(ctx context.Context, row encodeRow) error {
// Ensure that row updates are strictly newer than the least resolved timestamp
// being tracked by the local span frontier. The poller should not be forwarding
// row updates that have timestamps less than or equal to any resolved timestamp
// it's forwarded before.
// TODO(aayush): This should be an assertion once we're confident this can never
// happen under any circumstance.
if !sf.Frontier().Less(row.updated) {
log.Errorf(ctx, "cdc ux violation: detected timestamp %s that is less than "+
"or equal to the local frontier %s.", cloudStorageFormatTime(row.updated),
cloudStorageFormatTime(sf.Frontier()))
return nil
}
var keyCopy, valueCopy []byte
encodedKey, err := encoder.EncodeKey(row)
if err != nil {
return err
}
scratch, keyCopy = scratch.Copy(encodedKey, 0 /* extraCap */)
encodedValue, err := encoder.EncodeValue(row)
if err != nil {
return err
}
scratch, valueCopy = scratch.Copy(encodedValue, 0 /* extraCap */)
if knobs.BeforeEmitRow != nil {
if err := knobs.BeforeEmitRow(ctx); err != nil {
return err
}
}
if err := sink.EmitRow(
ctx, row.tableDesc, keyCopy, valueCopy, row.updated,
); err != nil {
return err
}
if log.V(3) {
log.Infof(ctx, `row %s: %s -> %s`, row.tableDesc.Name, keyCopy, valueCopy)
}
return nil
}
var lastFlush time.Time
// TODO(dan): We could keep these in `sf` to eliminate dups.
var resolvedSpans []jobspb.ResolvedSpan
return func(ctx context.Context) ([]jobspb.ResolvedSpan, error) {
inputs, err := inputFn(ctx)
if err != nil {
return nil, err
}
for _, input := range inputs {
if input.bufferGetTimestamp == (time.Time{}) {
// We could gracefully handle this instead of panic'ing, but
// we'd really like to be able to reason about this data, so
// instead we're defensive. If this is ever seen in prod without
// breaking a unit test, then we have a pretty severe test
// coverage issue.
panic(`unreachable: bufferGetTimestamp is set by all codepaths`)
}
processingNanos := timeutil.Since(input.bufferGetTimestamp).Nanoseconds()
metrics.ProcessingNanos.Inc(processingNanos)
if input.row.datums != nil {
if err := emitRowFn(ctx, input.row); err != nil {
return nil, err
}
}
if input.resolved != nil {
_ = sf.Forward(input.resolved.Span, input.resolved.Timestamp)
resolvedSpans = append(resolvedSpans, *input.resolved)
}
}
// If the resolved timestamp frequency is specified, use it as a rough
// approximation of how latency-sensitive the changefeed user is. If
// it's not, fall back to the poll interval.
//
// The current poller implementation means we emit a changefeed-level
// resolved timestamps to the user once per changefeedPollInterval. This
// buffering adds on average timeBetweenFlushes/2 to that latency. With
// timeBetweenFlushes and changefeedPollInterval both set to 1s, TPCC
// was seeing about 100x more time spent emitting than flushing.
// Dividing by 5 tries to balance these a bit, but ultimately is fairly
// unprincipled.
//
// NB: As long as we periodically get new span-level resolved timestamps
// from the poller (which should always happen, even if the watched data
// is not changing), then this is sufficient and we don't have to do
// anything fancy with timers.
var timeBetweenFlushes time.Duration
if r, ok := details.Opts[optResolvedTimestamps]; ok && r != `` {
var err error
if timeBetweenFlushes, err = time.ParseDuration(r); err != nil {
return nil, err
}
} else {
timeBetweenFlushes = changefeedPollInterval.Get(&settings.SV) / 5
}
if len(resolvedSpans) == 0 || timeutil.Since(lastFlush) < timeBetweenFlushes {
return nil, nil
}
// Make sure to flush the sink before forwarding resolved spans,
// otherwise, we could lose buffered messages and violate the
// at-least-once guarantee. This is also true for checkpointing the
// resolved spans in the job progress.
if err := sink.Flush(ctx); err != nil {
return nil, err
}
lastFlush = timeutil.Now()
if knobs.AfterSinkFlush != nil {
if err := knobs.AfterSinkFlush(); err != nil {
return nil, err
}
}
ret := append([]jobspb.ResolvedSpan(nil), resolvedSpans...)
resolvedSpans = resolvedSpans[:0]
return ret, nil
}
}
// checkpointResolvedTimestamp checkpoints a changefeed-level resolved timestamp
// to the jobs record.
func checkpointResolvedTimestamp(
ctx context.Context,
jobProgressedFn func(context.Context, jobs.HighWaterProgressedFn) error,
sf *spanFrontier,
) error {
resolved := sf.Frontier()
var resolvedSpans []jobspb.ResolvedSpan
sf.Entries(func(span roachpb.Span, ts hlc.Timestamp) {
resolvedSpans = append(resolvedSpans, jobspb.ResolvedSpan{
Span: span, Timestamp: ts,
})
})
// Some benchmarks want to skip the job progress update for a bit more
// isolation.
//
// NB: To minimize the chance that a user sees duplicates from below
// this resolved timestamp, keep this update of the high-water mark
// before emitting the resolved timestamp to the sink.
if jobProgressedFn != nil {
progressedClosure := func(ctx context.Context, d jobspb.ProgressDetails) hlc.Timestamp {
// TODO(dan): This was making enormous jobs rows, especially in
// combination with how many mvcc versions there are. Cut down on
// the amount of data used here dramatically and re-enable.
//
// d.(*jobspb.Progress_Changefeed).Changefeed.ResolvedSpans = resolvedSpans
return resolved
}
if err := jobProgressedFn(ctx, progressedClosure); err != nil {
return err
}
}
return nil
}
// emitResolvedTimestamp emits a changefeed-level resolved timestamp to the
// sink.
func emitResolvedTimestamp(
ctx context.Context, encoder Encoder, sink Sink, resolved hlc.Timestamp,
) error {
// TODO(dan): Emit more fine-grained (table level) resolved
// timestamps.
if err := sink.EmitResolvedTimestamp(ctx, encoder, resolved); err != nil {
return err
}
if log.V(2) {
log.Infof(ctx, `resolved %s`, resolved)
}
return nil
}