-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
split_and_scatter_processor.go
472 lines (416 loc) · 15.4 KB
/
split_and_scatter_processor.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
469
470
471
472
// 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 backupccl
import (
"context"
"fmt"
"strings"
"time"
"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/kv"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/catenumpb"
"github.com/cockroachdb/cockroach/pkg/sql/execinfra"
"github.com/cockroachdb/cockroach/pkg/sql/execinfrapb"
"github.com/cockroachdb/cockroach/pkg/sql/rowenc"
"github.com/cockroachdb/cockroach/pkg/sql/rowexec"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/cockroachdb/cockroach/pkg/util/ctxgroup"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/protoutil"
"github.com/cockroachdb/cockroach/pkg/util/stop"
"github.com/cockroachdb/errors"
"github.com/cockroachdb/logtags"
)
type splitAndScatterer interface {
// split issues a split request at the given key, which may be rewritten to
// the RESTORE keyspace.
split(ctx context.Context, codec keys.SQLCodec, splitKey roachpb.Key) error
// scatter issues a scatter request at the given key. It returns the node ID
// of where the range was scattered to.
scatter(ctx context.Context, codec keys.SQLCodec, scatterKey roachpb.Key) (roachpb.NodeID, error)
}
type noopSplitAndScatterer struct {
scatterNode roachpb.NodeID
}
var _ splitAndScatterer = noopSplitAndScatterer{}
// split implements splitAndScatterer.
func (n noopSplitAndScatterer) split(_ context.Context, _ keys.SQLCodec, _ roachpb.Key) error {
return nil
}
// scatter implements splitAndScatterer.
func (n noopSplitAndScatterer) scatter(
_ context.Context, _ keys.SQLCodec, _ roachpb.Key,
) (roachpb.NodeID, error) {
return n.scatterNode, nil
}
// dbSplitAndScatter is the production implementation of this processor's
// scatterer. It actually issues the split and scatter requests against the KV
// layer.
type dbSplitAndScatterer struct {
db *kv.DB
kr *KeyRewriter
}
var _ splitAndScatterer = dbSplitAndScatterer{}
func makeSplitAndScatterer(db *kv.DB, kr *KeyRewriter) splitAndScatterer {
return dbSplitAndScatterer{db: db, kr: kr}
}
// split implements splitAndScatterer.
func (s dbSplitAndScatterer) split(
ctx context.Context, codec keys.SQLCodec, splitKey roachpb.Key,
) error {
if s.kr == nil {
return errors.AssertionFailedf("KeyRewriter was not set when expected to be")
}
if s.db == nil {
return errors.AssertionFailedf("split and scatterer's database was not set when expected")
}
expirationTime := s.db.Clock().Now().Add(time.Hour.Nanoseconds(), 0)
newSplitKey, err := rewriteBackupSpanKey(codec, s.kr, splitKey)
if err != nil {
return err
}
if splitAt, err := keys.EnsureSafeSplitKey(newSplitKey); err != nil {
// Ignore the error, not all keys are table keys.
} else if len(splitAt) != 0 {
newSplitKey = splitAt
}
log.VEventf(ctx, 1, "presplitting new key %+v", newSplitKey)
if err := s.db.AdminSplit(ctx, newSplitKey, expirationTime); err != nil {
return errors.Wrapf(err, "splitting key %s", newSplitKey)
}
return nil
}
// scatter implements splitAndScatterer.
func (s dbSplitAndScatterer) scatter(
ctx context.Context, codec keys.SQLCodec, scatterKey roachpb.Key,
) (roachpb.NodeID, error) {
if s.kr == nil {
return 0, errors.AssertionFailedf("KeyRewriter was not set when expected to be")
}
if s.db == nil {
return 0, errors.AssertionFailedf("split and scatterer's database was not set when expected")
}
newScatterKey, err := rewriteBackupSpanKey(codec, s.kr, scatterKey)
if err != nil {
return 0, err
}
if scatterAt, err := keys.EnsureSafeSplitKey(newScatterKey); err != nil {
// Ignore the error, not all keys are table keys.
} else if len(scatterAt) != 0 {
newScatterKey = scatterAt
}
log.VEventf(ctx, 1, "scattering new key %+v", newScatterKey)
req := &roachpb.AdminScatterRequest{
RequestHeader: roachpb.RequestHeaderFromSpan(roachpb.Span{
Key: newScatterKey,
EndKey: newScatterKey.Next(),
}),
// This is a bit of a hack, but it seems to be an effective one (see #36665
// for graphs). As of the commit that added this, scatter is not very good
// at actually balancing leases. This is likely for two reasons: 1) there's
// almost certainly some regression in scatter's behavior, it used to work
// much better and 2) scatter has to operate by balancing leases for all
// ranges in a cluster, but in RESTORE, we really just want it to be
// balancing the span being restored into.
RandomizeLeases: true,
MaxSize: 1, // don't scatter non-empty ranges on resume.
}
res, pErr := kv.SendWrapped(ctx, s.db.NonTransactionalSender(), req)
if pErr != nil {
// TODO(dt): typed error.
if !strings.Contains(pErr.String(), "existing range size") {
// TODO(pbardea): Unfortunately, Scatter is still too unreliable to
// fail the RESTORE when Scatter fails. I'm uncomfortable that
// this could break entirely and not start failing the tests,
// but on the bright side, it doesn't affect correctness, only
// throughput.
log.Errorf(ctx, "failed to scatter span [%s,%s): %+v",
newScatterKey, newScatterKey.Next(), pErr.GoError())
}
return 0, nil
}
return s.findDestination(res.(*roachpb.AdminScatterResponse)), nil
}
// findDestination returns the node ID of the node of the destination of the
// AdminScatter request. If the destination cannot be found, 0 is returned.
func (s dbSplitAndScatterer) findDestination(res *roachpb.AdminScatterResponse) roachpb.NodeID {
if len(res.RangeInfos) > 0 {
// If the lease is not populated, we return the 0 value anyway. We receive 1
// RangeInfo per range that was scattered. Since we send a scatter request
// to each range that we make, we are only interested in the first range,
// which contains the key at which we're splitting and scattering.
return res.RangeInfos[0].Lease.Replica.NodeID
}
return roachpb.NodeID(0)
}
const splitAndScatterProcessorName = "splitAndScatter"
var splitAndScatterOutputTypes = []*types.T{
types.Bytes, // Span key for the range router
types.Bytes, // RestoreDataEntry bytes
}
// splitAndScatterProcessor is given a set of spans (specified as
// RestoreSpanEntry's) to distribute across the cluster. Depending on which node
// the span ends up on, it forwards RestoreSpanEntry as bytes along with the key
// of the span on a row. It expects an output RangeRouter and before it emits
// each row, it updates the entry in the RangeRouter's map with the destination
// of the scatter.
type splitAndScatterProcessor struct {
execinfra.ProcessorBase
flowCtx *execinfra.FlowCtx
spec execinfrapb.SplitAndScatterSpec
output execinfra.RowReceiver
scatterer splitAndScatterer
// cancelScatterAndWaitForWorker cancels the scatter goroutine and waits for
// it to finish.
cancelScatterAndWaitForWorker func()
doneScatterCh chan entryNode
// A cache for routing datums, so only 1 is allocated per node.
routingDatumCache map[roachpb.NodeID]rowenc.EncDatum
scatterErr error
}
var _ execinfra.Processor = &splitAndScatterProcessor{}
func newSplitAndScatterProcessor(
ctx context.Context,
flowCtx *execinfra.FlowCtx,
processorID int32,
spec execinfrapb.SplitAndScatterSpec,
post *execinfrapb.PostProcessSpec,
output execinfra.RowReceiver,
) (execinfra.Processor, error) {
numEntries := 0
for _, chunk := range spec.Chunks {
numEntries += len(chunk.Entries)
}
db := flowCtx.Cfg.DB
kr, err := MakeKeyRewriterFromRekeys(flowCtx.Codec(), spec.TableRekeys, spec.TenantRekeys,
false /* restoreTenantFromStream */)
if err != nil {
return nil, err
}
scatterer := makeSplitAndScatterer(db.KV(), kr)
if spec.ValidateOnly {
nodeID, _ := flowCtx.NodeID.OptionalNodeID()
scatterer = noopSplitAndScatterer{nodeID}
}
ssp := &splitAndScatterProcessor{
flowCtx: flowCtx,
spec: spec,
output: output,
scatterer: scatterer,
// Large enough so that it never blocks.
doneScatterCh: make(chan entryNode, numEntries),
routingDatumCache: make(map[roachpb.NodeID]rowenc.EncDatum),
}
if err := ssp.Init(ctx, ssp, post, splitAndScatterOutputTypes, flowCtx, processorID, output, nil, /* memMonitor */
execinfra.ProcStateOpts{
InputsToDrain: nil, // there are no inputs to drain
TrailingMetaCallback: func() []execinfrapb.ProducerMetadata {
ssp.close()
return nil
},
}); err != nil {
return nil, err
}
return ssp, nil
}
// Start is part of the RowSource interface.
func (ssp *splitAndScatterProcessor) Start(ctx context.Context) {
ctx = logtags.AddTag(ctx, "job", ssp.spec.JobID)
ctx = ssp.StartInternal(ctx, splitAndScatterProcessorName)
// Note that the loop over doneScatterCh in Next should prevent the goroutine
// below from leaking when there are no errors. However, if that loop needs to
// exit early, runSplitAndScatter's context will be canceled.
scatterCtx, cancel := context.WithCancel(ctx)
workerDone := make(chan struct{})
ssp.cancelScatterAndWaitForWorker = func() {
cancel()
<-workerDone
}
if err := ssp.flowCtx.Stopper().RunAsyncTaskEx(scatterCtx, stop.TaskOpts{
TaskName: "splitAndScatter-worker",
SpanOpt: stop.ChildSpan,
}, func(ctx context.Context) {
ssp.scatterErr = runSplitAndScatter(scatterCtx, ssp.flowCtx, &ssp.spec, ssp.scatterer, ssp.doneScatterCh)
cancel()
close(ssp.doneScatterCh)
close(workerDone)
}); err != nil {
ssp.scatterErr = err
cancel()
close(workerDone)
}
}
type entryNode struct {
entry execinfrapb.RestoreSpanEntry
node roachpb.NodeID
}
// Next implements the execinfra.RowSource interface.
func (ssp *splitAndScatterProcessor) Next() (rowenc.EncDatumRow, *execinfrapb.ProducerMetadata) {
if ssp.State != execinfra.StateRunning {
return nil, ssp.DrainHelper()
}
scatteredEntry, ok := <-ssp.doneScatterCh
if ok {
entry := scatteredEntry.entry
entryBytes, err := protoutil.Marshal(&entry)
if err != nil {
ssp.MoveToDraining(err)
return nil, ssp.DrainHelper()
}
// The routing datums informs the router which output stream should be used.
routingDatum, ok := ssp.routingDatumCache[scatteredEntry.node]
if !ok {
routingDatum, _ = routingDatumsForSQLInstance(base.SQLInstanceID(scatteredEntry.node))
ssp.routingDatumCache[scatteredEntry.node] = routingDatum
}
row := rowenc.EncDatumRow{
routingDatum,
rowenc.DatumToEncDatum(types.Bytes, tree.NewDBytes(tree.DBytes(entryBytes))),
}
return row, nil
}
if ssp.scatterErr != nil {
ssp.MoveToDraining(ssp.scatterErr)
return nil, ssp.DrainHelper()
}
ssp.MoveToDraining(nil /* error */)
return nil, ssp.DrainHelper()
}
// ConsumerClosed is part of the RowSource interface.
func (ssp *splitAndScatterProcessor) ConsumerClosed() {
// The consumer is done, Next() will not be called again.
ssp.close()
}
// close stops the production workers. This needs to be called if the consumer
// runs into an error and stops consuming scattered entries to make sure we
// don't leak goroutines.
func (ssp *splitAndScatterProcessor) close() {
ssp.cancelScatterAndWaitForWorker()
ssp.InternalClose()
}
// scatteredChunk is the entries of a chunk of entries to process along with the
// node the chunk was scattered to.
type scatteredChunk struct {
destination roachpb.NodeID
entries []execinfrapb.RestoreSpanEntry
}
func runSplitAndScatter(
ctx context.Context,
flowCtx *execinfra.FlowCtx,
spec *execinfrapb.SplitAndScatterSpec,
scatterer splitAndScatterer,
doneScatterCh chan<- entryNode,
) error {
g := ctxgroup.WithContext(ctx)
importSpanChunksCh := make(chan scatteredChunk)
g.GoCtx(func(ctx context.Context) error {
// Chunks' leaseholders should be randomly placed throughout the
// cluster.
defer close(importSpanChunksCh)
for i, importSpanChunk := range spec.Chunks {
scatterKey := importSpanChunk.Entries[0].Span.Key
if i+1 < len(spec.Chunks) {
// Split at the start of the next chunk, to partition off a
// prefix of the space to scatter.
splitKey := spec.Chunks[i+1].Entries[0].Span.Key
if err := scatterer.split(ctx, flowCtx.Codec(), splitKey); err != nil {
return err
}
}
chunkDestination, err := scatterer.scatter(ctx, flowCtx.Codec(), scatterKey)
if err != nil {
return err
}
if chunkDestination == 0 {
// If scatter failed to find a node for range ingestion, route the range
// to the node currently running the split and scatter processor.
if nodeID, ok := flowCtx.NodeID.OptionalNodeID(); ok {
chunkDestination = nodeID
log.Warningf(ctx, "scatter returned node 0. "+
"Route span starting at %s to current node %v", scatterKey, nodeID)
} else {
log.Warningf(ctx, "scatter returned node 0. "+
"Route span starting at %s to default stream", scatterKey)
}
}
sc := scatteredChunk{
destination: chunkDestination,
entries: importSpanChunk.Entries,
}
select {
case <-ctx.Done():
return ctx.Err()
case importSpanChunksCh <- sc:
}
}
return nil
})
// TODO(pbardea): This tries to cover for a bad scatter by having 2 * the
// number of nodes in the cluster. Is it necessary?
splitScatterWorkers := 2
for worker := 0; worker < splitScatterWorkers; worker++ {
g.GoCtx(func(ctx context.Context) error {
for importSpanChunk := range importSpanChunksCh {
chunkDestination := importSpanChunk.destination
for i, importEntry := range importSpanChunk.entries {
nextChunkIdx := i + 1
log.VInfof(ctx, 2, "processing a span [%s,%s)", importEntry.Span.Key, importEntry.Span.EndKey)
var splitKey roachpb.Key
if nextChunkIdx < len(importSpanChunk.entries) {
// Split at the next entry.
splitKey = importSpanChunk.entries[nextChunkIdx].Span.Key
if err := scatterer.split(ctx, flowCtx.Codec(), splitKey); err != nil {
return err
}
}
scatteredEntry := entryNode{
entry: importEntry,
node: chunkDestination,
}
select {
case <-ctx.Done():
return ctx.Err()
case doneScatterCh <- scatteredEntry:
}
}
}
return nil
})
}
return g.Wait()
}
func routingDatumsForSQLInstance(
sqlInstanceID base.SQLInstanceID,
) (rowenc.EncDatum, rowenc.EncDatum) {
routingBytes := roachpb.Key(fmt.Sprintf("node%d", sqlInstanceID))
startDatum := rowenc.DatumToEncDatum(types.Bytes, tree.NewDBytes(tree.DBytes(routingBytes)))
endDatum := rowenc.DatumToEncDatum(types.Bytes, tree.NewDBytes(tree.DBytes(routingBytes.Next())))
return startDatum, endDatum
}
// routingSpanForSQLInstance provides the mapping to be used during distsql planning
// when setting up the output router.
func routingSpanForSQLInstance(sqlInstanceID base.SQLInstanceID) ([]byte, []byte, error) {
var alloc tree.DatumAlloc
startDatum, endDatum := routingDatumsForSQLInstance(sqlInstanceID)
startBytes, endBytes := make([]byte, 0), make([]byte, 0)
startBytes, err := startDatum.Encode(splitAndScatterOutputTypes[0], &alloc, catenumpb.DatumEncoding_ASCENDING_KEY, startBytes)
if err != nil {
return nil, nil, err
}
endBytes, err = endDatum.Encode(splitAndScatterOutputTypes[0], &alloc, catenumpb.DatumEncoding_ASCENDING_KEY, endBytes)
if err != nil {
return nil, nil, err
}
return startBytes, endBytes, nil
}
func init() {
rowexec.NewSplitAndScatterProcessor = newSplitAndScatterProcessor
}