-
Notifications
You must be signed in to change notification settings - Fork 104
/
transaction_attemptcontext.go
354 lines (304 loc) · 8.63 KB
/
transaction_attemptcontext.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
package gocb
import (
"errors"
"sync"
"github.com/couchbase/gocbcore/v10"
)
type transactionAttempt struct {
State TransactionAttemptState
PreExpiryAutoRollback bool
Expired bool
}
type transactionQueryState struct {
queryTarget string
scope *Scope
}
// TransactionAttemptContext represents a single attempt to execute a transaction.
type TransactionAttemptContext struct {
txn *gocbcore.Transaction
transcoder Transcoder
cluster *Cluster
hooks TransactionHooks
// State applicable to when we move into query mode
queryState *transactionQueryState
// Pointer to satisfy go vet complaining about the hooks.
queryStateLock *sync.Mutex
queryConfig TransactionQueryOptions
logger *transactionLogger
attemptID string
}
func (c *TransactionAttemptContext) canCommit() bool {
return c.txn.CanCommit()
}
func (c *TransactionAttemptContext) shouldRollback() bool {
return c.txn.ShouldRollback()
}
func (c *TransactionAttemptContext) shouldRetry() bool {
return c.txn.ShouldRetry()
}
func (c *TransactionAttemptContext) finalErrorToRaise() gocbcore.TransactionErrorReason {
return c.txn.FinalErrorToRaise()
}
func (c *TransactionAttemptContext) attempt() transactionAttempt {
a := c.txn.Attempt()
return transactionAttempt{
State: TransactionAttemptState(a.State),
PreExpiryAutoRollback: a.PreExpiryAutoRollback,
Expired: c.txn.TimeRemaining() <= 0 || a.Expired,
}
}
// Internal is used for internal dealings.
// Internal: This should never be used and is not supported.
func (c *TransactionAttemptContext) Internal() *InternalTransactionAttemptContext {
return &InternalTransactionAttemptContext{
ac: c,
}
}
// InternalTransactionAttemptContext is used for internal dealings.
// Internal: This should never be used and is not supported.
type InternalTransactionAttemptContext struct {
ac *TransactionAttemptContext
}
func (iac *InternalTransactionAttemptContext) IsExpired() bool {
return iac.ac.txn.HasExpired()
}
// Get will attempt to fetch a document, and fail the transaction if it does not exist.
func (c *TransactionAttemptContext) Get(collection *Collection, id string) (*TransactionGetResult, error) {
c.queryStateLock.Lock()
if c.queryModeLocked() {
res, err := c.getQueryMode(collection, id)
if err != nil {
c.logger.logInfof(c.attemptID, "Query mode get failed")
c.txn.UpdateState(gocbcore.TransactionUpdateStateOptions{
ShouldNotCommit: !errors.Is(err, ErrDocumentNotFound),
})
c.queryStateLock.Unlock()
return nil, err
}
c.queryStateLock.Unlock()
return res, nil
}
c.queryStateLock.Unlock()
return c.get(collection, id)
}
func (c *TransactionAttemptContext) get(collection *Collection, id string) (resOut *TransactionGetResult, errOut error) {
a, err := collection.Bucket().Internal().IORouter()
if err != nil {
return nil, err
}
waitCh := make(chan struct{}, 1)
err = c.txn.Get(gocbcore.TransactionGetOptions{
Agent: a,
ScopeName: collection.ScopeName(),
CollectionName: collection.Name(),
Key: []byte(id),
}, func(res *gocbcore.TransactionGetResult, err error) {
if err == nil {
resOut = &TransactionGetResult{
collection: collection,
docID: id,
transcoder: NewJSONTranscoder(),
flags: 2 << 24,
coreRes: res,
}
}
if errors.Is(err, ErrDocumentNotFound) {
errOut = err
waitCh <- struct{}{}
return
}
errOut = createTransactionOperationFailedError(err)
waitCh <- struct{}{}
})
if err != nil {
resOut = nil
errOut = createTransactionOperationFailedError(err)
return
}
<-waitCh
return
}
// Replace will replace the contents of a document, failing if the document does not already exist.
func (c *TransactionAttemptContext) Replace(doc *TransactionGetResult, value interface{}) (*TransactionGetResult, error) {
// TODO: Use Transcoder here
valueBytes, _, err := c.transcoder.Encode(value)
if err != nil {
return nil, err
}
c.queryStateLock.Lock()
if c.queryModeLocked() {
res, err := c.replaceQueryMode(doc, valueBytes)
c.queryStateLock.Unlock()
if err != nil {
c.logger.logInfof(c.attemptID, "Query mode replace failed")
return nil, err
}
return res, nil
}
c.queryStateLock.Unlock()
return c.replace(doc, valueBytes)
}
func (c *TransactionAttemptContext) replace(doc *TransactionGetResult, valueBytes []byte) (resOut *TransactionGetResult, errOut error) {
collection := doc.collection
id := doc.docID
waitCh := make(chan struct{}, 1)
err := c.txn.Replace(gocbcore.TransactionReplaceOptions{
Document: doc.coreRes,
Value: valueBytes,
}, func(res *gocbcore.TransactionGetResult, err error) {
if err == nil {
resOut = &TransactionGetResult{
collection: collection,
docID: id,
transcoder: NewJSONTranscoder(),
flags: 2 << 24,
coreRes: res,
}
}
errOut = createTransactionOperationFailedError(err)
waitCh <- struct{}{}
})
if err != nil {
resOut = nil
errOut = createTransactionOperationFailedError(err)
return
}
<-waitCh
return
}
// Insert will insert a new document, failing if the document already exists.
func (c *TransactionAttemptContext) Insert(collection *Collection, id string, value interface{}) (*TransactionGetResult, error) {
// TODO: Use Transcoder here
valueBytes, _, err := c.transcoder.Encode(value)
if err != nil {
return nil, err
}
c.queryStateLock.Lock()
if c.queryModeLocked() {
res, err := c.insertQueryMode(collection, id, valueBytes)
c.queryStateLock.Unlock()
if err != nil {
c.logger.logInfof(c.attemptID, "Query mode insert failed")
return nil, err
}
return res, nil
}
c.queryStateLock.Unlock()
return c.insert(collection, id, valueBytes)
}
func (c *TransactionAttemptContext) insert(collection *Collection, id string, valueBytes []byte) (resOut *TransactionGetResult, errOut error) {
a, err := collection.Bucket().Internal().IORouter()
if err != nil {
return nil, err
}
waitCh := make(chan struct{}, 1)
err = c.txn.Insert(gocbcore.TransactionInsertOptions{
Agent: a,
ScopeName: collection.ScopeName(),
CollectionName: collection.Name(),
Key: []byte(id),
Value: valueBytes,
}, func(res *gocbcore.TransactionGetResult, err error) {
if err == nil {
resOut = &TransactionGetResult{
collection: collection,
docID: id,
transcoder: NewJSONTranscoder(),
flags: 2 << 24,
coreRes: res,
}
}
// Handling for ExtInsertExisting
if errors.Is(err, gocbcore.ErrDocumentExists) {
errOut = err
} else {
errOut = createTransactionOperationFailedError(err)
}
waitCh <- struct{}{}
})
if err != nil {
resOut = nil
errOut = createTransactionOperationFailedError(err)
return
}
<-waitCh
return
}
// Remove will delete a document.
func (c *TransactionAttemptContext) Remove(doc *TransactionGetResult) error {
c.queryStateLock.Lock()
if c.queryModeLocked() {
err := c.removeQueryMode(doc)
c.queryStateLock.Unlock()
if err != nil {
c.logger.logInfof(c.attemptID, "Query mode remove failed")
return err
}
return nil
}
c.queryStateLock.Unlock()
return c.remove(doc)
}
func (c *TransactionAttemptContext) remove(doc *TransactionGetResult) (errOut error) {
waitCh := make(chan struct{}, 1)
err := c.txn.Remove(gocbcore.TransactionRemoveOptions{
Document: doc.coreRes,
}, func(res *gocbcore.TransactionGetResult, err error) {
errOut = createTransactionOperationFailedError(err)
waitCh <- struct{}{}
})
if err != nil {
errOut = createTransactionOperationFailedError(err)
return
}
<-waitCh
return
}
func (c *TransactionAttemptContext) commit() (errOut error) {
c.queryStateLock.Lock()
if c.queryModeLocked() {
err := c.commitQueryMode()
c.queryStateLock.Unlock()
if err != nil {
c.logger.logInfof(c.attemptID, "Query mode commit failed")
return err
}
return nil
}
c.queryStateLock.Unlock()
waitCh := make(chan struct{}, 1)
err := c.txn.Commit(func(err error) {
errOut = createTransactionOperationFailedError(err)
waitCh <- struct{}{}
})
if err != nil {
errOut = createTransactionOperationFailedError(err)
return
}
<-waitCh
return
}
func (c *TransactionAttemptContext) rollback() (errOut error) {
c.queryStateLock.Lock()
if c.queryModeLocked() {
err := c.rollbackQueryMode()
c.queryStateLock.Unlock()
if err != nil {
c.logger.logInfof(c.attemptID, "Query mode rollback failed")
return err
}
return nil
}
c.queryStateLock.Unlock()
waitCh := make(chan struct{}, 1)
err := c.txn.Rollback(func(err error) {
errOut = createTransactionOperationFailedError(err)
waitCh <- struct{}{}
})
if err != nil {
errOut = createTransactionOperationFailedError(err)
return
}
<-waitCh
return
}