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