@@ -15,6 +15,9 @@ const (
15
15
N_ACCOUNTS = 100000
16
16
//ISOLATION_LEVEL = "repeatable read"
17
17
ISOLATION_LEVEL = "read committed"
18
+ GLOBAL_UPDATES = true
19
+ LOCAL_UPDATES = false
20
+ CURSORS = false
18
21
)
19
22
20
23
@@ -134,28 +137,71 @@ func transfer(id int, cCommits chan int, cAborts chan int, wg *sync.WaitGroup) {
134
137
amount := 1
135
138
account1 := rand .Intn (N_ACCOUNTS )
136
139
account2 := rand .Intn (N_ACCOUNTS )
140
+ srci := rand .Intn (2 )
141
+ dsti := rand .Intn (2 )
142
+ if (srci > dsti ) {
143
+ srci , dsti = dsti , srci
144
+ }
137
145
138
- src := conn [0 ]
139
- dst := conn [1 ]
146
+ src := conn [srci ]
147
+ dst := conn [dsti ]
140
148
141
- xid = execQuery (src , "select dtm_begin_transaction()" )
142
- exec (dst , "select dtm_join_transaction($1)" , xid )
149
+ if src == dst {
150
+ // local update
151
+ if ! LOCAL_UPDATES {
152
+ // which we do not want
153
+ continue
154
+ }
143
155
144
- // start transaction
145
- exec (src , "begin transaction isolation level " + ISOLATION_LEVEL )
146
- exec (dst , "begin transaction isolation level " + ISOLATION_LEVEL )
156
+ exec (src , "begin transaction isolation level " + ISOLATION_LEVEL )
157
+ ok1 := execUpdate (src , "update t set v = v - $1 where u=$2" , amount , account1 )
158
+ ok2 := execUpdate (src , "update t set v = v + $1 where u=$2" , amount , account2 )
159
+ if ! ok1 || ! ok2 {
160
+ exec (src , "rollback" )
161
+ nAborts += 1
162
+ } else {
163
+ exec (src , "commit" )
164
+ nCommits += 1
165
+ myCommits += 1
166
+ }
167
+ } else {
168
+ // global update
169
+ if ! GLOBAL_UPDATES {
170
+ // which we do not want
171
+ continue
172
+ }
147
173
148
- ok1 := execUpdate ( src , "update t set v = v - $1 where u=$2" , amount , account1 )
149
- ok2 := execUpdate (dst , "update t set v = v + $1 where u=$2 " , amount , account2 )
174
+ xid = execQuery ( src , "select dtm_begin_transaction()" )
175
+ exec (dst , "select dtm_join_transaction($1) " , xid )
150
176
151
- if ! ok1 || ! ok2 {
152
- exec (src , "rollback" )
153
- exec (dst , "rollback" )
154
- nAborts += 1
155
- } else {
156
- commit (src , dst )
157
- nCommits += 1
158
- myCommits += 1
177
+ // start transaction
178
+ exec (src , "begin transaction isolation level " + ISOLATION_LEVEL )
179
+ exec (dst , "begin transaction isolation level " + ISOLATION_LEVEL )
180
+
181
+ ok := true
182
+ if (CURSORS ) {
183
+ exec (src , "declare cur0 cursor for select * from t where u=$1 for update" , account1 )
184
+ exec (dst , "declare cur0 cursor for select * from t where u=$1 for update" , account2 )
185
+
186
+ ok = execUpdate (src , "fetch from cur0" ) && ok
187
+ ok = execUpdate (dst , "fetch from cur0" ) && ok
188
+
189
+ ok = execUpdate (src , "update t set v = v - $1 where current of cur0" , amount ) && ok
190
+ ok = execUpdate (dst , "update t set v = v + $1 where current of cur0" , amount ) && ok
191
+ } else {
192
+ ok = execUpdate (src , "update t set v = v - $1 where u=$2" , amount , account1 ) && ok
193
+ ok = execUpdate (dst , "update t set v = v + $1 where u=$2" , amount , account2 ) && ok
194
+ }
195
+
196
+ if ok {
197
+ commit (src , dst )
198
+ nCommits += 1
199
+ myCommits += 1
200
+ } else {
201
+ exec (src , "rollback" )
202
+ exec (dst , "rollback" )
203
+ nAborts += 1
204
+ }
159
205
}
160
206
161
207
if time .Since (start ).Seconds () > 1 {
@@ -197,11 +243,13 @@ func inspect(wg *sync.WaitGroup) {
197
243
if (sum != prevSum ) {
198
244
xmin1 := execQuery (conn1 , "select dtm_get_current_snapshot_xmin()" )
199
245
xmax1 := execQuery (conn1 , "select dtm_get_current_snapshot_xmax()" )
246
+ xcnt1 := execQuery (conn1 , "select dtm_get_current_snapshot_xcnt()" )
200
247
xmin2 := execQuery (conn2 , "select dtm_get_current_snapshot_xmin()" )
201
248
xmax2 := execQuery (conn2 , "select dtm_get_current_snapshot_xmax()" )
249
+ xcnt2 := execQuery (conn2 , "select dtm_get_current_snapshot_xcnt()" )
202
250
fmt .Printf (
203
- "Total=%d xid=%d snap1=[%d, %d) snap2=[%d, %d)\n " ,
204
- sum , xid , xmin1 , xmax1 , xmin2 , xmax2 ,
251
+ "Total=%d xid=%d snap1=[%d, %d){%d} snap2=[%d, %d){%d} \n " ,
252
+ sum , xid , xmin1 , xmax1 , xcnt1 , xmin2 , xmax2 , xcnt2 ,
205
253
)
206
254
prevSum = sum
207
255
}
0 commit comments