-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
changefeed_processors.go
468 lines (420 loc) · 14.3 KB
/
changefeed_processors.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
// 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"
"github.com/cockroachdb/cockroach/pkg/jobs"
"github.com/cockroachdb/cockroach/pkg/jobs/jobspb"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/sql"
"github.com/cockroachdb/cockroach/pkg/sql/distsqlrun"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/mon"
"github.com/cockroachdb/cockroach/pkg/util/protoutil"
"github.com/pkg/errors"
)
type changeAggregator struct {
distsqlrun.ProcessorBase
flowCtx *distsqlrun.FlowCtx
spec distsqlrun.ChangeAggregatorSpec
memAcc mon.BoundAccount
// cancel shuts down the processor, both the `Next()` flow and the poller.
cancel func()
// errCh contains the return values of poller and tableHistUpdater.
errCh chan error
// poller runs in the background and puts kv changes and resolved spans into
// a buffer, which is used by `Next()`.
poller *poller
// pollerDoneCh is closed when the poller exits.
pollerDoneCh chan struct{}
// tableHistUpdater runs in the background and continually advances the
// high-water of a tableHistory.
tableHistUpdater *tableHistoryUpdater
// tableHistUpdaterDoneCh is closed when the tableHistUpdater exits.
tableHistUpdaterDoneCh chan struct{}
// sink is the Sink to write rows to. Resolved timestamps are never written
// by changeAggregator.
sink Sink
// tickFn is the workhorse behind Next(). It pulls kv changes from the
// buffer that poller fills, handles table leasing, converts them to rows,
// and writes them to the sink.
tickFn func(context.Context) ([]jobspb.ResolvedSpan, error)
// changedRowBuf, if non-nil, contains changed rows to be emitted. Anything
// queued in `resolvedSpanBuf` is dependent on these having been emitted, so
// this one must be empty before moving on to that one.
changedRowBuf *encDatumRowBuffer
// resolvedSpanBuf contains resolved span updates to send to changeFrontier.
// If sink is a bufferSink, it must be emptied before these are sent.
resolvedSpanBuf encDatumRowBuffer
}
var _ distsqlrun.Processor = &changeAggregator{}
var _ distsqlrun.RowSource = &changeAggregator{}
func newChangeAggregatorProcessor(
flowCtx *distsqlrun.FlowCtx,
processorID int32,
spec distsqlrun.ChangeAggregatorSpec,
output distsqlrun.RowReceiver,
) (distsqlrun.Processor, error) {
ctx := flowCtx.EvalCtx.Ctx()
memMonitor := distsqlrun.NewMonitor(ctx, flowCtx.EvalCtx.Mon, "changeagg-mem")
ca := &changeAggregator{
flowCtx: flowCtx,
spec: spec,
memAcc: memMonitor.MakeBoundAccount(),
}
if err := ca.Init(
ca,
&distsqlrun.PostProcessSpec{},
nil, /* types */
flowCtx,
processorID,
output,
memMonitor,
distsqlrun.ProcStateOpts{
TrailingMetaCallback: func() []distsqlrun.ProducerMetadata {
ca.close()
return nil
},
},
); err != nil {
return nil, err
}
initialHighWater := hlc.Timestamp{WallTime: -1}
var spans []roachpb.Span
for _, watch := range spec.Watches {
spans = append(spans, watch.Span)
if initialHighWater.WallTime == -1 || watch.InitialResolved.Less(initialHighWater) {
initialHighWater = watch.InitialResolved
}
}
var err error
if ca.sink, err = getSink(spec.Feed.SinkURI, spec.Feed.Targets); err != nil {
return nil, err
}
if b, ok := ca.sink.(*bufferSink); ok {
ca.changedRowBuf = &b.buf
}
// The job registry has a set of metrics used to monitor the various jobs it
// runs. They're all stored as the `metric.Struct` interface because of
// dependency cycles.
metrics := flowCtx.JobRegistry.MetricsStruct().Changefeed.(*Metrics)
ca.sink = makeMetricsSink(metrics, ca.sink)
buf := makeBuffer()
ca.poller = makePoller(
flowCtx.Settings, flowCtx.ClientDB, flowCtx.ClientDB.Clock(), flowCtx.Gossip, spans,
spec.Feed.Targets, initialHighWater, buf)
leaseMgr := flowCtx.LeaseManager.(*sql.LeaseManager)
tableHist := makeTableHistory(func(desc *sqlbase.TableDescriptor) error {
// NB: Each new `tableDesc.Version` is initially written with an mvcc
// timestamp equal to its `ModificationTime`. It might later update that
// `Version` with backfill progress, but we only validate a table
// descriptor through its `ModificationTime` before using it, so this
// validation function can't depend on anything that changes after a new
// `Version` of a table desc is written.
return validateChangefeedTable(spec.Feed.Targets, desc)
}, initialHighWater)
ca.tableHistUpdater = &tableHistoryUpdater{
settings: flowCtx.Settings,
db: flowCtx.ClientDB,
targets: spec.Feed.Targets,
m: tableHist,
}
rowsFn := kvsToRows(leaseMgr, tableHist, spec.Feed, buf.Get)
ca.tickFn = emitEntries(spec.Feed, ca.sink, rowsFn)
return ca, nil
}
func (ca *changeAggregator) OutputTypes() []sqlbase.ColumnType {
return changefeedResultTypes
}
// Start is part of the RowSource interface.
func (ca *changeAggregator) Start(ctx context.Context) context.Context {
ctx, ca.cancel = context.WithCancel(ctx)
// Give errCh enough buffer for both of these, but only the first one is
// ever used.
ca.errCh = make(chan error, 2)
ca.pollerDoneCh = make(chan struct{})
go func(ctx context.Context) {
defer close(ca.pollerDoneCh)
err := ca.poller.Run(ctx)
// Trying to call MoveToDraining here is racy (`MoveToDraining called in
// state stateTrailingMeta`), so return the error via a channel.
ca.errCh <- err
ca.cancel()
}(ctx)
ca.tableHistUpdaterDoneCh = make(chan struct{})
go func(ctx context.Context) {
defer close(ca.tableHistUpdaterDoneCh)
err := ca.tableHistUpdater.PollTableDescs(ctx)
// Trying to call MoveToDraining here is racy (`MoveToDraining called in
// state stateTrailingMeta`), so return the error via a channel.
ca.errCh <- err
ca.cancel()
}(ctx)
ctx = ca.StartInternal(ctx, changeAggregatorProcName)
return ctx
}
func (ca *changeAggregator) close() {
// Wait for the poller and tableHistUpdater to finish shutting down.
<-ca.pollerDoneCh
<-ca.tableHistUpdaterDoneCh
if err := ca.sink.Close(); err != nil {
log.Warningf(ca.Ctx, `error closing sink. goroutines may have leaked: %v`, err)
}
// Need to close the mem accounting while the context is still valid.
ca.memAcc.Close(ca.Ctx)
ca.InternalClose()
ca.MemMonitor.Stop(ca.Ctx)
}
// Next is part of the RowSource interface.
func (ca *changeAggregator) Next() (sqlbase.EncDatumRow, *distsqlrun.ProducerMetadata) {
for ca.State == distsqlrun.StateRunning {
if !ca.changedRowBuf.IsEmpty() {
return ca.changedRowBuf.Pop(), nil
} else if !ca.resolvedSpanBuf.IsEmpty() {
return ca.resolvedSpanBuf.Pop(), nil
}
if err := ca.tick(); err != nil {
select {
// If the poller or tableHistUpdater errored first, that's the
// interesting one, so overwrite `err`.
case err = <-ca.errCh:
default:
}
// Shut down the poller and tableHistUpdater if they weren't
// already.
ca.cancel()
ca.MoveToDraining(err)
break
}
}
return nil, ca.DrainHelper()
}
func (ca *changeAggregator) tick() error {
resolvedSpans, err := ca.tickFn(ca.Ctx)
if err != nil {
return err
}
for _, resolvedSpan := range resolvedSpans {
resolvedBytes, err := protoutil.Marshal(&resolvedSpan)
if err != nil {
return err
}
// Enqueue a row to be returned that indicates some span-level resolved
// timestamp has advanced. If any rows were queued in `sink`, they must
// be emitted first.
ca.resolvedSpanBuf.Push(sqlbase.EncDatumRow{
sqlbase.EncDatum{Datum: tree.NewDBytes(tree.DBytes(resolvedBytes))},
sqlbase.EncDatum{Datum: tree.DNull}, // topic
sqlbase.EncDatum{Datum: tree.DNull}, // key
sqlbase.EncDatum{Datum: tree.DNull}, // value
})
}
return nil
}
// ConsumerDone is part of the RowSource interface.
func (ca *changeAggregator) ConsumerDone() {
ca.MoveToDraining(nil /* err */)
}
// ConsumerClosed is part of the RowSource interface.
func (ca *changeAggregator) ConsumerClosed() {
// The consumer is done, Next() will not be called again.
ca.InternalClose()
}
type changeFrontier struct {
distsqlrun.ProcessorBase
flowCtx *distsqlrun.FlowCtx
spec distsqlrun.ChangeFrontierSpec
memAcc mon.BoundAccount
a sqlbase.DatumAlloc
// input returns rows from one or more changeAggregator processors
input distsqlrun.RowSource
// sf contains the current resolved timestamp high-water for the tracked
// span set.
sf *spanFrontier
// sink is the Sink to write resolved timestamps to. Rows are never written
// by changeFrontier.
sink Sink
// jobProgressedFn, if non-nil, is called to checkpoint the changefeed's
// progress in the corresponding system job entry.
jobProgressedFn func(context.Context, jobs.HighWaterProgressedFn) error
// passthroughBuf, in some but not all flows, contains changed row data to
// pass through unchanged to the gateway node.
passthroughBuf encDatumRowBuffer
// resolvedBuf, if non-nil, contains rows indicating a changefeed-level
// resolved timestamp to be returned. It depends on everything in
// `passthroughBuf` being sent, so that one needs to be emptied first.
resolvedBuf *encDatumRowBuffer
// metrics are monitoring counters shared between all changefeeds.
metrics *Metrics
// metricsID is used as the unique id of this changefeed in the
// metrics.MinHighWater map.
metricsID int
}
var _ distsqlrun.Processor = &changeFrontier{}
var _ distsqlrun.RowSource = &changeFrontier{}
func newChangeFrontierProcessor(
flowCtx *distsqlrun.FlowCtx,
processorID int32,
spec distsqlrun.ChangeFrontierSpec,
input distsqlrun.RowSource,
output distsqlrun.RowReceiver,
) (distsqlrun.Processor, error) {
ctx := flowCtx.EvalCtx.Ctx()
memMonitor := distsqlrun.NewMonitor(ctx, flowCtx.EvalCtx.Mon, "changefntr-mem")
cf := &changeFrontier{
flowCtx: flowCtx,
spec: spec,
memAcc: memMonitor.MakeBoundAccount(),
input: input,
sf: makeSpanFrontier(spec.TrackedSpans...),
}
if err := cf.Init(
cf, &distsqlrun.PostProcessSpec{},
input.OutputTypes(),
flowCtx,
processorID,
output,
memMonitor,
distsqlrun.ProcStateOpts{
TrailingMetaCallback: func() []distsqlrun.ProducerMetadata {
cf.close()
return nil
},
InputsToDrain: []distsqlrun.RowSource{cf.input},
},
); err != nil {
return nil, err
}
var err error
if cf.sink, err = getSink(spec.Feed.SinkURI, spec.Feed.Targets); err != nil {
return nil, err
}
if b, ok := cf.sink.(*bufferSink); ok {
cf.resolvedBuf = &b.buf
}
// The job registry has a set of metrics used to monitor the various jobs it
// runs. They're all stored as the `metric.Struct` interface because of
// dependency cycles.
cf.metrics = flowCtx.JobRegistry.MetricsStruct().Changefeed.(*Metrics)
cf.sink = makeMetricsSink(cf.metrics, cf.sink)
if spec.JobID != 0 {
job, err := flowCtx.JobRegistry.LoadJob(ctx, spec.JobID)
if err != nil {
return nil, err
}
cf.jobProgressedFn = job.HighWaterProgressed
}
return cf, nil
}
func (cf *changeFrontier) OutputTypes() []sqlbase.ColumnType {
return changefeedResultTypes
}
// Start is part of the RowSource interface.
func (cf *changeFrontier) Start(ctx context.Context) context.Context {
cf.input.Start(ctx)
ctx = cf.StartInternal(ctx, changeFrontierProcName)
cf.metrics.mu.Lock()
cf.metricsID = cf.metrics.mu.id
cf.metrics.mu.id++
cf.metrics.mu.Unlock()
go func() {
// Delete this feed from the MinHighwater metric so it's no longer
// considered by the gauge.
//
// TODO(dan): Ideally this would be done in something like `close` but
// there's nothing that's guaranteed to be called when a processor shuts
// down.
<-ctx.Done()
cf.metrics.mu.Lock()
delete(cf.metrics.mu.resolved, cf.metricsID)
cf.metricsID = -1
cf.metrics.mu.Unlock()
}()
return ctx
}
func (cf *changeFrontier) close() {
if err := cf.sink.Close(); err != nil {
log.Warningf(cf.Ctx, `error closing sink. goroutines may have leaked: %v`, err)
}
// Need to close the mem accounting while the context is still valid.
cf.memAcc.Close(cf.Ctx)
cf.InternalClose()
cf.MemMonitor.Stop(cf.Ctx)
}
// Next is part of the RowSource interface.
func (cf *changeFrontier) Next() (sqlbase.EncDatumRow, *distsqlrun.ProducerMetadata) {
for cf.State == distsqlrun.StateRunning {
if !cf.passthroughBuf.IsEmpty() {
return cf.passthroughBuf.Pop(), nil
} else if !cf.resolvedBuf.IsEmpty() {
return cf.resolvedBuf.Pop(), nil
}
row, meta := cf.input.Next()
if meta != nil {
if meta.Err != nil {
cf.MoveToDraining(nil /* err */)
}
return nil, meta
}
if row == nil {
cf.MoveToDraining(nil /* err */)
break
}
if row[0].IsNull() {
// In changefeeds with a sink, this will never happen. But in the
// core changefeed, which returns changed rows directly via pgwire,
// a row with a null resolved_span field is a changed row that needs
// to be forwarded to the gateway.
cf.passthroughBuf.Push(row)
continue
}
if err := cf.noteResolvedSpan(row[0]); err != nil {
cf.MoveToDraining(err)
break
}
}
return nil, cf.DrainHelper()
}
func (cf *changeFrontier) noteResolvedSpan(d sqlbase.EncDatum) error {
if err := d.EnsureDecoded(&changefeedResultTypes[0], &cf.a); err != nil {
return err
}
raw, ok := d.Datum.(*tree.DBytes)
if !ok {
return errors.Errorf(`unexpected datum type %T: %s`, d.Datum, d.Datum)
}
var resolved jobspb.ResolvedSpan
if err := protoutil.Unmarshal([]byte(*raw), &resolved); err != nil {
return errors.Wrapf(err, `unmarshalling resolved span: %x`, raw)
}
if cf.sf.Forward(resolved.Span, resolved.Timestamp) {
cf.metrics.mu.Lock()
if cf.metricsID != -1 {
cf.metrics.mu.resolved[cf.metricsID] = cf.sf.Frontier()
}
cf.metrics.mu.Unlock()
if err := emitResolvedTimestamp(
cf.Ctx, cf.spec.Feed, cf.sink, cf.jobProgressedFn, cf.sf,
); err != nil {
return err
}
}
return nil
}
// ConsumerDone is part of the RowSource interface.
func (cf *changeFrontier) ConsumerDone() {
cf.MoveToDraining(nil /* err */)
}
// ConsumerClosed is part of the RowSource interface.
func (cf *changeFrontier) ConsumerClosed() {
// The consumer is done, Next() will not be called again.
cf.InternalClose()
}