File: bdb_deadlock.test

package info (click to toggle)
db5.3 5.3.28%2Bdfsg2-9
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 158,620 kB
  • sloc: ansic: 448,573; java: 111,824; tcl: 80,544; sh: 44,264; cs: 33,697; cpp: 21,600; perl: 14,557; xml: 10,799; makefile: 4,028; javascript: 1,998; yacc: 1,003; awk: 965; sql: 801; erlang: 342; python: 216; php: 24; asm: 14
file content (517 lines) | stat: -rw-r--r-- 9,565 bytes parent folder | download | duplicates (8)
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
#
#    May you do good and not evil.
#    May you find forgiveness for yourself and forgive others.
#    May you share freely, never taking more than you give.
#
#***********************************************************************
# This file implements regression tests for statement level
# deadlock resolution.

set testdir [file dirname $argv0]/../../lang/sql/sqlite/test

source $testdir/../../../../test/sql/bdb_util.tcl
if {[run_thread_tests]==0} { finish_test ; return }

sqlite3 db test.db -fullmutex 1

proc table_cleanup {} {
  db eval { 
   BEGIN;
   DROP TABLE t1;
   DROP TABLE t2;
   DROP TABLE t3;
   CREATE TABLE t1(a);
   CREATE TABLE t2(a);
   CREATE TABLE t3(a);
   COMMIT;
  }
}

proc table_cleanup_incrblob {} {
  db eval { 
   BEGIN;
   DROP TABLE t1;
   DROP TABLE t2;
   DROP TABLE t3;
   CREATE TABLE t1(a BLOB);
   CREATE TABLE t2(a BLOB);
   CREATE TABLE t3(a BLOB);
   COMMIT;
  }
}

do_test bdb_deadlock-1.0 {
   db eval { 
     CREATE TABLE t1(a);
     CREATE TABLE t2(a);
     CREATE TABLE t3(a);
   }
} {}

# Executes operations under an exclusive txn
set exclusive_test {
  set key ""
  if {[sqlite -has-codec]} {
    set key "xyzzy"
  }
  set ::DB [sqlthread open test.db $key]
  do_test e1 {
    execsql { BEGIN IMMEDIATE }
  } {SQLITE_OK} 
  do_test e2 {
    execsql { PRAGMA txn_priority=10000; }
  } {SQLITE_OK} 
  do_test e3 {
    execsql { INSERT INTO t2 VALUES(2) }
  } {SQLITE_OK}
  do_test e4 {
    execsql { INSERT INTO t1 VALUES(2) } 
  } {SQLITE_OK}
  do_test e5 {
    execsql { COMMIT } 
  } {SQLITE_OK}
  sqlite3_close $::DB
}

# Experience deadlock after read-only operations then commit
do_test bdb_deadlock-2.0 {
  db eval {
    BEGIN;
    SELECT * from t1;
  }
} {}

# The exclusive thread will block when it attempts to insert into t1
array unset finished
thread_spawn finished(0) "" $bdb_thread_procs $exclusive_test

after 5000

do_test bdb_deadlock-2.1 {
  db eval { SELECT * FROM t2 }
} {}

do_test bdb_deadlock-2.2 {
  db eval { INSERT INTO t3 VALUES(1) }
} {}

do_test bdb_deadlock-2.3 {
  db eval { COMMIT }
} {}

vwait finished(0)

do_test bdb_deadlock-2.4 {
  db eval { SELECT * FROM t3 }
} {1}

do_test bdb_deadlock-2.5 {
  table_cleanup
} {}

# Experience deadlock after real-only operations then rollback
do_test bdb_deadlock-3.0 {
  db eval {
    BEGIN;
    SELECT * from t1
  }
} {}

# The exclusive thread will block when it attempts to insert into t1
array unset finished
thread_spawn finished(0) "" $bdb_thread_procs $exclusive_test

after 5000

do_test bdb_deadlock-3.1 {
  catchsql { SELECT * FROM t2 }
} {1 {database table is locked}}

do_test bdb_deadlock-3.2 {
  db eval { INSERT INTO t3 VALUES(1) }
} {}

do_test bdb_deadlock-3.3 {
  db eval { ROLLBACK }
} {}

vwait finished(0)

do_test bdb_deadlock-3.4 {
  db eval { SELECT * FROM t3 }
} {}

do_test bdb_deadlock-3.5 {
  table_cleanup
} {}

# Tests 4 check that an insert interrupted due to deadlock
# does not change the table it was inserting into.
do_test bdb_deadlock-4.0 {
  db eval {
    BEGIN;
    INSERT INTO T1 values(1);
  }
} {}

# The exclusive thread will block when it attempts to insert into t1
array unset finished
thread_spawn finished(0) "" $bdb_thread_procs $exclusive_test

after 5000

do_test bdb_deadlock-4.1 {
  catchsql {
    INSERT INTO T2 values(1)
  }
} {1 {database table is locked}}

do_test bdb_deadlock-4.2 {
  db eval {
    COMMIT;
  }
} {}

vwait finished(0)

do_test bdb_deadlock-4.3 {
  db eval {
    SELECT * from t1;
  }
} {1 2}

do_test bdb_deadlock-4.4 {
  db eval {
    SELECT * from t2;
  }
} {2}

do_test bdb_deadlock-4.5 {
  table_cleanup
} {}

# Tests 5 check that rollback still works after an insert
# is interrupted due to deadlock.
do_test bdb_deadlock-5.0 {
  db eval {
    BEGIN;
    INSERT INTO T1 values(1);
  }
} {}

# The exclusive thread will block when it attempts to insert into t1
array unset finished
thread_spawn finished(0) "" $bdb_thread_procs $exclusive_test

after 5000

do_test bdb_deadlock-5.1 {
  catchsql {
    INSERT INTO T2 values(1)
  }
} {1 {database table is locked}}

do_test bdb_deadlock-5.2 {
  db eval {
    ROLLBACK
  }
} {}

vwait finished(0)

do_test bdb_deadlock-5.3 {
  db eval {
    SELECT * from t1;
  }
} {2}

do_test bdb_deadlock-5.4 {
  db eval {
    SELECT * from t2;
  }
} {2}

do_test bdb_deadlock-5.5 {
  table_cleanup
} {}

# Tests 6 check that inserts still work after an insert
# is interrupted by deadlock detection
do_test bdb_deadlock-6.0 {
  db eval {
    BEGIN;
    INSERT INTO T1 values(1);
  }
} {}

# The exclusive thread will block when it attempts to insert into t1
array unset finished
thread_spawn finished(0) "" $bdb_thread_procs $exclusive_test

after 5000

do_test bdb_deadlock-6.1 {
  catchsql {
    INSERT INTO T2 values(1)
  }
} {1 {database table is locked}}

do_test bdb_deadlock-6.2 {
  db eval {
    INSERT INTO T3 values(1)
  }
} {}

do_test bdb_deadlock-6.3 {
  db eval {
    COMMIT;
  }
} {}

vwait finished(0)

do_test bdb_deadlock-6.4 {
  db eval {
    SELECT * from t1;
  }
} {1 2}

do_test bdb_deadlock-6.5 {
  db eval {
    SELECT * from t2;
  }
} {2}

do_test bdb_deadlock-6.6 {
  db eval {
    SELECT * from t3;
  }
} {1}

do_test bdb_deadlock-6.7 {
  table_cleanup_incrblob
} {}

# Experience deadlock after read-only incrblob operations then commit
do_test bdb_deadlock-7.0 {
  db eval {
    INSERT INTO t1 VALUES('1');
    BEGIN;
  }
  set fd1 [db incrblob -readonly t1 a 1]
} {incrblob_1}

# The exclusive thread will block when it attempts to insert into t1
array unset finished
thread_spawn finished(0) "" $bdb_thread_procs $exclusive_test

after 5000

do_test bdb_deadlock-7.1 {
  set rc [catch {db incrblob -readonly t2 a 1} msg]
  list $rc $msg
} {1 {database table is locked}}

do_test bdb_deadlock-7.2 {
  db eval { INSERT INTO t3 VALUES(1) }
} {}

do_test bdb_deadlock-7.3 {
  close $fd1
} {}

do_test bdb_deadlock-7.4 {
  db eval { COMMIT }
} {}

vwait finished(0)

do_test bdb_deadlock-7.5 {
  db eval { SELECT * FROM t3 }
} {1}

do_test bdb_deadlock-7.6 {
  table_cleanup_incrblob
} {}

# Experience deadlock after read-only incrblob operations then aborts
do_test bdb_deadlock-8.0 {
  db eval {
    INSERT INTO t1 VALUES('1');
    BEGIN;
  }
  set fd1 [db incrblob -readonly t1 a 1]
} {incrblob_2}

# The exclusive thread will block when it attempts to insert into t1
array unset finished
thread_spawn finished(0) "" $bdb_thread_procs $exclusive_test

after 5000

do_test bdb_deadlock-8.1 {
  set rc [catch {db incrblob -readonly t2 a 1} msg]
  list $rc $msg
} {1 {database table is locked}}

do_test bdb_deadlock-8.2 {
  db eval { INSERT INTO t3 VALUES(1) }
} {}

do_test bdb_deadlock-8.3 {
  close $fd1
} {}

do_test bdb_deadlock-8.4 {
  db eval { ROLLBACK }
} {}

vwait finished(0)

do_test bdb_deadlock-8.5 {
  db eval { SELECT * FROM t3 }
} {}

do_test bdb_deadlock-8.6 {
  table_cleanup_incrblob
} {}

# Experience deadlock after incrblob operations then commits
do_test bdb_deadlock-9.0 {
  db eval {
    INSERT INTO t1 VALUES('1');
    BEGIN;
  }
  set fd1 [db incrblob t1 a 1]
  sqlite3_blob_write $fd1 0 3 1
} {}

# The exclusive thread will block when it attempts to insert into t1
array unset finished
thread_spawn finished(0) "" $bdb_thread_procs $exclusive_test

after 5000

do_test bdb_deadlock-9.1 {
  set rc [catch {db incrblob t2 a 1} msg]
  list $rc $msg
} {1 {database table is locked}}

do_test bdb_deadlock-9.2 {
  close $fd1
} {}

do_test bdb_deadlock-9.3 {
  db eval { COMMIT }
} {}

vwait finished(0)

do_test bdb_deadlock-9.4 {
  db eval { SELECT * FROM t1 }
} {3 2}

do_test bdb_deadlock-9.5 {
  table_cleanup_incrblob
} {}

# Experience deadlock after incrblob operations then aborts
do_test bdb_deadlock-10.0 {
  db eval {
    INSERT INTO t1 VALUES('1');
    BEGIN;
  }
  set fd1 [db incrblob t1 a 1]
  sqlite3_blob_write $fd1 0 3 1
} {}

# The exclusive thread will block when it attempts to insert into t1
array unset finished
thread_spawn finished(0) "" $bdb_thread_procs $exclusive_test

after 5000

do_test bdb_deadlock-10.1 {
  set rc [catch {db incrblob t2 a 1} msg]
  list $rc $msg
} {1 {database table is locked}}

do_test bdb_deadlock-10.2 {
  close $fd1
} {}

do_test bdb_deadlock-10.3 {
  db eval { ROLLBACK }
} {}

vwait finished(0)

do_test bdb_deadlock-10.4 {
  db eval { SELECT * FROM t1 }
} {1 2}

do_test bdb_deadlock-10.5 {
  table_cleanup_incrblob
} {}

# Executes operations under an exclusive txn
set read_test {
  set key ""
  if {[sqlite -has-codec]} {
    set key "xyzzy"
  }
  set ::DB [sqlthread open test.db $key]
  do_test e1 {
    execsql { BEGIN IMMEDIATE }
  } {SQLITE_OK} 
  do_test e2 {
    execsql { PRAGMA txn_priority=10000; }
  } {SQLITE_OK} 
  do_test e3 {
    execsql { INSERT INTO t2 VALUES(2) }
  } {SQLITE_OK}
  do_test e4 {
    execsql { SELECT * FROM t1; } 
  } {SQLITE_OK}
  do_test e5 {
    execsql { COMMIT } 
  } {SQLITE_OK}
  sqlite3_close $::DB
}

# Check that read-only operations can be executed after the
# read-only transaction experiences deadlock
do_test bdb_deadlock-11.0 {
  db eval {
    BEGIN;
    INSERT INTO t1 VALUES(1);
  }
} {}

# The exclusive thread will block when it attempts to select from t1
array unset finished
thread_spawn finished(0) "" $bdb_thread_procs $read_test

after 5000

do_test bdb_deadlock-11.1 {
  catchsql { SELECT * FROM t2 }
} {1 {database table is locked}}

do_test bdb_deadlock-11.2 {
  db eval { SELECT * FROM t1 }
} {1}

do_test bdb_deadlock-11.3 {
  db eval { COMMIT }
} {}

vwait finished(0)

do_test bdb_deadlock-11.5 {
  table_cleanup
} {}

db close

finish_test