-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
txn_interceptor_span_refresher.go
378 lines (345 loc) · 14 KB
/
txn_interceptor_span_refresher.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
// Copyright 2018 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
package kv
import (
"context"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/settings"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/metric"
)
const (
// maxTxnRefreshAttempts defines the maximum number of times a single
// transactional batch can trigger a refresh spans attempt. A batch
// may need multiple refresh attempts if it runs into progressively
// larger timestamps as more and more of its component requests are
// executed.
maxTxnRefreshAttempts = 5
)
// MaxTxnRefreshSpansBytes is a threshold in bytes for refresh spans stored
// on the coordinator during the lifetime of a transaction. Refresh spans
// are used for SERIALIZABLE transactions to avoid client restarts.
var MaxTxnRefreshSpansBytes = settings.RegisterIntSetting(
"kv.transaction.max_refresh_spans_bytes",
"maximum number of bytes used to track refresh spans in serializable transactions",
256*1000,
)
// txnSpanRefresher is a txnInterceptor that collects the read spans
// of a serializable transaction in the event we get a serializable
// retry error. We can use the set of read spans to avoid retrying
// the transaction if all the spans can be updated to the current
// transaction timestamp.
type txnSpanRefresher struct {
st *cluster.Settings
knobs *ClientTestingKnobs
wrapped lockedSender
// See TxnCoordMeta.RefreshReads and TxnCoordMeta.RefreshWrites.
refreshReads []roachpb.Span
refreshWrites []roachpb.Span
// See TxnCoordMeta.RefreshInvalid.
refreshInvalid bool
// refreshSpansBytes is the total size in bytes of the spans
// encountered during this transaction that need to be refreshed
// to avoid serializable restart.
refreshSpansBytes int64
// refreshedTimestamp keeps track of the largest timestamp that a
// transaction was able to refresh all of its refreshable spans at.
// It is updated under lock and used to ensure that concurrent requests
// don't cause the refresh spans to get out of sync.
// See appendRefreshSpans.
refreshedTimestamp hlc.Timestamp
// canAutoRetry is set if the txnSpanRefresher is allowed to auto-retry.
canAutoRetry bool
// autoRetryCounter counts the number of auto retries which avoid
// client-side restarts.
autoRetryCounter *metric.Counter
}
// SendLocked implements the lockedSender interface.
func (sr *txnSpanRefresher) SendLocked(
ctx context.Context, ba roachpb.BatchRequest,
) (*roachpb.BatchResponse, *roachpb.Error) {
if rArgs, hasET := ba.GetArg(roachpb.EndTransaction); hasET {
et := rArgs.(*roachpb.EndTransactionRequest)
if !sr.refreshInvalid && len(sr.refreshReads) == 0 && len(sr.refreshWrites) == 0 {
et.NoRefreshSpans = true
}
}
maxAttempts := maxTxnRefreshAttempts
if knob := sr.knobs.MaxTxnRefreshAttempts; knob != 0 {
if knob == -1 {
maxAttempts = 0
} else {
maxAttempts = knob
}
}
// Send through wrapped lockedSender. Unlocks while sending then re-locks.
br, pErr, largestRefreshTS := sr.sendLockedWithRefreshAttempts(ctx, ba, maxAttempts)
if pErr != nil {
return nil, pErr
}
// If the transaction is no longer pending, just return without
// attempting to record its refresh spans.
if br.Txn.Status != roachpb.PENDING {
return br, nil
}
// Iterate over and aggregate refresh spans in the requests,
// qualified by possible resume spans in the responses, if we
// haven't yet exceeded the max read key bytes.
if !sr.refreshInvalid {
ba.Txn.RefreshedTimestamp.Forward(largestRefreshTS)
if !sr.appendRefreshSpans(ctx, ba, br) {
// The refresh spans are out of date, return a generic client-side retry error.
return nil, roachpb.NewErrorWithTxn(
roachpb.NewTransactionRetryError(
roachpb.RETRY_SERIALIZABLE, "refresh spans are out of date",
), br.Txn,
)
}
}
// Verify and enforce the size in bytes of all read-only spans
// doesn't exceed the max threshold.
if sr.refreshSpansBytes > MaxTxnRefreshSpansBytes.Get(&sr.st.SV) {
log.VEventf(ctx, 2, "refresh spans max size exceeded; clearing")
sr.refreshReads = nil
sr.refreshWrites = nil
sr.refreshInvalid = true
sr.refreshSpansBytes = 0
}
return br, nil
}
// sendLockedWithRefreshAttempts sends the batch through the wrapped sender. It
// catches serializable errors and attempts to avoid them by refreshing the txn
// at a larger timestamp. It returns the response, an error, and the largest
// timestamp that the request was able to refresh at.
func (sr *txnSpanRefresher) sendLockedWithRefreshAttempts(
ctx context.Context, ba roachpb.BatchRequest, maxRefreshAttempts int,
) (_ *roachpb.BatchResponse, _ *roachpb.Error, largestRefreshTS hlc.Timestamp) {
br, pErr := sr.wrapped.SendLocked(ctx, ba)
if pErr != nil && maxRefreshAttempts > 0 {
br, pErr, largestRefreshTS = sr.maybeRetrySend(ctx, ba, br, pErr, maxRefreshAttempts)
}
return br, pErr, largestRefreshTS
}
// maybeRetrySend attempts to catch serializable errors and avoid them by
// refreshing the txn at a larger timestamp. If it succeeds at refreshing the
// txn timestamp, it recurses into sendLockedWithRefreshAttempts and retries the
// suffix of the original batch that has not yet completed successfully.
func (sr *txnSpanRefresher) maybeRetrySend(
ctx context.Context,
ba roachpb.BatchRequest,
br *roachpb.BatchResponse,
pErr *roachpb.Error,
maxRefreshAttempts int,
) (*roachpb.BatchResponse, *roachpb.Error, hlc.Timestamp) {
// With mixed success, we can't attempt a retry without potentially
// succeeding at the same conditional put or increment request
// twice; return the wrapped error instead. Because the dist sender
// splits up batches to send to multiple ranges in parallel, and
// then combines the results, partial success makes it very
// difficult to determine what can be retried.
if aPSErr, ok := pErr.GetDetail().(*roachpb.MixedSuccessError); ok {
log.VEventf(ctx, 2, "got partial success; cannot retry %s (pErr=%s)", ba, aPSErr.Wrapped)
return nil, aPSErr.Wrapped, hlc.Timestamp{}
}
// Check for an error which can be retried after updating spans.
canRetryTxn, retryTxn := roachpb.CanTransactionRetryAtRefreshedTimestamp(ctx, pErr)
if !canRetryTxn || !sr.canAutoRetry {
return nil, pErr, hlc.Timestamp{}
}
// If a prefix of the batch was executed, collect refresh spans for
// that executed portion, and retry the remainder. The canonical
// case is a batch split between everything up to but not including
// the EndTransaction. Requests up to the EndTransaction succeed,
// but the EndTransaction fails with a retryable error. We want to
// retry only the EndTransaction.
ba.UpdateTxn(retryTxn)
retryBa := ba
if br != nil {
doneBa := ba
doneBa.Requests = ba.Requests[:len(br.Responses)]
log.VEventf(ctx, 2, "collecting refresh spans after partial batch execution of %s", doneBa)
if !sr.appendRefreshSpans(ctx, doneBa, br) {
return nil, pErr, hlc.Timestamp{}
}
retryBa.Requests = ba.Requests[len(br.Responses):]
}
log.VEventf(ctx, 2, "retrying %s at refreshed timestamp %s because of %s",
retryBa, retryTxn.RefreshedTimestamp, pErr)
// Try updating the txn spans so we can retry.
if ok := sr.tryUpdatingTxnSpans(ctx, retryTxn); !ok {
return nil, pErr, hlc.Timestamp{}
}
// We've refreshed all of the read spans successfully and set
// newBa.Txn.RefreshedTimestamp to the current timestamp. Submit the
// batch again.
retryBr, retryErr, retryLargestRefreshTS := sr.sendLockedWithRefreshAttempts(
ctx, retryBa, maxRefreshAttempts-1,
)
if retryErr != nil {
log.VEventf(ctx, 2, "retry failed with %s", retryErr)
return nil, retryErr, hlc.Timestamp{}
}
log.VEventf(ctx, 2, "retry successful @%s", retryBa.Txn.Timestamp)
sr.autoRetryCounter.Inc(1)
retryTxn.RefreshedTimestamp.Forward(retryLargestRefreshTS)
// On success, combine responses if applicable and set error to nil.
if br != nil {
br.Responses = append(br.Responses, retryBr.Responses...)
retryBr.CollectedSpans = append(br.CollectedSpans, retryBr.CollectedSpans...)
br.BatchResponse_Header = retryBr.BatchResponse_Header
} else {
br = retryBr
}
return br, nil, retryTxn.RefreshedTimestamp
}
// tryUpdatingTxnSpans sends Refresh and RefreshRange commands to all spans read
// during the transaction to ensure that no writes were written more recently
// than the original transaction timestamp. All implicated timestamp caches are
// updated with the final transaction timestamp. Returns whether the refresh was
// successful or not.
func (sr *txnSpanRefresher) tryUpdatingTxnSpans(
ctx context.Context, refreshTxn *roachpb.Transaction,
) bool {
// Forward the refreshed timestamp under lock. This in conjunction with a
// check in appendRefreshSpans prevents a race where a concurrent request
// may add new refresh spans only "verified" up to its batch timestamp after
// we've refreshed past that timestamp.
sr.refreshedTimestamp.Forward(refreshTxn.RefreshedTimestamp)
if sr.refreshInvalid {
log.VEvent(ctx, 2, "can't refresh txn spans; not valid")
return false
} else if len(sr.refreshReads) == 0 && len(sr.refreshWrites) == 0 {
log.VEvent(ctx, 2, "there are no txn spans to refresh")
return true
}
// Refresh all spans (merge first).
refreshSpanBa := roachpb.BatchRequest{}
refreshSpanBa.Txn = refreshTxn
addRefreshes := func(refreshes []roachpb.Span, write bool) {
for _, u := range refreshes {
var req roachpb.Request
if len(u.EndKey) == 0 {
req = &roachpb.RefreshRequest{
RequestHeader: roachpb.RequestHeaderFromSpan(u),
Write: write,
}
} else {
req = &roachpb.RefreshRangeRequest{
RequestHeader: roachpb.RequestHeaderFromSpan(u),
Write: write,
}
}
refreshSpanBa.Add(req)
log.VEventf(ctx, 2, "updating span %s @%s - @%s to avoid serializable restart",
req.Header().Span(), refreshTxn.OrigTimestamp, refreshTxn.Timestamp)
}
}
addRefreshes(sr.refreshReads, false)
addRefreshes(sr.refreshWrites, true)
// Send through wrapped lockedSender. Unlocks while sending then re-locks.
if _, batchErr := sr.wrapped.SendLocked(ctx, refreshSpanBa); batchErr != nil {
log.VEventf(ctx, 2, "failed to refresh txn spans (%s); propagating original retry error", batchErr)
return false
}
return true
}
// appendRefreshSpans appends refresh spans from the supplied batch request,
// qualified by the batch response where appropriate.
//
// Returns whether the batch transaction's refreshed timestamp is greater or
// equal to the max refreshed timestamp used so far with this sender. In other
// words, returns false if this batch ran straddled a refresh request, in which
// case the refresh was invalid - to be valid, it should have included the this
// request's spans. In that case the caller should return an error for
// client-side retry.
// Note that batches that don't produce any refresh spans are allowed to run
// concurrently with refreshes. This is useful for the async rollbacks performed
// by the heartbeater.
func (sr *txnSpanRefresher) appendRefreshSpans(
ctx context.Context, ba roachpb.BatchRequest, br *roachpb.BatchResponse,
) bool {
origTS := ba.Txn.OrigTimestamp
origTS.Forward(ba.Txn.RefreshedTimestamp)
var concurrentRefresh bool
if origTS.Less(sr.refreshedTimestamp) {
// We'll return an error if any of the requests generate a refresh span.
concurrentRefresh = true
}
if !ba.RefreshSpanIterate(br, func(span roachpb.Span, write bool) bool {
if concurrentRefresh {
return false
}
if log.V(3) {
log.Infof(ctx, "refresh: %s write=%t", span, write)
}
if write {
sr.refreshWrites = append(sr.refreshWrites, span)
} else {
sr.refreshReads = append(sr.refreshReads, span)
}
sr.refreshSpansBytes += int64(len(span.Key) + len(span.EndKey))
return true
}) {
log.VEventf(ctx, 2, "txn orig timestamp %s < sender refreshed timestamp %s",
origTS, sr.refreshedTimestamp)
return false
}
return true
}
// setWrapped implements the txnInterceptor interface.
func (sr *txnSpanRefresher) setWrapped(wrapped lockedSender) { sr.wrapped = wrapped }
// populateMetaLocked implements the txnInterceptor interface.
func (sr *txnSpanRefresher) populateMetaLocked(meta *roachpb.TxnCoordMeta) {
meta.RefreshInvalid = sr.refreshInvalid
if !sr.refreshInvalid {
// Copy mutable state so access is safe for the caller.
meta.RefreshReads = append([]roachpb.Span(nil), sr.refreshReads...)
meta.RefreshWrites = append([]roachpb.Span(nil), sr.refreshWrites...)
}
}
// augmentMetaLocked implements the txnInterceptor interface.
func (sr *txnSpanRefresher) augmentMetaLocked(meta roachpb.TxnCoordMeta) {
// Do not modify existing span slices when copying.
if meta.RefreshInvalid {
sr.refreshInvalid = true
sr.refreshReads = nil
sr.refreshWrites = nil
} else if !sr.refreshInvalid {
sr.refreshReads, _ = roachpb.MergeSpans(
append(append([]roachpb.Span(nil), sr.refreshReads...), meta.RefreshReads...),
)
sr.refreshWrites, _ = roachpb.MergeSpans(
append(append([]roachpb.Span(nil), sr.refreshWrites...), meta.RefreshWrites...),
)
}
// Recompute the size of the refreshes.
sr.refreshSpansBytes = 0
for _, u := range sr.refreshReads {
sr.refreshSpansBytes += int64(len(u.Key) + len(u.EndKey))
}
for _, u := range sr.refreshWrites {
sr.refreshSpansBytes += int64(len(u.Key) + len(u.EndKey))
}
}
// epochBumpedLocked implements the txnInterceptor interface.
func (sr *txnSpanRefresher) epochBumpedLocked() {
sr.refreshReads = nil
sr.refreshWrites = nil
sr.refreshInvalid = false
sr.refreshSpansBytes = 0
}
// closeLocked implements the txnInterceptor interface.
func (*txnSpanRefresher) closeLocked() {}