Debian Codegraph @

includes/filebackend/README.md

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