README 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*!
  2. \ingroup FileBackend
  3. \page file_backend_design File backend design
  4. Some notes on the FileBackend architecture.
  5. \section intro Introduction
  6. To abstract away the differences among different types of storage media,
  7. MediaWiki is providing an interface known as FileBackend. Any MediaWiki
  8. interaction with stored files should thus use a FileBackend object.
  9. Different types of backing storage media are supported (ranging from local
  10. file system to distributed object stores). The types include:
  11. * FSFileBackend (used for mounted file systems)
  12. * SwiftFileBackend (used for Swift or Ceph Rados+RGW object stores)
  13. * FileBackendMultiWrite (useful for transitioning from one backend to another)
  14. Configuration documentation for each type of backend is to be found in their
  15. __construct() inline documentation.
  16. \section setup Setup
  17. File backends are registered in LocalSettings.php via the global variable
  18. $wgFileBackends. To access one of those defined backends, one would use
  19. FileBackendStore::get( <name> ) which will bring back a FileBackend object
  20. handle. Such handles are reused for any subsequent get() call (via singleton).
  21. The FileBackends objects are caching request calls such as file stats,
  22. SHA1 requests or TCP connection handles.
  23. \par Note:
  24. Some backends may require additional PHP extensions to be enabled or can rely on a
  25. MediaWiki extension. This is often the case when a FileBackend subclass makes use of an
  26. upstream client API for communicating with the backing store.
  27. \section fileoperations File operations
  28. The MediaWiki FileBackend API supports various operations on either files or
  29. directories. See FileBackend.php for full documentation for each function.
  30. \subsection reading Reading
  31. The following basic operations are supported for reading from a backend:
  32. On files:
  33. * stat a file for basic information (timestamp, size)
  34. * read a file into a string or several files into a map of path names to strings
  35. * download a file or set of files to a temporary file (on a mounted file system)
  36. * get the SHA1 hash of a file
  37. * get various properties of a file (stat information, content time, MIME information, ...)
  38. On directories:
  39. * get a list of files directly under a directory
  40. * get a recursive list of files under a directory
  41. * get a list of directories directly under a directory
  42. * get a recursive list of directories under a directory
  43. \par Note:
  44. Backend handles should return directory listings as iterators, all though in some cases
  45. they may just be simple arrays (which can still be iterated over). Iterators allow for
  46. callers to traverse a large number of file listings without consuming excessive RAM in
  47. the process. Either the memory consumed is flatly bounded (if the iterator does paging)
  48. or it is proportional to the depth of the portion of the directory tree being traversed
  49. (if the iterator works via recursion).
  50. \subsection writing Writing
  51. The following basic operations are supported for writing or changing in the backend:
  52. On files:
  53. * store (copying a mounted file system file into storage)
  54. * create (creating a file within storage from a string)
  55. * copy (within storage)
  56. * move (within storage)
  57. * delete (within storage)
  58. * lock/unlock (lock or unlock a file in storage)
  59. The following operations are supported for writing directories in the backend:
  60. * prepare (create parent container and directories for a path)
  61. * secure (try to lock-down access to a container)
  62. * publish (try to reverse the effects of secure)
  63. * clean (remove empty containers or directories)
  64. \subsection invokingoperation Invoking an operation
  65. Generally, callers should use doOperations() or doQuickOperations() when doing
  66. batches of changes, rather than making a suite of single operation calls. This
  67. makes the system tolerate high latency much better by pipelining operations
  68. when possible.
  69. doOperations() should be used for working on important original data, i.e. when
  70. consistency is important. The former will only pipeline operations that do not
  71. depend on each other. It is best if the operations that do not depend on each
  72. other occur in consecutive groups. This function can also log file changes to
  73. a journal (see FileJournal), which can be used to sync two backend instances.
  74. One might use this function for user uploads of file for example.
  75. doQuickOperations() is more geared toward ephemeral items that can be easily
  76. regenerated from original data. It will always pipeline without checking for
  77. dependencies within the operation batch. One might use this function for
  78. creating and purging generated thumbnails of original files for example.
  79. \section consistency Consistency
  80. Not all backing stores are sequentially consistent by default. Various FileBackend
  81. functions offer a "latest" option that can be passed in to assure (or try to assure)
  82. that the latest version of the file is read. Some backing stores are consistent by
  83. default, but callers should always assume that without this option, stale data may
  84. be read. This is actually true for stores that have eventual consistency.
  85. Note that file listing functions have no "latest" flag, and thus some systems may
  86. return stale data. Thus callers should avoid assuming that listings contain changes
  87. made my the current client or any other client from a very short time ago. For example,
  88. creating a file under a directory and then immediately doing a file listing operation
  89. on that directory may result in a listing that does not include that file.
  90. \section locking Locking
  91. Locking is effective if and only if a proper lock manager is registered and is
  92. actually being used by the backend. Lock managers can be registered in LocalSettings.php
  93. using the $wgLockManagers global configuration variable.
  94. For object stores, locking is not generally useful for avoiding partially
  95. written or read objects, since most stores use Multi Version Concurrency
  96. Control (MVCC) to avoid this. However, locking can be important when:
  97. * One or more operations must be done without objects changing in the meantime.
  98. * It can also be useful when a file read is used to determine a file write or DB change.
  99. For example, doOperations() first checks that there will be no "file already exists"
  100. or "file does not exist" type errors before attempting an operation batch. This works
  101. by stating the files first, and is only safe if the files are locked in the meantime.
  102. When locking, callers should use the latest available file data for reads.
  103. Also, one should always lock the file *before* reading it, not after. If stale data is
  104. used to determine a write, there will be some data corruption, even when reads of the
  105. original file finally start returning the updated data without needing the "latest"
  106. option (eventual consistency). The "scoped" lock functions are preferable since
  107. there is not the problem of forgetting to unlock due to early returns or exceptions.
  108. Since acquiring locks can fail, and lock managers can be non-blocking, callers should:
  109. * Acquire all required locks up font
  110. * Be prepared for the case where locks fail to be acquired
  111. * Possible retry acquiring certain locks
  112. MVCC is also a useful pattern to use on top of the backend interface, because operations
  113. are not atomic, even with doOperations(), so doing complex batch file changes or changing
  114. files and updating a database row can result in partially written "transactions". Thus one
  115. should avoid changing files once they have been stored, except perhaps with ephemeral data
  116. that are tolerant of some degree of inconsistency.
  117. Callers can use their own locking (e.g. SELECT FOR UPDATE) if it is more convenient, but
  118. note that all callers that change any of the files should then go through functions that
  119. acquire these locks. For example, if a caller just directly uses the file backend store()
  120. function, it will ignore any custom "FOR UPDATE" locks, which can cause problems.
  121. \section objectstore Object stores
  122. Support for object stores (like Amazon S3/Swift) drive much of the API and design
  123. decisions of FileBackend, but using any POSIX compliant file systems works fine.
  124. The system essentially stores "files" in "containers". For a mounted file system
  125. as a backing store, "files" will just be files under directories. For an object store
  126. as a backing store, the "files" will be objects stored in actual containers.
  127. \section file_obj_diffs File system and Object store differences
  128. An advantage of object stores is the reduced Round-Trip Times. This is
  129. achieved by avoiding the need to create each parent directory before placing a
  130. file somewhere. It gets worse the deeper the directory hierarchy is. Another
  131. advantage of object stores is that object listings tend to use databases, which
  132. scale better than the linked list directories that file sytems sometimes use.
  133. File systems like btrfs and xfs use tree structures, which scale better.
  134. For both object stores and file systems, using "/" in filenames will allow for the
  135. intuitive use of directory functions. For example, creating a file in Swift
  136. called "container/a/b/file1" will mean that:
  137. - a "directory listing" of "container/a" will contain "b",
  138. - and a "file listing" of "b" will contain "file1"
  139. This means that switching from an object store to a file system and vise versa
  140. using the FileBackend interface will generally be harmless. However, one must be
  141. aware of some important differences:
  142. * In a file system, you cannot have a file and a directory within the same path
  143. whereas it is possible in an object stores. Calling code should avoid any layouts
  144. which allow files and directories at the same path.
  145. * Some file systems have file name length restrictions or overall path length
  146. restrictions that others do not. The same goes with object stores which might
  147. have a maximum object length or a limitation regarding the number of files
  148. under a container or volume.
  149. * Latency varies among systems, certain access patterns may not be tolerable for
  150. certain backends but may hold up for others. Some backend subclasses use
  151. MediaWiki's object caching for serving stat requests, which can greatly
  152. reduce latency. Making sure that the backend has pipelining (see the
  153. "parallelize" and "concurrency" settings) enabled can also mask latency in
  154. batch operation scenarios.
  155. * File systems may implement directories as linked-lists or other structures
  156. with poor scalability, so calling code should use layouts that shard the data.
  157. Instead of storing files like "container/file.txt", one can store files like
  158. "container/<x>/<y>/file.txt". It is best if "sharding" optional or configurable.
  159. */