-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
backup_processor.go
581 lines (525 loc) · 20.1 KB
/
backup_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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
// 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"
"time"
"github.com/cockroachdb/cockroach/pkg/ccl/backupccl/backuppb"
"github.com/cockroachdb/cockroach/pkg/cloud"
"github.com/cockroachdb/cockroach/pkg/kv"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/batcheval"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/concurrency/lock"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/settings"
"github.com/cockroachdb/cockroach/pkg/sql"
"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/types"
"github.com/cockroachdb/cockroach/pkg/util/admission/admissionpb"
"github.com/cockroachdb/cockroach/pkg/util/contextutil"
"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/mon"
"github.com/cockroachdb/cockroach/pkg/util/stop"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/cockroach/pkg/util/tracing"
"github.com/cockroachdb/errors"
"github.com/cockroachdb/logtags"
"github.com/kr/pretty"
)
var backupOutputTypes = []*types.T{}
var (
priorityAfter = settings.RegisterDurationSetting(
settings.TenantWritable,
"bulkio.backup.read_with_priority_after",
"amount of time since the read-as-of time above which a BACKUP should use priority when retrying reads",
time.Minute,
settings.NonNegativeDuration,
).WithPublic()
delayPerAttmpt = settings.RegisterDurationSetting(
settings.TenantWritable,
"bulkio.backup.read_retry_delay",
"amount of time since the read-as-of time, per-prior attempt, to wait before making another attempt",
time.Second*5,
settings.NonNegativeDuration,
)
timeoutPerAttempt = settings.RegisterDurationSetting(
settings.TenantWritable,
"bulkio.backup.read_timeout",
"amount of time after which a read attempt is considered timed out, which causes the backup to fail",
time.Minute*5,
settings.NonNegativeDuration,
).WithPublic()
targetFileSize = settings.RegisterByteSizeSetting(
settings.TenantWritable,
"bulkio.backup.file_size",
"target size for individual data files produced during BACKUP",
128<<20,
).WithPublic()
splitKeysOnTimestamps = settings.RegisterBoolSetting(
settings.TenantWritable,
"bulkio.backup.split_keys_on_timestamps",
"split backup data on timestamps when writing revision history",
true,
)
)
const backupProcessorName = "backupDataProcessor"
// TODO(pbardea): It would be nice if we could add some DistSQL processor tests
// we would probably want to have a mock cloudStorage object that we could
// verify with.
// backupDataProcessor represents the work each node in a cluster performs
// during a BACKUP. It is assigned a set of spans to export to a given URI. It
// will create parallel workers (whose parallelism is also specified), which
// will each export a span at a time. After exporting the span, it will stream
// back its progress through the metadata channel provided by DistSQL.
type backupDataProcessor struct {
execinfra.ProcessorBase
flowCtx *execinfra.FlowCtx
spec execinfrapb.BackupDataSpec
output execinfra.RowReceiver
// cancelAndWaitForWorker cancels the producer goroutine and waits for it to
// finish. It can be called multiple times.
cancelAndWaitForWorker func()
progCh chan execinfrapb.RemoteProducerMetadata_BulkProcessorProgress
backupErr error
// BoundAccount that reserves the memory usage of the backup processor.
memAcc *mon.BoundAccount
}
var (
_ execinfra.Processor = &backupDataProcessor{}
_ execinfra.RowSource = &backupDataProcessor{}
)
func newBackupDataProcessor(
flowCtx *execinfra.FlowCtx,
processorID int32,
spec execinfrapb.BackupDataSpec,
post *execinfrapb.PostProcessSpec,
output execinfra.RowReceiver,
) (execinfra.Processor, error) {
memMonitor := flowCtx.Cfg.BackupMonitor
if knobs, ok := flowCtx.TestingKnobs().BackupRestoreTestingKnobs.(*sql.BackupRestoreTestingKnobs); ok {
if knobs.BackupMemMonitor != nil {
memMonitor = knobs.BackupMemMonitor
}
}
ba := memMonitor.MakeBoundAccount()
bp := &backupDataProcessor{
flowCtx: flowCtx,
spec: spec,
output: output,
progCh: make(chan execinfrapb.RemoteProducerMetadata_BulkProcessorProgress),
memAcc: &ba,
}
if err := bp.Init(bp, post, backupOutputTypes, flowCtx, processorID, output, nil, /* memMonitor */
execinfra.ProcStateOpts{
// This processor doesn't have any inputs to drain.
InputsToDrain: nil,
TrailingMetaCallback: func() []execinfrapb.ProducerMetadata {
bp.close()
return nil
},
}); err != nil {
return nil, err
}
return bp, nil
}
// Start is part of the RowSource interface.
func (bp *backupDataProcessor) Start(ctx context.Context) {
ctx = logtags.AddTag(ctx, "job", bp.spec.JobID)
ctx = bp.StartInternal(ctx, backupProcessorName)
ctx, cancel := context.WithCancel(ctx)
bp.cancelAndWaitForWorker = func() {
cancel()
for range bp.progCh {
}
}
log.Infof(ctx, "starting backup data")
if err := bp.flowCtx.Stopper().RunAsyncTaskEx(ctx, stop.TaskOpts{
TaskName: "backup-worker",
SpanOpt: stop.ChildSpan,
}, func(ctx context.Context) {
bp.backupErr = runBackupProcessor(ctx, bp.flowCtx, &bp.spec, bp.progCh, bp.memAcc)
cancel()
close(bp.progCh)
}); err != nil {
// The closure above hasn't run, so we have to do the cleanup.
bp.backupErr = err
cancel()
close(bp.progCh)
}
}
// Next is part of the RowSource interface.
func (bp *backupDataProcessor) Next() (rowenc.EncDatumRow, *execinfrapb.ProducerMetadata) {
if bp.State != execinfra.StateRunning {
return nil, bp.DrainHelper()
}
for prog := range bp.progCh {
// Take a copy so that we can send the progress address to the output
// processor.
p := prog
return nil, &execinfrapb.ProducerMetadata{BulkProcessorProgress: &p}
}
if bp.backupErr != nil {
bp.MoveToDraining(bp.backupErr)
return nil, bp.DrainHelper()
}
bp.MoveToDraining(nil /* error */)
return nil, bp.DrainHelper()
}
func (bp *backupDataProcessor) close() {
bp.cancelAndWaitForWorker()
if bp.InternalClose() {
bp.memAcc.Close(bp.Ctx)
}
}
// ConsumerClosed is part of the RowSource interface. We have to override the
// implementation provided by ProcessorBase.
func (bp *backupDataProcessor) ConsumerClosed() {
bp.close()
}
type spanAndTime struct {
// spanIdx is a unique identifier of this object.
spanIdx int
span roachpb.Span
firstKeyTS hlc.Timestamp
start, end hlc.Timestamp
attempts int
lastTried time.Time
}
type exportedSpan struct {
metadata backuppb.BackupManifest_File
dataSST []byte
revStart hlc.Timestamp
completedSpans int32
atKeyBoundary bool
}
func runBackupProcessor(
ctx context.Context,
flowCtx *execinfra.FlowCtx,
spec *execinfrapb.BackupDataSpec,
progCh chan execinfrapb.RemoteProducerMetadata_BulkProcessorProgress,
memAcc *mon.BoundAccount,
) error {
backupProcessorSpan := tracing.SpanFromContext(ctx)
clusterSettings := flowCtx.Cfg.Settings
totalSpans := len(spec.Spans) + len(spec.IntroducedSpans)
todo := make(chan spanAndTime, totalSpans)
var spanIdx int
for _, s := range spec.IntroducedSpans {
todo <- spanAndTime{
spanIdx: spanIdx, span: s, firstKeyTS: hlc.Timestamp{}, start: hlc.Timestamp{},
end: spec.BackupStartTime,
}
spanIdx++
}
for _, s := range spec.Spans {
todo <- spanAndTime{
spanIdx: spanIdx, span: s, firstKeyTS: hlc.Timestamp{}, start: spec.BackupStartTime,
end: spec.BackupEndTime,
}
spanIdx++
}
destURI := spec.DefaultURI
var destLocalityKV string
if len(spec.URIsByLocalityKV) > 0 {
var localitySinkURI string
// When matching, more specific KVs in the node locality take precedence
// over less specific ones so search back to front.
for i := len(flowCtx.EvalCtx.Locality.Tiers) - 1; i >= 0; i-- {
tier := flowCtx.EvalCtx.Locality.Tiers[i].String()
if dest, ok := spec.URIsByLocalityKV[tier]; ok {
localitySinkURI = dest
destLocalityKV = tier
break
}
}
if localitySinkURI != "" {
log.Infof(ctx, "backing up %d spans to destination specified by locality %s", totalSpans, destLocalityKV)
destURI = localitySinkURI
} else {
nodeLocalities := make([]string, 0, len(flowCtx.EvalCtx.Locality.Tiers))
for _, i := range flowCtx.EvalCtx.Locality.Tiers {
nodeLocalities = append(nodeLocalities, i.String())
}
backupLocalities := make([]string, 0, len(spec.URIsByLocalityKV))
for i := range spec.URIsByLocalityKV {
backupLocalities = append(backupLocalities, i)
}
log.Infof(ctx, "backing up %d spans to default locality because backup localities %s have no match in node's localities %s", totalSpans, backupLocalities, nodeLocalities)
}
}
dest, err := cloud.ExternalStorageConfFromURI(destURI, spec.User())
if err != nil {
return err
}
returnedSpansChan := make(chan exportedSpan, 1)
grp := ctxgroup.WithContext(ctx)
// Start a goroutine that will then start a group of goroutines which each
// pull spans off of `todo` and send export requests. Any resume spans are put
// back on `todo`. Any returned SSTs are put on a `returnedSpansChan` to be routed
// to a buffered sink that merges them until they are large enough to flush.
grp.GoCtx(func(ctx context.Context) error {
defer close(returnedSpansChan)
// TODO(pbardea): Check to see if this benefits from any tuning (e.g. +1, or
// *2). See #49798.
numSenders := int(kvserver.ExportRequestsLimit.Get(&clusterSettings.SV)) * 2
return ctxgroup.GroupWorkers(ctx, numSenders, func(ctx context.Context, _ int) error {
readTime := spec.BackupEndTime.GoTime()
// priority becomes true when we're sending re-attempts of reads far enough
// in the past that we want to run them with priority.
var priority bool
timer := timeutil.NewTimer()
defer timer.Stop()
ctxDone := ctx.Done()
for {
select {
case <-ctxDone:
return ctx.Err()
case span := <-todo:
header := roachpb.Header{Timestamp: span.end}
splitMidKey := splitKeysOnTimestamps.Get(&clusterSettings.SV)
// If we started splitting already, we must continue until we reach the end
// of split span.
if !span.firstKeyTS.IsEmpty() {
splitMidKey = true
}
req := &roachpb.ExportRequest{
RequestHeader: roachpb.RequestHeaderFromSpan(span.span),
ResumeKeyTS: span.firstKeyTS,
StartTime: span.start,
EnableTimeBoundIteratorOptimization: true, // NB: Must set for 22.1 compatibility.
MVCCFilter: spec.MVCCFilter,
TargetFileSize: batcheval.ExportRequestTargetFileSize.Get(&clusterSettings.SV),
ReturnSST: true,
SplitMidKey: splitMidKey,
}
// If we're doing re-attempts but are not yet in the priority regime,
// check to see if it is time to switch to priority.
if !priority && span.attempts > 0 {
// Check if this is starting a new pass and we should delay first.
// We're okay with delaying this worker until then since we assume any
// other work it could pull off the queue will likely want to delay to
// a similar or later time anyway.
if delay := delayPerAttmpt.Get(&clusterSettings.SV) - timeutil.Since(span.lastTried); delay > 0 {
timer.Reset(delay)
log.Infof(ctx, "waiting %s to start attempt %d of remaining spans", delay, span.attempts+1)
select {
case <-ctxDone:
return ctx.Err()
case <-timer.C:
timer.Read = true
}
}
priority = timeutil.Since(readTime) > priorityAfter.Get(&clusterSettings.SV)
}
if priority {
// This re-attempt is reading far enough in the past that we just want
// to abort any transactions it hits.
header.UserPriority = roachpb.MaxUserPriority
} else {
// On the initial attempt to export this span and re-attempts that are
// done while it is still less than the configured time above the read
// time, we set WaitPolicy to Error, so that the export will return an
// error to us instead of instead doing blocking wait if it hits any
// other txns. This lets us move on to other ranges we have to export,
// provide an indication of why we're blocked, etc instead and come
// back to this range later.
header.WaitPolicy = lock.WaitPolicy_Error
}
// We set the DistSender response target bytes field to a sentinel
// value. The sentinel value of 1 forces the ExportRequest to paginate
// after creating a single SST.
header.TargetBytes = 1
admissionHeader := roachpb.AdmissionHeader{
// Export requests are currently assigned NormalPri.
//
// TODO(dt): Consider linking this to/from the UserPriority field.
Priority: int32(admissionpb.BulkNormalPri),
CreateTime: timeutil.Now().UnixNano(),
Source: roachpb.AdmissionHeader_FROM_SQL,
NoMemoryReservedAtSource: true,
}
log.Infof(ctx, "sending ExportRequest for span %s (attempt %d, priority %s)",
span.span, span.attempts+1, header.UserPriority.String())
var rawResp roachpb.Response
var pErr *roachpb.Error
var reqSentTime time.Time
var respReceivedTime time.Time
exportRequestErr := contextutil.RunWithTimeout(ctx,
fmt.Sprintf("ExportRequest for span %s", span.span),
timeoutPerAttempt.Get(&clusterSettings.SV), func(ctx context.Context) error {
reqSentTime = timeutil.Now()
backupProcessorSpan.RecordStructured(&backuppb.BackupExportTraceRequestEvent{
Span: span.span.String(),
Attempt: int32(span.attempts + 1),
Priority: header.UserPriority.String(),
ReqSentTime: reqSentTime.String(),
})
rawResp, pErr = kv.SendWrappedWithAdmission(
ctx, flowCtx.Cfg.DB.NonTransactionalSender(), header, admissionHeader, req)
respReceivedTime = timeutil.Now()
if pErr != nil {
return pErr.GoError()
}
return nil
})
if exportRequestErr != nil {
if intentErr, ok := pErr.GetDetail().(*roachpb.WriteIntentError); ok {
span.lastTried = timeutil.Now()
span.attempts++
todo <- span
// TODO(dt): send a progress update to update job progress to note
// the intents being hit.
backupProcessorSpan.RecordStructured(&backuppb.BackupExportTraceResponseEvent{
RetryableError: tracing.RedactAndTruncateError(intentErr),
})
continue
}
// TimeoutError improves the opaque `context deadline exceeded` error
// message so use that instead.
if errors.HasType(exportRequestErr, (*contextutil.TimeoutError)(nil)) {
return errors.Wrap(exportRequestErr, "export request timeout")
}
// BatchTimestampBeforeGCError is returned if the ExportRequest
// attempts to read below the range's GC threshold.
if batchTimestampBeforeGCError, ok := pErr.GetDetail().(*roachpb.BatchTimestampBeforeGCError); ok {
// If the range we are exporting is marked to be excluded from
// backup, it is safe to ignore the error. It is likely that the
// table has been configured with a low GC TTL, and so the data
// the backup is targeting has already been gc'ed.
if batchTimestampBeforeGCError.DataExcludedFromBackup {
continue
}
}
return errors.Wrapf(exportRequestErr, "exporting %s", span.span)
}
resp := rawResp.(*roachpb.ExportResponse)
// If the reply has a resume span, put the remaining span on
// todo to be picked up again in the next round.
if resp.ResumeSpan != nil {
if !resp.ResumeSpan.Valid() {
return errors.Errorf("invalid resume span: %s", resp.ResumeSpan)
}
resumeTS := hlc.Timestamp{}
// Taking resume timestamp from the last file of response since files must
// always be consecutive even if we currently expect only one.
if fileCount := len(resp.Files); fileCount > 0 {
resumeTS = resp.Files[fileCount-1].EndKeyTS
}
resumeSpan := spanAndTime{
span: *resp.ResumeSpan,
firstKeyTS: resumeTS,
start: span.start,
end: span.end,
attempts: span.attempts,
lastTried: span.lastTried,
}
todo <- resumeSpan
}
if backupKnobs, ok := flowCtx.TestingKnobs().BackupRestoreTestingKnobs.(*sql.BackupRestoreTestingKnobs); ok {
if backupKnobs.RunAfterExportingSpanEntry != nil {
backupKnobs.RunAfterExportingSpanEntry(ctx, resp)
}
}
var completedSpans int32
if resp.ResumeSpan == nil {
completedSpans = 1
}
duration := respReceivedTime.Sub(reqSentTime)
exportResponseTraceEvent := &backuppb.BackupExportTraceResponseEvent{
Duration: duration.String(),
FileSummaries: make([]roachpb.RowCount, 0),
}
if len(resp.Files) > 1 {
log.Warning(ctx, "unexpected multi-file response using header.TargetBytes = 1")
}
for i, file := range resp.Files {
entryCounts := countRows(file.Exported, spec.PKIDs)
exportResponseTraceEvent.FileSummaries = append(exportResponseTraceEvent.FileSummaries, entryCounts)
ret := exportedSpan{
// BackupManifest_File just happens to contain the exact fields
// to store the metadata we need, but there's no actual File
// on-disk anywhere yet.
metadata: backuppb.BackupManifest_File{
Span: file.Span,
Path: file.Path,
EntryCounts: entryCounts,
},
dataSST: file.SST,
revStart: resp.StartTime,
atKeyBoundary: file.EndKeyTS.IsEmpty()}
if span.start != spec.BackupStartTime {
ret.metadata.StartTime = span.start
ret.metadata.EndTime = span.end
}
// If multiple files were returned for this span, only one -- the
// last -- should count as completing the requested span.
if i == len(resp.Files)-1 {
ret.completedSpans = completedSpans
}
select {
case returnedSpansChan <- ret:
case <-ctxDone:
return ctx.Err()
}
}
exportResponseTraceEvent.NumFiles = int32(len(resp.Files))
backupProcessorSpan.RecordStructured(exportResponseTraceEvent)
default:
// No work left to do, so we can exit. Note that another worker could
// still be running and may still push new work (a retry) on to todo but
// that is OK, since that also means it is still running and thus can
// pick up that work on its next iteration.
return nil
}
}
})
})
// Start another goroutine which will read from returnedSpansChan ch and push
// ssts from it into an fileSSTSink responsible for actually writing their
// contents to cloud storage.
grp.GoCtx(func(ctx context.Context) error {
sinkConf := sstSinkConf{
id: flowCtx.NodeID.SQLInstanceID(),
enc: spec.Encryption,
progCh: progCh,
settings: &flowCtx.Cfg.Settings.SV,
}
storage, err := flowCtx.Cfg.ExternalStorage(ctx, dest)
if err != nil {
return err
}
sink, err := makeFileSSTSink(ctx, sinkConf, storage, memAcc)
if err != nil {
return err
}
defer func() {
err := sink.Close()
err = errors.CombineErrors(storage.Close(), err)
if err != nil {
log.Warningf(ctx, "failed to close backup sink(s): % #v", pretty.Formatter(err))
}
}()
for returnedSpans := range returnedSpansChan {
returnedSpans.metadata.LocalityKV = destLocalityKV
if err := sink.push(ctx, returnedSpans); err != nil {
return err
}
}
return sink.flush(ctx)
})
return grp.Wait()
}
func init() {
rowexec.NewBackupDataProcessor = newBackupDataProcessor
}