Add/update function level parameter documentation
[lhc/web/wiklou.git] / includes / filerepo / backend / FileBackendMultiWrite.php
1 <?php
2 /**
3 * Proxy backend that mirrors writes to several internal backends.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup FileBackend
22 * @author Aaron Schulz
23 */
24
25 /**
26 * @brief Proxy backend that mirrors writes to several internal backends.
27 *
28 * This class defines a multi-write backend. Multiple backends can be
29 * registered to this proxy backend and it will act as a single backend.
30 * Use this when all access to those backends is through this proxy backend.
31 * At least one of the backends must be declared the "master" backend.
32 *
33 * Only use this class when transitioning from one storage system to another.
34 *
35 * Read operations are only done on the 'master' backend for consistency.
36 * Write operations are performed on all backends, in the order defined.
37 * If an operation fails on one backend it will be rolled back from the others.
38 *
39 * @ingroup FileBackend
40 * @since 1.19
41 */
42 class FileBackendMultiWrite extends FileBackend {
43 /** @var Array Prioritized list of FileBackendStore objects */
44 protected $backends = array(); // array of (backend index => backends)
45 protected $masterIndex = -1; // integer; index of master backend
46 protected $syncChecks = 0; // integer bitfield
47
48 /* Possible internal backend consistency checks */
49 const CHECK_SIZE = 1;
50 const CHECK_TIME = 2;
51
52 /**
53 * Construct a proxy backend that consists of several internal backends.
54 * Additional $config params include:
55 * 'backends' : Array of backend config and multi-backend settings.
56 * Each value is the config used in the constructor of a
57 * FileBackendStore class, but with these additional settings:
58 * 'class' : The name of the backend class
59 * 'isMultiMaster' : This must be set for one backend.
60 * 'syncChecks' : Integer bitfield of internal backend sync checks to perform.
61 * Possible bits include self::CHECK_SIZE and self::CHECK_TIME.
62 * The checks are done before allowing any file operations.
63 * @param $config Array
64 */
65 public function __construct( array $config ) {
66 parent::__construct( $config );
67 $namesUsed = array();
68 // Construct backends here rather than via registration
69 // to keep these backends hidden from outside the proxy.
70 foreach ( $config['backends'] as $index => $config ) {
71 $name = $config['name'];
72 if ( isset( $namesUsed[$name] ) ) { // don't break FileOp predicates
73 throw new MWException( "Two or more backends defined with the name $name." );
74 }
75 $namesUsed[$name] = 1;
76 if ( !isset( $config['class'] ) ) {
77 throw new MWException( 'No class given for a backend config.' );
78 }
79 $class = $config['class'];
80 $this->backends[$index] = new $class( $config );
81 if ( !empty( $config['isMultiMaster'] ) ) {
82 if ( $this->masterIndex >= 0 ) {
83 throw new MWException( 'More than one master backend defined.' );
84 }
85 $this->masterIndex = $index;
86 }
87 }
88 if ( $this->masterIndex < 0 ) { // need backends and must have a master
89 throw new MWException( 'No master backend defined.' );
90 }
91 $this->syncChecks = isset( $config['syncChecks'] )
92 ? $config['syncChecks']
93 : self::CHECK_SIZE;
94 }
95
96 /**
97 * @see FileBackend::doOperationsInternal()
98 * @return Status
99 */
100 final protected function doOperationsInternal( array $ops, array $opts ) {
101 $status = Status::newGood();
102
103 $performOps = array(); // list of FileOp objects
104 $paths = array(); // storage paths read from or written to
105 // Build up a list of FileOps. The list will have all the ops
106 // for one backend, then all the ops for the next, and so on.
107 // These batches of ops are all part of a continuous array.
108 // Also build up a list of files read/changed...
109 foreach ( $this->backends as $index => $backend ) {
110 $backendOps = $this->substOpBatchPaths( $ops, $backend );
111 // Add on the operation batch for this backend
112 $performOps = array_merge( $performOps,
113 $backend->getOperationsInternal( $backendOps ) );
114 if ( $index == 0 ) { // first batch
115 // Get the files used for these operations. Each backend has a batch of
116 // the same operations, so we only need to get them from the first batch.
117 $paths = $backend->getPathsToLockForOpsInternal( $performOps );
118 // Get the paths under the proxy backend's name
119 $paths['sh'] = $this->unsubstPaths( $paths['sh'] );
120 $paths['ex'] = $this->unsubstPaths( $paths['ex'] );
121 }
122 }
123
124 // Try to lock those files for the scope of this function...
125 if ( empty( $opts['nonLocking'] ) ) {
126 // Try to lock those files for the scope of this function...
127 $scopeLockS = $this->getScopedFileLocks( $paths['sh'], LockManager::LOCK_UW, $status );
128 $scopeLockE = $this->getScopedFileLocks( $paths['ex'], LockManager::LOCK_EX, $status );
129 if ( !$status->isOK() ) {
130 return $status; // abort
131 }
132 }
133
134 // Clear any cache entries (after locks acquired)
135 $this->clearCache();
136
137 // Do a consistency check to see if the backends agree
138 if ( count( $this->backends ) > 1 ) {
139 $status->merge( $this->consistencyCheck( array_merge( $paths['sh'], $paths['ex'] ) ) );
140 if ( !$status->isOK() ) {
141 return $status; // abort
142 }
143 }
144
145 // Actually attempt the operation batch...
146 $subStatus = FileOp::attemptBatch( $performOps, $opts, $this->fileJournal );
147
148 $success = array();
149 $failCount = 0;
150 $successCount = 0;
151 // Make 'success', 'successCount', and 'failCount' fields reflect
152 // the overall operation, rather than all the batches for each backend.
153 // Do this by only using success values from the master backend's batch.
154 $batchStart = $this->masterIndex * count( $ops );
155 $batchEnd = $batchStart + count( $ops ) - 1;
156 for ( $i = $batchStart; $i <= $batchEnd; $i++ ) {
157 if ( !isset( $subStatus->success[$i] ) ) {
158 break; // failed out before trying this op
159 } elseif ( $subStatus->success[$i] ) {
160 ++$successCount;
161 } else {
162 ++$failCount;
163 }
164 $success[] = $subStatus->success[$i];
165 }
166 $subStatus->success = $success;
167 $subStatus->successCount = $successCount;
168 $subStatus->failCount = $failCount;
169
170 // Merge errors into status fields
171 $status->merge( $subStatus );
172 $status->success = $subStatus->success; // not done in merge()
173
174 return $status;
175 }
176
177 /**
178 * Check that a set of files are consistent across all internal backends
179 *
180 * @param $paths Array
181 * @return Status
182 */
183 public function consistencyCheck( array $paths ) {
184 $status = Status::newGood();
185 if ( $this->syncChecks == 0 ) {
186 return $status; // skip checks
187 }
188
189 $mBackend = $this->backends[$this->masterIndex];
190 foreach ( array_unique( $paths ) as $path ) {
191 $params = array( 'src' => $path, 'latest' => true );
192 // Stat the file on the 'master' backend
193 $mStat = $mBackend->getFileStat( $this->substOpPaths( $params, $mBackend ) );
194 // Check of all clone backends agree with the master...
195 foreach ( $this->backends as $index => $cBackend ) {
196 if ( $index === $this->masterIndex ) {
197 continue; // master
198 }
199 $cStat = $cBackend->getFileStat( $this->substOpPaths( $params, $cBackend ) );
200 if ( $mStat ) { // file is in master
201 if ( !$cStat ) { // file should exist
202 $status->fatal( 'backend-fail-synced', $path );
203 continue;
204 }
205 if ( $this->syncChecks & self::CHECK_SIZE ) {
206 if ( $cStat['size'] != $mStat['size'] ) { // wrong size
207 $status->fatal( 'backend-fail-synced', $path );
208 continue;
209 }
210 }
211 if ( $this->syncChecks & self::CHECK_TIME ) {
212 $mTs = wfTimestamp( TS_UNIX, $mStat['mtime'] );
213 $cTs = wfTimestamp( TS_UNIX, $cStat['mtime'] );
214 if ( abs( $mTs - $cTs ) > 30 ) { // outdated file somewhere
215 $status->fatal( 'backend-fail-synced', $path );
216 continue;
217 }
218 }
219 } else { // file is not in master
220 if ( $cStat ) { // file should not exist
221 $status->fatal( 'backend-fail-synced', $path );
222 }
223 }
224 }
225 }
226
227 return $status;
228 }
229
230 /**
231 * Substitute the backend name in storage path parameters
232 * for a set of operations with that of a given internal backend.
233 *
234 * @param $ops Array List of file operation arrays
235 * @param $backend FileBackendStore
236 * @return Array
237 */
238 protected function substOpBatchPaths( array $ops, FileBackendStore $backend ) {
239 $newOps = array(); // operations
240 foreach ( $ops as $op ) {
241 $newOp = $op; // operation
242 foreach ( array( 'src', 'srcs', 'dst', 'dir' ) as $par ) {
243 if ( isset( $newOp[$par] ) ) { // string or array
244 $newOp[$par] = $this->substPaths( $newOp[$par], $backend );
245 }
246 }
247 $newOps[] = $newOp;
248 }
249 return $newOps;
250 }
251
252 /**
253 * Same as substOpBatchPaths() but for a single operation
254 *
255 * @param $op File operation array
256 * @param $backend FileBackendStore
257 * @return Array
258 */
259 protected function substOpPaths( array $ops, FileBackendStore $backend ) {
260 $newOps = $this->substOpBatchPaths( array( $ops ), $backend );
261 return $newOps[0];
262 }
263
264 /**
265 * Substitute the backend of storage paths with an internal backend's name
266 *
267 * @param $paths Array|string List of paths or single string path
268 * @param $backend FileBackendStore
269 * @return Array|string
270 */
271 protected function substPaths( $paths, FileBackendStore $backend ) {
272 return preg_replace(
273 '!^mwstore://' . preg_quote( $this->name ) . '/!',
274 StringUtils::escapeRegexReplacement( "mwstore://{$backend->getName()}/" ),
275 $paths // string or array
276 );
277 }
278
279 /**
280 * Substitute the backend of internal storage paths with the proxy backend's name
281 *
282 * @param $paths Array|string List of paths or single string path
283 * @return Array|string
284 */
285 protected function unsubstPaths( $paths ) {
286 return preg_replace(
287 '!^mwstore://([^/]+)!',
288 StringUtils::escapeRegexReplacement( "mwstore://{$this->name}" ),
289 $paths // string or array
290 );
291 }
292
293 /**
294 * @see FileBackend::doPrepare()
295 * @return Status
296 */
297 protected function doPrepare( array $params ) {
298 $status = Status::newGood();
299 foreach ( $this->backends as $backend ) {
300 $realParams = $this->substOpPaths( $params, $backend );
301 $status->merge( $backend->doPrepare( $realParams ) );
302 }
303 return $status;
304 }
305
306 /**
307 * @see FileBackend::doSecure()
308 * @return Status
309 */
310 protected function doSecure( array $params ) {
311 $status = Status::newGood();
312 foreach ( $this->backends as $backend ) {
313 $realParams = $this->substOpPaths( $params, $backend );
314 $status->merge( $backend->doSecure( $realParams ) );
315 }
316 return $status;
317 }
318
319 /**
320 * @see FileBackend::doClean()
321 * @return Status
322 */
323 protected function doClean( array $params ) {
324 $status = Status::newGood();
325 foreach ( $this->backends as $backend ) {
326 $realParams = $this->substOpPaths( $params, $backend );
327 $status->merge( $backend->doClean( $realParams ) );
328 }
329 return $status;
330 }
331
332 /**
333 * @see FileBackend::concatenate()
334 */
335 public function concatenate( array $params ) {
336 // We are writing to an FS file, so we don't need to do this per-backend
337 $realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] );
338 return $this->backends[$this->masterIndex]->concatenate( $realParams );
339 }
340
341 /**
342 * @see FileBackend::fileExists()
343 */
344 public function fileExists( array $params ) {
345 $realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] );
346 return $this->backends[$this->masterIndex]->fileExists( $realParams );
347 }
348
349 /**
350 * @see FileBackend::getFileTimestamp()
351 */
352 public function getFileTimestamp( array $params ) {
353 $realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] );
354 return $this->backends[$this->masterIndex]->getFileTimestamp( $realParams );
355 }
356
357 /**
358 * @see FileBackend::getFileSize()
359 */
360 public function getFileSize( array $params ) {
361 $realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] );
362 return $this->backends[$this->masterIndex]->getFileSize( $realParams );
363 }
364
365 /**
366 * @see FileBackend::getFileStat()
367 */
368 public function getFileStat( array $params ) {
369 $realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] );
370 return $this->backends[$this->masterIndex]->getFileStat( $realParams );
371 }
372
373 /**
374 * @see FileBackend::getFileContents()
375 */
376 public function getFileContents( array $params ) {
377 $realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] );
378 return $this->backends[$this->masterIndex]->getFileContents( $realParams );
379 }
380
381 /**
382 * @see FileBackend::getFileSha1Base36()
383 */
384 public function getFileSha1Base36( array $params ) {
385 $realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] );
386 return $this->backends[$this->masterIndex]->getFileSha1Base36( $realParams );
387 }
388
389 /**
390 * @see FileBackend::getFileProps()
391 */
392 public function getFileProps( array $params ) {
393 $realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] );
394 return $this->backends[$this->masterIndex]->getFileProps( $realParams );
395 }
396
397 /**
398 * @see FileBackend::streamFile()
399 */
400 public function streamFile( array $params ) {
401 $realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] );
402 return $this->backends[$this->masterIndex]->streamFile( $realParams );
403 }
404
405 /**
406 * @see FileBackend::getLocalReference()
407 */
408 public function getLocalReference( array $params ) {
409 $realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] );
410 return $this->backends[$this->masterIndex]->getLocalReference( $realParams );
411 }
412
413 /**
414 * @see FileBackend::getLocalCopy()
415 */
416 public function getLocalCopy( array $params ) {
417 $realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] );
418 return $this->backends[$this->masterIndex]->getLocalCopy( $realParams );
419 }
420
421 /**
422 * @see FileBackend::directoryExists()
423 */
424 public function directoryExists( array $params ) {
425 $realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] );
426 return $this->backends[$this->masterIndex]->directoryExists( $realParams );
427 }
428
429 /**
430 * @see FileBackend::getSubdirectoryList()
431 */
432 public function getDirectoryList( array $params ) {
433 $realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] );
434 return $this->backends[$this->masterIndex]->getDirectoryList( $realParams );
435 }
436
437 /**
438 * @see FileBackend::getFileList()
439 */
440 public function getFileList( array $params ) {
441 $realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] );
442 return $this->backends[$this->masterIndex]->getFileList( $realParams );
443 }
444
445 /**
446 * @see FileBackend::clearCache()
447 */
448 public function clearCache( array $paths = null ) {
449 foreach ( $this->backends as $backend ) {
450 $realPaths = is_array( $paths ) ? $this->substPaths( $paths, $backend ) : null;
451 $backend->clearCache( $realPaths );
452 }
453 }
454 }