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