dan | a58f26f | 2010-11-16 18:56:51 +0000 | [diff] [blame] | 1 | # 2010 April 13 |
| 2 | # |
| 3 | # The author disclaims copyright to this source code. In place of |
| 4 | # a legal notice, here is a blessing: |
| 5 | # |
| 6 | # May you do good and not evil. |
| 7 | # May you find forgiveness for yourself and forgive others. |
| 8 | # May you share freely, never taking more than you give. |
| 9 | # |
| 10 | #*********************************************************************** |
| 11 | # This file implements regression tests for SQLite library. The |
| 12 | # focus of this file is testing the operation of "blocking-checkpoint" |
| 13 | # operations. |
| 14 | # |
| 15 | |
| 16 | set testdir [file dirname $argv0] |
| 17 | source $testdir/tester.tcl |
| 18 | source $testdir/lock_common.tcl |
| 19 | source $testdir/wal_common.tcl |
| 20 | ifcapable !wal {finish_test ; return } |
| 21 | |
| 22 | set testprefix wal5 |
| 23 | |
dan | f2b8dd5 | 2010-11-18 19:28:01 +0000 | [diff] [blame] | 24 | proc db_page_count {{file test.db}} { expr [file size $file] / 1024 } |
| 25 | proc wal_page_count {{file test.db}} { wal_frame_count ${file}-wal 1024 } |
| 26 | |
| 27 | |
dan | 9c5e368 | 2011-02-07 15:12:12 +0000 | [diff] [blame] | 28 | # A checkpoint may be requested either using the C API or by executing |
| 29 | # an SQL PRAGMA command. To test both methods, all tests in this file are |
| 30 | # run twice - once using each method to request checkpoints. |
dan | f2b8dd5 | 2010-11-18 19:28:01 +0000 | [diff] [blame] | 31 | # |
dan | 9c5e368 | 2011-02-07 15:12:12 +0000 | [diff] [blame] | 32 | foreach {testprefix do_wal_checkpoint} { |
dan | f2b8dd5 | 2010-11-18 19:28:01 +0000 | [diff] [blame] | 33 | |
dan | 9c5e368 | 2011-02-07 15:12:12 +0000 | [diff] [blame] | 34 | wal5-pragma { |
| 35 | proc do_wal_checkpoint { dbhandle args } { |
| 36 | array set a $args |
| 37 | foreach key [array names a] { |
| 38 | if {[lsearch {-mode -db} $key]<0} { error "unknown switch: $key" } |
dan | 0875637 | 2010-11-19 07:17:09 +0000 | [diff] [blame] | 39 | } |
dan | 9c5e368 | 2011-02-07 15:12:12 +0000 | [diff] [blame] | 40 | |
| 41 | set sql "PRAGMA " |
| 42 | if {[info exists a(-db)]} { append sql "$a(-db)." } |
| 43 | append sql "wal_checkpoint" |
| 44 | if {[info exists a(-mode)]} { append sql " = $a(-mode)" } |
| 45 | |
| 46 | uplevel [list $dbhandle eval $sql] |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | wal5-capi { |
| 51 | proc do_wal_checkpoint { dbhandle args } { |
| 52 | set a(-mode) passive |
| 53 | array set a $args |
| 54 | foreach key [array names a] { |
| 55 | if {[lsearch {-mode -db} $key]<0} { error "unknown switch: $key" } |
| 56 | } |
| 57 | |
| 58 | if {$a(-mode)!="restart" && $a(-mode)!="full"} { set a(-mode) passive } |
| 59 | |
| 60 | set cmd [list sqlite3_wal_checkpoint_v2 $dbhandle $a(-mode)] |
| 61 | if {[info exists a(-db)]} { lappend sql $a(-db) } |
| 62 | |
| 63 | uplevel $cmd |
| 64 | } |
| 65 | } |
| 66 | } { |
| 67 | |
| 68 | eval $do_wal_checkpoint |
| 69 | |
| 70 | do_multiclient_test tn { |
| 71 | |
| 72 | set ::nBusyHandler 0 |
| 73 | set ::busy_handler_script "" |
| 74 | proc busyhandler {n} { |
| 75 | incr ::nBusyHandler |
| 76 | eval $::busy_handler_script |
dan | 0875637 | 2010-11-19 07:17:09 +0000 | [diff] [blame] | 77 | return 0 |
| 78 | } |
dan | 9c5e368 | 2011-02-07 15:12:12 +0000 | [diff] [blame] | 79 | |
| 80 | proc reopen_all {} { |
| 81 | code1 {db close} |
| 82 | code2 {db2 close} |
| 83 | code3 {db3 close} |
| 84 | |
| 85 | code1 {sqlite3 db test.db} |
| 86 | code2 {sqlite3 db2 test.db} |
| 87 | code3 {sqlite3 db3 test.db} |
| 88 | |
| 89 | sql1 { PRAGMA synchronous = NORMAL } |
| 90 | code1 { db busy busyhandler } |
| 91 | } |
| 92 | |
| 93 | do_test 1.$tn.1 { |
| 94 | reopen_all |
| 95 | sql1 { |
| 96 | PRAGMA page_size = 1024; |
| 97 | PRAGMA auto_vacuum = 0; |
| 98 | CREATE TABLE t1(x, y); |
| 99 | PRAGMA journal_mode = WAL; |
| 100 | INSERT INTO t1 VALUES(1, zeroblob(1200)); |
| 101 | INSERT INTO t1 VALUES(2, zeroblob(1200)); |
| 102 | INSERT INTO t1 VALUES(3, zeroblob(1200)); |
| 103 | } |
| 104 | expr [file size test.db] / 1024 |
| 105 | } {2} |
| 106 | |
| 107 | # Have connection 2 grab a read-lock on the current snapshot. |
| 108 | do_test 1.$tn.2 { sql2 { BEGIN; SELECT x FROM t1 } } {1 2 3} |
| 109 | |
| 110 | # Attempt a checkpoint. |
| 111 | do_test 1.$tn.3 { |
| 112 | code1 { do_wal_checkpoint db } |
| 113 | list [db_page_count] [wal_page_count] |
| 114 | } {5 9} |
| 115 | |
| 116 | # Write to the db again. The log cannot wrap because of the lock still |
| 117 | # held by connection 2. The busy-handler has not yet been invoked. |
| 118 | do_test 1.$tn.4 { |
| 119 | sql1 { INSERT INTO t1 VALUES(4, zeroblob(1200)) } |
| 120 | list [db_page_count] [wal_page_count] $::nBusyHandler |
| 121 | } {5 12 0} |
| 122 | |
| 123 | # Now do a blocking-checkpoint. Set the busy-handler up so that connection |
| 124 | # 2 releases its lock on the 6th invocation. The checkpointer should then |
| 125 | # proceed to checkpoint the entire log file. Next write should go to the |
| 126 | # start of the log file. |
| 127 | # |
| 128 | set ::busy_handler_script { if {$n==5} { sql2 COMMIT } } |
| 129 | do_test 1.$tn.5 { |
| 130 | code1 { do_wal_checkpoint db -mode restart } |
| 131 | list [db_page_count] [wal_page_count] $::nBusyHandler |
| 132 | } {6 12 6} |
| 133 | do_test 1.$tn.6 { |
| 134 | set ::nBusyHandler 0 |
| 135 | sql1 { INSERT INTO t1 VALUES(5, zeroblob(1200)) } |
| 136 | list [db_page_count] [wal_page_count] $::nBusyHandler |
| 137 | } {6 12 0} |
| 138 | |
| 139 | do_test 1.$tn.7 { |
| 140 | reopen_all |
| 141 | list [db_page_count] [wal_page_count] $::nBusyHandler |
| 142 | } {7 0 0} |
| 143 | |
| 144 | do_test 1.$tn.8 { sql2 { BEGIN ; SELECT x FROM t1 } } {1 2 3 4 5} |
| 145 | do_test 1.$tn.9 { |
| 146 | sql1 { INSERT INTO t1 VALUES(6, zeroblob(1200)) } |
| 147 | list [db_page_count] [wal_page_count] $::nBusyHandler |
| 148 | } {7 5 0} |
| 149 | do_test 1.$tn.10 { sql3 { BEGIN ; SELECT x FROM t1 } } {1 2 3 4 5 6} |
| 150 | |
| 151 | set ::busy_handler_script { |
| 152 | if {$n==5} { sql2 COMMIT } |
| 153 | if {$n==6} { set ::db_file_size [db_page_count] } |
| 154 | if {$n==7} { sql3 COMMIT } |
| 155 | } |
| 156 | do_test 1.$tn.11 { |
| 157 | code1 { do_wal_checkpoint db -mode restart } |
| 158 | list [db_page_count] [wal_page_count] $::nBusyHandler |
| 159 | } {10 5 8} |
| 160 | do_test 1.$tn.12 { set ::db_file_size } 10 |
| 161 | } |
| 162 | |
| 163 | #------------------------------------------------------------------------- |
| 164 | # This block of tests explores checkpoint operations on more than one |
| 165 | # database file. |
| 166 | # |
| 167 | proc setup_and_attach_aux {} { |
| 168 | sql1 { ATTACH 'test.db2' AS aux } |
| 169 | sql2 { ATTACH 'test.db2' AS aux } |
| 170 | sql3 { ATTACH 'test.db2' AS aux } |
| 171 | sql1 { |
dan | 7fa65fb | 2011-04-01 19:14:40 +0000 | [diff] [blame] | 172 | PRAGMA aux.auto_vacuum = 0; |
| 173 | PRAGMA main.auto_vacuum = 0; |
dan | 9c5e368 | 2011-02-07 15:12:12 +0000 | [diff] [blame] | 174 | PRAGMA main.page_size=1024; PRAGMA main.journal_mode=WAL; |
| 175 | PRAGMA aux.page_size=1024; PRAGMA aux.journal_mode=WAL; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | proc file_page_counts {} { |
| 180 | list [db_page_count test.db ] \ |
| 181 | [wal_page_count test.db ] \ |
| 182 | [db_page_count test.db2] \ |
| 183 | [wal_page_count test.db2] |
| 184 | } |
| 185 | |
| 186 | # Test that executing "PRAGMA wal_checkpoint" checkpoints all attached |
| 187 | # databases, not just the main db. In capi mode, check that this is |
| 188 | # true if a NULL pointer is passed to wal_checkpoint_v2() in place of a |
| 189 | # database name. |
| 190 | do_multiclient_test tn { |
| 191 | setup_and_attach_aux |
| 192 | do_test 2.1.$tn.1 { |
dan | 0875637 | 2010-11-19 07:17:09 +0000 | [diff] [blame] | 193 | sql1 { |
| 194 | CREATE TABLE t1(a, b); |
| 195 | INSERT INTO t1 VALUES(1, 2); |
dan | 9c5e368 | 2011-02-07 15:12:12 +0000 | [diff] [blame] | 196 | CREATE TABLE aux.t2(a, b); |
| 197 | INSERT INTO t2 VALUES(1, 2); |
dan | 0875637 | 2010-11-19 07:17:09 +0000 | [diff] [blame] | 198 | } |
dan | 9c5e368 | 2011-02-07 15:12:12 +0000 | [diff] [blame] | 199 | } {} |
dan | 0774bb5 | 2011-12-19 10:07:56 +0000 | [diff] [blame] | 200 | do_test 2.2.$tn.2 { file_page_counts } {1 3 1 3} |
| 201 | do_test 2.1.$tn.3 { code1 { do_wal_checkpoint db } } {0 3 3} |
| 202 | do_test 2.1.$tn.4 { file_page_counts } {2 3 2 3} |
dan | 9c5e368 | 2011-02-07 15:12:12 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | do_multiclient_test tn { |
| 206 | setup_and_attach_aux |
| 207 | do_test 2.2.$tn.1 { |
| 208 | execsql { |
| 209 | CREATE TABLE t1(a, b); |
| 210 | INSERT INTO t1 VALUES(1, 2); |
| 211 | CREATE TABLE aux.t2(a, b); |
| 212 | INSERT INTO t2 VALUES(1, 2); |
| 213 | INSERT INTO t2 VALUES(3, 4); |
| 214 | } |
| 215 | } {} |
dan | 0774bb5 | 2011-12-19 10:07:56 +0000 | [diff] [blame] | 216 | do_test 2.2.$tn.2 { file_page_counts } {1 3 1 4} |
dan | 9c5e368 | 2011-02-07 15:12:12 +0000 | [diff] [blame] | 217 | do_test 2.2.$tn.3 { sql2 { BEGIN; SELECT * FROM t1 } } {1 2} |
dan | 0774bb5 | 2011-12-19 10:07:56 +0000 | [diff] [blame] | 218 | do_test 2.2.$tn.4 { code1 { do_wal_checkpoint db -mode restart } } {1 3 3} |
| 219 | do_test 2.2.$tn.5 { file_page_counts } {2 3 2 4} |
dan | 9c5e368 | 2011-02-07 15:12:12 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | do_multiclient_test tn { |
| 223 | setup_and_attach_aux |
| 224 | do_test 2.3.$tn.1 { |
| 225 | execsql { |
| 226 | CREATE TABLE t1(a, b); |
| 227 | INSERT INTO t1 VALUES(1, 2); |
| 228 | CREATE TABLE aux.t2(a, b); |
| 229 | INSERT INTO t2 VALUES(1, 2); |
| 230 | } |
| 231 | } {} |
dan | 0774bb5 | 2011-12-19 10:07:56 +0000 | [diff] [blame] | 232 | do_test 2.3.$tn.2 { file_page_counts } {1 3 1 3} |
dan | 9c5e368 | 2011-02-07 15:12:12 +0000 | [diff] [blame] | 233 | do_test 2.3.$tn.3 { sql2 { BEGIN; SELECT * FROM t1 } } {1 2} |
| 234 | do_test 2.3.$tn.4 { sql1 { INSERT INTO t1 VALUES(3, 4) } } {} |
| 235 | do_test 2.3.$tn.5 { sql1 { INSERT INTO t2 VALUES(3, 4) } } {} |
dan | 0774bb5 | 2011-12-19 10:07:56 +0000 | [diff] [blame] | 236 | do_test 2.3.$tn.6 { file_page_counts } {1 4 1 4} |
| 237 | do_test 2.3.$tn.7 { code1 { do_wal_checkpoint db -mode full } } {1 4 3} |
dan | 7909e54 | 2013-03-22 20:15:31 +0000 | [diff] [blame] | 238 | |
| 239 | # The checkpoint above only writes page 1 of the db file. The other |
| 240 | # page (page 2) is locked by the read-transaction opened by the |
| 241 | # [sql2] commmand above. So normally, the db is 1 page in size here. |
| 242 | # However, in mmap() mode, the db is pre-allocated to 2 pages at the |
| 243 | # start of the checkpoint, even though page 2 cannot be written. |
drh | 0d0614b | 2013-03-25 23:09:28 +0000 | [diff] [blame] | 244 | set nDb 2 |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 245 | if {[permutation]!="mmap"} {set nDb 1} |
drh | 188d488 | 2013-04-08 20:47:49 +0000 | [diff] [blame] | 246 | ifcapable !mmap {set nDb 1} |
dan | 7909e54 | 2013-03-22 20:15:31 +0000 | [diff] [blame] | 247 | do_test 2.3.$tn.8 { file_page_counts } [list $nDb 4 2 4] |
dan | 9c5e368 | 2011-02-07 15:12:12 +0000 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | # Check that checkpoints block on the correct locks. And respond correctly |
| 251 | # if they cannot obtain those locks. There are three locks that a checkpoint |
| 252 | # may block on (in the following order): |
| 253 | # |
| 254 | # 1. The writer lock: FULL and RESTART checkpoints block until any writer |
| 255 | # process releases its lock. |
| 256 | # |
| 257 | # 2. Readers using part of the log file. FULL and RESTART checkpoints block |
| 258 | # until readers using part (but not all) of the log file have finished. |
| 259 | # |
| 260 | # 3. Readers using any of the log file. After copying data into the |
| 261 | # database file, RESTART checkpoints block until readers using any part |
| 262 | # of the log file have finished. |
| 263 | # |
| 264 | # This test case involves running a checkpoint while there exist other |
| 265 | # processes holding all three types of locks. |
| 266 | # |
| 267 | foreach {tn1 checkpoint busy_on ckpt_expected expected} { |
dan | 0774bb5 | 2011-12-19 10:07:56 +0000 | [diff] [blame] | 268 | 1 PASSIVE - {0 3 3} - |
| 269 | 2 TYPO - {0 3 3} - |
dan | 9c5e368 | 2011-02-07 15:12:12 +0000 | [diff] [blame] | 270 | |
dan | 0774bb5 | 2011-12-19 10:07:56 +0000 | [diff] [blame] | 271 | 3 FULL - {0 4 4} 2 |
| 272 | 4 FULL 1 {1 3 3} 1 |
| 273 | 5 FULL 2 {1 4 3} 2 |
| 274 | 6 FULL 3 {0 4 4} 2 |
dan | 9c5e368 | 2011-02-07 15:12:12 +0000 | [diff] [blame] | 275 | |
dan | 0774bb5 | 2011-12-19 10:07:56 +0000 | [diff] [blame] | 276 | 7 RESTART - {0 4 4} 3 |
| 277 | 8 RESTART 1 {1 3 3} 1 |
| 278 | 9 RESTART 2 {1 4 3} 2 |
| 279 | 10 RESTART 3 {1 4 4} 3 |
dan | 9c5e368 | 2011-02-07 15:12:12 +0000 | [diff] [blame] | 280 | |
| 281 | } { |
| 282 | do_multiclient_test tn { |
| 283 | setup_and_attach_aux |
| 284 | |
| 285 | proc busyhandler {x} { |
| 286 | set ::max_busyhandler $x |
| 287 | if {$::busy_on!="-" && $x==$::busy_on} { return 1 } |
| 288 | switch -- $x { |
| 289 | 1 { sql2 "COMMIT ; BEGIN ; SELECT * FROM t1" } |
| 290 | 2 { sql3 "COMMIT" } |
| 291 | 3 { sql2 "COMMIT" } |
| 292 | } |
| 293 | return 0 |
| 294 | } |
| 295 | set ::max_busyhandler - |
| 296 | |
| 297 | do_test 2.4.$tn1.$tn.1 { |
| 298 | sql1 { |
| 299 | CREATE TABLE t1(a, b); |
| 300 | INSERT INTO t1 VALUES(1, 2); |
| 301 | } |
| 302 | sql2 { BEGIN; INSERT INTO t1 VALUES(3, 4) } |
| 303 | sql3 { BEGIN; SELECT * FROM t1 } |
| 304 | } {1 2} |
| 305 | |
| 306 | do_test 2.4.$tn1.$tn.2 { |
| 307 | code1 { db busy busyhandler } |
| 308 | code1 { do_wal_checkpoint db -mode [string tolower $checkpoint] } |
| 309 | } $ckpt_expected |
| 310 | do_test 2.4.$tn1.$tn.3 { set ::max_busyhandler } $expected |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | |
| 315 | do_multiclient_test tn { |
| 316 | |
| 317 | code1 $do_wal_checkpoint |
| 318 | code2 $do_wal_checkpoint |
| 319 | code3 $do_wal_checkpoint |
| 320 | |
| 321 | do_test 3.$tn.1 { |
| 322 | sql1 { |
dan | 7fa65fb | 2011-04-01 19:14:40 +0000 | [diff] [blame] | 323 | PRAGMA auto_vacuum = 0; |
dan | 9c5e368 | 2011-02-07 15:12:12 +0000 | [diff] [blame] | 324 | PRAGMA journal_mode = WAL; |
| 325 | PRAGMA synchronous = normal; |
| 326 | CREATE TABLE t1(x, y); |
| 327 | } |
| 328 | |
| 329 | sql2 { PRAGMA journal_mode } |
| 330 | sql3 { PRAGMA journal_mode } |
| 331 | } {wal} |
| 332 | |
| 333 | do_test 3.$tn.2 { code2 { do_wal_checkpoint db2 } } {0 2 2} |
| 334 | |
| 335 | do_test 3.$tn.3 { code2 { do_wal_checkpoint db2 } } {0 2 2} |
| 336 | |
| 337 | do_test 3.$tn.4 { code3 { do_wal_checkpoint db3 } } {0 2 2} |
| 338 | |
| 339 | code1 {db close} |
| 340 | code2 {db2 close} |
| 341 | code3 {db3 close} |
| 342 | |
| 343 | code1 {sqlite3 db test.db} |
| 344 | code2 {sqlite3 db2 test.db} |
| 345 | code3 {sqlite3 db3 test.db} |
| 346 | |
| 347 | do_test 3.$tn.5 { sql3 { PRAGMA journal_mode } } {wal} |
| 348 | |
| 349 | do_test 3.$tn.6 { code3 { do_wal_checkpoint db3 } } {0 0 0} |
dan | 0875637 | 2010-11-19 07:17:09 +0000 | [diff] [blame] | 350 | } |
| 351 | } |
dan | f2b8dd5 | 2010-11-18 19:28:01 +0000 | [diff] [blame] | 352 | |
| 353 | |
dan | a58f26f | 2010-11-16 18:56:51 +0000 | [diff] [blame] | 354 | finish_test |