forked from ethersphere/bee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transaction.go
375 lines (316 loc) · 9.12 KB
/
transaction.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
// Copyright 2022 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package storage
import (
"context"
"errors"
"fmt"
"sync"
"sync/atomic"
"github.com/ethersphere/bee/pkg/swarm"
)
// ErrTxDone is returned by any operation that is performed on
// a transaction that has already been committed or rolled back.
var ErrTxDone = errors.New("storage: transaction has already been committed or rolled back")
// Tx represents an in-progress Store transaction.
// A transaction must end with a call to Commit or Rollback.
type Tx interface {
// Commit commits the transaction.
Commit() error
// Rollback aborts the transaction.
Rollback() error
}
// TxStore represents a Tx Store where all
// operations are completed in a transaction.
type TxStore interface {
Tx
Store
NewTx(*TxState) TxStore
}
// TxChunkStore represents a Tx ChunkStore where
// all operations are completed in a transaction.
type TxChunkStore interface {
Tx
ChunkStore
NewTx(*TxState) TxChunkStore
}
// TxState is a mix-in for Tx. It provides basic
// functionality for transaction state lifecycle.
type TxState struct {
// ctx lives for the life of the transaction.
ctx context.Context
// cancel is this context cancel function
// that signals the end of this transaction.
cancel context.CancelCauseFunc
}
// AwaitDone returns a channel that blocks until the context
// in TxState is canceled or the transaction is done.
func (tx *TxState) AwaitDone() <-chan struct{} {
if tx == nil {
c := make(chan struct{})
close(c)
return c
}
// Wait for either the transaction to be committed or rolled
// back, or for the associated context to be closed.
return tx.ctx.Done()
}
// IsDone returns ErrTxDone if the transaction has already been committed
// or rolled back. If the transaction was in progress and the context was
// canceled, it returns the context.Canceled error.
func (tx *TxState) IsDone() error {
if tx == nil {
return nil
}
return context.Cause(tx.ctx)
}
// Done marks this transaction as complete. It returns ErrTxDone if the
// transaction has already been committed or rolled back or if the transaction
// was in progress and the context was canceled, it returns the context.Canceled
// error.
func (tx *TxState) Done() error {
if tx == nil {
return nil
}
if tx.ctx.Err() == nil {
tx.cancel(ErrTxDone)
return nil
}
return context.Cause(tx.ctx)
}
// NewTxState is a convenient constructor for creating instances of TxState.
func NewTxState(ctx context.Context) *TxState {
ctx, cancel := context.WithCancelCause(ctx)
return &TxState{ctx: ctx, cancel: cancel}
}
var _ Store = (*TxStoreBase)(nil)
// TxStoreBase implements the Store interface where
// the operations are guarded by a transaction.
type TxStoreBase struct {
*TxState
Store
rolledBack atomic.Bool
}
// Close implements the Store interface.
// The operation is blocked until the
// transaction is not done.
func (s *TxStoreBase) Close() error {
<-s.AwaitDone()
return s.Store.Close()
}
// Get implements the Store interface.
func (s *TxStoreBase) Get(item Item) error {
if err := s.IsDone(); err != nil {
return err
}
return s.Store.Get(item)
}
// Has implements the Store interface.
func (s *TxStoreBase) Has(key Key) (bool, error) {
if err := s.IsDone(); err != nil {
return false, err
}
return s.Store.Has(key)
}
// GetSize implements the Store interface.
func (s *TxStoreBase) GetSize(key Key) (int, error) {
if err := s.IsDone(); err != nil {
return 0, err
}
return s.Store.GetSize(key)
}
// Iterate implements the Store interface.
func (s *TxStoreBase) Iterate(query Query, fn IterateFn) error {
if err := s.IsDone(); err != nil {
return err
}
return s.Store.Iterate(query, fn)
}
// Count implements the Store interface.
func (s *TxStoreBase) Count(key Key) (int, error) {
if err := s.IsDone(); err != nil {
return 0, err
}
return s.Store.Count(key)
}
// Put implements the Store interface.
func (s *TxStoreBase) Put(item Item) error {
if err := s.IsDone(); err != nil {
return err
}
return s.Store.Put(item)
}
// Delete implements the Store interface.
func (s *TxStoreBase) Delete(item Item) error {
if err := s.IsDone(); err != nil {
return err
}
return s.Store.Delete(item)
}
// Rollback implements the TxStore interface.
func (s *TxStoreBase) Rollback() error {
if s.rolledBack.CompareAndSwap(false, true) {
if err := s.Done(); err == nil ||
errors.Is(err, context.Canceled) ||
errors.Is(err, context.DeadlineExceeded) {
return nil
}
}
return s.IsDone()
}
var _ ChunkStore = (*TxChunkStoreBase)(nil)
// TxChunkStoreBase implements the ChunkStore interface
// where the operations are guarded by a transaction.
type TxChunkStoreBase struct {
*TxState
ChunkStore
rolledBack atomic.Bool
}
// Close implements the ChunkStore interface.
// The operation is blocked until the
// transaction is not done.
func (s *TxChunkStoreBase) Close() error {
<-s.AwaitDone()
return s.ChunkStore.Close()
}
// Get implements the ChunkStore interface.
func (s *TxChunkStoreBase) Get(ctx context.Context, address swarm.Address) (swarm.Chunk, error) {
if err := s.IsDone(); err != nil {
return nil, err
}
return s.ChunkStore.Get(ctx, address)
}
// Put implements the ChunkStore interface.
func (s *TxChunkStoreBase) Put(ctx context.Context, chunk swarm.Chunk) error {
if err := s.IsDone(); err != nil {
return err
}
return s.ChunkStore.Put(ctx, chunk)
}
// Iterate implements the ChunkStore interface.
func (s *TxChunkStoreBase) Iterate(ctx context.Context, fn IterateChunkFn) error {
if err := s.IsDone(); err != nil {
return err
}
return s.ChunkStore.Iterate(ctx, fn)
}
// Has implements the ChunkStore interface.
func (s *TxChunkStoreBase) Has(ctx context.Context, address swarm.Address) (bool, error) {
if err := s.IsDone(); err != nil {
return false, err
}
return s.ChunkStore.Has(ctx, address)
}
// Delete implements the ChunkStore interface.
func (s *TxChunkStoreBase) Delete(ctx context.Context, address swarm.Address) error {
if err := s.IsDone(); err != nil {
return err
}
return s.ChunkStore.Delete(ctx, address)
}
// Rollback implements the TxChunkStore interface.
func (s *TxChunkStoreBase) Rollback() error {
if s.rolledBack.CompareAndSwap(false, true) {
if err := s.Done(); err == nil ||
errors.Is(err, context.Canceled) ||
errors.Is(err, context.DeadlineExceeded) {
return nil
}
}
return s.IsDone()
}
// TxOpCode represents code for tx operations.
type TxOpCode string
const (
PutOp TxOpCode = "put"
PutCreateOp TxOpCode = "putCreate"
PutUpdateOp TxOpCode = "putUpdate"
DeleteOp TxOpCode = "delete"
)
// TxRevertOp represents a reverse operation.
type TxRevertOp[K, V any] struct {
Origin TxOpCode
ObjectID string
Key K
Val V
}
// TxRevertFn represents a function that can be invoked
// to reverse the operation that was performed by the
// corresponding TxOpCode.
type TxRevertFn[K, V any] func(K, V) error
// TxRevertOpStore represents a store for TxRevertOp.
type TxRevertOpStore[K, V any] interface {
// Append appends a Revert operation to the store.
Append(*TxRevertOp[K, V]) error
// Revert executes all the revere operations
// in the store in reverse order.
Revert() error
// Clean cleans the store.
Clean() error
}
// NoOpTxRevertOpStore is a no-op implementation of TxRevertOpStore.
type NoOpTxRevertOpStore[K, V any] struct{}
func (s *NoOpTxRevertOpStore[K, V]) Append(*TxRevertOp[K, V]) error { return nil }
func (s *NoOpTxRevertOpStore[K, V]) Revert() error { return nil }
func (s *NoOpTxRevertOpStore[K, V]) Clean() error { return nil }
// InMemTxRevertOpStore is an in-memory implementation of TxRevertOpStore.
type InMemTxRevertOpStore[K, V any] struct {
revOpsFn map[TxOpCode]TxRevertFn[K, V]
mu sync.Mutex
ops []*TxRevertOp[K, V]
}
// Append implements TxRevertOpStore.
func (s *InMemTxRevertOpStore[K, V]) Append(op *TxRevertOp[K, V]) error {
if s == nil || op == nil {
return nil
}
s.mu.Lock()
s.ops = append(s.ops, op)
s.mu.Unlock()
return nil
}
// Revert implements TxRevertOpStore.
func (s *InMemTxRevertOpStore[K, V]) Revert() error {
if s == nil {
return nil
}
s.mu.Lock()
defer s.mu.Unlock()
var errs error
for i := len(s.ops) - 1; i >= 0; i-- {
op := s.ops[i]
if fn, ok := s.revOpsFn[op.Origin]; !ok {
errs = errors.Join(errs, fmt.Errorf(
"revert operation %q for object %s not found",
op.Origin,
op.ObjectID,
))
} else if err := fn(op.Key, op.Val); err != nil {
errs = errors.Join(errs, fmt.Errorf(
"revert operation %q for object %s failed: %w",
op.Origin,
op.ObjectID,
err,
))
}
}
s.ops = nil
return errs
}
// Clean implements TxRevertOpStore.
func (s *InMemTxRevertOpStore[K, V]) Clean() error {
if s == nil {
return nil
}
s.mu.Lock()
s.ops = nil
s.mu.Unlock()
return nil
}
// NewInMemTxRevertOpStore is a convenient constructor for creating instances of
// InMemTxRevertOpStore. The revOpsFn map is used to look up the revert function
// for a given TxOpCode.
func NewInMemTxRevertOpStore[K, V any](revOpsFn map[TxOpCode]TxRevertFn[K, V]) *InMemTxRevertOpStore[K, V] {
return &InMemTxRevertOpStore[K, V]{revOpsFn: revOpsFn}
}