@@ -40,11 +40,13 @@ struct thread
40
40
{
41
41
pthread_t t;
42
42
size_t proceeded;
43
+ size_t aborts;
43
44
int id;
44
45
45
46
void start (int tid, thread_proc_t proc) {
46
47
id = tid;
47
48
proceeded = 0 ;
49
+ aborts = 0 ;
48
50
pthread_create (&t, NULL , proc, this );
49
51
}
50
52
@@ -160,12 +162,13 @@ void* writer(void* arg)
160
162
int srcAcc = cfg.startId + random () % cfg.diapason ;
161
163
int dstAcc = cfg.startId + random () % cfg.diapason ;
162
164
165
+ #if 1 // avoid deadlocks
163
166
if (srcAcc > dstAcc) {
164
167
int tmpAcc = dstAcc;
165
168
dstAcc = srcAcc;
166
169
srcAcc = tmpAcc;
167
170
}
168
-
171
+ # endif
169
172
sprintf (gtid, " %d.%d.%d" , cfg.startId , t.id , i);
170
173
171
174
nontransaction srcTx (*srcCon);
@@ -180,8 +183,16 @@ void* writer(void* arg)
180
183
exec (srcTx, " savepoint c1" );
181
184
exec (dstTx, " savepoint c2" );
182
185
183
- exec (srcTx, " update t set v = v - 1 where u=%d" , srcAcc);
184
- exec (dstTx, " update t set v = v + 1 where u=%d" , dstAcc);
186
+ try {
187
+ exec (srcTx, " update t set v = v - 1 where u=%d" , srcAcc);
188
+ exec (dstTx, " update t set v = v + 1 where u=%d" , dstAcc);
189
+ } catch (pqxx_exception const & x) {
190
+ exec (srcTx, " rollback" );
191
+ exec (dstTx, " rollback" );
192
+ t.aborts += 1 ;
193
+ i -= 1 ;
194
+ continue ;
195
+ }
185
196
186
197
exec (srcTx, " prepare transaction '%s'" , gtid);
187
198
exec (dstTx, " prepare transaction '%s'" , gtid);
@@ -281,7 +292,8 @@ int main (int argc, char* argv[])
281
292
vector<thread> writers (cfg.nWriters );
282
293
size_t nReads = 0 ;
283
294
size_t nWrites = 0 ;
284
-
295
+ size_t nAborts = 0 ;
296
+
285
297
for (int i = 0 ; i < cfg.nReaders ; i++) {
286
298
readers[i].start (i, reader);
287
299
}
@@ -292,6 +304,7 @@ int main (int argc, char* argv[])
292
304
for (int i = 0 ; i < cfg.nWriters ; i++) {
293
305
writers[i].wait ();
294
306
nWrites += writers[i].proceeded ;
307
+ nAborts += writers[i].aborts ;
295
308
}
296
309
297
310
running = false ;
@@ -307,12 +320,14 @@ int main (int argc, char* argv[])
307
320
308
321
printf (
309
322
" {\" update_tps\" :%f, \" read_tps\" :%f,"
310
- " \" readers\" :%d, \" writers\" :%d,"
323
+ " \" readers\" :%d, \" writers\" :%d, \" aborts \" :%ld, \" abort_percent \" : %d, "
311
324
" \" accounts\" :%d, \" iterations\" :%d, \" hosts\" :%ld}\n " ,
312
325
(double )(nWrites*USEC)/elapsed,
313
326
(double )(nReads*USEC)/elapsed,
314
327
cfg.nReaders ,
315
328
cfg.nWriters ,
329
+ nAborts,
330
+ (int )(nAborts*100 /nWrites),
316
331
cfg.nAccounts ,
317
332
cfg.nIterations ,
318
333
cfg.connections .size ()
0 commit comments