Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | The crash tests now compile, at least. But they get wrong results. The problem is fundamental and suggests I need to completely rethink how the new OS backend should work. (CVS 2792) |
---|---|
Downloads: | Tarball | ZIP archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA1: |
966bc68e1bf4e0cc88407871c162ee30 |
User & Date: | drh 2005-11-29 18:37:16.000 |
Context
2005-11-29
| ||
19:50 | Make the build work on windows again. Ticket #1544. (CVS 2793) (check-in: 59bdca2552 user: drh tags: trunk) | |
18:37 | The crash tests now compile, at least. But they get wrong results. The problem is fundamental and suggests I need to completely rethink how the new OS backend should work. (CVS 2792) (check-in: 966bc68e1b user: drh tags: trunk) | |
03:13 | Make the OsFile structure opaque with its internal structure known only to the appropriate os_*.c implementation. Omit the os_unix.h and os_win.h include files. The crash tests are broken by this patch. (CVS 2791) (check-in: 058f317538 user: drh tags: trunk) | |
Changes
Changes to src/test6.c.
︙ | ︙ | |||
14 15 16 17 18 19 20 | ** the effect on the database file of an OS crash or power failure. This ** is used to test the ability of SQLite to recover from those situations. */ #if SQLITE_TEST /* This file is used for the testing only */ #include "sqliteInt.h" #include "os.h" #include "tcl.h" | < > | 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 | ** the effect on the database file of an OS crash or power failure. This ** is used to test the ability of SQLite to recover from those situations. */ #if SQLITE_TEST /* This file is used for the testing only */ #include "sqliteInt.h" #include "os.h" #include "tcl.h" /* ** A copy of the original sqlite3Io structure */ static struct sqlite3IoVtbl origIo; /* ** The OsFile structure for the crash-test backend. The pBase field ** points to an OsFile structure for the native backend. */ struct OsFile { u8 **apBlk; /* Array of blocks that have been written to. */ int nBlk; /* Size of apBlock. */ i64 offset; /* Next character to be read from the file */ int nMaxWrite; /* Largest offset written to. */ char *zName; /* File name */ OsFile *pBase; /* Base class */ OsFile *pNext; /* Next in a list of them all */ }; /* |
︙ | ︙ | |||
94 95 96 97 98 99 100 | sqlite3OsLeaveMutex(); return r; } /* ** A list of all open files. */ | | | > | | | | | > < < < < < < | < < | | 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 | sqlite3OsLeaveMutex(); return r; } /* ** A list of all open files. */ static OsFile *pAllFiles = 0; /* ** Initialise the os_test.c specific fields of pFile. */ static void initFile(OsFile **pId, char const *zName, OsFile *pBase){ OsFile *pFile = *pId = sqliteMalloc(sizeof(OsFile) + strlen(zName)+1); pFile->nMaxWrite = 0; pFile->offset = 0; pFile->nBlk = 0; pFile->apBlk = 0; pFile->zName = (char *)(&pFile[1]); strcpy(pFile->zName, zName); pFile->pBase = pBase; pFile->pNext = pAllFiles; pAllFiles = pFile; } /* ** Undo the work done by initFile. Delete the OsFile structure ** and unlink the structure from the pAllFiles list. */ static void closeFile(OsFile **pId){ OsFile *pFile = *pId; if( pFile==pAllFiles ){ pAllFiles = pFile->pNext; }else{ OsFile *p; for(p=pAllFiles; p->pNext!=pFile; p=p->pNext ){ assert( p ); } p->pNext = pFile->pNext; } sqliteFree(*pId); *pId = 0; } /* ** Read block 'blk' off of the real disk file and into the cache of pFile. */ static int readBlockIntoCache(OsFile *pFile, int blk){ if( blk>=pFile->nBlk ){ int n = ((pFile->nBlk * 2) + 100 + blk); /* if( pFile->nBlk==0 ){ printf("DIRTY %s\n", pFile->zName); } */ pFile->apBlk = (u8 **)sqliteRealloc(pFile->apBlk, n * sizeof(u8*)); if( !pFile->apBlk ) return SQLITE_NOMEM; memset(&pFile->apBlk[pFile->nBlk], 0, (n - pFile->nBlk)*sizeof(u8*)); pFile->nBlk = n; |
︙ | ︙ | |||
183 184 185 186 187 188 189 | /* ** Write the cache of pFile to disk. If crash is non-zero, randomly ** skip blocks when writing. The cache is deleted before returning. */ static int writeCache2(OsFile *pFile, int crash){ int i; int nMax = pFile->nMaxWrite; | < < | 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | /* ** Write the cache of pFile to disk. If crash is non-zero, randomly ** skip blocks when writing. The cache is deleted before returning. */ static int writeCache2(OsFile *pFile, int crash){ int i; int nMax = pFile->nMaxWrite; int rc = SQLITE_OK; for(i=0; i<pFile->nBlk; i++){ u8 *p = pFile->apBlk[i]; if( p ){ int skip = 0; int trash = 0; if( crash ){ char random; |
︙ | ︙ | |||
235 236 237 238 239 240 241 | sqliteFree(p); } } sqliteFree(pFile->apBlk); pFile->nBlk = 0; pFile->apBlk = 0; pFile->nMaxWrite = 0; | < < < < | | | | | | | | > > > > > | | | 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 | sqliteFree(p); } } sqliteFree(pFile->apBlk); pFile->nBlk = 0; pFile->apBlk = 0; pFile->nMaxWrite = 0; return rc; } /* ** Write the cache to disk. */ static int writeCache(OsFile *pFile){ if( pFile->apBlk ){ int c = crashRequired(pFile->zName); if( c ){ OsFile *p; #ifdef TRACE_WRITECACHE printf("\nCrash during sync of %s\n", pFile->zName); #endif for(p=pAllFiles; p; p=p->pNext){ writeCache2(p, 1); } exit(-1); }else{ return writeCache2(pFile, 0); } } return SQLITE_OK; } /* ** Close the file. */ static int crashClose(OsFile **pId){ OsFile *pFile = *pId; if( pFile ){ /* printf("CLOSE %s (%d blocks)\n", pFile->zName, pFile->nBlk); */ writeCache(pFile); origIo.xClose(&pFile->pBase); } closeFile(pId); return SQLITE_OK; } static int crashSeek(OsFile *id, i64 offset){ id->offset = offset; return SQLITE_OK; } static int crashRead(OsFile *id, void *pBuf, int amt){ i64 offset; /* The current offset from the start of the file */ i64 end; /* The byte just past the last byte read */ int blk; /* Block number the read starts on */ int i; u8 *zCsr; int rc = SQLITE_OK; OsFile *pFile = id; offset = pFile->offset; end = offset+amt; blk = (offset/BLOCKSIZE); zCsr = (u8 *)pBuf; for(i=blk; i*BLOCKSIZE<end; i++){ int off = 0; int len = 0; |
︙ | ︙ | |||
309 310 311 312 313 314 315 | len = len - (BLOCK_OFFSET(i+1)-end); } if( i<pFile->nBlk && pFile->apBlk[i]){ u8 *pBlk = pFile->apBlk[i]; memcpy(zCsr, &pBlk[off], len); }else{ | | | | < | | | | | | < | | < < | | | < | | | | | > > | > > | | | > > | > > | | | > > | > | > > > > > > > > > | > > > > > > > > > > > > > > > | > > | > > > > | | 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 | len = len - (BLOCK_OFFSET(i+1)-end); } if( i<pFile->nBlk && pFile->apBlk[i]){ u8 *pBlk = pFile->apBlk[i]; memcpy(zCsr, &pBlk[off], len); }else{ rc = origIo.xSeek(id->pBase, BLOCK_OFFSET(i) + off); if( rc!=SQLITE_OK ) return rc; rc = origIo.xRead(id->pBase, zCsr, len); if( rc!=SQLITE_OK ) return rc; } zCsr += len; } assert( zCsr==&((u8 *)pBuf)[amt] ); id->offset = end; return rc; } static int crashWrite(OsFile *id, const void *pBuf, int amt){ i64 offset; /* The current offset from the start of the file */ i64 end; /* The byte just past the last byte written */ int blk; /* Block number the write starts on */ int i; const u8 *zCsr; int rc = SQLITE_OK; offset = id->offset; end = offset+amt; blk = (offset/BLOCKSIZE); zCsr = (u8 *)pBuf; for(i=blk; i*BLOCKSIZE<end; i++){ u8 *pBlk; int off = 0; int len = 0; /* Make sure the block is in the cache */ rc = readBlockIntoCache(id, i); if( rc!=SQLITE_OK ) return rc; /* Write into the cache */ pBlk = id->apBlk[i]; assert( pBlk ); if( BLOCK_OFFSET(i) < offset ){ off = offset-BLOCK_OFFSET(i); } len = BLOCKSIZE - off; if( BLOCK_OFFSET(i+1) > end ){ len = len - (BLOCK_OFFSET(i+1)-end); } memcpy(&pBlk[off], zCsr, len); zCsr += len; } if( id->nMaxWrite<end ){ id->nMaxWrite = end; } assert( zCsr==&((u8 *)pBuf)[amt] ); id->offset = end; return rc; } /* ** Sync the file. First flush the write-cache to disk, then call the ** real sync() function. */ static int crashSync(OsFile *id, int dataOnly){ int rc; /* printf("SYNC %s (%d blocks)\n", (*id)->zName, (*id)->nBlk); */ rc = writeCache(id); /* if( rc!=SQLITE_OK ) return rc; rc = origIo.xSync(id->pBase, dataOnly); */ return rc; } /* ** Truncate the file. Set the internal OsFile.nMaxWrite variable to the new ** file size to ensure that nothing in the write-cache past this point ** is written to disk. */ static int crashTruncate(OsFile *id, i64 nByte){ id->nMaxWrite = nByte; return origIo.xTruncate(id->pBase, nByte); } /* ** Return the size of the file. If the cache contains a write that extended ** the file, then return this size instead of the on-disk size. */ static int crashFileSize(OsFile *id, i64 *pSize){ int rc = origIo.xFileSize(id->pBase, pSize); if( rc==SQLITE_OK && pSize && *pSize<id->nMaxWrite ){ *pSize = id->nMaxWrite; } return rc; } /* ** The three functions used to open files. All that is required is to ** initialise the os_test.c specific fields and then call the corresponding ** os_unix.c function to really open the file. */ static int crashOpenReadWrite(const char *zFilename, OsFile **pId,int *pRdonly){ OsFile *pBase = 0; int rc = origIo.xOpenReadWrite(zFilename, &pBase, pRdonly); if( !rc ){ initFile(pId, zFilename, pBase); } return rc; } static int crashOpenExclusive(const char *zFilename, OsFile **pId, int delFlag){ OsFile *pBase = 0; int rc = origIo.xOpenExclusive(zFilename, &pBase, delFlag); if( !rc ){ initFile(pId, zFilename, pBase); } return rc; } static int crashOpenReadOnly(const char *zFilename, OsFile **pId){ OsFile *pBase = 0; int rc = origIo.xOpenReadOnly(zFilename, &pBase); if( !rc ){ initFile(pId, zFilename, pBase); } return rc; } /* ** OpenDirectory and SyncDirectory are no-ops */ static int crashOpenDir(const char *zName, OsFile *id){ return SQLITE_OK; } static int crashSyncDir(const char *zName){ return SQLITE_OK; } /* ** Locking primitives are passed through into the underlying ** file descriptor. */ int crashLock(OsFile *id, int lockType){ return origIo.xLock(id->pBase, lockType); } int crashUnlock(OsFile *id, int lockType){ return origIo.xUnlock(id->pBase, lockType); } int crashCheckReservedLock(OsFile *id){ return origIo.xCheckReservedLock(id->pBase); } void crashSetFullSync(OsFile *id, int setting){ return; /* This is a no-op */ } int crashLockState(OsFile *id){ return origIo.xLockState(id->pBase); } /* ** Return the underlying file handle. */ int crashFileHandle(OsFile *id){ return origIo.xFileHandle(id->pBase); } /* ** tclcmd: sqlite_crashparams DELAY CRASHFILE ** ** This procedure implements a TCL command that enables crash testing ** in testfixture. Once enabled, crash testing cannot be disabled. |
︙ | ︙ | |||
459 460 461 462 463 464 465 466 467 | zFile = Tcl_GetStringFromObj(objv[2], &nFile); if( nFile>=sizeof(zCrashFile)-1 ){ Tcl_AppendResult(interp, "crash file name too big", 0); return TCL_ERROR; } setCrashParams(delay, zFile); origIo = sqlite3Io; sqlite3Io.xRead = crashRead; sqlite3Io.xWrite = crashWrite; | > > > > > > > > > > | > | | | | > > < < < | 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 518 519 520 521 522 523 524 525 526 527 528 529 | zFile = Tcl_GetStringFromObj(objv[2], &nFile); if( nFile>=sizeof(zCrashFile)-1 ){ Tcl_AppendResult(interp, "crash file name too big", 0); return TCL_ERROR; } setCrashParams(delay, zFile); origIo = sqlite3Io; /* xDelete unchanged */ /* xFileExists unchanged */ sqlite3Io.xOpenReadWrite = crashOpenReadWrite; sqlite3Io.xOpenExclusive = crashOpenExclusive; sqlite3Io.xOpenReadOnly = crashOpenReadOnly; sqlite3Io.xOpenDirectory = crashOpenDir; sqlite3Io.xSyncDirectory = crashSyncDir; /* xTempFileName unchanged */ /* xIsDirWritable unchanged */ sqlite3Io.xClose = crashClose; sqlite3Io.xRead = crashRead; sqlite3Io.xWrite = crashWrite; sqlite3Io.xSeek = crashSeek; sqlite3Io.xSync = crashSync; sqlite3Io.xTruncate = crashTruncate; sqlite3Io.xFileSize = crashFileSize; /* xFullPathname unchanged */ sqlite3Io.xLock = crashLock; sqlite3Io.xUnlock = crashUnlock; sqlite3Io.xCheckReservedLock = crashCheckReservedLock; sqlite3Io.xSetFullSync = crashSetFullSync; sqlite3Io.xFileHandle = crashFileHandle; sqlite3Io.xLockState = crashLockState; return TCL_OK; } /* ** This procedure registers the TCL procedures defined in this file. */ int Sqlitetest6_Init(Tcl_Interp *interp){ Tcl_CreateObjCommand(interp, "sqlite3_crashparams", crashParamsObjCmd, 0, 0); return TCL_OK; } #endif /* SQLITE_TEST */ |