indexfault.test 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. # 2011 August 08
  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. #
  12. set testdir [file dirname $argv0]
  13. source $testdir/tester.tcl
  14. source $testdir/lock_common.tcl
  15. source $testdir/malloc_common.tcl
  16. ifcapable !mergesort {
  17. finish_test
  18. return
  19. }
  20. set testprefix indexfault
  21. # Set up the custom fault-injector. This is further configured by using
  22. # different values for $::custom_filter and different implementations
  23. # of Tcl proc [xCustom] for each test case.
  24. #
  25. proc install_custom_faultsim {} {
  26. set ::FAULTSIM(custom) [list \
  27. -injectinstall custom_injectinstall \
  28. -injectstart custom_injectstart \
  29. -injectstop custom_injectstop \
  30. -injecterrlist {{1 {disk I/O error}}} \
  31. -injectuninstall custom_injectuninstall \
  32. ]
  33. proc custom_injectinstall {} {
  34. testvfs shmfault -default true
  35. shmfault filter $::custom_filter
  36. shmfault script xCustom
  37. }
  38. proc custom_injectuninstall {} {
  39. catch {db close}
  40. catch {db2 close}
  41. shmfault delete
  42. }
  43. set ::custom_ifail -1
  44. set ::custom_nfail -1
  45. proc custom_injectstart {iFail} {
  46. set ::custom_ifail $iFail
  47. set ::custom_nfail 0
  48. }
  49. proc custom_injectstop {} {
  50. set ::custom_ifail -1
  51. return $::custom_nfail
  52. }
  53. }
  54. proc uninstall_custom_faultsim {} {
  55. unset -nocomplain ::FAULTSIM(custom)
  56. }
  57. #-------------------------------------------------------------------------
  58. # These tests - indexfault-1.* - Build an index on a smallish table with
  59. # all different kinds of fault-injection. The CREATE INDEX is run once
  60. # with default options and once with a 50KB soft-heap-limit.
  61. #
  62. do_execsql_test 1.0 {
  63. BEGIN;
  64. CREATE TABLE t1(x);
  65. INSERT INTO t1 VALUES(randomblob(202));
  66. INSERT INTO t1 SELECT randomblob(202) FROM t1; -- 2
  67. INSERT INTO t1 SELECT randomblob(202) FROM t1; -- 4
  68. INSERT INTO t1 SELECT randomblob(202) FROM t1; -- 8
  69. INSERT INTO t1 SELECT randomblob(202) FROM t1; -- 16
  70. INSERT INTO t1 SELECT randomblob(202) FROM t1; -- 32
  71. INSERT INTO t1 SELECT randomblob(202) FROM t1; -- 64
  72. INSERT INTO t1 SELECT randomblob(202) FROM t1; -- 128
  73. INSERT INTO t1 SELECT randomblob(202) FROM t1; -- 256
  74. COMMIT;
  75. }
  76. faultsim_save_and_close
  77. do_faultsim_test 1.1 -prep {
  78. faultsim_restore_and_reopen
  79. } -body {
  80. execsql { CREATE INDEX i1 ON t1(x) }
  81. faultsim_test_result {0 {}}
  82. faultsim_integrity_check
  83. }
  84. ifcapable memorymanage {
  85. set soft_limit [sqlite3_soft_heap_limit 50000]
  86. do_faultsim_test 2.1 -prep {
  87. faultsim_restore_and_reopen
  88. } -body {
  89. execsql { CREATE INDEX i1 ON t1(x) }
  90. faultsim_test_result {0 {}}
  91. }
  92. sqlite3_soft_heap_limit $soft_limit
  93. }
  94. #-------------------------------------------------------------------------
  95. # These are similar to the indexfault-1.* tests, except they create an
  96. # index with more than one column.
  97. #
  98. sqlite3 db test.db
  99. do_execsql_test 2.0 {
  100. BEGIN;
  101. DROP TABLE IF EXISTS t1;
  102. CREATE TABLE t1(t,u,v,w,x,y,z);
  103. INSERT INTO t1 VALUES(
  104. randomblob(30), randomblob(30), randomblob(30), randomblob(30),
  105. randomblob(30), randomblob(30), randomblob(30)
  106. );
  107. INSERT INTO t1 SELECT
  108. randomblob(30), randomblob(30), randomblob(30), randomblob(30),
  109. randomblob(30), randomblob(30), randomblob(30) FROM t1; -- 2
  110. INSERT INTO t1 SELECT
  111. randomblob(30), randomblob(30), randomblob(30), randomblob(30),
  112. randomblob(30), randomblob(30), randomblob(30) FROM t1; -- 4
  113. INSERT INTO t1 SELECT
  114. randomblob(30), randomblob(30), randomblob(30), randomblob(30),
  115. randomblob(30), randomblob(30), randomblob(30) FROM t1; -- 8
  116. INSERT INTO t1 SELECT
  117. randomblob(30), randomblob(30), randomblob(30), randomblob(30),
  118. randomblob(30), randomblob(30), randomblob(30) FROM t1; -- 16
  119. INSERT INTO t1 SELECT
  120. randomblob(30), randomblob(30), randomblob(30), randomblob(30),
  121. randomblob(30), randomblob(30), randomblob(30) FROM t1; -- 32
  122. INSERT INTO t1 SELECT
  123. randomblob(30), randomblob(30), randomblob(30), randomblob(30),
  124. randomblob(30), randomblob(30), randomblob(30) FROM t1; -- 64
  125. INSERT INTO t1 SELECT
  126. randomblob(30), randomblob(30), randomblob(30), randomblob(30),
  127. randomblob(30), randomblob(30), randomblob(30) FROM t1; -- 128
  128. COMMIT;
  129. }
  130. faultsim_save_and_close
  131. do_faultsim_test 2.1 -prep {
  132. faultsim_restore_and_reopen
  133. } -body {
  134. execsql { CREATE INDEX i1 ON t1(t,u,v,w,x,y,z) }
  135. faultsim_test_result {0 {}}
  136. faultsim_integrity_check
  137. }
  138. ifcapable memorymanage {
  139. set soft_limit [sqlite3_soft_heap_limit 50000]
  140. do_faultsim_test 2.2 -prep {
  141. faultsim_restore_and_reopen
  142. } -body {
  143. execsql { CREATE INDEX i1 ON t1(t,u,v,w,x,y,z) }
  144. faultsim_test_result {0 {}}
  145. }
  146. sqlite3_soft_heap_limit $soft_limit
  147. }
  148. #-------------------------------------------------------------------------
  149. # The following tests - indexfault-2.* - all attempt to build a index
  150. # on table t1 in the main database with injected IO errors. Individual
  151. # test cases work as follows:
  152. #
  153. # 3.1: IO errors injected into xOpen() calls.
  154. # 3.2: As 7.1, but with a low (50KB) soft-heap-limit.
  155. #
  156. # 3.3: IO errors injected into the first 200 write() calls made on the
  157. # second temporary file.
  158. # 3.4: As 7.3, but with a low (50KB) soft-heap-limit.
  159. #
  160. # 3.5: After a certain amount of data has been read from the main database
  161. # file (and written into the temporary b-tree), sqlite3_release_memory()
  162. # is called to free as much memory as possible. This causes the temp
  163. # b-tree to be flushed to disk. So that before its contents can be
  164. # transfered to a PMA they must be read back from disk - creating extra
  165. # opportunities for IO errors.
  166. #
  167. install_custom_faultsim
  168. # Set up a table to build indexes on. Save the setup using the
  169. # [faultsim_save_and_close] mechanism.
  170. #
  171. sqlite3 db test.db
  172. do_execsql_test 3.0 {
  173. BEGIN;
  174. DROP TABLE IF EXISTS t1;
  175. CREATE TABLE t1(x);
  176. INSERT INTO t1 VALUES(randomblob(11000));
  177. INSERT INTO t1 SELECT randomblob(11001) FROM t1; -- 2
  178. INSERT INTO t1 SELECT randomblob(11002) FROM t1; -- 4
  179. INSERT INTO t1 SELECT randomblob(11003) FROM t1; -- 8
  180. INSERT INTO t1 SELECT randomblob(11004) FROM t1; -- 16
  181. INSERT INTO t1 SELECT randomblob(11005) FROM t1; -- 32
  182. INSERT INTO t1 SELECT randomblob(11006) FROM t1; -- 64
  183. INSERT INTO t1 SELECT randomblob(11007) FROM t1; -- 128
  184. INSERT INTO t1 SELECT randomblob(11008) FROM t1; -- 256
  185. INSERT INTO t1 SELECT randomblob(11009) FROM t1; -- 512
  186. COMMIT;
  187. }
  188. faultsim_save_and_close
  189. set ::custom_filter xOpen
  190. proc xCustom {args} {
  191. incr ::custom_ifail -1
  192. if {$::custom_ifail==0} {
  193. incr ::custom_nfail
  194. return "SQLITE_IOERR"
  195. }
  196. return "SQLITE_OK"
  197. }
  198. do_faultsim_test 3.1 -faults custom -prep {
  199. faultsim_restore_and_reopen
  200. } -body {
  201. execsql { CREATE INDEX i1 ON t1(x) }
  202. faultsim_test_result {0 {}}
  203. }
  204. ifcapable memorymanage {
  205. set soft_limit [sqlite3_soft_heap_limit 50000]
  206. do_faultsim_test 3.2 -faults custom -prep {
  207. faultsim_restore_and_reopen
  208. } -body {
  209. execsql { CREATE INDEX i1 ON t1(x) }
  210. faultsim_test_result {0 {}}
  211. }
  212. sqlite3_soft_heap_limit $soft_limit
  213. }
  214. set ::custom_filter {xOpen xWrite}
  215. proc xCustom {method args} {
  216. if {$method == "xOpen"} {
  217. if {[lindex $args 0] == ""} {
  218. incr ::nTmpOpen 1
  219. if {$::nTmpOpen == 3} { return "failme" }
  220. }
  221. return "SQLITE_OK"
  222. }
  223. if {$::custom_ifail<200 && [lindex $args 1] == "failme"} {
  224. incr ::custom_ifail -1
  225. if {$::custom_ifail==0} {
  226. incr ::custom_nfail
  227. return "SQLITE_IOERR"
  228. }
  229. }
  230. return "SQLITE_OK"
  231. }
  232. do_faultsim_test 3.3 -faults custom -prep {
  233. faultsim_restore_and_reopen
  234. set ::nTmpOpen 0
  235. } -body {
  236. execsql { CREATE INDEX i1 ON t1(x) }
  237. faultsim_test_result {0 {}}
  238. }
  239. ifcapable memorymanage {
  240. set soft_limit [sqlite3_soft_heap_limit 50000]
  241. do_faultsim_test 3.4 -faults custom -prep {
  242. faultsim_restore_and_reopen
  243. set ::nTmpOpen 0
  244. } -body {
  245. execsql { CREATE INDEX i1 ON t1(x) }
  246. faultsim_test_result {0 {}}
  247. }
  248. sqlite3_soft_heap_limit $soft_limit
  249. }
  250. uninstall_custom_faultsim
  251. #-------------------------------------------------------------------------
  252. # Test 4: After a certain amount of data has been read from the main database
  253. # file (and written into the temporary b-tree), sqlite3_release_memory() is
  254. # called to free as much memory as possible. This causes the temp b-tree to be
  255. # flushed to disk. So that before its contents can be transfered to a PMA they
  256. # must be read back from disk - creating extra opportunities for IO errors.
  257. #
  258. install_custom_faultsim
  259. catch { db close }
  260. forcedelete test.db
  261. sqlite3 db test.db
  262. do_execsql_test 4.0 {
  263. BEGIN;
  264. DROP TABLE IF EXISTS t1;
  265. CREATE TABLE t1(x);
  266. INSERT INTO t1 VALUES(randomblob(11000));
  267. INSERT INTO t1 SELECT randomblob(11001) FROM t1; -- 2
  268. INSERT INTO t1 SELECT randomblob(11002) FROM t1; -- 4
  269. INSERT INTO t1 SELECT randomblob(11003) FROM t1; -- 8
  270. INSERT INTO t1 SELECT randomblob(11004) FROM t1; -- 16
  271. INSERT INTO t1 SELECT randomblob(11005) FROM t1; -- 32
  272. INSERT INTO t1 SELECT randomblob(11005) FROM t1; -- 64
  273. COMMIT;
  274. }
  275. faultsim_save_and_close
  276. testvfs tvfs
  277. tvfs script xRead
  278. tvfs filter xRead
  279. set ::nRead 0
  280. proc xRead {method file args} {
  281. if {[file tail $file] == "test.db"} { incr ::nRead }
  282. }
  283. do_test 4.1 {
  284. sqlite3 db test.db -vfs tvfs
  285. execsql { CREATE INDEX i1 ON t1(x) }
  286. } {}
  287. db close
  288. tvfs delete
  289. set ::custom_filter xRead
  290. proc xCustom {method file args} {
  291. incr ::nReadCall
  292. if {$::nReadCall >= ($::nRead/5)} {
  293. if {$::nReadCall == ($::nRead/5)} {
  294. set nByte [sqlite3_release_memory [expr 64*1024*1024]]
  295. sqlite3_soft_heap_limit 20000
  296. }
  297. if {$file == ""} {
  298. incr ::custom_ifail -1
  299. if {$::custom_ifail==0} {
  300. incr ::custom_nfail
  301. return "SQLITE_IOERR"
  302. }
  303. }
  304. }
  305. return "SQLITE_OK"
  306. }
  307. do_faultsim_test 4.2 -faults custom -prep {
  308. faultsim_restore_and_reopen
  309. set ::nReadCall 0
  310. sqlite3_soft_heap_limit 0
  311. } -body {
  312. execsql { CREATE INDEX i1 ON t1(x) }
  313. faultsim_test_result {0 {}}
  314. }
  315. uninstall_custom_faultsim
  316. finish_test