-
Notifications
You must be signed in to change notification settings - Fork 175
/
transaction_coordinator.go
193 lines (159 loc) · 4.36 KB
/
transaction_coordinator.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
package computer
import (
"sync"
"time"
"github.com/onflow/flow-go/fvm"
"github.com/onflow/flow-go/fvm/storage"
"github.com/onflow/flow-go/fvm/storage/derived"
"github.com/onflow/flow-go/fvm/storage/logical"
"github.com/onflow/flow-go/fvm/storage/snapshot"
)
type TransactionWriteBehindLogger interface {
AddTransactionResult(
txn TransactionRequest,
snapshot *snapshot.ExecutionSnapshot,
output fvm.ProcedureOutput,
timeSpent time.Duration,
numTxnConflictRetries int,
)
}
// transactionCoordinator provides synchronization functionality for driving
// transaction execution.
type transactionCoordinator struct {
vm fvm.VM
mutex *sync.Mutex
cond *sync.Cond
snapshotTime logical.Time // guarded by mutex, cond broadcast on updates.
abortErr error // guarded by mutex, cond broadcast on updates.
// Note: database commit and result logging must occur within the same
// critical section (guraded by mutex).
database *storage.BlockDatabase
writeBehindLog TransactionWriteBehindLogger
}
type transaction struct {
request TransactionRequest
numConflictRetries int
coordinator *transactionCoordinator
startedAt time.Time
storage.Transaction
fvm.ProcedureExecutor
}
func newTransactionCoordinator(
vm fvm.VM,
storageSnapshot snapshot.StorageSnapshot,
cachedDerivedBlockData *derived.DerivedBlockData,
writeBehindLog TransactionWriteBehindLogger,
) *transactionCoordinator {
mutex := &sync.Mutex{}
cond := sync.NewCond(mutex)
database := storage.NewBlockDatabase(
storageSnapshot,
0,
cachedDerivedBlockData)
return &transactionCoordinator{
vm: vm,
mutex: mutex,
cond: cond,
snapshotTime: 0,
abortErr: nil,
database: database,
writeBehindLog: writeBehindLog,
}
}
func (coordinator *transactionCoordinator) SnapshotTime() logical.Time {
coordinator.mutex.Lock()
defer coordinator.mutex.Unlock()
return coordinator.snapshotTime
}
func (coordinator *transactionCoordinator) Error() error {
coordinator.mutex.Lock()
defer coordinator.mutex.Unlock()
return coordinator.abortErr
}
func (coordinator *transactionCoordinator) AbortAllOutstandingTransactions(
err error,
) {
coordinator.mutex.Lock()
defer coordinator.mutex.Unlock()
if coordinator.abortErr != nil { // Transactions are already aborting.
return
}
coordinator.abortErr = err
coordinator.cond.Broadcast()
}
func (coordinator *transactionCoordinator) NewTransaction(
request TransactionRequest,
attempt int,
) (
*transaction,
error,
) {
err := coordinator.Error()
if err != nil {
return nil, err
}
txn, err := coordinator.database.NewTransaction(
request.ExecutionTime(),
fvm.ProcedureStateParameters(request.ctx, request))
if err != nil {
return nil, err
}
return &transaction{
request: request,
coordinator: coordinator,
numConflictRetries: attempt,
startedAt: time.Now(),
Transaction: txn,
ProcedureExecutor: coordinator.vm.NewExecutor(
request.ctx,
request.TransactionProcedure,
txn),
}, nil
}
func (coordinator *transactionCoordinator) commit(txn *transaction) error {
coordinator.mutex.Lock()
defer coordinator.mutex.Unlock()
if coordinator.abortErr != nil {
return coordinator.abortErr
}
executionSnapshot, err := txn.Transaction.Commit()
if err != nil {
return err
}
coordinator.writeBehindLog.AddTransactionResult(
txn.request,
executionSnapshot,
txn.Output(),
time.Since(txn.startedAt),
txn.numConflictRetries)
// Commit advances the database's snapshot.
coordinator.snapshotTime += 1
coordinator.cond.Broadcast()
return nil
}
func (txn *transaction) Commit() error {
return txn.coordinator.commit(txn)
}
func (coordinator *transactionCoordinator) waitForUpdatesNewerThan(
snapshotTime logical.Time,
) (
logical.Time,
error,
logical.Time,
error,
) {
coordinator.mutex.Lock()
defer coordinator.mutex.Unlock()
startTime := coordinator.snapshotTime
startErr := coordinator.abortErr
for coordinator.snapshotTime <= snapshotTime && coordinator.abortErr == nil {
coordinator.cond.Wait()
}
return startTime, startErr, coordinator.snapshotTime, coordinator.abortErr
}
func (txn *transaction) WaitForUpdates() error {
// Note: the frist three returned values are only used by tests to ensure
// the function correctly waited.
_, _, _, err := txn.coordinator.waitForUpdatesNewerThan(txn.SnapshotTime())
return err
}