-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathtxn.go
361 lines (339 loc) · 12.9 KB
/
txn.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
// Copyright 2020 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 descs
import (
"context"
"fmt"
"time"
"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/kv"
"github.com/cockroachdb/cockroach/pkg/spanconfig"
"github.com/cockroachdb/cockroach/pkg/sql/catalog"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/lease"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
"github.com/cockroachdb/cockroach/pkg/sql/sessiondata"
"github.com/cockroachdb/cockroach/pkg/sql/sqlutil"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/retry"
"github.com/cockroachdb/errors"
)
var errTwoVersionInvariantViolated = errors.Errorf("two version invariant violated")
// Txn enables callers to run transactions with a *Collection such that all
// retrieved immutable descriptors are properly leased and all mutable
// descriptors are handled. The function deals with verifying the two version
// invariant and retrying when it is violated. Callers need not worry that they
// write mutable descriptors multiple times. The call will explicitly wait for
// the leases to drain on old versions of descriptors modified or deleted in the
// transaction; callers do not need to call lease.WaitForOneVersion.
//
// The passed transaction is pre-emptively anchored to the system config key on
// the system tenant.
// Deprecated: Use cf.TxnWithExecutor().
func (cf *CollectionFactory) Txn(
ctx context.Context,
ie sqlutil.InternalExecutor,
db *kv.DB,
f func(ctx context.Context, txn *kv.Txn, descriptors *Collection) error,
) error {
// Waits for descriptors that were modified, skipping
// over ones that had their descriptor wiped.
waitForDescriptors := func(modifiedDescriptors []lease.IDVersion, deletedDescs catalog.DescriptorIDSet) error {
// Wait for a single version on leased descriptors.
for _, ld := range modifiedDescriptors {
waitForNoVersion := deletedDescs.Contains(ld.ID)
retryOpts := retry.Options{
InitialBackoff: time.Millisecond,
Multiplier: 1.5,
MaxBackoff: time.Second,
}
// Detect unpublished ones.
if waitForNoVersion {
err := cf.leaseMgr.WaitForNoVersion(ctx, ld.ID, retryOpts)
if err != nil {
return err
}
} else {
_, err := cf.leaseMgr.WaitForOneVersion(ctx, ld.ID, retryOpts)
if err != nil {
return err
}
}
}
return nil
}
for {
var modifiedDescriptors []lease.IDVersion
var deletedDescs catalog.DescriptorIDSet
var descsCol *Collection
if err := db.Txn(ctx, func(ctx context.Context, txn *kv.Txn) error {
modifiedDescriptors = nil
deletedDescs = catalog.DescriptorIDSet{}
descsCol = cf.NewCollection(ctx, nil /* temporarySchemaProvider */, nil /* monitor */)
defer descsCol.ReleaseAll(ctx)
if err := f(ctx, txn, descsCol); err != nil {
return err
}
if err := descsCol.ValidateUncommittedDescriptors(ctx, txn); err != nil {
return err
}
modifiedDescriptors = descsCol.GetDescriptorsWithNewVersion()
if err := CheckSpanCountLimit(
ctx, descsCol, cf.spanConfigSplitter, cf.spanConfigLimiter, txn,
); err != nil {
return err
}
retryErr, err := CheckTwoVersionInvariant(
ctx, db.Clock(), ie, descsCol, txn, nil /* onRetryBackoff */)
if retryErr {
return errTwoVersionInvariantViolated
}
deletedDescs = descsCol.deletedDescs
return err
}); errors.Is(err, errTwoVersionInvariantViolated) {
continue
} else {
if err == nil {
err = waitForDescriptors(modifiedDescriptors, deletedDescs)
}
return err
}
}
}
// TxnWithExecutor enables callers to run transactions with a *Collection such that all
// retrieved immutable descriptors are properly leased and all mutable
// descriptors are handled. The function deals with verifying the two version
// invariant and retrying when it is violated. Callers need not worry that they
// write mutable descriptors multiple times. The call will explicitly wait for
// the leases to drain on old versions of descriptors modified or deleted in the
// transaction; callers do not need to call lease.WaitForOneVersion.
// It also enables using internal executor to run sql queries in a txn manner.
//
// The passed transaction is pre-emptively anchored to the system config key on
// the system tenant.
func (cf *CollectionFactory) TxnWithExecutor(
ctx context.Context,
db *kv.DB,
sd *sessiondata.SessionData,
f func(ctx context.Context, txn *kv.Txn, descriptors *Collection, ie sqlutil.InternalExecutor) error,
) error {
// Waits for descriptors that were modified, skipping
// over ones that had their descriptor wiped.
waitForDescriptors := func(modifiedDescriptors []lease.IDVersion, deletedDescs catalog.DescriptorIDSet) error {
// Wait for a single version on leased descriptors.
for _, ld := range modifiedDescriptors {
waitForNoVersion := deletedDescs.Contains(ld.ID)
retryOpts := retry.Options{
InitialBackoff: time.Millisecond,
Multiplier: 1.5,
MaxBackoff: time.Second,
}
// Detect unpublished ones.
if waitForNoVersion {
err := cf.leaseMgr.WaitForNoVersion(ctx, ld.ID, retryOpts)
if err != nil {
return err
}
} else {
_, err := cf.leaseMgr.WaitForOneVersion(ctx, ld.ID, retryOpts)
if err != nil {
return err
}
}
}
return nil
}
for {
var modifiedDescriptors []lease.IDVersion
var deletedDescs catalog.DescriptorIDSet
var descsCol *Collection
if err := db.Txn(ctx, func(ctx context.Context, txn *kv.Txn) error {
modifiedDescriptors = nil
deletedDescs = catalog.DescriptorIDSet{}
descsCol = cf.NewCollection(ctx, nil /* temporarySchemaProvider */, nil /* monitor */)
defer func() {
descsCol.ReleaseAll(ctx)
}()
ie, commitTxnFn := cf.ieFactoryWithTxn.NewInternalExecutorWithTxn(sd, &cf.settings.SV, txn, descsCol)
if err := f(ctx, txn, descsCol, ie); err != nil {
return err
}
if err := commitTxnFn(ctx); err != nil {
return err
}
if err := descsCol.ValidateUncommittedDescriptors(ctx, txn); err != nil {
return err
}
modifiedDescriptors = descsCol.GetDescriptorsWithNewVersion()
if err := CheckSpanCountLimit(
ctx, descsCol, cf.spanConfigSplitter, cf.spanConfigLimiter, txn,
); err != nil {
return err
}
retryErr, err := CheckTwoVersionInvariant(
ctx, db.Clock(), ie, descsCol, txn, nil /* onRetryBackoff */)
if retryErr {
return errTwoVersionInvariantViolated
}
deletedDescs = descsCol.deletedDescs
return err
}); errors.Is(err, errTwoVersionInvariantViolated) {
continue
} else {
if err == nil {
err = waitForDescriptors(modifiedDescriptors, deletedDescs)
}
return err
}
}
}
// CheckTwoVersionInvariant checks whether any new schema being modified written
// at a version V has only valid leases at version = V - 1. A transaction retry
// error as well as a boolean is returned whenever the invariant is violated.
// Before returning the retry error the current transaction is rolled-back and
// the function waits until there are only outstanding leases on the current
// version. This affords the retry to succeed in the event that there are no
// other schema changes simultaneously contending with this txn.
//
// checkDescriptorTwoVersionInvariant blocks until it's legal for the modified
// descriptors (if any) to be committed.
//
// Reminder: a descriptor version v can only be written at a timestamp
// that's not covered by a lease on version v-2. So, if the current
// txn wants to write some updated descriptors, it needs
// to wait until all incompatible leases are revoked or expire. If
// incompatible leases exist, we'll block waiting for these leases to
// go away. Then, the transaction is restarted by generating a retriable error.
// Note that we're relying on the fact that the number of conflicting
// leases will only go down over time: no new conflicting leases can be
// created as of the time of this call because v-2 can't be leased once
// v-1 exists.
//
// If this method succeeds it is the caller's responsibility to release the
// executor's leases after the txn commits so that schema changes can
// proceed.
func CheckTwoVersionInvariant(
ctx context.Context,
clock *hlc.Clock,
ie sqlutil.InternalExecutor,
descsCol *Collection,
txn *kv.Txn,
onRetryBackoff func(),
) (retryDueToViolation bool, _ error) {
descs := descsCol.GetDescriptorsWithNewVersion()
if descs == nil {
return false, nil
}
if txn.IsCommitted() {
panic("transaction has already committed")
}
// We potentially hold leases for descriptors which we've modified which
// we need to drop. Say we're updating descriptors at version V. All leases
// for version V-2 need to be dropped immediately, otherwise the check
// below that nobody holds leases for version V-2 will fail. Worse yet,
// the code below loops waiting for nobody to hold leases on V-2. We also
// may hold leases for version V-1 of modified descriptors that are good to drop
// but not as vital for correctness. It's good to drop them because as soon
// as this transaction commits jobs may start and will need to wait until
// the lease expires. It is safe because V-1 must remain valid until this
// transaction commits; if we commit then nobody else could have written
// a new V beneath us because we've already laid down an intent.
//
// All this being said, we must retain our leases on descriptors which we have
// not modified to ensure that our writes to those other descriptors in this
// transaction remain valid.
descsCol.ReleaseSpecifiedLeases(ctx, descs)
// We know that so long as there are no leases on the updated descriptors as of
// the current provisional commit timestamp for this transaction then if this
// transaction ends up committing then there won't have been any created
// in the meantime.
count, err := lease.CountLeases(ctx, ie, descs, txn.ProvisionalCommitTimestamp())
if err != nil {
return false, err
}
if count == 0 {
// This is the last step before committing a transaction which modifies
// descriptors. This is a perfect time to refresh the deadline prior to
// committing.
return false, descsCol.MaybeUpdateDeadline(ctx, txn)
}
// Restart the transaction so that it is able to replay itself at a newer timestamp
// with the hope that the next time around there will be leases only at the current
// version.
retryErr := txn.PrepareRetryableError(ctx,
fmt.Sprintf(
`cannot publish new versions for descriptors: %v, old versions still in use`,
descs))
// We cleanup the transaction and create a new transaction after
// waiting for the invariant to be satisfied because the wait time
// might be extensive and intents can block out leases being created
// on a descriptor.
//
// TODO(vivek): Change this to restart a txn while fixing #20526 . All the
// descriptor intents can be laid down here after the invariant
// has been checked.
txn.CleanupOnError(ctx, retryErr)
// Release the rest of our leases on unmodified descriptors so we don't hold
// up schema changes there and potentially create a deadlock.
descsCol.ReleaseLeases(ctx)
// Wait until all older version leases have been released or expired.
for r := retry.StartWithCtx(ctx, base.DefaultRetryOptions()); r.Next(); {
// Use the current clock time.
now := clock.Now()
count, err := lease.CountLeases(ctx, ie, descs, now)
if err != nil {
return false, err
}
if count == 0 {
break
}
if onRetryBackoff != nil {
onRetryBackoff()
}
}
return true, retryErr
}
// CheckSpanCountLimit checks whether committing the set of uncommitted tables
// would exceed the span count limit we're allowed (applicable only to secondary
// tenants).
func CheckSpanCountLimit(
ctx context.Context,
descsCol *Collection,
splitter spanconfig.Splitter,
limiter spanconfig.Limiter,
txn *kv.Txn,
) error {
if !descsCol.codec().ForSystemTenant() {
var totalSpanCountDelta int
for _, ut := range descsCol.GetUncommittedTables() {
uncommittedMutTable, err := descsCol.GetUncommittedMutableTableByID(ut.GetID())
if err != nil {
return err
}
var originalTableDesc catalog.TableDescriptor
if originalDesc := uncommittedMutTable.OriginalDescriptor(); originalDesc != nil {
originalTableDesc = originalDesc.(catalog.TableDescriptor)
}
delta, err := spanconfig.Delta(ctx, splitter, originalTableDesc, uncommittedMutTable)
if err != nil {
return err
}
totalSpanCountDelta += delta
}
shouldLimit, err := limiter.ShouldLimit(ctx, txn, totalSpanCountDelta)
if err != nil {
return err
}
if shouldLimit {
return pgerror.New(pgcode.ConfigurationLimitExceeded, "exceeded limit for number of table spans")
}
}
return nil
}