-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathseparated_intents.go
445 lines (398 loc) · 13.5 KB
/
separated_intents.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
// Copyright 2021 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package migrations
import (
"bytes"
"context"
"fmt"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/cockroachdb/cockroach/pkg/clusterversion"
"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/kv/kvserver/intentresolver"
"github.com/cockroachdb/cockroach/pkg/migration"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/storage/enginepb"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/stop"
"github.com/cockroachdb/cockroach/pkg/util/syncutil"
"github.com/cockroachdb/cockroach/pkg/util/uuid"
"github.com/cockroachdb/errors"
)
// The number of concurrent migrateLockTableRequests requests to run. This
// is effectively a cluster-wide setting as the actual legwork of the migration
// happens when the destination replica(s) are sending replies back to the
// original node.
//
// TODO(bilal): Add logic to make this concurrency limit a per-leaseholder limit
// as opposed to a cluster-wide limit. That way, we could limit
// migrateLockTableRequests to 1 per leaseholder as opposed to 4 for the entire
// cluster, avoiding the case where all 4 ranges at a time could have the same node
// as their leaseholder.
const concurrentMigrateLockTableRequests = 4
// The maximum number of times to retry a migrateLockTableRequest before failing
// the migration.
const migrateLockTableRetries = 3
// migrateLockTableRequest represents migration of one slice of the keyspace. As
// part of this request, multiple non-transactional requests would need to be
// run: a Barrier, a ScanInterleavedIntents, then multiple txn pushes and intent
// resolutions.
//
// One request will correspond to one range at the time of running the
// IterateRangeDescriptors command. If range boundaries change during the
// course of the migration, that is okay as the migration logic does not rely on
// that assumption. The debugRangeID is the range ID for this range at the time
// of the range descriptor iteration, and is
// present solely for observability / logging purposes.
type migrateLockTableRequest struct {
start, end roachpb.Key
debugRangeID roachpb.RangeID
barrierDone bool
barrierTS hlc.Timestamp
}
type intentResolver interface {
PushTransaction(
ctx context.Context, pushTxn *enginepb.TxnMeta, h roachpb.Header, pushType roachpb.PushTxnType,
) (*roachpb.Transaction, *roachpb.Error)
ResolveIntents(
ctx context.Context, intents []roachpb.LockUpdate, opts intentresolver.ResolveOptions,
) (pErr *roachpb.Error)
}
type migrateLockTablePool struct {
requests chan migrateLockTableRequest
wg sync.WaitGroup
stopper *stop.Stopper
ir intentResolver
db *kv.DB
clock *hlc.Clock
done chan bool
status [concurrentMigrateLockTableRequests]int64
mu struct {
syncutil.Mutex
errorCount int
combinedErr error
}
}
func (m *migrateLockTablePool) runMigrateRequestsForRanges(
ctx context.Context, ri rangeIterator, concurrentRequests int,
) (int, error) {
var numMigratedRanges int
m.wg.Add(concurrentRequests)
for i := 0; i < concurrentRequests; i++ {
idx := i // Copy for closure below.
taskName := fmt.Sprintf("migrate-lock-table-%d", i)
if err := m.stopper.RunAsyncTask(ctx, taskName, func(ctx context.Context) {
m.run(ctx, idx)
}); err != nil {
return 0, err
}
}
m.startStatusLogger(ctx)
defer m.wg.Wait()
defer m.stopStatusLogger()
rs := roachpb.RSpan{Key: roachpb.RKeyMin, EndKey: roachpb.RKeyMax}
for ri.Seek(ctx, roachpb.RKeyMin, kvcoord.Ascending); ri.Valid(); ri.Next(ctx) {
desc := ri.Desc()
start, end := desc.StartKey, desc.EndKey
if ignoreSeparatedIntentsMigrationForRange(start, end) {
continue
}
{
startKeyRaw := desc.StartKey.AsRawKey()
if bytes.Compare(desc.StartKey, keys.LocalMax) < 0 {
startKeyRaw = keys.LocalMax
}
request := migrateLockTableRequest{
start: startKeyRaw,
end: end.AsRawKey(),
debugRangeID: desc.RangeID,
}
select {
case m.requests <- request:
case <-ctx.Done():
return numMigratedRanges, errors.Wrap(ctx.Err(), "lock table migration canceled")
}
}
{
// Also enqueue a request for range local keys.
rangeKeyStart := keys.MakeRangeKeyPrefix(desc.StartKey)
rangeKeyEnd := keys.MakeRangeKeyPrefix(desc.EndKey)
request := migrateLockTableRequest{
start: rangeKeyStart,
end: rangeKeyEnd,
debugRangeID: desc.RangeID,
}
select {
case m.requests <- request:
case <-ctx.Done():
return numMigratedRanges, errors.Wrap(ctx.Err(), "lock table migration canceled")
}
}
numMigratedRanges++
if !ri.NeedAnother(rs) {
break
}
}
if err := ri.Error(); err != nil {
log.Errorf(ctx, "error when iterating through ranges in lock table migration: %s", err)
close(m.requests)
return numMigratedRanges, err
}
close(m.requests)
return numMigratedRanges, nil
}
func (m *migrateLockTablePool) attemptMigrateRequest(
ctx context.Context, req *migrateLockTableRequest,
) (nextReq *migrateLockTableRequest, err error) {
// The barrier command needs to be invoked if it hasn't been invoked on this
// key range yet. This command does not return a resume span, so once it has
// returned successfully, it doesn't need to be called again unless there's
// an error.
barrierTS := req.barrierTS
if !req.barrierDone {
var err error
barrierTS, err = m.db.Barrier(ctx, req.start, req.end)
if err != nil {
return nil, errors.Wrap(err, "error when invoking Barrier command")
}
}
barrierTS.Forward(m.clock.Now())
req.barrierDone = true
intents, resumeSpan, err := m.db.ScanInterleavedIntents(ctx, req.start, req.end, barrierTS)
if err != nil {
return nil, errors.Wrap(err, "error when invoking ScanInterleavedIntents command")
}
txnIntents := make(map[uuid.UUID][]roachpb.Intent)
for _, intent := range intents {
txnIntents[intent.Txn.ID] = append(txnIntents[intent.Txn.ID], intent)
}
for _, intents := range txnIntents {
txn := &intents[0].Txn
// Create a request for a PushTxn request of type PUSH_ABORT. If this
// transaction is still running, it will abort. The retry of that
// transaction will then write separated intents.
h := roachpb.Header{
Timestamp: m.clock.Now(),
UserPriority: roachpb.MinUserPriority,
}
pushedTxn, err := m.ir.PushTransaction(ctx, txn, h, roachpb.PUSH_ABORT)
if err != nil {
return nil, err.GoError()
}
lockUpdates := make([]roachpb.LockUpdate, 0, len(intents))
for _, intent := range intents {
resolve := roachpb.MakeLockUpdate(pushedTxn, roachpb.Span{Key: intent.Key})
lockUpdates = append(lockUpdates, resolve)
}
opts := intentresolver.ResolveOptions{Poison: true}
if err := m.ir.ResolveIntents(ctx, lockUpdates, opts); err != nil {
return nil, err.GoError()
}
}
if resumeSpan != nil {
nextReq = req
nextReq.start = resumeSpan.Key
nextReq.end = resumeSpan.EndKey
nextReq.barrierDone = true
nextReq.barrierTS = barrierTS
}
return nextReq, nil
}
func (m *migrateLockTablePool) run(ctx context.Context, workerIdx int) {
defer m.wg.Done()
ctx, cancel := m.stopper.WithCancelOnQuiesce(ctx)
defer cancel()
var retryRequest *migrateLockTableRequest
retryAttempt := 0
statusSlot := &m.status[workerIdx]
atomic.StoreInt64(statusSlot, 0)
for {
if retryRequest == nil {
// Pull a new request out of the channel.
select {
case r, ok := <-m.requests:
if !ok {
return
}
retryRequest = &r
retryAttempt = 0
case <-ctx.Done():
log.Warningf(ctx, "lock table migration canceled")
return
}
}
if ctx.Err() != nil {
log.Warningf(ctx, "lock table migration canceled on range r%d", retryRequest.debugRangeID)
return
}
atomic.StoreInt64(statusSlot, int64(retryRequest.debugRangeID))
handleError := func(err error) {
log.Errorf(ctx, "error when running migrate lock table command for range r%d: %s",
retryRequest.debugRangeID, err)
retryAttempt++
if retryAttempt >= migrateLockTableRetries {
// Report this error to the migration manager. This will cause the
// whole migration to be retried later. In the meantime, continue
// migrating any other ranges in the queue, instead of stalling the
// pipeline.
m.mu.Lock()
// Limit the number of errors chained. This prevents excessive memory
// usage in case of error blowup (rangeCount * migrateLockTableRetries).
if m.mu.errorCount < 16 {
m.mu.combinedErr = errors.CombineErrors(m.mu.combinedErr, err)
}
m.mu.errorCount++
m.mu.Unlock()
retryAttempt = 0
retryRequest = nil
atomic.StoreInt64(statusSlot, 0)
}
}
nextReq, err := m.attemptMigrateRequest(ctx, retryRequest)
if err != nil {
handleError(err)
continue
} else {
retryRequest = nextReq
retryAttempt = 0
atomic.StoreInt64(statusSlot, 0)
}
}
}
func (m *migrateLockTablePool) startStatusLogger(ctx context.Context) {
m.done = make(chan bool)
m.wg.Add(1)
_ = m.stopper.RunAsyncTask(ctx, "migrate-lock-table-status", m.runStatusLogger)
}
func (m *migrateLockTablePool) stopStatusLogger() {
close(m.done)
}
func (m *migrateLockTablePool) runStatusLogger(ctx context.Context) {
defer m.wg.Done()
ctx, cancel := m.stopper.WithCancelOnQuiesce(ctx)
defer cancel()
const statusTickDuration = 5 * time.Second
ticker := time.NewTicker(statusTickDuration)
defer ticker.Stop()
for {
select {
case <-ticker.C:
var ranges strings.Builder
for i := 0; i < concurrentMigrateLockTableRequests; i++ {
rangeID := atomic.LoadInt64(&m.status[i])
if rangeID == 0 {
continue
}
if ranges.Len() != 0 {
fmt.Fprintf(&ranges, ", ")
}
fmt.Fprintf(&ranges, "%s", roachpb.RangeID(rangeID))
}
if ranges.Len() > 0 {
log.Infof(ctx, "currently migrating lock table on ranges %s", ranges.String())
}
case <-m.done:
return
case <-ctx.Done():
return
}
}
}
// rangeIterator provides a not-necessarily-transactional view of KV ranges
// spanning a key range.
type rangeIterator interface {
Desc() *roachpb.RangeDescriptor
Error() error
NeedAnother(rs roachpb.RSpan) bool
Next(ctx context.Context)
Seek(ctx context.Context, key roachpb.RKey, scanDir kvcoord.ScanDirection)
Valid() bool
}
// ignoreSeparatedIntentsMigrationForRange returns true if the migration should
// be skipped for this range. Only returns true for range containing timeseries
// keys; those ranges are guaranteed to not contain intents.
func ignoreSeparatedIntentsMigrationForRange(start, end roachpb.RKey) bool {
return bytes.HasPrefix(start, keys.TimeseriesPrefix) && bytes.HasPrefix(end, keys.TimeseriesPrefix)
}
func runSeparatedIntentsMigration(
ctx context.Context,
clock *hlc.Clock,
stopper *stop.Stopper,
db *kv.DB,
ri rangeIterator,
ir intentResolver,
) error {
workerPool := migrateLockTablePool{
requests: make(chan migrateLockTableRequest, concurrentMigrateLockTableRequests),
stopper: stopper,
db: db,
ir: ir,
clock: clock,
}
migratedRanges, err := workerPool.runMigrateRequestsForRanges(ctx, ri, concurrentMigrateLockTableRequests)
if err != nil {
return err
}
if workerPool.mu.combinedErr != nil {
return workerPool.mu.combinedErr
}
log.Infof(ctx, "finished lock table migrations for %d ranges", migratedRanges)
return nil
}
func separatedIntentsMigration(
ctx context.Context, cv clusterversion.ClusterVersion, deps migration.SystemDeps,
) error {
ir := intentresolver.New(intentresolver.Config{
Clock: deps.DB.Clock(),
Stopper: deps.Stopper,
RangeDescriptorCache: deps.DistSender.RangeDescriptorCache(),
DB: deps.DB,
})
ri := kvcoord.NewRangeIterator(deps.DistSender)
return runSeparatedIntentsMigration(ctx, deps.DB.Clock(), deps.Stopper, deps.DB, ri, ir)
}
func postSeparatedIntentsMigration(
ctx context.Context, cv clusterversion.ClusterVersion, deps migration.SystemDeps,
) error {
var batchIdx, numMigratedRanges int
init := func() { batchIdx, numMigratedRanges = 1, 0 }
// Issue no-op Migrate commands to all ranges. This has the only
// purpose of clearing out any orphaned replicas, preventing interleaved
// intents in them from resurfacing.
if err := deps.Cluster.IterateRangeDescriptors(ctx, defaultPageSize, init, func(descriptors ...roachpb.RangeDescriptor) error {
for _, desc := range descriptors {
start, end := desc.StartKey, desc.EndKey
if bytes.Compare(desc.StartKey, keys.LocalMax) < 0 {
start, _ = keys.Addr(keys.LocalMax)
}
// Check if this range is a timeseries range. If it is, we can just skip
// it - it will not contain any intents.
if bytes.HasPrefix(start, keys.TimeseriesPrefix) && bytes.HasPrefix(end, keys.TimeseriesPrefix) {
continue
}
if err := deps.DB.Migrate(ctx, start, end, cv.Version); err != nil {
return err
}
}
numMigratedRanges += len(descriptors)
log.Infof(ctx, "[batch %d/??] started no-op migrations for %d ranges", batchIdx, numMigratedRanges)
batchIdx++
return nil
}); err != nil {
return err
}
log.Infof(ctx, "[batch %d/%d] finished no-op migrations for %d ranges", batchIdx, batchIdx, numMigratedRanges)
return nil
}