Merge "Fix UID test and make debugging easier"
[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, starting with the master.
37 * This makes a best-effort to have transactional semantics, but since requests
38 * may sometimes fail, the use of "autoResync" or background scripts to fix
39 * inconsistencies is important.
40 *
41 * @ingroup FileBackend
42 * @since 1.19
43 */
44 class FileBackendMultiWrite extends FileBackend {
45 /** @var FileBackendStore[] Prioritized list of FileBackendStore objects */
46 protected $backends = [];
47
48 /** @var int Index of master backend */
49 protected $masterIndex = -1;
50 /** @var int Index of read affinity backend */
51 protected $readIndex = -1;
52
53 /** @var int Bitfield */
54 protected $syncChecks = 0;
55 /** @var string|bool */
56 protected $autoResync = false;
57
58 /** @var bool */
59 protected $asyncWrites = false;
60
61 /* Possible internal backend consistency checks */
62 const CHECK_SIZE = 1;
63 const CHECK_TIME = 2;
64 const CHECK_SHA1 = 4;
65
66 /**
67 * Construct a proxy backend that consists of several internal backends.
68 * Locking, journaling, and read-only checks are handled by the proxy backend.
69 *
70 * Additional $config params include:
71 * - backends : Array of backend config and multi-backend settings.
72 * Each value is the config used in the constructor of a
73 * FileBackendStore class, but with these additional settings:
74 * - class : The name of the backend class
75 * - isMultiMaster : This must be set for one backend.
76 * - readAffinity : Use this for reads without 'latest' set.
77 * - template: : If given a backend name, this will use
78 * the config of that backend as a template.
79 * Values specified here take precedence.
80 * - syncChecks : Integer bitfield of internal backend sync checks to perform.
81 * Possible bits include the FileBackendMultiWrite::CHECK_* constants.
82 * There are constants for SIZE, TIME, and SHA1.
83 * The checks are done before allowing any file operations.
84 * - autoResync : Automatically resync the clone backends to the master backend
85 * when pre-operation sync checks fail. This should only be used
86 * if the master backend is stable and not missing any files.
87 * Use "conservative" to limit resyncing to copying newer master
88 * backend files over older (or non-existing) clone backend files.
89 * Cases that cannot be handled will result in operation abortion.
90 * - replication : Set to 'async' to defer file operations on the non-master backends.
91 * This will apply such updates post-send for web requests. Note that
92 * any checks from "syncChecks" are still synchronous.
93 *
94 * @param array $config
95 * @throws FileBackendError
96 */
97 public function __construct( array $config ) {
98 parent::__construct( $config );
99 $this->syncChecks = isset( $config['syncChecks'] )
100 ? $config['syncChecks']
101 : self::CHECK_SIZE;
102 $this->autoResync = isset( $config['autoResync'] )
103 ? $config['autoResync']
104 : false;
105 $this->asyncWrites = isset( $config['replication'] ) && $config['replication'] === 'async';
106 // Construct backends here rather than via registration
107 // to keep these backends hidden from outside the proxy.
108 $namesUsed = [];
109 foreach ( $config['backends'] as $index => $config ) {
110 if ( isset( $config['template'] ) ) {
111 // Config is just a modified version of a registered backend's.
112 // This should only be used when that config is used only by this backend.
113 $config = $config + FileBackendGroup::singleton()->config( $config['template'] );
114 }
115 $name = $config['name'];
116 if ( isset( $namesUsed[$name] ) ) { // don't break FileOp predicates
117 throw new FileBackendError( "Two or more backends defined with the name $name." );
118 }
119 $namesUsed[$name] = 1;
120 // Alter certain sub-backend settings for sanity
121 unset( $config['readOnly'] ); // use proxy backend setting
122 unset( $config['fileJournal'] ); // use proxy backend journal
123 unset( $config['lockManager'] ); // lock under proxy backend
124 $config['wikiId'] = $this->wikiId; // use the proxy backend wiki ID
125 if ( !empty( $config['isMultiMaster'] ) ) {
126 if ( $this->masterIndex >= 0 ) {
127 throw new FileBackendError( 'More than one master backend defined.' );
128 }
129 $this->masterIndex = $index; // this is the "master"
130 $config['fileJournal'] = $this->fileJournal; // log under proxy backend
131 }
132 if ( !empty( $config['readAffinity'] ) ) {
133 $this->readIndex = $index; // prefer this for reads
134 }
135 // Create sub-backend object
136 if ( !isset( $config['class'] ) ) {
137 throw new FileBackendError( 'No class given for a backend config.' );
138 }
139 $class = $config['class'];
140 $this->backends[$index] = new $class( $config );
141 }
142 if ( $this->masterIndex < 0 ) { // need backends and must have a master
143 throw new FileBackendError( 'No master backend defined.' );
144 }
145 if ( $this->readIndex < 0 ) {
146 $this->readIndex = $this->masterIndex; // default
147 }
148 }
149
150 final protected function doOperationsInternal( array $ops, array $opts ) {
151 $status = Status::newGood();
152
153 $mbe = $this->backends[$this->masterIndex]; // convenience
154
155 // Try to lock those files for the scope of this function...
156 $scopeLock = null;
157 if ( empty( $opts['nonLocking'] ) ) {
158 // Try to lock those files for the scope of this function...
159 /** @noinspection PhpUnusedLocalVariableInspection */
160 $scopeLock = $this->getScopedLocksForOps( $ops, $status );
161 if ( !$status->isOK() ) {
162 return $status; // abort
163 }
164 }
165 // Clear any cache entries (after locks acquired)
166 $this->clearCache();
167 $opts['preserveCache'] = true; // only locked files are cached
168 // Get the list of paths to read/write...
169 $relevantPaths = $this->fileStoragePathsForOps( $ops );
170 // Check if the paths are valid and accessible on all backends...
171 $status->merge( $this->accessibilityCheck( $relevantPaths ) );
172 if ( !$status->isOK() ) {
173 return $status; // abort
174 }
175 // Do a consistency check to see if the backends are consistent...
176 $syncStatus = $this->consistencyCheck( $relevantPaths );
177 if ( !$syncStatus->isOK() ) {
178 wfDebugLog( 'FileOperation', get_class( $this ) .
179 " failed sync check: " . FormatJson::encode( $relevantPaths ) );
180 // Try to resync the clone backends to the master on the spot...
181 if ( $this->autoResync === false
182 || !$this->resyncFiles( $relevantPaths, $this->autoResync )->isOK()
183 ) {
184 $status->merge( $syncStatus );
185
186 return $status; // abort
187 }
188 }
189 // Actually attempt the operation batch on the master backend...
190 $realOps = $this->substOpBatchPaths( $ops, $mbe );
191 $masterStatus = $mbe->doOperations( $realOps, $opts );
192 $status->merge( $masterStatus );
193 // Propagate the operations to the clone backends if there were no unexpected errors
194 // and if there were either no expected errors or if the 'force' option was used.
195 // However, if nothing succeeded at all, then don't replicate any of the operations.
196 // If $ops only had one operation, this might avoid backend sync inconsistencies.
197 if ( $masterStatus->isOK() && $masterStatus->successCount > 0 ) {
198 foreach ( $this->backends as $index => $backend ) {
199 if ( $index === $this->masterIndex ) {
200 continue; // done already
201 }
202
203 $realOps = $this->substOpBatchPaths( $ops, $backend );
204 if ( $this->asyncWrites && !$this->hasVolatileSources( $ops ) ) {
205 // Bind $scopeLock to the callback to preserve locks
206 DeferredUpdates::addCallableUpdate(
207 function() use ( $backend, $realOps, $opts, $scopeLock, $relevantPaths ) {
208 wfDebugLog( 'FileOperationReplication',
209 "'{$backend->getName()}' async replication; paths: " .
210 FormatJson::encode( $relevantPaths ) );
211 $backend->doOperations( $realOps, $opts );
212 }
213 );
214 } else {
215 wfDebugLog( 'FileOperationReplication',
216 "'{$backend->getName()}' sync replication; paths: " .
217 FormatJson::encode( $relevantPaths ) );
218 $status->merge( $backend->doOperations( $realOps, $opts ) );
219 }
220 }
221 }
222 // Make 'success', 'successCount', and 'failCount' fields reflect
223 // the overall operation, rather than all the batches for each backend.
224 // Do this by only using success values from the master backend's batch.
225 $status->success = $masterStatus->success;
226 $status->successCount = $masterStatus->successCount;
227 $status->failCount = $masterStatus->failCount;
228
229 return $status;
230 }
231
232 /**
233 * Check that a set of files are consistent across all internal backends
234 *
235 * @param array $paths List of storage paths
236 * @return Status
237 */
238 public function consistencyCheck( array $paths ) {
239 $status = Status::newGood();
240 if ( $this->syncChecks == 0 || count( $this->backends ) <= 1 ) {
241 return $status; // skip checks
242 }
243
244 $mBackend = $this->backends[$this->masterIndex];
245 foreach ( $paths as $path ) {
246 $params = [ 'src' => $path, 'latest' => true ];
247 $mParams = $this->substOpPaths( $params, $mBackend );
248 // Stat the file on the 'master' backend
249 $mStat = $mBackend->getFileStat( $mParams );
250 if ( $this->syncChecks & self::CHECK_SHA1 ) {
251 $mSha1 = $mBackend->getFileSha1Base36( $mParams );
252 } else {
253 $mSha1 = false;
254 }
255 // Check if all clone backends agree with the master...
256 foreach ( $this->backends as $index => $cBackend ) {
257 if ( $index === $this->masterIndex ) {
258 continue; // master
259 }
260 $cParams = $this->substOpPaths( $params, $cBackend );
261 $cStat = $cBackend->getFileStat( $cParams );
262 if ( $mStat ) { // file is in master
263 if ( !$cStat ) { // file should exist
264 $status->fatal( 'backend-fail-synced', $path );
265 continue;
266 }
267 if ( $this->syncChecks & self::CHECK_SIZE ) {
268 if ( $cStat['size'] != $mStat['size'] ) { // wrong size
269 $status->fatal( 'backend-fail-synced', $path );
270 continue;
271 }
272 }
273 if ( $this->syncChecks & self::CHECK_TIME ) {
274 $mTs = wfTimestamp( TS_UNIX, $mStat['mtime'] );
275 $cTs = wfTimestamp( TS_UNIX, $cStat['mtime'] );
276 if ( abs( $mTs - $cTs ) > 30 ) { // outdated file somewhere
277 $status->fatal( 'backend-fail-synced', $path );
278 continue;
279 }
280 }
281 if ( $this->syncChecks & self::CHECK_SHA1 ) {
282 if ( $cBackend->getFileSha1Base36( $cParams ) !== $mSha1 ) { // wrong SHA1
283 $status->fatal( 'backend-fail-synced', $path );
284 continue;
285 }
286 }
287 } else { // file is not in master
288 if ( $cStat ) { // file should not exist
289 $status->fatal( 'backend-fail-synced', $path );
290 }
291 }
292 }
293 }
294
295 return $status;
296 }
297
298 /**
299 * Check that a set of file paths are usable across all internal backends
300 *
301 * @param array $paths List of storage paths
302 * @return Status
303 */
304 public function accessibilityCheck( array $paths ) {
305 $status = Status::newGood();
306 if ( count( $this->backends ) <= 1 ) {
307 return $status; // skip checks
308 }
309
310 foreach ( $paths as $path ) {
311 foreach ( $this->backends as $backend ) {
312 $realPath = $this->substPaths( $path, $backend );
313 if ( !$backend->isPathUsableInternal( $realPath ) ) {
314 $status->fatal( 'backend-fail-usable', $path );
315 }
316 }
317 }
318
319 return $status;
320 }
321
322 /**
323 * Check that a set of files are consistent across all internal backends
324 * and re-synchronize those files against the "multi master" if needed.
325 *
326 * @param array $paths List of storage paths
327 * @param string|bool $resyncMode False, True, or "conservative"; see __construct()
328 * @return Status
329 */
330 public function resyncFiles( array $paths, $resyncMode = true ) {
331 $status = Status::newGood();
332
333 $mBackend = $this->backends[$this->masterIndex];
334 foreach ( $paths as $path ) {
335 $mPath = $this->substPaths( $path, $mBackend );
336 $mSha1 = $mBackend->getFileSha1Base36( [ 'src' => $mPath, 'latest' => true ] );
337 $mStat = $mBackend->getFileStat( [ 'src' => $mPath, 'latest' => true ] );
338 if ( $mStat === null || ( $mSha1 !== false && !$mStat ) ) { // sanity
339 $status->fatal( 'backend-fail-internal', $this->name );
340 wfDebugLog( 'FileOperation', __METHOD__
341 . ': File is not available on the master backend' );
342 continue; // file is not available on the master backend...
343 }
344 // Check of all clone backends agree with the master...
345 foreach ( $this->backends as $index => $cBackend ) {
346 if ( $index === $this->masterIndex ) {
347 continue; // master
348 }
349 $cPath = $this->substPaths( $path, $cBackend );
350 $cSha1 = $cBackend->getFileSha1Base36( [ 'src' => $cPath, 'latest' => true ] );
351 $cStat = $cBackend->getFileStat( [ 'src' => $cPath, 'latest' => true ] );
352 if ( $cStat === null || ( $cSha1 !== false && !$cStat ) ) { // sanity
353 $status->fatal( 'backend-fail-internal', $cBackend->getName() );
354 wfDebugLog( 'FileOperation', __METHOD__ .
355 ': File is not available on the clone backend' );
356 continue; // file is not available on the clone backend...
357 }
358 if ( $mSha1 === $cSha1 ) {
359 // already synced; nothing to do
360 } elseif ( $mSha1 !== false ) { // file is in master
361 if ( $resyncMode === 'conservative'
362 && $cStat && $cStat['mtime'] > $mStat['mtime']
363 ) {
364 $status->fatal( 'backend-fail-synced', $path );
365 continue; // don't rollback data
366 }
367 $fsFile = $mBackend->getLocalReference(
368 [ 'src' => $mPath, 'latest' => true ] );
369 $status->merge( $cBackend->quickStore(
370 [ 'src' => $fsFile->getPath(), 'dst' => $cPath ]
371 ) );
372 } elseif ( $mStat === false ) { // file is not in master
373 if ( $resyncMode === 'conservative' ) {
374 $status->fatal( 'backend-fail-synced', $path );
375 continue; // don't delete data
376 }
377 $status->merge( $cBackend->quickDelete( [ 'src' => $cPath ] ) );
378 }
379 }
380 }
381
382 return $status;
383 }
384
385 /**
386 * Get a list of file storage paths to read or write for a list of operations
387 *
388 * @param array $ops Same format as doOperations()
389 * @return array List of storage paths to files (does not include directories)
390 */
391 protected function fileStoragePathsForOps( array $ops ) {
392 $paths = [];
393 foreach ( $ops as $op ) {
394 if ( isset( $op['src'] ) ) {
395 // For things like copy/move/delete with "ignoreMissingSource" and there
396 // is no source file, nothing should happen and there should be no errors.
397 if ( empty( $op['ignoreMissingSource'] )
398 || $this->fileExists( [ 'src' => $op['src'] ] )
399 ) {
400 $paths[] = $op['src'];
401 }
402 }
403 if ( isset( $op['srcs'] ) ) {
404 $paths = array_merge( $paths, $op['srcs'] );
405 }
406 if ( isset( $op['dst'] ) ) {
407 $paths[] = $op['dst'];
408 }
409 }
410
411 return array_values( array_unique( array_filter( $paths, 'FileBackend::isStoragePath' ) ) );
412 }
413
414 /**
415 * Substitute the backend name in storage path parameters
416 * for a set of operations with that of a given internal backend.
417 *
418 * @param array $ops List of file operation arrays
419 * @param FileBackendStore $backend
420 * @return array
421 */
422 protected function substOpBatchPaths( array $ops, FileBackendStore $backend ) {
423 $newOps = []; // operations
424 foreach ( $ops as $op ) {
425 $newOp = $op; // operation
426 foreach ( [ 'src', 'srcs', 'dst', 'dir' ] as $par ) {
427 if ( isset( $newOp[$par] ) ) { // string or array
428 $newOp[$par] = $this->substPaths( $newOp[$par], $backend );
429 }
430 }
431 $newOps[] = $newOp;
432 }
433
434 return $newOps;
435 }
436
437 /**
438 * Same as substOpBatchPaths() but for a single operation
439 *
440 * @param array $ops File operation array
441 * @param FileBackendStore $backend
442 * @return array
443 */
444 protected function substOpPaths( array $ops, FileBackendStore $backend ) {
445 $newOps = $this->substOpBatchPaths( [ $ops ], $backend );
446
447 return $newOps[0];
448 }
449
450 /**
451 * Substitute the backend of storage paths with an internal backend's name
452 *
453 * @param array|string $paths List of paths or single string path
454 * @param FileBackendStore $backend
455 * @return array|string
456 */
457 protected function substPaths( $paths, FileBackendStore $backend ) {
458 return preg_replace(
459 '!^mwstore://' . preg_quote( $this->name, '!' ) . '/!',
460 StringUtils::escapeRegexReplacement( "mwstore://{$backend->getName()}/" ),
461 $paths // string or array
462 );
463 }
464
465 /**
466 * Substitute the backend of internal storage paths with the proxy backend's name
467 *
468 * @param array|string $paths List of paths or single string path
469 * @return array|string
470 */
471 protected function unsubstPaths( $paths ) {
472 return preg_replace(
473 '!^mwstore://([^/]+)!',
474 StringUtils::escapeRegexReplacement( "mwstore://{$this->name}" ),
475 $paths // string or array
476 );
477 }
478
479 /**
480 * @param array $ops File operations for FileBackend::doOperations()
481 * @return bool Whether there are file path sources with outside lifetime/ownership
482 */
483 protected function hasVolatileSources( array $ops ) {
484 foreach ( $ops as $op ) {
485 if ( $op['op'] === 'store' && !isset( $op['srcRef'] ) ) {
486 return true; // source file might be deleted anytime after do*Operations()
487 }
488 }
489
490 return false;
491 }
492
493 protected function doQuickOperationsInternal( array $ops ) {
494 $status = Status::newGood();
495 // Do the operations on the master backend; setting Status fields...
496 $realOps = $this->substOpBatchPaths( $ops, $this->backends[$this->masterIndex] );
497 $masterStatus = $this->backends[$this->masterIndex]->doQuickOperations( $realOps );
498 $status->merge( $masterStatus );
499 // Propagate the operations to the clone backends...
500 foreach ( $this->backends as $index => $backend ) {
501 if ( $index === $this->masterIndex ) {
502 continue; // done already
503 }
504
505 $realOps = $this->substOpBatchPaths( $ops, $backend );
506 if ( $this->asyncWrites && !$this->hasVolatileSources( $ops ) ) {
507 DeferredUpdates::addCallableUpdate(
508 function() use ( $backend, $realOps ) {
509 $backend->doQuickOperations( $realOps );
510 }
511 );
512 } else {
513 $status->merge( $backend->doQuickOperations( $realOps ) );
514 }
515 }
516 // Make 'success', 'successCount', and 'failCount' fields reflect
517 // the overall operation, rather than all the batches for each backend.
518 // Do this by only using success values from the master backend's batch.
519 $status->success = $masterStatus->success;
520 $status->successCount = $masterStatus->successCount;
521 $status->failCount = $masterStatus->failCount;
522
523 return $status;
524 }
525
526 protected function doPrepare( array $params ) {
527 return $this->doDirectoryOp( 'prepare', $params );
528 }
529
530 protected function doSecure( array $params ) {
531 return $this->doDirectoryOp( 'secure', $params );
532 }
533
534 protected function doPublish( array $params ) {
535 return $this->doDirectoryOp( 'publish', $params );
536 }
537
538 protected function doClean( array $params ) {
539 return $this->doDirectoryOp( 'clean', $params );
540 }
541
542 /**
543 * @param string $method One of (doPrepare,doSecure,doPublish,doClean)
544 * @param array $params Method arguments
545 * @return Status
546 */
547 protected function doDirectoryOp( $method, array $params ) {
548 $status = Status::newGood();
549
550 $realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] );
551 $masterStatus = $this->backends[$this->masterIndex]->$method( $realParams );
552 $status->merge( $masterStatus );
553
554 foreach ( $this->backends as $index => $backend ) {
555 if ( $index === $this->masterIndex ) {
556 continue; // already done
557 }
558
559 $realParams = $this->substOpPaths( $params, $backend );
560 if ( $this->asyncWrites ) {
561 DeferredUpdates::addCallableUpdate(
562 function() use ( $backend, $method, $realParams ) {
563 $backend->$method( $realParams );
564 }
565 );
566 } else {
567 $status->merge( $backend->$method( $realParams ) );
568 }
569 }
570
571 return $status;
572 }
573
574 public function concatenate( array $params ) {
575 // We are writing to an FS file, so we don't need to do this per-backend
576 $index = $this->getReadIndexFromParams( $params );
577 $realParams = $this->substOpPaths( $params, $this->backends[$index] );
578
579 return $this->backends[$index]->concatenate( $realParams );
580 }
581
582 public function fileExists( array $params ) {
583 $index = $this->getReadIndexFromParams( $params );
584 $realParams = $this->substOpPaths( $params, $this->backends[$index] );
585
586 return $this->backends[$index]->fileExists( $realParams );
587 }
588
589 public function getFileTimestamp( array $params ) {
590 $index = $this->getReadIndexFromParams( $params );
591 $realParams = $this->substOpPaths( $params, $this->backends[$index] );
592
593 return $this->backends[$index]->getFileTimestamp( $realParams );
594 }
595
596 public function getFileSize( array $params ) {
597 $index = $this->getReadIndexFromParams( $params );
598 $realParams = $this->substOpPaths( $params, $this->backends[$index] );
599
600 return $this->backends[$index]->getFileSize( $realParams );
601 }
602
603 public function getFileStat( array $params ) {
604 $index = $this->getReadIndexFromParams( $params );
605 $realParams = $this->substOpPaths( $params, $this->backends[$index] );
606
607 return $this->backends[$index]->getFileStat( $realParams );
608 }
609
610 public function getFileXAttributes( array $params ) {
611 $index = $this->getReadIndexFromParams( $params );
612 $realParams = $this->substOpPaths( $params, $this->backends[$index] );
613
614 return $this->backends[$index]->getFileXAttributes( $realParams );
615 }
616
617 public function getFileContentsMulti( array $params ) {
618 $index = $this->getReadIndexFromParams( $params );
619 $realParams = $this->substOpPaths( $params, $this->backends[$index] );
620
621 $contentsM = $this->backends[$index]->getFileContentsMulti( $realParams );
622
623 $contents = []; // (path => FSFile) mapping using the proxy backend's name
624 foreach ( $contentsM as $path => $data ) {
625 $contents[$this->unsubstPaths( $path )] = $data;
626 }
627
628 return $contents;
629 }
630
631 public function getFileSha1Base36( array $params ) {
632 $index = $this->getReadIndexFromParams( $params );
633 $realParams = $this->substOpPaths( $params, $this->backends[$index] );
634
635 return $this->backends[$index]->getFileSha1Base36( $realParams );
636 }
637
638 public function getFileProps( array $params ) {
639 $index = $this->getReadIndexFromParams( $params );
640 $realParams = $this->substOpPaths( $params, $this->backends[$index] );
641
642 return $this->backends[$index]->getFileProps( $realParams );
643 }
644
645 public function streamFile( array $params ) {
646 $index = $this->getReadIndexFromParams( $params );
647 $realParams = $this->substOpPaths( $params, $this->backends[$index] );
648
649 return $this->backends[$index]->streamFile( $realParams );
650 }
651
652 public function getLocalReferenceMulti( array $params ) {
653 $index = $this->getReadIndexFromParams( $params );
654 $realParams = $this->substOpPaths( $params, $this->backends[$index] );
655
656 $fsFilesM = $this->backends[$index]->getLocalReferenceMulti( $realParams );
657
658 $fsFiles = []; // (path => FSFile) mapping using the proxy backend's name
659 foreach ( $fsFilesM as $path => $fsFile ) {
660 $fsFiles[$this->unsubstPaths( $path )] = $fsFile;
661 }
662
663 return $fsFiles;
664 }
665
666 public function getLocalCopyMulti( array $params ) {
667 $index = $this->getReadIndexFromParams( $params );
668 $realParams = $this->substOpPaths( $params, $this->backends[$index] );
669
670 $tempFilesM = $this->backends[$index]->getLocalCopyMulti( $realParams );
671
672 $tempFiles = []; // (path => TempFSFile) mapping using the proxy backend's name
673 foreach ( $tempFilesM as $path => $tempFile ) {
674 $tempFiles[$this->unsubstPaths( $path )] = $tempFile;
675 }
676
677 return $tempFiles;
678 }
679
680 public function getFileHttpUrl( array $params ) {
681 $index = $this->getReadIndexFromParams( $params );
682 $realParams = $this->substOpPaths( $params, $this->backends[$index] );
683
684 return $this->backends[$index]->getFileHttpUrl( $realParams );
685 }
686
687 public function directoryExists( array $params ) {
688 $realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] );
689
690 return $this->backends[$this->masterIndex]->directoryExists( $realParams );
691 }
692
693 public function getDirectoryList( array $params ) {
694 $realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] );
695
696 return $this->backends[$this->masterIndex]->getDirectoryList( $realParams );
697 }
698
699 public function getFileList( array $params ) {
700 $realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] );
701
702 return $this->backends[$this->masterIndex]->getFileList( $realParams );
703 }
704
705 public function getFeatures() {
706 return $this->backends[$this->masterIndex]->getFeatures();
707 }
708
709 public function clearCache( array $paths = null ) {
710 foreach ( $this->backends as $backend ) {
711 $realPaths = is_array( $paths ) ? $this->substPaths( $paths, $backend ) : null;
712 $backend->clearCache( $realPaths );
713 }
714 }
715
716 public function preloadCache( array $paths ) {
717 $realPaths = $this->substPaths( $paths, $this->backends[$this->readIndex] );
718 $this->backends[$this->readIndex]->preloadCache( $realPaths );
719 }
720
721 public function preloadFileStat( array $params ) {
722 $index = $this->getReadIndexFromParams( $params );
723 $realParams = $this->substOpPaths( $params, $this->backends[$index] );
724
725 return $this->backends[$index]->preloadFileStat( $realParams );
726 }
727
728 public function getScopedLocksForOps( array $ops, Status $status ) {
729 $realOps = $this->substOpBatchPaths( $ops, $this->backends[$this->masterIndex] );
730 $fileOps = $this->backends[$this->masterIndex]->getOperationsInternal( $realOps );
731 // Get the paths to lock from the master backend
732 $paths = $this->backends[$this->masterIndex]->getPathsToLockForOpsInternal( $fileOps );
733 // Get the paths under the proxy backend's name
734 $pbPaths = [
735 LockManager::LOCK_UW => $this->unsubstPaths( $paths[LockManager::LOCK_UW] ),
736 LockManager::LOCK_EX => $this->unsubstPaths( $paths[LockManager::LOCK_EX] )
737 ];
738
739 // Actually acquire the locks
740 return $this->getScopedFileLocks( $pbPaths, 'mixed', $status );
741 }
742
743 /**
744 * @param array $params
745 * @return int The master or read affinity backend index, based on $params['latest']
746 */
747 protected function getReadIndexFromParams( array $params ) {
748 return !empty( $params['latest'] ) ? $this->masterIndex : $this->readIndex;
749 }
750 }