-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathtask.go
239 lines (215 loc) · 8.88 KB
/
task.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
// Copyright 2018 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 rangefeed
import (
"context"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/storage/engine"
"github.com/cockroachdb/cockroach/pkg/storage/engine/enginepb"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/protoutil"
"github.com/pkg/errors"
)
// A runnable can be run as an async task.
type runnable interface {
// Run executes the runnable. Cannot be called multiple times.
Run(context.Context)
// Must be called if runnable is not Run.
Cancel()
}
// initResolvedTSScan scans over all keys using the provided iterator and
// informs the rangefeed Processor of any intents. This allows the Processor to
// backfill its unresolvedIntentQueue with any intents that were written before
// the Processor was started and hooked up to a stream of logical operations.
// The Processor can initialize its resolvedTimestamp once the scan completes
// because it knows it is now tracking all intents in its key range.
//
// Iterator Contract:
// The provided Iterator must observe all intents in the Processor's keyspan.
// An important implication of this is that if the iterator is a
// TimeBoundIterator, its MinTimestamp cannot be above the keyspan's largest
// known resolved timestamp, if one has ever been recorded. If one has never
// been recorded, the TimeBoundIterator cannot have any lower bound.
//
type initResolvedTSScan struct {
p *Processor
it engine.SimpleIterator
}
func newInitResolvedTSScan(p *Processor, it engine.SimpleIterator) runnable {
return &initResolvedTSScan{p: p, it: it}
}
func (s *initResolvedTSScan) Run(ctx context.Context) {
defer s.Cancel()
if err := s.iterateAndConsume(ctx); err != nil {
err = errors.Wrap(err, "initial resolved timestamp scan failed")
log.Error(ctx, err)
s.p.StopWithErr(roachpb.NewError(err))
} else {
// Inform the processor that its resolved timestamp can be initialized.
s.p.setResolvedTSInitialized()
}
}
func (s *initResolvedTSScan) iterateAndConsume(ctx context.Context) error {
startKey := engine.MakeMVCCMetadataKey(s.p.Span.Key.AsRawKey())
endKey := engine.MakeMVCCMetadataKey(s.p.Span.EndKey.AsRawKey())
// Iterate through all keys using NextKey. This will look at the first MVCC
// version for each key. We're only looking for MVCCMetadata versions, which
// will always be the first version of a key if it exists, so its fine that
// we skip over all other versions of keys.
var meta enginepb.MVCCMetadata
for s.it.Seek(startKey); ; s.it.NextKey() {
if ok, err := s.it.Valid(); err != nil {
return err
} else if !ok || !s.it.UnsafeKey().Less(endKey) {
break
}
// If the key is not a metadata key, ignore it.
unsafeKey := s.it.UnsafeKey()
if unsafeKey.IsValue() {
continue
}
// Found a metadata key. Unmarshal.
if err := protoutil.Unmarshal(s.it.UnsafeValue(), &meta); err != nil {
return errors.Wrapf(err, "unmarshaling mvcc meta: %v", unsafeKey)
}
// If this is an intent, inform the Processor.
if meta.Txn != nil {
var ops [1]enginepb.MVCCLogicalOp
ops[0].SetValue(&enginepb.MVCCWriteIntentOp{
TxnID: meta.Txn.ID,
TxnKey: meta.Txn.Key,
TxnMinTimestamp: meta.Txn.MinTimestamp,
Timestamp: meta.Txn.Timestamp,
})
s.p.sendEvent(event{ops: ops[:]}, 0 /* timeout */)
}
}
return nil
}
func (s *initResolvedTSScan) Cancel() {
s.it.Close()
}
// TxnPusher is capable of pushing transactions to a new timestamp and
// cleaning up the intents of transactions that are found to be committed.
type TxnPusher interface {
// PushTxns attempts to push the specified transactions to a new
// timestamp. It returns the resulting transaction protos.
PushTxns(context.Context, []enginepb.TxnMeta, hlc.Timestamp) ([]roachpb.Transaction, error)
// CleanupTxnIntentsAsync asynchronously cleans up intents owned
// by the specified transactions.
CleanupTxnIntentsAsync(context.Context, []roachpb.Transaction) error
}
// txnPushAttempt pushes all old transactions that have unresolved intents on
// the range which are blocking the resolved timestamp from moving forward. It
// does so in two steps.
// 1. it pushes all old transactions to the current timestamp and gathers
// up the transactions' authoritative transaction records.
// 2. for each transaction that is pushed, it checks the transaction's current
// status and reacts accordingly:
// - PENDING: inform the Processor that the transaction's timestamp has
// increased so that the transaction's intents no longer need
// to block the resolved timestamp. Even though the intents
// may still be at an older timestamp, we know that they can't
// commit at that timestamp.
// - COMMITTED: launch async processes to resolve the transaction's intents
// so they will be resolved sometime soon and unblock the
// resolved timestamp.
// - ABORTED: inform the Processor to stop caring about the transaction.
// It will never commit and its intents can be safely ignored.
type txnPushAttempt struct {
p *Processor
txns []enginepb.TxnMeta
ts hlc.Timestamp
doneC chan struct{}
}
func newTxnPushAttempt(
p *Processor, txns []enginepb.TxnMeta, ts hlc.Timestamp, doneC chan struct{},
) runnable {
return &txnPushAttempt{
p: p,
txns: txns,
ts: ts,
doneC: doneC,
}
}
func (a *txnPushAttempt) Run(ctx context.Context) {
defer a.Cancel()
if err := a.pushOldTxns(ctx); err != nil {
log.Error(ctx, errors.Wrap(err, "pushing old intents failed"))
}
}
func (a *txnPushAttempt) pushOldTxns(ctx context.Context) error {
// Push all transactions using the TxnPusher to the current time.
// This may cause transaction restarts, but span refreshing should
// prevent a restart for any transaction that has not been written
// over at a larger timestamp.
pushedTxns, err := a.p.TxnPusher.PushTxns(ctx, a.txns, a.ts)
if err != nil {
return err
}
// Inform the Processor of the results of the push for each transaction.
ops := make([]enginepb.MVCCLogicalOp, len(pushedTxns))
var toCleanup []roachpb.Transaction
for i, txn := range pushedTxns {
switch txn.Status {
case roachpb.PENDING, roachpb.STAGING:
// The transaction is still in progress but its timestamp was moved
// forward to the current time. Inform the Processor that it can
// forward the txn's timestamp in its unresolvedIntentQueue.
ops[i].SetValue(&enginepb.MVCCUpdateIntentOp{
TxnID: txn.ID,
Timestamp: txn.Timestamp,
})
case roachpb.COMMITTED:
// The transaction is committed and its timestamp may have moved
// forward since we last saw an intent. Inform the Processor
// immediately in case this is the transaction that is holding back
// the resolved timestamp. However, we still need to wait for the
// transaction's intents to actually be resolved.
ops[i].SetValue(&enginepb.MVCCUpdateIntentOp{
TxnID: txn.ID,
Timestamp: txn.Timestamp,
})
// Asynchronously clean up the transaction's intents, which should
// eventually cause all unresolved intents for this transaction on the
// rangefeed's range to be resolved. We'll have to wait until the
// intents are resolved before the resolved timestamp can advance past
// the transaction's commit timestamp, so the best we can do is help
// speed up the resolution.
toCleanup = append(toCleanup, txn)
case roachpb.ABORTED:
// The transaction is aborted, so it doesn't need to be tracked
// anymore nor does it need to prevent the resolved timestamp from
// advancing. Inform the Processor that it can remove the txn from
// its unresolvedIntentQueue.
//
// NOTE: the unresolvedIntentQueue will ignore MVCCAbortTxn operations
// before it has been initialized. This is not a concern here though
// because we never launch txnPushAttempt tasks before the queue has
// been initialized.
ops[i].SetValue(&enginepb.MVCCAbortTxnOp{
TxnID: txn.ID,
})
// While we're here, we might as well also clean up the transaction's
// intents so that no-one else needs to deal with them. However, it's
// likely that if our push caused the abort then the transaction's
// intents will be unknown and we won't be doing much good here.
toCleanup = append(toCleanup, txn)
}
}
// Inform the processor of all logical ops.
a.p.sendEvent(event{ops: ops}, 0 /* timeout */)
// Clean up txns, if necessary,
return a.p.TxnPusher.CleanupTxnIntentsAsync(ctx, toCleanup)
}
func (a *txnPushAttempt) Cancel() {
close(a.doneC)
}