-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathscanner.go
287 lines (258 loc) · 8.36 KB
/
scanner.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
// Copyright 2020 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 kvfeed
import (
"context"
"sync/atomic"
"time"
"github.com/cockroachdb/cockroach/pkg/ccl/changefeedccl/changefeedbase"
"github.com/cockroachdb/cockroach/pkg/ccl/changefeedccl/kvevent"
"github.com/cockroachdb/cockroach/pkg/gossip"
"github.com/cockroachdb/cockroach/pkg/jobs/jobspb"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/kv"
"github.com/cockroachdb/cockroach/pkg/kv/kvclient/kvcoord"
"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/covering"
"github.com/cockroachdb/cockroach/pkg/storage/enginepb"
"github.com/cockroachdb/cockroach/pkg/util/ctxgroup"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/limit"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/errors"
)
type kvScanner interface {
// Scan will scan all of the KVs in the spans specified by the physical config
// at the specified timestamp and write them to the buffer.
Scan(ctx context.Context, sink kvevent.Writer, cfg physicalConfig) error
}
type scanRequestScanner struct {
settings *cluster.Settings
gossip gossip.OptionalGossip
db *kv.DB
}
var _ kvScanner = (*scanRequestScanner)(nil)
func (p *scanRequestScanner) Scan(
ctx context.Context, sink kvevent.Writer, cfg physicalConfig,
) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
if log.V(2) {
log.Infof(ctx, "performing scan on %v at %v withDiff %v",
cfg.Spans, cfg.Timestamp, cfg.WithDiff)
}
sender := p.db.NonTransactionalSender()
distSender := sender.(*kv.CrossRangeTxnWrapperSender).Wrapped().(*kvcoord.DistSender)
spans, err := getSpansToProcess(ctx, distSender, cfg.Spans)
if err != nil {
return err
}
maxConcurrentExports := maxConcurrentExportRequests(p.gossip, &p.settings.SV)
exportLim := limit.MakeConcurrentRequestLimiter("changefeedExportRequestLimiter", maxConcurrentExports)
g := ctxgroup.WithContext(ctx)
// atomicFinished is used only to enhance debugging messages.
var atomicFinished int64
for _, span := range spans {
span := span
limAlloc, err := exportLim.Begin(ctx)
if err != nil {
cancel()
return errors.CombineErrors(err, g.Wait())
}
g.GoCtx(func(ctx context.Context) error {
defer limAlloc.Release()
err := p.exportSpan(ctx, span, cfg.Timestamp, cfg.WithDiff, sink, cfg.Knobs)
finished := atomic.AddInt64(&atomicFinished, 1)
if log.V(2) {
log.Infof(ctx, `exported %d of %d: %v`, finished, len(spans), err)
}
return err
})
}
return g.Wait()
}
func (p *scanRequestScanner) exportSpan(
ctx context.Context,
span roachpb.Span,
ts hlc.Timestamp,
withDiff bool,
sink kvevent.Writer,
knobs TestingKnobs,
) error {
txn := p.db.NewTxn(ctx, "changefeed backfill")
if log.V(2) {
log.Infof(ctx, `sending ScanRequest %s at %s`, span, ts)
}
txn.SetFixedTimestamp(ctx, ts)
stopwatchStart := timeutil.Now()
var scanDuration, bufferDuration time.Duration
const targetBytesPerScan = 16 << 20 // 16 MiB
for remaining := &span; remaining != nil; {
start := timeutil.Now()
b := txn.NewBatch()
r := roachpb.NewScan(remaining.Key, remaining.EndKey, false /* forUpdate */).(*roachpb.ScanRequest)
r.ScanFormat = roachpb.BATCH_RESPONSE
b.Header.TargetBytes = targetBytesPerScan
// NB: We use a raw request rather than the Scan() method because we want
// the MVCC timestamps which are encoded in the response but are filtered
// during result parsing.
b.AddRawRequest(r)
if knobs.BeforeScanRequest != nil {
knobs.BeforeScanRequest(b)
}
if err := txn.Run(ctx, b); err != nil {
return errors.Wrapf(err, `fetching changes for %s`, span)
}
afterScan := timeutil.Now()
res := b.RawResponse().Responses[0].GetScan()
if err := slurpScanResponse(ctx, sink, res, ts, withDiff, *remaining); err != nil {
return err
}
afterBuffer := timeutil.Now()
scanDuration += afterScan.Sub(start)
bufferDuration += afterBuffer.Sub(afterScan)
if res.ResumeSpan != nil {
consumed := roachpb.Span{Key: remaining.Key, EndKey: res.ResumeSpan.Key}
if err := sink.AddResolved(ctx, consumed, ts, jobspb.ResolvedSpan_NONE); err != nil {
return err
}
}
remaining = res.ResumeSpan
}
// p.metrics.PollRequestNanosHist.RecordValue(scanDuration.Nanoseconds())
if err := sink.AddResolved(ctx, span, ts, jobspb.ResolvedSpan_NONE); err != nil {
return err
}
if log.V(2) {
log.Infof(ctx, `finished Scan of %s at %s took %s`,
span, ts.AsOfSystemTime(), timeutil.Since(stopwatchStart))
}
return nil
}
func getSpansToProcess(
ctx context.Context, ds *kvcoord.DistSender, targetSpans []roachpb.Span,
) ([]roachpb.Span, error) {
ranges, err := allRangeSpans(ctx, ds, targetSpans)
if err != nil {
return nil, err
}
type spanMarker struct{}
type rangeMarker struct{}
var spanCovering covering.Covering
for _, span := range targetSpans {
spanCovering = append(spanCovering, covering.Range{
Start: []byte(span.Key),
End: []byte(span.EndKey),
Payload: spanMarker{},
})
}
var rangeCovering covering.Covering
for _, r := range ranges {
rangeCovering = append(rangeCovering, covering.Range{
Start: []byte(r.Key),
End: []byte(r.EndKey),
Payload: rangeMarker{},
})
}
chunks := covering.OverlapCoveringMerge(
[]covering.Covering{spanCovering, rangeCovering},
)
var requests []roachpb.Span
for _, chunk := range chunks {
if _, ok := chunk.Payload.([]interface{})[0].(spanMarker); !ok {
continue
}
requests = append(requests, roachpb.Span{Key: chunk.Start, EndKey: chunk.End})
}
return requests, nil
}
// slurpScanResponse iterates the ScanResponse and inserts the contained kvs into
// the KVFeed's buffer.
func slurpScanResponse(
ctx context.Context,
sink kvevent.Writer,
res *roachpb.ScanResponse,
ts hlc.Timestamp,
withDiff bool,
span roachpb.Span,
) error {
for _, br := range res.BatchResponses {
for len(br) > 0 {
var kv roachpb.KeyValue
var err error
kv.Key, kv.Value.Timestamp, kv.Value.RawBytes, br, err = enginepb.ScanDecodeKeyValue(br)
if err != nil {
return errors.Wrapf(err, `decoding changes for %s`, span)
}
var prevVal roachpb.Value
if withDiff {
// Include the same value for the "before" and "after" KV, but
// interpret them at different timestamp. Specifically, interpret
// the "before" KV at the timestamp immediately before the schema
// change. This is handled in kvsToRows.
prevVal = kv.Value
}
if err = sink.AddKV(ctx, kv, prevVal, ts); err != nil {
return errors.Wrapf(err, `buffering changes for %s`, span)
}
}
}
return nil
}
func allRangeSpans(
ctx context.Context, ds *kvcoord.DistSender, spans []roachpb.Span,
) ([]roachpb.Span, error) {
ranges := make([]roachpb.Span, 0, len(spans))
it := kvcoord.NewRangeIterator(ds)
for i := range spans {
rSpan, err := keys.SpanAddr(spans[i])
if err != nil {
return nil, err
}
for it.Seek(ctx, rSpan.Key, kvcoord.Ascending); ; it.Next(ctx) {
if !it.Valid() {
return nil, it.Error()
}
ranges = append(ranges, roachpb.Span{
Key: it.Desc().StartKey.AsRawKey(), EndKey: it.Desc().EndKey.AsRawKey(),
})
if !it.NeedAnother(rSpan) {
break
}
}
}
return ranges, nil
}
// maxConcurrentExportRequests returns the number of concurrent scan requests.
func maxConcurrentExportRequests(gw gossip.OptionalGossip, sv *settings.Values) int {
// If the user specified ScanRequestLimit -- use that value.
if max := changefeedbase.ScanRequestLimit.Get(sv); max > 0 {
return int(max)
}
var nodes int
g, err := gw.OptionalErr(47971)
if err != nil {
// can't count nodes in tenants
nodes = 1
}
_ = g.IterateInfos(gossip.KeyNodeIDPrefix, func(_ string, _ gossip.Info) error {
nodes++
return nil
})
// This is all hand-wavy: 3 per node used to be the default for a very long time.
// However, this could get out of hand if the clusters are large.
// So cap the max to an arbitrary value of a 100.
max := 3 * nodes
if max > 100 {
max = 100
}
return max
}