vfs-shm.txt 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. The 5 states of an historical rollback lock as implemented by the
  2. xLock, xUnlock, and xCheckReservedLock methods of the sqlite3_io_methods
  3. objec are:
  4. UNLOCKED
  5. SHARED
  6. RESERVED
  7. PENDING
  8. EXCLUSIVE
  9. The wal-index file has a similar locking hierarchy implemented using
  10. the xShmLock method of the sqlite3_vfs object, but with 7
  11. states. Each connection to a wal-index file must be in one of
  12. the following 7 states:
  13. UNLOCKED
  14. READ
  15. READ_FULL
  16. WRITE
  17. PENDING
  18. CHECKPOINT
  19. RECOVER
  20. These roughly correspond to the 5 states of a rollback lock except
  21. that SHARED is split out into 2 states: READ and READ_FULL and
  22. there is an extra RECOVER state used for wal-index reconstruction.
  23. The meanings of the various wal-index locking states is as follows:
  24. UNLOCKED - The wal-index is not in use.
  25. READ - Some prefix of the wal-index is being read. Additional
  26. wal-index information can be appended at any time. The
  27. newly appended content will be ignored by the holder of
  28. the READ lock.
  29. READ_FULL - The entire wal-index is being read. No new information
  30. can be added to the wal-index. The holder of a READ_FULL
  31. lock promises never to read pages from the database file
  32. that are available anywhere in the wal-index.
  33. WRITE - It is OK to append to the wal-index file and to adjust
  34. the header to indicate the new "last valid frame".
  35. PENDING - Waiting on all READ locks to clear so that a
  36. CHECKPOINT lock can be acquired.
  37. CHECKPOINT - It is OK to write any WAL data into the database file
  38. and zero the last valid frame field of the wal-index
  39. header. The wal-index file itself may not be changed
  40. other than to zero the last valid frame field in the
  41. header.
  42. RECOVER - Held during wal-index recovery. Used to prevent a
  43. race if multiple clients try to recover a wal-index at
  44. the same time.
  45. A particular lock manager implementation may coalesce one or more of
  46. the wal-index locking states, though with a reduction in concurrency.
  47. For example, an implemention might implement only exclusive locking,
  48. in which case all states would be equivalent to CHECKPOINT, meaning that
  49. only one reader or one writer or one checkpointer could be active at a
  50. time. Or, an implementation might combine READ and READ_FULL into
  51. a single state equivalent to READ, meaning that a writer could
  52. coexist with a reader, but no reader or writers could coexist with a
  53. checkpointer.
  54. The lock manager must obey the following rules:
  55. (1) A READ cannot coexist with CHECKPOINT.
  56. (2) A READ_FULL cannot coexist with WRITE.
  57. (3) None of WRITE, PENDING, CHECKPOINT, or RECOVER can coexist.
  58. The SQLite core will obey the next set of rules. These rules are
  59. assertions on the behavior of the SQLite core which might be verified
  60. during testing using an instrumented lock manager.
  61. (5) No part of the wal-index will be read without holding either some
  62. kind of SHM lock or an EXCLUSIVE lock on the original database.
  63. The original database is the file named in the 2nd parameter to
  64. the xShmOpen method.
  65. (6) A holder of a READ_FULL will never read any page of the database
  66. file that is contained anywhere in the wal-index.
  67. (7) No part of the wal-index other than the header will be written nor
  68. will the size of the wal-index grow without holding a WRITE or
  69. an EXCLUSIVE on the original database file.
  70. (8) The wal-index header will not be written without holding one of
  71. WRITE, CHECKPOINT, or RECOVER on the wal-index or an EXCLUSIVE on
  72. the original database files.
  73. (9) A CHECKPOINT or RECOVER must be held on the wal-index, or an
  74. EXCLUSIVE on the original database file, in order to reset the
  75. last valid frame counter in the header of the wal-index back to zero.
  76. (10) A WRITE can only increase the last valid frame pointer in the header.
  77. The SQLite core will only ever send requests for UNLOCK, READ, WRITE,
  78. CHECKPOINT, or RECOVER to the lock manager. The SQLite core will never
  79. request a READ_FULL or PENDING lock though the lock manager may deliver
  80. those locking states in response to READ and CHECKPOINT requests,
  81. respectively, if and only if the requested READ or CHECKPOINT cannot
  82. be delivered.
  83. The following are the allowed lock transitions:
  84. Original-State Request New-State
  85. -------------- ---------- ----------
  86. (11a) UNLOCK READ READ
  87. (11b) UNLOCK READ READ_FULL
  88. (11c) UNLOCK CHECKPOINT PENDING
  89. (11d) UNLOCK CHECKPOINT CHECKPOINT
  90. (11e) READ UNLOCK UNLOCK
  91. (11f) READ WRITE WRITE
  92. (11g) READ RECOVER RECOVER
  93. (11h) READ_FULL UNLOCK UNLOCK
  94. (11i) READ_FULL WRITE WRITE
  95. (11j) READ_FULL RECOVER RECOVER
  96. (11k) WRITE READ READ
  97. (11l) PENDING UNLOCK UNLOCK
  98. (11m) PENDING CHECKPOINT CHECKPOINT
  99. (11n) CHECKPOINT UNLOCK UNLOCK
  100. (11o) RECOVER READ READ
  101. These 15 transitions are all that needs to be supported. The lock
  102. manager implementation can assert that fact. The other 27 possible
  103. transitions among the 7 locking states will never occur.