-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
scanner.go
328 lines (294 loc) · 9.91 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
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
// 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/admission/admissionpb"
"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 scanConfig struct {
Spans []roachpb.Span
Timestamp hlc.Timestamp
WithDiff bool
Knobs TestingKnobs
}
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 scanConfig) error
}
type scanRequestScanner struct {
settings *cluster.Settings
gossip gossip.OptionalGossip
db *kv.DB
onBackfillRangeCallback func(int64) (func(), func())
}
var _ kvScanner = (*scanRequestScanner)(nil)
func (p *scanRequestScanner) Scan(ctx context.Context, sink kvevent.Writer, cfg scanConfig) 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
}
var backfillDec, backfillClear func()
if p.onBackfillRangeCallback != nil {
backfillDec, backfillClear = p.onBackfillRangeCallback(int64(len(spans)))
defer backfillClear()
}
maxConcurrentScans := maxConcurrentScanRequests(p.gossip, &p.settings.SV)
exportLim := limit.MakeConcurrentRequestLimiter("changefeedScanRequestLimiter", maxConcurrentScans)
lastScanLimitUserSetting := changefeedbase.ScanRequestLimit.Get(&p.settings.SV)
g := ctxgroup.WithContext(ctx)
// atomicFinished is used only to enhance debugging messages.
var atomicFinished int64
for _, span := range spans {
span := span
// If the user defined scan request limit has changed, recalculate it
if currentUserScanLimit := changefeedbase.ScanRequestLimit.Get(&p.settings.SV); currentUserScanLimit != lastScanLimitUserSetting {
lastScanLimitUserSetting = currentUserScanLimit
exportLim.SetLimit(maxConcurrentScanRequests(p.gossip, &p.settings.SV))
}
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 backfillDec != nil {
backfillDec()
}
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)
}
if err := txn.SetFixedTimestamp(ctx, ts); err != nil {
return err
}
stopwatchStart := timeutil.Now()
var scanDuration, bufferDuration time.Duration
targetBytesPerScan := changefeedbase.ScanRequestSize.Get(&p.settings.SV)
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
b.AdmissionHeader = roachpb.AdmissionHeader{
Priority: int32(admissionpb.BulkNormalPri),
CreateTime: start.UnixNano(),
Source: roachpb.AdmissionHeader_FROM_SQL,
NoMemoryReservedAtSource: true,
}
// 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 {
if err := knobs.BeforeScanRequest(b); err != nil {
return err
}
}
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.Add(
ctx, kvevent.NewBackfillResolvedEvent(consumed, ts, jobspb.ResolvedSpan_NONE),
); err != nil {
return err
}
}
remaining = res.ResumeSpan
}
// p.metrics.PollRequestNanosHist.RecordValue(scanDuration.Nanoseconds())
if err := sink.Add(
ctx, kvevent.NewBackfillResolvedEvent(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,
backfillTS hlc.Timestamp,
withDiff bool,
span roachpb.Span,
) error {
var keyBytes, valBytes []byte
var ts hlc.Timestamp
var err error
for _, br := range res.BatchResponses {
for len(br) > 0 {
keyBytes, ts, valBytes, br, err = enginepb.ScanDecodeKeyValue(br)
if err != nil {
return errors.Wrapf(err, `decoding changes for %s`, span)
}
if err = sink.Add(ctx, kvevent.NewBackfillKVEvent(keyBytes, ts, valBytes, withDiff, backfillTS)); err != nil {
return errors.Wrapf(err, `buffering changes for %s`, span)
}
}
}
return nil
}
// AllRangeSpans returns the list of all ranges that for the specified list of spans.
func AllRangeSpans(
ctx context.Context, ds *kvcoord.DistSender, spans []roachpb.Span,
) ([]roachpb.Span, error) {
ranges := make([]roachpb.Span, 0, len(spans))
it := kvcoord.MakeRangeIterator(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
}
// clusterNodeCount returns the approximate number of nodes in the cluster.
func clusterNodeCount(gw gossip.OptionalGossip) int {
g, err := gw.OptionalErr(47971)
if err != nil {
// can't count nodes in tenants
return 1
}
var nodes int
_ = g.IterateInfos(gossip.KeyNodeDescPrefix, func(_ string, _ gossip.Info) error {
nodes++
return nil
})
return nodes
}
// maxConcurrentScanRequests returns the number of concurrent scan requests.
func maxConcurrentScanRequests(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)
}
// TODO(yevgeniy): Currently, issuing multiple concurrent updates scaled to the size of
// the cluster only make sense for the core change feeds. This configuration shoould
// be specified explicitly when creating scanner.
nodes := clusterNodeCount(gw)
// 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
}