-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathscanner.go
250 lines (225 loc) · 7.35 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
// 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/gossip"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/kv"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"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/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 EventBufferWriter, cfg physicalConfig) error
}
type scanRequestScanner struct {
settings *cluster.Settings
gossip *gossip.Gossip
db *kv.DB
}
var _ kvScanner = (*scanRequestScanner)(nil)
func (p *scanRequestScanner) Scan(
ctx context.Context, sink EventBufferWriter, cfg physicalConfig,
) error {
if log.V(2) {
log.Infof(ctx, "performing scan on %v at %v withDiff %v",
cfg.Spans, cfg.Timestamp, cfg.WithDiff)
}
spans, err := getSpansToProcess(ctx, p.db, cfg.Spans)
if err != nil {
return err
}
// Export requests for the various watched spans are executed in parallel,
// with a semaphore-enforced limit based on a cluster setting.
// The spans here generally correspond with range boundaries.
maxConcurrentExports := clusterNodeCount(p.gossip) *
int(kvserver.ExportRequestsLimit.Get(&p.settings.SV))
exportsSem := make(chan struct{}, maxConcurrentExports)
g := ctxgroup.WithContext(ctx)
// atomicFinished is used only to enhance debugging messages.
var atomicFinished int64
for _, span := range spans {
span := span
// Wait for our semaphore.
select {
case <-ctx.Done():
return ctx.Err()
case exportsSem <- struct{}{}:
}
g.GoCtx(func(ctx context.Context) error {
defer func() { <-exportsSem }()
err := p.exportSpan(ctx, span, cfg.Timestamp, cfg.WithDiff, sink)
finished := atomic.AddInt64(&atomicFinished, 1)
if log.V(2) {
log.Infof(ctx, `exported %d of %d: %v`, finished, len(spans), err)
}
if err != nil {
return err
}
return nil
})
}
return g.Wait()
}
func (p *scanRequestScanner) exportSpan(
ctx context.Context, span roachpb.Span, ts hlc.Timestamp, withDiff bool, sink EventBufferWriter,
) 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; ; {
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 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 {
remaining = *res.ResumeSpan
} else {
break
}
}
// p.metrics.PollRequestNanosHist.RecordValue(scanDuration.Nanoseconds())
if err := sink.AddResolved(ctx, span, ts, false); 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, db *kv.DB, targetSpans []roachpb.Span,
) ([]roachpb.Span, error) {
var ranges []roachpb.RangeDescriptor
if err := db.Txn(ctx, func(ctx context.Context, txn *kv.Txn) error {
var err error
ranges, err = allRangeDescriptors(ctx, txn)
return err
}); err != nil {
return nil, errors.Wrapf(err, "fetching range descriptors")
}
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 _, rangeDesc := range ranges {
rangeCovering = append(rangeCovering, covering.Range{
Start: []byte(rangeDesc.StartKey),
End: []byte(rangeDesc.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 EventBufferWriter,
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 allRangeDescriptors(ctx context.Context, txn *kv.Txn) ([]roachpb.RangeDescriptor, error) {
rows, err := txn.Scan(ctx, keys.Meta2Prefix, keys.MetaMax, 0)
if err != nil {
return nil, err
}
rangeDescs := make([]roachpb.RangeDescriptor, len(rows))
for i, row := range rows {
if err := row.ValueProto(&rangeDescs[i]); err != nil {
return nil, errors.NewAssertionErrorWithWrappedErrf(err,
"%s: unable to unmarshal range descriptor", row.Key)
}
}
return rangeDescs, nil
}
// clusterNodeCount returns the approximate number of nodes in the cluster.
func clusterNodeCount(g *gossip.Gossip) int {
var nodes int
_ = g.IterateInfos(gossip.KeyNodeIDPrefix, func(_ string, _ gossip.Info) error {
nodes++
return nil
})
return nodes
}