-
Notifications
You must be signed in to change notification settings - Fork 104
/
transactions.go
508 lines (440 loc) · 15.6 KB
/
transactions.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
package gocb
import (
"errors"
"math"
"sync"
"time"
"github.com/couchbase/gocbcore/v10"
)
// AttemptFunc represents the lambda used by the Transactions Run function.
type AttemptFunc func(*TransactionAttemptContext) error
// Transactions can be used to perform transactions.
type Transactions struct {
unsupported bool
config TransactionsConfig
cluster *Cluster
transcoder Transcoder
txns *gocbcore.TransactionsManager
hooksWrapper transactionHooksWrapper
cleanupHooksWrapper transactionCleanupHooksWrapper
cleanupCollections []gocbcore.TransactionLostATRLocation
}
// initTransactions will initialize the transactions library and return a Transactions
// object which can be used to perform transactions.
func (c *Cluster) initTransactions(config TransactionsConfig) (*Transactions, error) {
// TODO: protostellar doesn't support transactions.
// so we'll just bail early.
if c.cSpec.Scheme == "couchbase2" {
return &Transactions{
unsupported: true,
}, nil
}
// Note that gocbcore will handle a lot of default values for us.
if config.QueryConfig.ScanConsistency == 0 {
config.QueryConfig.ScanConsistency = QueryScanConsistencyRequestPlus
}
if config.DurabilityLevel == DurabilityLevelUnknown {
config.DurabilityLevel = DurabilityLevelMajority
}
var hooksWrapper transactionHooksWrapper
if config.Internal.Hooks == nil {
hooksWrapper = &noopHooksWrapper{
TransactionDefaultHooks: gocbcore.TransactionDefaultHooks{},
hooks: transactionsDefaultHooks{},
}
} else {
hooksWrapper = &coreTxnsHooksWrapper{
hooks: config.Internal.Hooks,
}
}
var cleanupHooksWrapper transactionCleanupHooksWrapper
if config.Internal.CleanupHooks == nil {
cleanupHooksWrapper = &noopCleanupHooksWrapper{
TransactionDefaultCleanupHooks: gocbcore.TransactionDefaultCleanupHooks{},
}
} else {
cleanupHooksWrapper = &coreTxnsCleanupHooksWrapper{
CleanupHooks: config.Internal.CleanupHooks,
}
}
var clientRecordHooksWrapper clientRecordHooksWrapper
if config.Internal.ClientRecordHooks == nil {
clientRecordHooksWrapper = &noopClientRecordHooksWrapper{
TransactionDefaultCleanupHooks: gocbcore.TransactionDefaultCleanupHooks{},
TransactionDefaultClientRecordHooks: gocbcore.TransactionDefaultClientRecordHooks{},
}
} else {
clientRecordHooksWrapper = &coreTxnsClientRecordHooksWrapper{
coreTxnsCleanupHooksWrapper: coreTxnsCleanupHooksWrapper{
CleanupHooks: config.Internal.CleanupHooks,
},
ClientRecordHooks: config.Internal.ClientRecordHooks,
}
}
atrLocation := gocbcore.TransactionATRLocation{}
if config.MetadataCollection != nil {
customATRAgent, err := c.Bucket(config.MetadataCollection.BucketName).Internal().IORouter()
if err != nil {
return nil, err
}
atrLocation.Agent = customATRAgent
atrLocation.CollectionName = config.MetadataCollection.CollectionName
atrLocation.ScopeName = config.MetadataCollection.ScopeName
// We add the custom metadata collection to the cleanup collections so that lost cleanup starts watching it
// immediately. Note that we don't do the same for the custom metadata on TransactionOptions, this is because
// we know that that collection will be used in a transaction.
var alreadyInCleanup bool
for _, keySpace := range config.CleanupConfig.CleanupCollections {
if keySpace == *config.MetadataCollection {
alreadyInCleanup = true
break
}
}
if !alreadyInCleanup {
config.CleanupConfig.CleanupCollections = append(config.CleanupConfig.CleanupCollections, *config.MetadataCollection)
}
}
var cleanupLocs []gocbcore.TransactionLostATRLocation
for _, keyspace := range config.CleanupConfig.CleanupCollections {
cleanupLocs = append(cleanupLocs, gocbcore.TransactionLostATRLocation{
BucketName: keyspace.BucketName,
ScopeName: keyspace.ScopeName,
CollectionName: keyspace.CollectionName,
})
}
t := &Transactions{
cluster: c,
config: config,
transcoder: NewJSONTranscoder(),
hooksWrapper: hooksWrapper,
cleanupHooksWrapper: cleanupHooksWrapper,
cleanupCollections: cleanupLocs,
}
corecfg := &gocbcore.TransactionsConfig{}
corecfg.DurabilityLevel = gocbcore.TransactionDurabilityLevel(config.DurabilityLevel)
corecfg.BucketAgentProvider = t.agentProvider
corecfg.LostCleanupATRLocationProvider = t.atrLocationsProvider
corecfg.CleanupClientAttempts = !config.CleanupConfig.DisableClientAttemptCleanup
corecfg.CleanupQueueSize = config.CleanupConfig.CleanupQueueSize
corecfg.ExpirationTime = config.Timeout
corecfg.CleanupWindow = config.CleanupConfig.CleanupWindow
corecfg.CleanupLostAttempts = !config.CleanupConfig.DisableLostAttemptCleanup
corecfg.CustomATRLocation = atrLocation
corecfg.Internal.Hooks = hooksWrapper
corecfg.Internal.CleanUpHooks = cleanupHooksWrapper
corecfg.Internal.ClientRecordHooks = clientRecordHooksWrapper
corecfg.Internal.NumATRs = config.Internal.NumATRs
corecfg.KeyValueTimeout = c.timeoutsConfig.KVTimeout
txns, err := gocbcore.InitTransactions(corecfg)
if err != nil {
return nil, err
}
t.txns = txns
return t, nil
}
// Run runs a lambda to perform a number of operations as part of a
// singular transaction.
func (t *Transactions) Run(logicFn AttemptFunc, perConfig *TransactionOptions) (*TransactionResult, error) {
if t.unsupported {
return nil, wrapError(ErrFeatureNotAvailable, "transactions are not currently supported against the couchbase2 protocol")
}
return t.run(logicFn, perConfig, false)
}
func (t *Transactions) run(logicFn AttemptFunc, perConfig *TransactionOptions, singleQueryMode bool) (*TransactionResult, error) {
if perConfig == nil {
perConfig = &TransactionOptions{
DurabilityLevel: t.config.DurabilityLevel,
Timeout: t.config.Timeout,
}
}
scanConsistency := t.config.QueryConfig.ScanConsistency
// Gocbcore looks at whether the location agent is nil to verify whether CustomATRLocation has been set.
atrLocation := gocbcore.TransactionATRLocation{}
if perConfig.MetadataCollection != nil {
customATRAgent, err := perConfig.MetadataCollection.bucket.Internal().IORouter()
if err != nil {
return nil, err
}
atrLocation.Agent = customATRAgent
atrLocation.CollectionName = perConfig.MetadataCollection.Name()
atrLocation.ScopeName = perConfig.MetadataCollection.ScopeName()
}
logger := newTransactionLogger()
// TODO: fill in the rest of this config
config := &gocbcore.TransactionOptions{
DurabilityLevel: gocbcore.TransactionDurabilityLevel(perConfig.DurabilityLevel),
ExpirationTime: perConfig.Timeout,
CustomATRLocation: atrLocation,
TransactionLogger: logger,
}
hooksWrapper := t.hooksWrapper
if perConfig.Internal.Hooks != nil {
hooksWrapper = &coreTxnsHooksWrapper{
hooks: perConfig.Internal.Hooks,
}
config.Internal.Hooks = hooksWrapper
}
txn, err := t.txns.BeginTransaction(config)
if err != nil {
return nil, err
}
logger.setTxnID(txn.ID())
retries := 0
backoffCalc := func() time.Duration {
var max float64 = 100000000 // 100 Milliseconds
var min float64 = 1000000 // 1 Millisecond
retries++
backoff := min * (math.Pow(2, float64(retries)))
if backoff > max {
backoff = max
}
if backoff < min {
backoff = min
}
return time.Duration(backoff)
}
for {
err = txn.NewAttempt()
if err != nil {
return nil, err
}
attemptID := txn.Attempt().ID
logDebugf("New transaction attempt starting for %s, %s", txn.ID(), attemptID)
logger.logInfof(attemptID, "New transaction attempt starting")
attempt := TransactionAttemptContext{
txn: txn,
transcoder: t.transcoder,
hooks: hooksWrapper.Hooks(),
cluster: t.cluster,
queryStateLock: new(sync.Mutex),
queryConfig: TransactionQueryOptions{
ScanConsistency: scanConsistency,
},
logger: logger,
attemptID: attemptID,
}
if hooksWrapper != nil {
hooksWrapper.SetAttemptContext(attempt)
}
lambdaErr := logicFn(&attempt)
if !singleQueryMode && lambdaErr != nil {
logger.logInfof(attemptID, "Lambda returned error and not single query mode")
var txnErr *TransactionOperationFailedError
if !errors.As(lambdaErr, &txnErr) {
// We wrap non-TOF errors in a TOF.
lambdaErr = operationFailed(transactionQueryOperationFailedDef{
ShouldNotRetry: true,
ShouldNotRollback: false,
Reason: gocbcore.TransactionErrorReasonTransactionFailed,
ErrorCause: lambdaErr,
ShouldNotCommit: true,
}, &attempt)
}
}
finalErr := lambdaErr
if !singleQueryMode {
if attempt.canCommit() {
finalErr = attempt.commit()
}
if attempt.shouldRollback() {
rollbackErr := attempt.rollback()
if rollbackErr != nil {
logWarnf("rollback after error failed: %s", rollbackErr)
}
}
}
toRaise := attempt.finalErrorToRaise()
if attempt.shouldRetry() && toRaise != gocbcore.TransactionErrorReasonSuccess {
logDebugf("retrying lambda after backoff")
sleep := backoffCalc()
logger.logInfof(attemptID, "Will retry lambda after %s", sleep)
time.Sleep(sleep)
continue
}
// We don't want the TOF to be the cause in the final error we return so we unwrap it.
var finalErrCause error
if finalErr != nil {
var txnErr *TransactionOperationFailedError
if errors.As(finalErr, &txnErr) {
finalErrCause = txnErr.InternalUnwrap()
} else {
finalErrCause = finalErr
}
}
switch toRaise {
case gocbcore.TransactionErrorReasonSuccess:
if singleQueryMode && finalErr != nil {
return nil, finalErr
}
unstagingComplete := attempt.attempt().State == TransactionAttemptStateCompleted
return &TransactionResult{
TransactionID: txn.ID(),
UnstagingComplete: unstagingComplete,
Logs: logger.Logs(),
}, nil
case gocbcore.TransactionErrorReasonTransactionFailed:
return nil, &TransactionFailedError{
cause: finalErrCause,
result: &TransactionResult{
TransactionID: txn.ID(),
UnstagingComplete: false,
Logs: logger.Logs(),
},
}
case gocbcore.TransactionErrorReasonTransactionExpired:
// If we expired during gocbcore auto-rollback then we return failed with the error cause rather
// than expired. This occurs when we commit itself errors and gocbcore auto rolls back the transaction.
if attempt.attempt().PreExpiryAutoRollback {
return nil, &TransactionFailedError{
cause: finalErrCause,
result: &TransactionResult{
TransactionID: txn.ID(),
UnstagingComplete: false,
Logs: logger.Logs(),
},
}
}
return nil, &TransactionExpiredError{
result: &TransactionResult{
TransactionID: txn.ID(),
UnstagingComplete: false,
Logs: logger.Logs(),
},
}
case gocbcore.TransactionErrorReasonTransactionCommitAmbiguous:
return nil, &TransactionCommitAmbiguousError{
cause: finalErrCause,
result: &TransactionResult{
TransactionID: txn.ID(),
UnstagingComplete: false,
Logs: logger.Logs(),
},
}
case gocbcore.TransactionErrorReasonTransactionFailedPostCommit:
return &TransactionResult{
TransactionID: txn.ID(),
UnstagingComplete: false,
Logs: logger.Logs(),
}, nil
default:
return nil, errors.New("invalid final transaction state")
}
}
}
// Close will shut down this Transactions object, shutting down all
// background tasks associated with it.
func (t *Transactions) close() error {
return t.txns.Close()
}
func (t *Transactions) agentProvider(bucketName string) (*gocbcore.Agent, string, error) {
b := t.cluster.Bucket(bucketName)
agent, err := b.Internal().IORouter()
if err != nil {
return nil, "", err
}
return agent, "", err
}
func (t *Transactions) atrLocationsProvider() ([]gocbcore.TransactionLostATRLocation, error) {
return t.cleanupCollections, nil
}
func (t *Transactions) singleQuery(statement string, scope *Scope, opts QueryOptions) (*QueryResult, error) {
if t.unsupported {
return nil, wrapError(ErrFeatureNotAvailable, "transactions are not currently supported against the couchbase2 protocol")
}
if opts.Context != nil {
return nil, makeInvalidArgumentsError("cannot use context and transactions together")
}
config := &TransactionOptions{
DurabilityLevel: opts.AsTransaction.DurabilityLevel,
Timeout: opts.Timeout,
}
config.Internal.Hooks = opts.AsTransaction.Internal.Hooks
var queryRes *QueryResult
res, err := t.run(func(context *TransactionAttemptContext) error {
// We need to tell the core loop that autocommit and autorollback are disabled.
// context.txn.UpdateState(gocbcore.TransactionUpdateStateOptions{
// ShouldNotCommit: true,
// ShouldNotRollback: true,
// })
qRes, err := context.queryWrapper(scope, statement, opts, "query", false, false,
nil, true)
if err != nil {
return err
}
queryRes = qRes
// If the result contains rows then we can't immediately check for errors, so we need to return here.
if len(queryRes.peekNext()) > 0 {
// We consider this success so tell the core to not retry - any errors on stream will happen outside the
// context of the core loop.
// context.txn.UpdateState(gocbcore.TransactionUpdateStateOptions{
// ShouldNotRetry: true,
// })
return nil
}
if err := qRes.Err(); err != nil {
return queryMaybeTranslateToTransactionsError(err, context)
}
meta, err := qRes.MetaData()
if err != nil {
return queryMaybeTranslateToTransactionsError(err, context)
}
if meta.Status == QueryStatusFatal {
return operationFailed(transactionQueryOperationFailedDef{
ShouldNotRetry: true,
Reason: gocbcore.TransactionErrorReasonTransactionFailed,
ShouldNotCommit: true,
}, context)
}
// We won't do autocommit or autorollback so tell the core loop to not retry.
// context.txn.UpdateState(gocbcore.TransactionUpdateStateOptions{
// ShouldNotRetry: true,
// })
return nil
}, config, true)
if err != nil {
var expiredErr *TransactionExpiredError
if errors.As(err, &expiredErr) {
return nil, ErrUnambiguousTimeout
}
return nil, err
}
queryRes.transactionID = res.TransactionID
return queryRes, nil
}
// TransactionsInternal exposes internal methods that are useful for testing and/or
// other forms of internal use.
type TransactionsInternal struct {
parent *Transactions
}
// Internal returns an TransactionsInternal object which can be used for specialized
// internal use cases.
func (t *Transactions) Internal() *TransactionsInternal {
return &TransactionsInternal{
parent: t,
}
}
// ForceCleanupQueue forces the transactions client cleanup queue to drain without waiting for expirations.
func (t *TransactionsInternal) ForceCleanupQueue() []TransactionCleanupAttempt {
waitCh := make(chan []gocbcore.TransactionsCleanupAttempt, 1)
t.parent.txns.Internal().ForceCleanupQueue(func(attempts []gocbcore.TransactionsCleanupAttempt) {
waitCh <- attempts
})
coreAttempts := <-waitCh
var attempts []TransactionCleanupAttempt
for _, attempt := range coreAttempts {
attempts = append(attempts, cleanupAttemptFromCore(attempt))
}
return attempts
}
// CleanupQueueLength returns the current length of the client cleanup queue.
func (t *TransactionsInternal) CleanupQueueLength() int32 {
return t.parent.txns.Internal().CleanupQueueLength()
}
// ClientCleanupEnabled returns whether the client cleanup process is enabled.
func (t *TransactionsInternal) ClientCleanupEnabled() bool {
return t.parent.txns.Config().CleanupClientAttempts
}
// CleanupLocations returns the set of locations currently being watched by the lost transactions process.
func (t *TransactionsInternal) CleanupLocations() []gocbcore.TransactionLostATRLocation {
return t.parent.txns.Internal().CleanupLocations()
}