[FileBackend] Removed @since formatting for options to fix doxygen.
[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 const CHECK_SHA1 = 4;
52
53 /**
54 * Construct a proxy backend that consists of several internal backends.
55 * Locking, journaling, and read-only checks are handled by the proxy backend.
56 *
57 * Additional $config params include:
58 * 'backends' : Array of backend config and multi-backend settings.
59 * Each value is the config used in the constructor of a
60 * FileBackendStore class, but with these additional settings:
61 * 'class' : The name of the backend class
62 * 'isMultiMaster' : This must be set for one backend.
63 * 'template: : If given a backend name, this will use
64 * the config of that backend as a template.
65 * Values specified here take precedence.
66 * 'syncChecks' : Integer bitfield of internal backend sync checks to perform.
67 * Possible bits include the FileBackendMultiWrite::CHECK_* constants.
68 * There are constants for SIZE, TIME, and SHA1.
69 * The checks are done before allowing any file operations.
70 * @param $config Array
71 * @throws MWException
72 */
73 public function __construct( array $config ) {
74 parent::__construct( $config );
75 $namesUsed = array();
76 // Construct backends here rather than via registration
77 // to keep these backends hidden from outside the proxy.
78 foreach ( $config['backends'] as $index => $config ) {
79 if ( isset( $config['template'] ) ) {
80 // Config is just a modified version of a registered backend's.
81 // This should only be used when that config is used only be this backend.
82 $config = $config + FileBackendGroup::singleton()->config( $config['template'] );
83 }
84 $name = $config['name'];
85 if ( isset( $namesUsed[$name] ) ) { // don't break FileOp predicates
86 throw new MWException( "Two or more backends defined with the name $name." );
87 }
88 $namesUsed[$name] = 1;
89 // Alter certain sub-backend settings for sanity
90 unset( $config['readOnly'] ); // use proxy backend setting
91 unset( $config['fileJournal'] ); // use proxy backend journal
92 $config['lockManager'] = 'nullLockManager'; // lock under proxy backend
93 if ( !empty( $config['isMultiMaster'] ) ) {
94 if ( $this->masterIndex >= 0 ) {
95 throw new MWException( 'More than one master backend defined.' );
96 }
97 $this->masterIndex = $index; // this is the "master"
98 $config['fileJournal'] = $this->fileJournal; // log under proxy backend
99 }
100 // Create sub-backend object
101 if ( !isset( $config['class'] ) ) {
102 throw new MWException( 'No class given for a backend config.' );
103 }
104 $class = $config['class'];
105 $this->backends[$index] = new $class( $config );
106 }
107 if ( $this->masterIndex < 0 ) { // need backends and must have a master
108 throw new MWException( 'No master backend defined.' );
109 }
110 $this->syncChecks = isset( $config['syncChecks'] )
111 ? $config['syncChecks']
112 : self::CHECK_SIZE;
113 }
114
115 /**
116 * @see FileBackend::doOperationsInternal()
117 * @return Status
118 */
119 final protected function doOperationsInternal( array $ops, array $opts ) {
120 $status = Status::newGood();
121
122 $mbe = $this->backends[$this->masterIndex]; // convenience
123
124 // Get the paths to lock from the master backend
125 $realOps = $this->substOpBatchPaths( $ops, $mbe );
126 $paths = $mbe->getPathsToLockForOpsInternal( $mbe->getOperationsInternal( $realOps ) );
127 // Get the paths under the proxy backend's name
128 $paths['sh'] = $this->unsubstPaths( $paths['sh'] );
129 $paths['ex'] = $this->unsubstPaths( $paths['ex'] );
130 // Try to lock those files for the scope of this function...
131 if ( empty( $opts['nonLocking'] ) ) {
132 // Try to lock those files for the scope of this function...
133 $scopeLockS = $this->getScopedFileLocks( $paths['sh'], LockManager::LOCK_UW, $status );
134 $scopeLockE = $this->getScopedFileLocks( $paths['ex'], LockManager::LOCK_EX, $status );
135 if ( !$status->isOK() ) {
136 return $status; // abort
137 }
138 }
139 // Clear any cache entries (after locks acquired)
140 $this->clearCache();
141 // Do a consistency check to see if the backends agree
142 $status->merge( $this->consistencyCheck( array_merge( $paths['sh'], $paths['ex'] ) ) );
143 if ( !$status->isOK() ) {
144 return $status; // abort
145 }
146 // Actually attempt the operation batch on the master backend...
147 $masterStatus = $mbe->doOperations( $realOps, $opts );
148 $status->merge( $masterStatus );
149 // Propagate the operations to the clone backends...
150 foreach ( $this->backends as $index => $backend ) {
151 if ( $index !== $this->masterIndex ) { // not done already
152 $realOps = $this->substOpBatchPaths( $ops, $backend );
153 $status->merge( $backend->doOperations( $realOps, $opts ) );
154 }
155 }
156 // Make 'success', 'successCount', and 'failCount' fields reflect
157 // the overall operation, rather than all the batches for each backend.
158 // Do this by only using success values from the master backend's batch.
159 $status->success = $masterStatus->success;
160 $status->successCount = $masterStatus->successCount;
161 $status->failCount = $masterStatus->failCount;
162
163 return $status;
164 }
165
166 /**
167 * Check that a set of files are consistent across all internal backends
168 *
169 * @param $paths Array
170 * @return Status
171 */
172 public function consistencyCheck( array $paths ) {
173 $status = Status::newGood();
174 if ( $this->syncChecks == 0 || count( $this->backends ) <= 1 ) {
175 return $status; // skip checks
176 }
177
178 $mBackend = $this->backends[$this->masterIndex];
179 foreach ( array_unique( $paths ) as $path ) {
180 $params = array( 'src' => $path, 'latest' => true );
181 $mParams = $this->substOpPaths( $params, $mBackend );
182 // Stat the file on the 'master' backend
183 $mStat = $mBackend->getFileStat( $mParams );
184 if ( $this->syncChecks & self::CHECK_SHA1 ) {
185 $mSha1 = $mBackend->getFileSha1( $mParams );
186 } else {
187 $mSha1 = false;
188 }
189 $mUsable = $mBackend->isPathUsableInternal( $mParams['src'] );
190 // Check of all clone backends agree with the master...
191 foreach ( $this->backends as $index => $cBackend ) {
192 if ( $index === $this->masterIndex ) {
193 continue; // master
194 }
195 $cParams = $this->substOpPaths( $params, $cBackend );
196 $cStat = $cBackend->getFileStat( $cParams );
197 if ( $mStat ) { // file is in master
198 if ( !$cStat ) { // file should exist
199 $status->fatal( 'backend-fail-synced', $path );
200 continue;
201 }
202 if ( $this->syncChecks & self::CHECK_SIZE ) {
203 if ( $cStat['size'] != $mStat['size'] ) { // wrong size
204 $status->fatal( 'backend-fail-synced', $path );
205 continue;
206 }
207 }
208 if ( $this->syncChecks & self::CHECK_TIME ) {
209 $mTs = wfTimestamp( TS_UNIX, $mStat['mtime'] );
210 $cTs = wfTimestamp( TS_UNIX, $cStat['mtime'] );
211 if ( abs( $mTs - $cTs ) > 30 ) { // outdated file somewhere
212 $status->fatal( 'backend-fail-synced', $path );
213 continue;
214 }
215 }
216 if ( $this->syncChecks & self::CHECK_SHA1 ) {
217 if ( $cBackend->getFileSha1( $cParams ) !== $mSha1 ) { // wrong SHA1
218 $status->fatal( 'backend-fail-synced', $path );
219 continue;
220 }
221 }
222 } else { // file is not in master
223 if ( $cStat ) { // file should not exist
224 $status->fatal( 'backend-fail-synced', $path );
225 }
226 }
227 if ( $mUsable !== $cBackend->isPathUsableInternal( $cParams['src'] ) ) {
228 $status->fatal( 'backend-fail-synced', $path );
229 }
230 }
231 }
232
233 return $status;
234 }
235
236 /**
237 * Substitute the backend name in storage path parameters
238 * for a set of operations with that of a given internal backend.
239 *
240 * @param $ops Array List of file operation arrays
241 * @param $backend FileBackendStore
242 * @return Array
243 */
244 protected function substOpBatchPaths( array $ops, FileBackendStore $backend ) {
245 $newOps = array(); // operations
246 foreach ( $ops as $op ) {
247 $newOp = $op; // operation
248 foreach ( array( 'src', 'srcs', 'dst', 'dir' ) as $par ) {
249 if ( isset( $newOp[$par] ) ) { // string or array
250 $newOp[$par] = $this->substPaths( $newOp[$par], $backend );
251 }
252 }
253 $newOps[] = $newOp;
254 }
255 return $newOps;
256 }
257
258 /**
259 * Same as substOpBatchPaths() but for a single operation
260 *
261 * @param $ops array File operation array
262 * @param $backend FileBackendStore
263 * @return Array
264 */
265 protected function substOpPaths( array $ops, FileBackendStore $backend ) {
266 $newOps = $this->substOpBatchPaths( array( $ops ), $backend );
267 return $newOps[0];
268 }
269
270 /**
271 * Substitute the backend of storage paths with an internal backend's name
272 *
273 * @param $paths Array|string List of paths or single string path
274 * @param $backend FileBackendStore
275 * @return Array|string
276 */
277 protected function substPaths( $paths, FileBackendStore $backend ) {
278 return preg_replace(
279 '!^mwstore://' . preg_quote( $this->name ) . '/!',
280 StringUtils::escapeRegexReplacement( "mwstore://{$backend->getName()}/" ),
281 $paths // string or array
282 );
283 }
284
285 /**
286 * Substitute the backend of internal storage paths with the proxy backend's name
287 *
288 * @param $paths Array|string List of paths or single string path
289 * @return Array|string
290 */
291 protected function unsubstPaths( $paths ) {
292 return preg_replace(
293 '!^mwstore://([^/]+)!',
294 StringUtils::escapeRegexReplacement( "mwstore://{$this->name}" ),
295 $paths // string or array
296 );
297 }
298
299 /**
300 * @see FileBackend::doQuickOperationsInternal()
301 * @return Status
302 */
303 protected function doQuickOperationsInternal( array $ops ) {
304 $status = Status::newGood();
305 // Do the operations on the master backend; setting Status fields...
306 $realOps = $this->substOpBatchPaths( $ops, $this->backends[$this->masterIndex] );
307 $masterStatus = $this->backends[$this->masterIndex]->doQuickOperations( $realOps );
308 $status->merge( $masterStatus );
309 // Propagate the operations to the clone backends...
310 foreach ( $this->backends as $index => $backend ) {
311 if ( $index !== $this->masterIndex ) { // not done already
312 $realOps = $this->substOpBatchPaths( $ops, $backend );
313 $status->merge( $backend->doQuickOperations( $realOps ) );
314 }
315 }
316 // Make 'success', 'successCount', and 'failCount' fields reflect
317 // the overall operation, rather than all the batches for each backend.
318 // Do this by only using success values from the master backend's batch.
319 $status->success = $masterStatus->success;
320 $status->successCount = $masterStatus->successCount;
321 $status->failCount = $masterStatus->failCount;
322 return $status;
323 }
324
325 /**
326 * @see FileBackend::doPrepare()
327 * @return Status
328 */
329 protected function doPrepare( array $params ) {
330 $status = Status::newGood();
331 foreach ( $this->backends as $backend ) {
332 $realParams = $this->substOpPaths( $params, $backend );
333 $status->merge( $backend->doPrepare( $realParams ) );
334 }
335 return $status;
336 }
337
338 /**
339 * @see FileBackend::doSecure()
340 * @param $params array
341 * @return Status
342 */
343 protected function doSecure( array $params ) {
344 $status = Status::newGood();
345 foreach ( $this->backends as $backend ) {
346 $realParams = $this->substOpPaths( $params, $backend );
347 $status->merge( $backend->doSecure( $realParams ) );
348 }
349 return $status;
350 }
351
352 /**
353 * @see FileBackend::doPublish()
354 * @param $params array
355 * @return Status
356 */
357 protected function doPublish( array $params ) {
358 $status = Status::newGood();
359 foreach ( $this->backends as $backend ) {
360 $realParams = $this->substOpPaths( $params, $backend );
361 $status->merge( $backend->doPublish( $realParams ) );
362 }
363 return $status;
364 }
365
366 /**
367 * @see FileBackend::doClean()
368 * @param $params array
369 * @return Status
370 */
371 protected function doClean( array $params ) {
372 $status = Status::newGood();
373 foreach ( $this->backends as $backend ) {
374 $realParams = $this->substOpPaths( $params, $backend );
375 $status->merge( $backend->doClean( $realParams ) );
376 }
377 return $status;
378 }
379
380 /**
381 * @see FileBackend::concatenate()
382 * @param $params array
383 * @return Status
384 */
385 public function concatenate( array $params ) {
386 // We are writing to an FS file, so we don't need to do this per-backend
387 $realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] );
388 return $this->backends[$this->masterIndex]->concatenate( $realParams );
389 }
390
391 /**
392 * @see FileBackend::fileExists()
393 * @param $params array
394 */
395 public function fileExists( array $params ) {
396 $realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] );
397 return $this->backends[$this->masterIndex]->fileExists( $realParams );
398 }
399
400 /**
401 * @see FileBackend::getFileTimestamp()
402 * @param $params array
403 * @return bool|string
404 */
405 public function getFileTimestamp( array $params ) {
406 $realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] );
407 return $this->backends[$this->masterIndex]->getFileTimestamp( $realParams );
408 }
409
410 /**
411 * @see FileBackend::getFileSize()
412 * @param $params array
413 * @return bool|int
414 */
415 public function getFileSize( array $params ) {
416 $realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] );
417 return $this->backends[$this->masterIndex]->getFileSize( $realParams );
418 }
419
420 /**
421 * @see FileBackend::getFileStat()
422 * @param $params array
423 * @return Array|bool|null
424 */
425 public function getFileStat( array $params ) {
426 $realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] );
427 return $this->backends[$this->masterIndex]->getFileStat( $realParams );
428 }
429
430 /**
431 * @see FileBackend::getFileContents()
432 * @param $params array
433 * @return bool|string
434 */
435 public function getFileContents( array $params ) {
436 $realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] );
437 return $this->backends[$this->masterIndex]->getFileContents( $realParams );
438 }
439
440 /**
441 * @see FileBackend::getFileSha1Base36()
442 * @param $params array
443 * @return bool|string
444 */
445 public function getFileSha1Base36( array $params ) {
446 $realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] );
447 return $this->backends[$this->masterIndex]->getFileSha1Base36( $realParams );
448 }
449
450 /**
451 * @see FileBackend::getFileProps()
452 * @param $params array
453 * @return Array
454 */
455 public function getFileProps( array $params ) {
456 $realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] );
457 return $this->backends[$this->masterIndex]->getFileProps( $realParams );
458 }
459
460 /**
461 * @see FileBackend::streamFile()
462 * @param $params array
463 * @return \Status
464 */
465 public function streamFile( array $params ) {
466 $realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] );
467 return $this->backends[$this->masterIndex]->streamFile( $realParams );
468 }
469
470 /**
471 * @see FileBackend::getLocalReference()
472 * @param $params array
473 * @return FSFile|null
474 */
475 public function getLocalReference( array $params ) {
476 $realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] );
477 return $this->backends[$this->masterIndex]->getLocalReference( $realParams );
478 }
479
480 /**
481 * @see FileBackend::getLocalCopy()
482 * @param $params array
483 * @return null|TempFSFile
484 */
485 public function getLocalCopy( array $params ) {
486 $realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] );
487 return $this->backends[$this->masterIndex]->getLocalCopy( $realParams );
488 }
489
490 /**
491 * @see FileBackend::directoryExists()
492 * @param $params array
493 * @return bool|null
494 */
495 public function directoryExists( array $params ) {
496 $realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] );
497 return $this->backends[$this->masterIndex]->directoryExists( $realParams );
498 }
499
500 /**
501 * @see FileBackend::getSubdirectoryList()
502 * @param $params array
503 * @return Array|null|Traversable
504 */
505 public function getDirectoryList( array $params ) {
506 $realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] );
507 return $this->backends[$this->masterIndex]->getDirectoryList( $realParams );
508 }
509
510 /**
511 * @see FileBackend::getFileList()
512 * @param $params array
513 * @return Array|null|\Traversable
514 */
515 public function getFileList( array $params ) {
516 $realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] );
517 return $this->backends[$this->masterIndex]->getFileList( $realParams );
518 }
519
520 /**
521 * @see FileBackend::clearCache()
522 */
523 public function clearCache( array $paths = null ) {
524 foreach ( $this->backends as $backend ) {
525 $realPaths = is_array( $paths ) ? $this->substPaths( $paths, $backend ) : null;
526 $backend->clearCache( $realPaths );
527 }
528 }
529
530 /**
531 * @see FileBackend::getScopedLocksForOps()
532 */
533 public function getScopedLocksForOps( array $ops, Status $status ) {
534 $fileOps = $this->backends[$this->masterIndex]->getOperationsInternal( $ops );
535 // Get the paths to lock from the master backend
536 $paths = $this->backends[$this->masterIndex]->getPathsToLockForOpsInternal( $fileOps );
537 // Get the paths under the proxy backend's name
538 $paths['sh'] = $this->unsubstPaths( $paths['sh'] );
539 $paths['ex'] = $this->unsubstPaths( $paths['ex'] );
540 return array(
541 $this->getScopedFileLocks( $paths['sh'], LockManager::LOCK_UW, $status ),
542 $this->getScopedFileLocks( $paths['ex'], LockManager::LOCK_EX, $status )
543 );
544 }
545 }