xfs-delayed-logging-design.txt 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. XFS Delayed Logging Design
  2. --------------------------
  3. Introduction to Re-logging in XFS
  4. ---------------------------------
  5. XFS logging is a combination of logical and physical logging. Some objects,
  6. such as inodes and dquots, are logged in logical format where the details
  7. logged are made up of the changes to in-core structures rather than on-disk
  8. structures. Other objects - typically buffers - have their physical changes
  9. logged. The reason for these differences is to reduce the amount of log space
  10. required for objects that are frequently logged. Some parts of inodes are more
  11. frequently logged than others, and inodes are typically more frequently logged
  12. than any other object (except maybe the superblock buffer) so keeping the
  13. amount of metadata logged low is of prime importance.
  14. The reason that this is such a concern is that XFS allows multiple separate
  15. modifications to a single object to be carried in the log at any given time.
  16. This allows the log to avoid needing to flush each change to disk before
  17. recording a new change to the object. XFS does this via a method called
  18. "re-logging". Conceptually, this is quite simple - all it requires is that any
  19. new change to the object is recorded with a *new copy* of all the existing
  20. changes in the new transaction that is written to the log.
  21. That is, if we have a sequence of changes A through to F, and the object was
  22. written to disk after change D, we would see in the log the following series
  23. of transactions, their contents and the log sequence number (LSN) of the
  24. transaction:
  25. Transaction Contents LSN
  26. A A X
  27. B A+B X+n
  28. C A+B+C X+n+m
  29. D A+B+C+D X+n+m+o
  30. <object written to disk>
  31. E E Y (> X+n+m+o)
  32. F E+F Yٍ+p
  33. In other words, each time an object is relogged, the new transaction contains
  34. the aggregation of all the previous changes currently held only in the log.
  35. This relogging technique also allows objects to be moved forward in the log so
  36. that an object being relogged does not prevent the tail of the log from ever
  37. moving forward. This can be seen in the table above by the changing
  38. (increasing) LSN of each subsequent transaction - the LSN is effectively a
  39. direct encoding of the location in the log of the transaction.
  40. This relogging is also used to implement long-running, multiple-commit
  41. transactions. These transaction are known as rolling transactions, and require
  42. a special log reservation known as a permanent transaction reservation. A
  43. typical example of a rolling transaction is the removal of extents from an
  44. inode which can only be done at a rate of two extents per transaction because
  45. of reservation size limitations. Hence a rolling extent removal transaction
  46. keeps relogging the inode and btree buffers as they get modified in each
  47. removal operation. This keeps them moving forward in the log as the operation
  48. progresses, ensuring that current operation never gets blocked by itself if the
  49. log wraps around.
  50. Hence it can be seen that the relogging operation is fundamental to the correct
  51. working of the XFS journalling subsystem. From the above description, most
  52. people should be able to see why the XFS metadata operations writes so much to
  53. the log - repeated operations to the same objects write the same changes to
  54. the log over and over again. Worse is the fact that objects tend to get
  55. dirtier as they get relogged, so each subsequent transaction is writing more
  56. metadata into the log.
  57. Another feature of the XFS transaction subsystem is that most transactions are
  58. asynchronous. That is, they don't commit to disk until either a log buffer is
  59. filled (a log buffer can hold multiple transactions) or a synchronous operation
  60. forces the log buffers holding the transactions to disk. This means that XFS is
  61. doing aggregation of transactions in memory - batching them, if you like - to
  62. minimise the impact of the log IO on transaction throughput.
  63. The limitation on asynchronous transaction throughput is the number and size of
  64. log buffers made available by the log manager. By default there are 8 log
  65. buffers available and the size of each is 32kB - the size can be increased up
  66. to 256kB by use of a mount option.
  67. Effectively, this gives us the maximum bound of outstanding metadata changes
  68. that can be made to the filesystem at any point in time - if all the log
  69. buffers are full and under IO, then no more transactions can be committed until
  70. the current batch completes. It is now common for a single current CPU core to
  71. be to able to issue enough transactions to keep the log buffers full and under
  72. IO permanently. Hence the XFS journalling subsystem can be considered to be IO
  73. bound.
  74. Delayed Logging: Concepts
  75. -------------------------
  76. The key thing to note about the asynchronous logging combined with the
  77. relogging technique XFS uses is that we can be relogging changed objects
  78. multiple times before they are committed to disk in the log buffers. If we
  79. return to the previous relogging example, it is entirely possible that
  80. transactions A through D are committed to disk in the same log buffer.
  81. That is, a single log buffer may contain multiple copies of the same object,
  82. but only one of those copies needs to be there - the last one "D", as it
  83. contains all the changes from the previous changes. In other words, we have one
  84. necessary copy in the log buffer, and three stale copies that are simply
  85. wasting space. When we are doing repeated operations on the same set of
  86. objects, these "stale objects" can be over 90% of the space used in the log
  87. buffers. It is clear that reducing the number of stale objects written to the
  88. log would greatly reduce the amount of metadata we write to the log, and this
  89. is the fundamental goal of delayed logging.
  90. From a conceptual point of view, XFS is already doing relogging in memory (where
  91. memory == log buffer), only it is doing it extremely inefficiently. It is using
  92. logical to physical formatting to do the relogging because there is no
  93. infrastructure to keep track of logical changes in memory prior to physically
  94. formatting the changes in a transaction to the log buffer. Hence we cannot avoid
  95. accumulating stale objects in the log buffers.
  96. Delayed logging is the name we've given to keeping and tracking transactional
  97. changes to objects in memory outside the log buffer infrastructure. Because of
  98. the relogging concept fundamental to the XFS journalling subsystem, this is
  99. actually relatively easy to do - all the changes to logged items are already
  100. tracked in the current infrastructure. The big problem is how to accumulate
  101. them and get them to the log in a consistent, recoverable manner.
  102. Describing the problems and how they have been solved is the focus of this
  103. document.
  104. One of the key changes that delayed logging makes to the operation of the
  105. journalling subsystem is that it disassociates the amount of outstanding
  106. metadata changes from the size and number of log buffers available. In other
  107. words, instead of there only being a maximum of 2MB of transaction changes not
  108. written to the log at any point in time, there may be a much greater amount
  109. being accumulated in memory. Hence the potential for loss of metadata on a
  110. crash is much greater than for the existing logging mechanism.
  111. It should be noted that this does not change the guarantee that log recovery
  112. will result in a consistent filesystem. What it does mean is that as far as the
  113. recovered filesystem is concerned, there may be many thousands of transactions
  114. that simply did not occur as a result of the crash. This makes it even more
  115. important that applications that care about their data use fsync() where they
  116. need to ensure application level data integrity is maintained.
  117. It should be noted that delayed logging is not an innovative new concept that
  118. warrants rigorous proofs to determine whether it is correct or not. The method
  119. of accumulating changes in memory for some period before writing them to the
  120. log is used effectively in many filesystems including ext3 and ext4. Hence
  121. no time is spent in this document trying to convince the reader that the
  122. concept is sound. Instead it is simply considered a "solved problem" and as
  123. such implementing it in XFS is purely an exercise in software engineering.
  124. The fundamental requirements for delayed logging in XFS are simple:
  125. 1. Reduce the amount of metadata written to the log by at least
  126. an order of magnitude.
  127. 2. Supply sufficient statistics to validate Requirement #1.
  128. 3. Supply sufficient new tracing infrastructure to be able to debug
  129. problems with the new code.
  130. 4. No on-disk format change (metadata or log format).
  131. 5. Enable and disable with a mount option.
  132. 6. No performance regressions for synchronous transaction workloads.
  133. Delayed Logging: Design
  134. -----------------------
  135. Storing Changes
  136. The problem with accumulating changes at a logical level (i.e. just using the
  137. existing log item dirty region tracking) is that when it comes to writing the
  138. changes to the log buffers, we need to ensure that the object we are formatting
  139. is not changing while we do this. This requires locking the object to prevent
  140. concurrent modification. Hence flushing the logical changes to the log would
  141. require us to lock every object, format them, and then unlock them again.
  142. This introduces lots of scope for deadlocks with transactions that are already
  143. running. For example, a transaction has object A locked and modified, but needs
  144. the delayed logging tracking lock to commit the transaction. However, the
  145. flushing thread has the delayed logging tracking lock already held, and is
  146. trying to get the lock on object A to flush it to the log buffer. This appears
  147. to be an unsolvable deadlock condition, and it was solving this problem that
  148. was the barrier to implementing delayed logging for so long.
  149. The solution is relatively simple - it just took a long time to recognise it.
  150. Put simply, the current logging code formats the changes to each item into an
  151. vector array that points to the changed regions in the item. The log write code
  152. simply copies the memory these vectors point to into the log buffer during
  153. transaction commit while the item is locked in the transaction. Instead of
  154. using the log buffer as the destination of the formatting code, we can use an
  155. allocated memory buffer big enough to fit the formatted vector.
  156. If we then copy the vector into the memory buffer and rewrite the vector to
  157. point to the memory buffer rather than the object itself, we now have a copy of
  158. the changes in a format that is compatible with the log buffer writing code.
  159. that does not require us to lock the item to access. This formatting and
  160. rewriting can all be done while the object is locked during transaction commit,
  161. resulting in a vector that is transactionally consistent and can be accessed
  162. without needing to lock the owning item.
  163. Hence we avoid the need to lock items when we need to flush outstanding
  164. asynchronous transactions to the log. The differences between the existing
  165. formatting method and the delayed logging formatting can be seen in the
  166. diagram below.
  167. Current format log vector:
  168. Object +---------------------------------------------+
  169. Vector 1 +----+
  170. Vector 2 +----+
  171. Vector 3 +----------+
  172. After formatting:
  173. Log Buffer +-V1-+-V2-+----V3----+
  174. Delayed logging vector:
  175. Object +---------------------------------------------+
  176. Vector 1 +----+
  177. Vector 2 +----+
  178. Vector 3 +----------+
  179. After formatting:
  180. Memory Buffer +-V1-+-V2-+----V3----+
  181. Vector 1 +----+
  182. Vector 2 +----+
  183. Vector 3 +----------+
  184. The memory buffer and associated vector need to be passed as a single object,
  185. but still need to be associated with the parent object so if the object is
  186. relogged we can replace the current memory buffer with a new memory buffer that
  187. contains the latest changes.
  188. The reason for keeping the vector around after we've formatted the memory
  189. buffer is to support splitting vectors across log buffer boundaries correctly.
  190. If we don't keep the vector around, we do not know where the region boundaries
  191. are in the item, so we'd need a new encapsulation method for regions in the log
  192. buffer writing (i.e. double encapsulation). This would be an on-disk format
  193. change and as such is not desirable. It also means we'd have to write the log
  194. region headers in the formatting stage, which is problematic as there is per
  195. region state that needs to be placed into the headers during the log write.
  196. Hence we need to keep the vector, but by attaching the memory buffer to it and
  197. rewriting the vector addresses to point at the memory buffer we end up with a
  198. self-describing object that can be passed to the log buffer write code to be
  199. handled in exactly the same manner as the existing log vectors are handled.
  200. Hence we avoid needing a new on-disk format to handle items that have been
  201. relogged in memory.
  202. Tracking Changes
  203. Now that we can record transactional changes in memory in a form that allows
  204. them to be used without limitations, we need to be able to track and accumulate
  205. them so that they can be written to the log at some later point in time. The
  206. log item is the natural place to store this vector and buffer, and also makes sense
  207. to be the object that is used to track committed objects as it will always
  208. exist once the object has been included in a transaction.
  209. The log item is already used to track the log items that have been written to
  210. the log but not yet written to disk. Such log items are considered "active"
  211. and as such are stored in the Active Item List (AIL) which is a LSN-ordered
  212. double linked list. Items are inserted into this list during log buffer IO
  213. completion, after which they are unpinned and can be written to disk. An object
  214. that is in the AIL can be relogged, which causes the object to be pinned again
  215. and then moved forward in the AIL when the log buffer IO completes for that
  216. transaction.
  217. Essentially, this shows that an item that is in the AIL can still be modified
  218. and relogged, so any tracking must be separate to the AIL infrastructure. As
  219. such, we cannot reuse the AIL list pointers for tracking committed items, nor
  220. can we store state in any field that is protected by the AIL lock. Hence the
  221. committed item tracking needs it's own locks, lists and state fields in the log
  222. item.
  223. Similar to the AIL, tracking of committed items is done through a new list
  224. called the Committed Item List (CIL). The list tracks log items that have been
  225. committed and have formatted memory buffers attached to them. It tracks objects
  226. in transaction commit order, so when an object is relogged it is removed from
  227. it's place in the list and re-inserted at the tail. This is entirely arbitrary
  228. and done to make it easy for debugging - the last items in the list are the
  229. ones that are most recently modified. Ordering of the CIL is not necessary for
  230. transactional integrity (as discussed in the next section) so the ordering is
  231. done for convenience/sanity of the developers.
  232. Delayed Logging: Checkpoints
  233. When we have a log synchronisation event, commonly known as a "log force",
  234. all the items in the CIL must be written into the log via the log buffers.
  235. We need to write these items in the order that they exist in the CIL, and they
  236. need to be written as an atomic transaction. The need for all the objects to be
  237. written as an atomic transaction comes from the requirements of relogging and
  238. log replay - all the changes in all the objects in a given transaction must
  239. either be completely replayed during log recovery, or not replayed at all. If
  240. a transaction is not replayed because it is not complete in the log, then
  241. no later transactions should be replayed, either.
  242. To fulfill this requirement, we need to write the entire CIL in a single log
  243. transaction. Fortunately, the XFS log code has no fixed limit on the size of a
  244. transaction, nor does the log replay code. The only fundamental limit is that
  245. the transaction cannot be larger than just under half the size of the log. The
  246. reason for this limit is that to find the head and tail of the log, there must
  247. be at least one complete transaction in the log at any given time. If a
  248. transaction is larger than half the log, then there is the possibility that a
  249. crash during the write of a such a transaction could partially overwrite the
  250. only complete previous transaction in the log. This will result in a recovery
  251. failure and an inconsistent filesystem and hence we must enforce the maximum
  252. size of a checkpoint to be slightly less than a half the log.
  253. Apart from this size requirement, a checkpoint transaction looks no different
  254. to any other transaction - it contains a transaction header, a series of
  255. formatted log items and a commit record at the tail. From a recovery
  256. perspective, the checkpoint transaction is also no different - just a lot
  257. bigger with a lot more items in it. The worst case effect of this is that we
  258. might need to tune the recovery transaction object hash size.
  259. Because the checkpoint is just another transaction and all the changes to log
  260. items are stored as log vectors, we can use the existing log buffer writing
  261. code to write the changes into the log. To do this efficiently, we need to
  262. minimise the time we hold the CIL locked while writing the checkpoint
  263. transaction. The current log write code enables us to do this easily with the
  264. way it separates the writing of the transaction contents (the log vectors) from
  265. the transaction commit record, but tracking this requires us to have a
  266. per-checkpoint context that travels through the log write process through to
  267. checkpoint completion.
  268. Hence a checkpoint has a context that tracks the state of the current
  269. checkpoint from initiation to checkpoint completion. A new context is initiated
  270. at the same time a checkpoint transaction is started. That is, when we remove
  271. all the current items from the CIL during a checkpoint operation, we move all
  272. those changes into the current checkpoint context. We then initialise a new
  273. context and attach that to the CIL for aggregation of new transactions.
  274. This allows us to unlock the CIL immediately after transfer of all the
  275. committed items and effectively allow new transactions to be issued while we
  276. are formatting the checkpoint into the log. It also allows concurrent
  277. checkpoints to be written into the log buffers in the case of log force heavy
  278. workloads, just like the existing transaction commit code does. This, however,
  279. requires that we strictly order the commit records in the log so that
  280. checkpoint sequence order is maintained during log replay.
  281. To ensure that we can be writing an item into a checkpoint transaction at
  282. the same time another transaction modifies the item and inserts the log item
  283. into the new CIL, then checkpoint transaction commit code cannot use log items
  284. to store the list of log vectors that need to be written into the transaction.
  285. Hence log vectors need to be able to be chained together to allow them to be
  286. detached from the log items. That is, when the CIL is flushed the memory
  287. buffer and log vector attached to each log item needs to be attached to the
  288. checkpoint context so that the log item can be released. In diagrammatic form,
  289. the CIL would look like this before the flush:
  290. CIL Head
  291. |
  292. V
  293. Log Item <-> log vector 1 -> memory buffer
  294. | -> vector array
  295. V
  296. Log Item <-> log vector 2 -> memory buffer
  297. | -> vector array
  298. V
  299. ......
  300. |
  301. V
  302. Log Item <-> log vector N-1 -> memory buffer
  303. | -> vector array
  304. V
  305. Log Item <-> log vector N -> memory buffer
  306. -> vector array
  307. And after the flush the CIL head is empty, and the checkpoint context log
  308. vector list would look like:
  309. Checkpoint Context
  310. |
  311. V
  312. log vector 1 -> memory buffer
  313. | -> vector array
  314. | -> Log Item
  315. V
  316. log vector 2 -> memory buffer
  317. | -> vector array
  318. | -> Log Item
  319. V
  320. ......
  321. |
  322. V
  323. log vector N-1 -> memory buffer
  324. | -> vector array
  325. | -> Log Item
  326. V
  327. log vector N -> memory buffer
  328. -> vector array
  329. -> Log Item
  330. Once this transfer is done, the CIL can be unlocked and new transactions can
  331. start, while the checkpoint flush code works over the log vector chain to
  332. commit the checkpoint.
  333. Once the checkpoint is written into the log buffers, the checkpoint context is
  334. attached to the log buffer that the commit record was written to along with a
  335. completion callback. Log IO completion will call that callback, which can then
  336. run transaction committed processing for the log items (i.e. insert into AIL
  337. and unpin) in the log vector chain and then free the log vector chain and
  338. checkpoint context.
  339. Discussion Point: I am uncertain as to whether the log item is the most
  340. efficient way to track vectors, even though it seems like the natural way to do
  341. it. The fact that we walk the log items (in the CIL) just to chain the log
  342. vectors and break the link between the log item and the log vector means that
  343. we take a cache line hit for the log item list modification, then another for
  344. the log vector chaining. If we track by the log vectors, then we only need to
  345. break the link between the log item and the log vector, which means we should
  346. dirty only the log item cachelines. Normally I wouldn't be concerned about one
  347. vs two dirty cachelines except for the fact I've seen upwards of 80,000 log
  348. vectors in one checkpoint transaction. I'd guess this is a "measure and
  349. compare" situation that can be done after a working and reviewed implementation
  350. is in the dev tree....
  351. Delayed Logging: Checkpoint Sequencing
  352. One of the key aspects of the XFS transaction subsystem is that it tags
  353. committed transactions with the log sequence number of the transaction commit.
  354. This allows transactions to be issued asynchronously even though there may be
  355. future operations that cannot be completed until that transaction is fully
  356. committed to the log. In the rare case that a dependent operation occurs (e.g.
  357. re-using a freed metadata extent for a data extent), a special, optimised log
  358. force can be issued to force the dependent transaction to disk immediately.
  359. To do this, transactions need to record the LSN of the commit record of the
  360. transaction. This LSN comes directly from the log buffer the transaction is
  361. written into. While this works just fine for the existing transaction
  362. mechanism, it does not work for delayed logging because transactions are not
  363. written directly into the log buffers. Hence some other method of sequencing
  364. transactions is required.
  365. As discussed in the checkpoint section, delayed logging uses per-checkpoint
  366. contexts, and as such it is simple to assign a sequence number to each
  367. checkpoint. Because the switching of checkpoint contexts must be done
  368. atomically, it is simple to ensure that each new context has a monotonically
  369. increasing sequence number assigned to it without the need for an external
  370. atomic counter - we can just take the current context sequence number and add
  371. one to it for the new context.
  372. Then, instead of assigning a log buffer LSN to the transaction commit LSN
  373. during the commit, we can assign the current checkpoint sequence. This allows
  374. operations that track transactions that have not yet completed know what
  375. checkpoint sequence needs to be committed before they can continue. As a
  376. result, the code that forces the log to a specific LSN now needs to ensure that
  377. the log forces to a specific checkpoint.
  378. To ensure that we can do this, we need to track all the checkpoint contexts
  379. that are currently committing to the log. When we flush a checkpoint, the
  380. context gets added to a "committing" list which can be searched. When a
  381. checkpoint commit completes, it is removed from the committing list. Because
  382. the checkpoint context records the LSN of the commit record for the checkpoint,
  383. we can also wait on the log buffer that contains the commit record, thereby
  384. using the existing log force mechanisms to execute synchronous forces.
  385. It should be noted that the synchronous forces may need to be extended with
  386. mitigation algorithms similar to the current log buffer code to allow
  387. aggregation of multiple synchronous transactions if there are already
  388. synchronous transactions being flushed. Investigation of the performance of the
  389. current design is needed before making any decisions here.
  390. The main concern with log forces is to ensure that all the previous checkpoints
  391. are also committed to disk before the one we need to wait for. Therefore we
  392. need to check that all the prior contexts in the committing list are also
  393. complete before waiting on the one we need to complete. We do this
  394. synchronisation in the log force code so that we don't need to wait anywhere
  395. else for such serialisation - it only matters when we do a log force.
  396. The only remaining complexity is that a log force now also has to handle the
  397. case where the forcing sequence number is the same as the current context. That
  398. is, we need to flush the CIL and potentially wait for it to complete. This is a
  399. simple addition to the existing log forcing code to check the sequence numbers
  400. and push if required. Indeed, placing the current sequence checkpoint flush in
  401. the log force code enables the current mechanism for issuing synchronous
  402. transactions to remain untouched (i.e. commit an asynchronous transaction, then
  403. force the log at the LSN of that transaction) and so the higher level code
  404. behaves the same regardless of whether delayed logging is being used or not.
  405. Delayed Logging: Checkpoint Log Space Accounting
  406. The big issue for a checkpoint transaction is the log space reservation for the
  407. transaction. We don't know how big a checkpoint transaction is going to be
  408. ahead of time, nor how many log buffers it will take to write out, nor the
  409. number of split log vector regions are going to be used. We can track the
  410. amount of log space required as we add items to the commit item list, but we
  411. still need to reserve the space in the log for the checkpoint.
  412. A typical transaction reserves enough space in the log for the worst case space
  413. usage of the transaction. The reservation accounts for log record headers,
  414. transaction and region headers, headers for split regions, buffer tail padding,
  415. etc. as well as the actual space for all the changed metadata in the
  416. transaction. While some of this is fixed overhead, much of it is dependent on
  417. the size of the transaction and the number of regions being logged (the number
  418. of log vectors in the transaction).
  419. An example of the differences would be logging directory changes versus logging
  420. inode changes. If you modify lots of inode cores (e.g. chmod -R g+w *), then
  421. there are lots of transactions that only contain an inode core and an inode log
  422. format structure. That is, two vectors totaling roughly 150 bytes. If we modify
  423. 10,000 inodes, we have about 1.5MB of metadata to write in 20,000 vectors. Each
  424. vector is 12 bytes, so the total to be logged is approximately 1.75MB. In
  425. comparison, if we are logging full directory buffers, they are typically 4KB
  426. each, so we in 1.5MB of directory buffers we'd have roughly 400 buffers and a
  427. buffer format structure for each buffer - roughly 800 vectors or 1.51MB total
  428. space. From this, it should be obvious that a static log space reservation is
  429. not particularly flexible and is difficult to select the "optimal value" for
  430. all workloads.
  431. Further, if we are going to use a static reservation, which bit of the entire
  432. reservation does it cover? We account for space used by the transaction
  433. reservation by tracking the space currently used by the object in the CIL and
  434. then calculating the increase or decrease in space used as the object is
  435. relogged. This allows for a checkpoint reservation to only have to account for
  436. log buffer metadata used such as log header records.
  437. However, even using a static reservation for just the log metadata is
  438. problematic. Typically log record headers use at least 16KB of log space per
  439. 1MB of log space consumed (512 bytes per 32k) and the reservation needs to be
  440. large enough to handle arbitrary sized checkpoint transactions. This
  441. reservation needs to be made before the checkpoint is started, and we need to
  442. be able to reserve the space without sleeping. For a 8MB checkpoint, we need a
  443. reservation of around 150KB, which is a non-trivial amount of space.
  444. A static reservation needs to manipulate the log grant counters - we can take a
  445. permanent reservation on the space, but we still need to make sure we refresh
  446. the write reservation (the actual space available to the transaction) after
  447. every checkpoint transaction completion. Unfortunately, if this space is not
  448. available when required, then the regrant code will sleep waiting for it.
  449. The problem with this is that it can lead to deadlocks as we may need to commit
  450. checkpoints to be able to free up log space (refer back to the description of
  451. rolling transactions for an example of this). Hence we *must* always have
  452. space available in the log if we are to use static reservations, and that is
  453. very difficult and complex to arrange. It is possible to do, but there is a
  454. simpler way.
  455. The simpler way of doing this is tracking the entire log space used by the
  456. items in the CIL and using this to dynamically calculate the amount of log
  457. space required by the log metadata. If this log metadata space changes as a
  458. result of a transaction commit inserting a new memory buffer into the CIL, then
  459. the difference in space required is removed from the transaction that causes
  460. the change. Transactions at this level will *always* have enough space
  461. available in their reservation for this as they have already reserved the
  462. maximal amount of log metadata space they require, and such a delta reservation
  463. will always be less than or equal to the maximal amount in the reservation.
  464. Hence we can grow the checkpoint transaction reservation dynamically as items
  465. are added to the CIL and avoid the need for reserving and regranting log space
  466. up front. This avoids deadlocks and removes a blocking point from the
  467. checkpoint flush code.
  468. As mentioned early, transactions can't grow to more than half the size of the
  469. log. Hence as part of the reservation growing, we need to also check the size
  470. of the reservation against the maximum allowed transaction size. If we reach
  471. the maximum threshold, we need to push the CIL to the log. This is effectively
  472. a "background flush" and is done on demand. This is identical to
  473. a CIL push triggered by a log force, only that there is no waiting for the
  474. checkpoint commit to complete. This background push is checked and executed by
  475. transaction commit code.
  476. If the transaction subsystem goes idle while we still have items in the CIL,
  477. they will be flushed by the periodic log force issued by the xfssyncd. This log
  478. force will push the CIL to disk, and if the transaction subsystem stays idle,
  479. allow the idle log to be covered (effectively marked clean) in exactly the same
  480. manner that is done for the existing logging method. A discussion point is
  481. whether this log force needs to be done more frequently than the current rate
  482. which is once every 30s.
  483. Delayed Logging: Log Item Pinning
  484. Currently log items are pinned during transaction commit while the items are
  485. still locked. This happens just after the items are formatted, though it could
  486. be done any time before the items are unlocked. The result of this mechanism is
  487. that items get pinned once for every transaction that is committed to the log
  488. buffers. Hence items that are relogged in the log buffers will have a pin count
  489. for every outstanding transaction they were dirtied in. When each of these
  490. transactions is completed, they will unpin the item once. As a result, the item
  491. only becomes unpinned when all the transactions complete and there are no
  492. pending transactions. Thus the pinning and unpinning of a log item is symmetric
  493. as there is a 1:1 relationship with transaction commit and log item completion.
  494. For delayed logging, however, we have an asymmetric transaction commit to
  495. completion relationship. Every time an object is relogged in the CIL it goes
  496. through the commit process without a corresponding completion being registered.
  497. That is, we now have a many-to-one relationship between transaction commit and
  498. log item completion. The result of this is that pinning and unpinning of the
  499. log items becomes unbalanced if we retain the "pin on transaction commit, unpin
  500. on transaction completion" model.
  501. To keep pin/unpin symmetry, the algorithm needs to change to a "pin on
  502. insertion into the CIL, unpin on checkpoint completion". In other words, the
  503. pinning and unpinning becomes symmetric around a checkpoint context. We have to
  504. pin the object the first time it is inserted into the CIL - if it is already in
  505. the CIL during a transaction commit, then we do not pin it again. Because there
  506. can be multiple outstanding checkpoint contexts, we can still see elevated pin
  507. counts, but as each checkpoint completes the pin count will retain the correct
  508. value according to it's context.
  509. Just to make matters more slightly more complex, this checkpoint level context
  510. for the pin count means that the pinning of an item must take place under the
  511. CIL commit/flush lock. If we pin the object outside this lock, we cannot
  512. guarantee which context the pin count is associated with. This is because of
  513. the fact pinning the item is dependent on whether the item is present in the
  514. current CIL or not. If we don't pin the CIL first before we check and pin the
  515. object, we have a race with CIL being flushed between the check and the pin
  516. (or not pinning, as the case may be). Hence we must hold the CIL flush/commit
  517. lock to guarantee that we pin the items correctly.
  518. Delayed Logging: Concurrent Scalability
  519. A fundamental requirement for the CIL is that accesses through transaction
  520. commits must scale to many concurrent commits. The current transaction commit
  521. code does not break down even when there are transactions coming from 2048
  522. processors at once. The current transaction code does not go any faster than if
  523. there was only one CPU using it, but it does not slow down either.
  524. As a result, the delayed logging transaction commit code needs to be designed
  525. for concurrency from the ground up. It is obvious that there are serialisation
  526. points in the design - the three important ones are:
  527. 1. Locking out new transaction commits while flushing the CIL
  528. 2. Adding items to the CIL and updating item space accounting
  529. 3. Checkpoint commit ordering
  530. Looking at the transaction commit and CIL flushing interactions, it is clear
  531. that we have a many-to-one interaction here. That is, the only restriction on
  532. the number of concurrent transactions that can be trying to commit at once is
  533. the amount of space available in the log for their reservations. The practical
  534. limit here is in the order of several hundred concurrent transactions for a
  535. 128MB log, which means that it is generally one per CPU in a machine.
  536. The amount of time a transaction commit needs to hold out a flush is a
  537. relatively long period of time - the pinning of log items needs to be done
  538. while we are holding out a CIL flush, so at the moment that means it is held
  539. across the formatting of the objects into memory buffers (i.e. while memcpy()s
  540. are in progress). Ultimately a two pass algorithm where the formatting is done
  541. separately to the pinning of objects could be used to reduce the hold time of
  542. the transaction commit side.
  543. Because of the number of potential transaction commit side holders, the lock
  544. really needs to be a sleeping lock - if the CIL flush takes the lock, we do not
  545. want every other CPU in the machine spinning on the CIL lock. Given that
  546. flushing the CIL could involve walking a list of tens of thousands of log
  547. items, it will get held for a significant time and so spin contention is a
  548. significant concern. Preventing lots of CPUs spinning doing nothing is the
  549. main reason for choosing a sleeping lock even though nothing in either the
  550. transaction commit or CIL flush side sleeps with the lock held.
  551. It should also be noted that CIL flushing is also a relatively rare operation
  552. compared to transaction commit for asynchronous transaction workloads - only
  553. time will tell if using a read-write semaphore for exclusion will limit
  554. transaction commit concurrency due to cache line bouncing of the lock on the
  555. read side.
  556. The second serialisation point is on the transaction commit side where items
  557. are inserted into the CIL. Because transactions can enter this code
  558. concurrently, the CIL needs to be protected separately from the above
  559. commit/flush exclusion. It also needs to be an exclusive lock but it is only
  560. held for a very short time and so a spin lock is appropriate here. It is
  561. possible that this lock will become a contention point, but given the short
  562. hold time once per transaction I think that contention is unlikely.
  563. The final serialisation point is the checkpoint commit record ordering code
  564. that is run as part of the checkpoint commit and log force sequencing. The code
  565. path that triggers a CIL flush (i.e. whatever triggers the log force) will enter
  566. an ordering loop after writing all the log vectors into the log buffers but
  567. before writing the commit record. This loop walks the list of committing
  568. checkpoints and needs to block waiting for checkpoints to complete their commit
  569. record write. As a result it needs a lock and a wait variable. Log force
  570. sequencing also requires the same lock, list walk, and blocking mechanism to
  571. ensure completion of checkpoints.
  572. These two sequencing operations can use the mechanism even though the
  573. events they are waiting for are different. The checkpoint commit record
  574. sequencing needs to wait until checkpoint contexts contain a commit LSN
  575. (obtained through completion of a commit record write) while log force
  576. sequencing needs to wait until previous checkpoint contexts are removed from
  577. the committing list (i.e. they've completed). A simple wait variable and
  578. broadcast wakeups (thundering herds) has been used to implement these two
  579. serialisation queues. They use the same lock as the CIL, too. If we see too
  580. much contention on the CIL lock, or too many context switches as a result of
  581. the broadcast wakeups these operations can be put under a new spinlock and
  582. given separate wait lists to reduce lock contention and the number of processes
  583. woken by the wrong event.
  584. Lifecycle Changes
  585. The existing log item life cycle is as follows:
  586. 1. Transaction allocate
  587. 2. Transaction reserve
  588. 3. Lock item
  589. 4. Join item to transaction
  590. If not already attached,
  591. Allocate log item
  592. Attach log item to owner item
  593. Attach log item to transaction
  594. 5. Modify item
  595. Record modifications in log item
  596. 6. Transaction commit
  597. Pin item in memory
  598. Format item into log buffer
  599. Write commit LSN into transaction
  600. Unlock item
  601. Attach transaction to log buffer
  602. <log buffer IO dispatched>
  603. <log buffer IO completes>
  604. 7. Transaction completion
  605. Mark log item committed
  606. Insert log item into AIL
  607. Write commit LSN into log item
  608. Unpin log item
  609. 8. AIL traversal
  610. Lock item
  611. Mark log item clean
  612. Flush item to disk
  613. <item IO completion>
  614. 9. Log item removed from AIL
  615. Moves log tail
  616. Item unlocked
  617. Essentially, steps 1-6 operate independently from step 7, which is also
  618. independent of steps 8-9. An item can be locked in steps 1-6 or steps 8-9
  619. at the same time step 7 is occurring, but only steps 1-6 or 8-9 can occur
  620. at the same time. If the log item is in the AIL or between steps 6 and 7
  621. and steps 1-6 are re-entered, then the item is relogged. Only when steps 8-9
  622. are entered and completed is the object considered clean.
  623. With delayed logging, there are new steps inserted into the life cycle:
  624. 1. Transaction allocate
  625. 2. Transaction reserve
  626. 3. Lock item
  627. 4. Join item to transaction
  628. If not already attached,
  629. Allocate log item
  630. Attach log item to owner item
  631. Attach log item to transaction
  632. 5. Modify item
  633. Record modifications in log item
  634. 6. Transaction commit
  635. Pin item in memory if not pinned in CIL
  636. Format item into log vector + buffer
  637. Attach log vector and buffer to log item
  638. Insert log item into CIL
  639. Write CIL context sequence into transaction
  640. Unlock item
  641. <next log force>
  642. 7. CIL push
  643. lock CIL flush
  644. Chain log vectors and buffers together
  645. Remove items from CIL
  646. unlock CIL flush
  647. write log vectors into log
  648. sequence commit records
  649. attach checkpoint context to log buffer
  650. <log buffer IO dispatched>
  651. <log buffer IO completes>
  652. 8. Checkpoint completion
  653. Mark log item committed
  654. Insert item into AIL
  655. Write commit LSN into log item
  656. Unpin log item
  657. 9. AIL traversal
  658. Lock item
  659. Mark log item clean
  660. Flush item to disk
  661. <item IO completion>
  662. 10. Log item removed from AIL
  663. Moves log tail
  664. Item unlocked
  665. From this, it can be seen that the only life cycle differences between the two
  666. logging methods are in the middle of the life cycle - they still have the same
  667. beginning and end and execution constraints. The only differences are in the
  668. committing of the log items to the log itself and the completion processing.
  669. Hence delayed logging should not introduce any constraints on log item
  670. behaviour, allocation or freeing that don't already exist.
  671. As a result of this zero-impact "insertion" of delayed logging infrastructure
  672. and the design of the internal structures to avoid on disk format changes, we
  673. can basically switch between delayed logging and the existing mechanism with a
  674. mount option. Fundamentally, there is no reason why the log manager would not
  675. be able to swap methods automatically and transparently depending on load
  676. characteristics, but this should not be necessary if delayed logging works as
  677. designed.