Merge "Special:Upload should not crash on failing previews"
[mediawiki.git] / includes / libs / filebackend / FileBackendMultiWrite.php
blob212e84f026285f464d304c36a4308ed34463ce7b
1 <?php
2 /**
3 * Proxy backend that mirrors writes to several internal backends.
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.
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.
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
20 * @file
21 * @ingroup FileBackend
22 * @author Aaron Schulz
25 /**
26 * @brief Proxy backend that mirrors writes to several internal backends.
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.
33 * Only use this class when transitioning from one storage system to another.
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.
41 * @ingroup FileBackend
42 * @since 1.19
44 class FileBackendMultiWrite extends FileBackend {
45 /** @var FileBackendStore[] Prioritized list of FileBackendStore objects */
46 protected $backends = [];
48 /** @var int Index of master backend */
49 protected $masterIndex = -1;
50 /** @var int Index of read affinity backend */
51 protected $readIndex = -1;
53 /** @var int Bitfield */
54 protected $syncChecks = 0;
55 /** @var string|bool */
56 protected $autoResync = false;
58 /** @var bool */
59 protected $asyncWrites = false;
61 /* Possible internal backend consistency checks */
62 const CHECK_SIZE = 1;
63 const CHECK_TIME = 2;
64 const CHECK_SHA1 = 4;
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.
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 * - syncChecks : Integer bitfield of internal backend sync checks to perform.
78 * Possible bits include the FileBackendMultiWrite::CHECK_* constants.
79 * There are constants for SIZE, TIME, and SHA1.
80 * The checks are done before allowing any file operations.
81 * - autoResync : Automatically resync the clone backends to the master backend
82 * when pre-operation sync checks fail. This should only be used
83 * if the master backend is stable and not missing any files.
84 * Use "conservative" to limit resyncing to copying newer master
85 * backend files over older (or non-existing) clone backend files.
86 * Cases that cannot be handled will result in operation abortion.
87 * - replication : Set to 'async' to defer file operations on the non-master backends.
88 * This will apply such updates post-send for web requests. Note that
89 * any checks from "syncChecks" are still synchronous.
91 * @param array $config
92 * @throws FileBackendError
94 public function __construct( array $config ) {
95 parent::__construct( $config );
96 $this->syncChecks = isset( $config['syncChecks'] )
97 ? $config['syncChecks']
98 : self::CHECK_SIZE;
99 $this->autoResync = isset( $config['autoResync'] )
100 ? $config['autoResync']
101 : false;
102 $this->asyncWrites = isset( $config['replication'] ) && $config['replication'] === 'async';
103 // Construct backends here rather than via registration
104 // to keep these backends hidden from outside the proxy.
105 $namesUsed = [];
106 foreach ( $config['backends'] as $index => $config ) {
107 $name = $config['name'];
108 if ( isset( $namesUsed[$name] ) ) { // don't break FileOp predicates
109 throw new LogicException( "Two or more backends defined with the name $name." );
111 $namesUsed[$name] = 1;
112 // Alter certain sub-backend settings for sanity
113 unset( $config['readOnly'] ); // use proxy backend setting
114 unset( $config['fileJournal'] ); // use proxy backend journal
115 unset( $config['lockManager'] ); // lock under proxy backend
116 $config['domainId'] = $this->domainId; // use the proxy backend wiki ID
117 if ( !empty( $config['isMultiMaster'] ) ) {
118 if ( $this->masterIndex >= 0 ) {
119 throw new LogicException( 'More than one master backend defined.' );
121 $this->masterIndex = $index; // this is the "master"
122 $config['fileJournal'] = $this->fileJournal; // log under proxy backend
124 if ( !empty( $config['readAffinity'] ) ) {
125 $this->readIndex = $index; // prefer this for reads
127 // Create sub-backend object
128 if ( !isset( $config['class'] ) ) {
129 throw new InvalidArgumentException( 'No class given for a backend config.' );
131 $class = $config['class'];
132 $this->backends[$index] = new $class( $config );
134 if ( $this->masterIndex < 0 ) { // need backends and must have a master
135 throw new LogicException( 'No master backend defined.' );
137 if ( $this->readIndex < 0 ) {
138 $this->readIndex = $this->masterIndex; // default
142 final protected function doOperationsInternal( array $ops, array $opts ) {
143 $status = $this->newStatus();
145 $mbe = $this->backends[$this->masterIndex]; // convenience
147 // Try to lock those files for the scope of this function...
148 $scopeLock = null;
149 if ( empty( $opts['nonLocking'] ) ) {
150 // Try to lock those files for the scope of this function...
151 /** @noinspection PhpUnusedLocalVariableInspection */
152 $scopeLock = $this->getScopedLocksForOps( $ops, $status );
153 if ( !$status->isOK() ) {
154 return $status; // abort
157 // Clear any cache entries (after locks acquired)
158 $this->clearCache();
159 $opts['preserveCache'] = true; // only locked files are cached
160 // Get the list of paths to read/write...
161 $relevantPaths = $this->fileStoragePathsForOps( $ops );
162 // Check if the paths are valid and accessible on all backends...
163 $status->merge( $this->accessibilityCheck( $relevantPaths ) );
164 if ( !$status->isOK() ) {
165 return $status; // abort
167 // Do a consistency check to see if the backends are consistent...
168 $syncStatus = $this->consistencyCheck( $relevantPaths );
169 if ( !$syncStatus->isOK() ) {
170 wfDebugLog( 'FileOperation', get_class( $this ) .
171 " failed sync check: " . FormatJson::encode( $relevantPaths ) );
172 // Try to resync the clone backends to the master on the spot...
173 if ( $this->autoResync === false
174 || !$this->resyncFiles( $relevantPaths, $this->autoResync )->isOK()
176 $status->merge( $syncStatus );
178 return $status; // abort
181 // Actually attempt the operation batch on the master backend...
182 $realOps = $this->substOpBatchPaths( $ops, $mbe );
183 $masterStatus = $mbe->doOperations( $realOps, $opts );
184 $status->merge( $masterStatus );
185 // Propagate the operations to the clone backends if there were no unexpected errors
186 // and if there were either no expected errors or if the 'force' option was used.
187 // However, if nothing succeeded at all, then don't replicate any of the operations.
188 // If $ops only had one operation, this might avoid backend sync inconsistencies.
189 if ( $masterStatus->isOK() && $masterStatus->successCount > 0 ) {
190 foreach ( $this->backends as $index => $backend ) {
191 if ( $index === $this->masterIndex ) {
192 continue; // done already
195 $realOps = $this->substOpBatchPaths( $ops, $backend );
196 if ( $this->asyncWrites && !$this->hasVolatileSources( $ops ) ) {
197 // Bind $scopeLock to the callback to preserve locks
198 DeferredUpdates::addCallableUpdate(
199 function() use ( $backend, $realOps, $opts, $scopeLock, $relevantPaths ) {
200 wfDebugLog( 'FileOperationReplication',
201 "'{$backend->getName()}' async replication; paths: " .
202 FormatJson::encode( $relevantPaths ) );
203 $backend->doOperations( $realOps, $opts );
206 } else {
207 wfDebugLog( 'FileOperationReplication',
208 "'{$backend->getName()}' sync replication; paths: " .
209 FormatJson::encode( $relevantPaths ) );
210 $status->merge( $backend->doOperations( $realOps, $opts ) );
214 // Make 'success', 'successCount', and 'failCount' fields reflect
215 // the overall operation, rather than all the batches for each backend.
216 // Do this by only using success values from the master backend's batch.
217 $status->success = $masterStatus->success;
218 $status->successCount = $masterStatus->successCount;
219 $status->failCount = $masterStatus->failCount;
221 return $status;
225 * Check that a set of files are consistent across all internal backends
227 * @param array $paths List of storage paths
228 * @return StatusValue
230 public function consistencyCheck( array $paths ) {
231 $status = $this->newStatus();
232 if ( $this->syncChecks == 0 || count( $this->backends ) <= 1 ) {
233 return $status; // skip checks
236 // Preload all of the stat info in as few round trips as possible...
237 foreach ( $this->backends as $backend ) {
238 $realPaths = $this->substPaths( $paths, $backend );
239 $backend->preloadFileStat( [ 'srcs' => $realPaths, 'latest' => true ] );
242 $mBackend = $this->backends[$this->masterIndex];
243 foreach ( $paths as $path ) {
244 $params = [ 'src' => $path, 'latest' => true ];
245 $mParams = $this->substOpPaths( $params, $mBackend );
246 // Stat the file on the 'master' backend
247 $mStat = $mBackend->getFileStat( $mParams );
248 if ( $this->syncChecks & self::CHECK_SHA1 ) {
249 $mSha1 = $mBackend->getFileSha1Base36( $mParams );
250 } else {
251 $mSha1 = false;
253 // Check if all clone backends agree with the master...
254 foreach ( $this->backends as $index => $cBackend ) {
255 if ( $index === $this->masterIndex ) {
256 continue; // master
258 $cParams = $this->substOpPaths( $params, $cBackend );
259 $cStat = $cBackend->getFileStat( $cParams );
260 if ( $mStat ) { // file is in master
261 if ( !$cStat ) { // file should exist
262 $status->fatal( 'backend-fail-synced', $path );
263 continue;
265 if ( $this->syncChecks & self::CHECK_SIZE ) {
266 if ( $cStat['size'] != $mStat['size'] ) { // wrong size
267 $status->fatal( 'backend-fail-synced', $path );
268 continue;
271 if ( $this->syncChecks & self::CHECK_TIME ) {
272 $mTs = wfTimestamp( TS_UNIX, $mStat['mtime'] );
273 $cTs = wfTimestamp( TS_UNIX, $cStat['mtime'] );
274 if ( abs( $mTs - $cTs ) > 30 ) { // outdated file somewhere
275 $status->fatal( 'backend-fail-synced', $path );
276 continue;
279 if ( $this->syncChecks & self::CHECK_SHA1 ) {
280 if ( $cBackend->getFileSha1Base36( $cParams ) !== $mSha1 ) { // wrong SHA1
281 $status->fatal( 'backend-fail-synced', $path );
282 continue;
285 } else { // file is not in master
286 if ( $cStat ) { // file should not exist
287 $status->fatal( 'backend-fail-synced', $path );
293 return $status;
297 * Check that a set of file paths are usable across all internal backends
299 * @param array $paths List of storage paths
300 * @return StatusValue
302 public function accessibilityCheck( array $paths ) {
303 $status = $this->newStatus();
304 if ( count( $this->backends ) <= 1 ) {
305 return $status; // skip checks
308 foreach ( $paths as $path ) {
309 foreach ( $this->backends as $backend ) {
310 $realPath = $this->substPaths( $path, $backend );
311 if ( !$backend->isPathUsableInternal( $realPath ) ) {
312 $status->fatal( 'backend-fail-usable', $path );
317 return $status;
321 * Check that a set of files are consistent across all internal backends
322 * and re-synchronize those files against the "multi master" if needed.
324 * @param array $paths List of storage paths
325 * @param string|bool $resyncMode False, True, or "conservative"; see __construct()
326 * @return StatusValue
328 public function resyncFiles( array $paths, $resyncMode = true ) {
329 $status = $this->newStatus();
331 $mBackend = $this->backends[$this->masterIndex];
332 foreach ( $paths as $path ) {
333 $mPath = $this->substPaths( $path, $mBackend );
334 $mSha1 = $mBackend->getFileSha1Base36( [ 'src' => $mPath, 'latest' => true ] );
335 $mStat = $mBackend->getFileStat( [ 'src' => $mPath, 'latest' => true ] );
336 if ( $mStat === null || ( $mSha1 !== false && !$mStat ) ) { // sanity
337 $status->fatal( 'backend-fail-internal', $this->name );
338 wfDebugLog( 'FileOperation', __METHOD__
339 . ': File is not available on the master backend' );
340 continue; // file is not available on the master backend...
342 // Check of all clone backends agree with the master...
343 foreach ( $this->backends as $index => $cBackend ) {
344 if ( $index === $this->masterIndex ) {
345 continue; // master
347 $cPath = $this->substPaths( $path, $cBackend );
348 $cSha1 = $cBackend->getFileSha1Base36( [ 'src' => $cPath, 'latest' => true ] );
349 $cStat = $cBackend->getFileStat( [ 'src' => $cPath, 'latest' => true ] );
350 if ( $cStat === null || ( $cSha1 !== false && !$cStat ) ) { // sanity
351 $status->fatal( 'backend-fail-internal', $cBackend->getName() );
352 wfDebugLog( 'FileOperation', __METHOD__ .
353 ': File is not available on the clone backend' );
354 continue; // file is not available on the clone backend...
356 if ( $mSha1 === $cSha1 ) {
357 // already synced; nothing to do
358 } elseif ( $mSha1 !== false ) { // file is in master
359 if ( $resyncMode === 'conservative'
360 && $cStat && $cStat['mtime'] > $mStat['mtime']
362 $status->fatal( 'backend-fail-synced', $path );
363 continue; // don't rollback data
365 $fsFile = $mBackend->getLocalReference(
366 [ 'src' => $mPath, 'latest' => true ] );
367 $status->merge( $cBackend->quickStore(
368 [ 'src' => $fsFile->getPath(), 'dst' => $cPath ]
369 ) );
370 } elseif ( $mStat === false ) { // file is not in master
371 if ( $resyncMode === 'conservative' ) {
372 $status->fatal( 'backend-fail-synced', $path );
373 continue; // don't delete data
375 $status->merge( $cBackend->quickDelete( [ 'src' => $cPath ] ) );
380 if ( !$status->isOK() ) {
381 wfDebugLog( 'FileOperation', get_class( $this ) .
382 " failed to resync: " . FormatJson::encode( $paths ) );
385 return $status;
389 * Get a list of file storage paths to read or write for a list of operations
391 * @param array $ops Same format as doOperations()
392 * @return array List of storage paths to files (does not include directories)
394 protected function fileStoragePathsForOps( array $ops ) {
395 $paths = [];
396 foreach ( $ops as $op ) {
397 if ( isset( $op['src'] ) ) {
398 // For things like copy/move/delete with "ignoreMissingSource" and there
399 // is no source file, nothing should happen and there should be no errors.
400 if ( empty( $op['ignoreMissingSource'] )
401 || $this->fileExists( [ 'src' => $op['src'] ] )
403 $paths[] = $op['src'];
406 if ( isset( $op['srcs'] ) ) {
407 $paths = array_merge( $paths, $op['srcs'] );
409 if ( isset( $op['dst'] ) ) {
410 $paths[] = $op['dst'];
414 return array_values( array_unique( array_filter( $paths, 'FileBackend::isStoragePath' ) ) );
418 * Substitute the backend name in storage path parameters
419 * for a set of operations with that of a given internal backend.
421 * @param array $ops List of file operation arrays
422 * @param FileBackendStore $backend
423 * @return array
425 protected function substOpBatchPaths( array $ops, FileBackendStore $backend ) {
426 $newOps = []; // operations
427 foreach ( $ops as $op ) {
428 $newOp = $op; // operation
429 foreach ( [ 'src', 'srcs', 'dst', 'dir' ] as $par ) {
430 if ( isset( $newOp[$par] ) ) { // string or array
431 $newOp[$par] = $this->substPaths( $newOp[$par], $backend );
434 $newOps[] = $newOp;
437 return $newOps;
441 * Same as substOpBatchPaths() but for a single operation
443 * @param array $ops File operation array
444 * @param FileBackendStore $backend
445 * @return array
447 protected function substOpPaths( array $ops, FileBackendStore $backend ) {
448 $newOps = $this->substOpBatchPaths( [ $ops ], $backend );
450 return $newOps[0];
454 * Substitute the backend of storage paths with an internal backend's name
456 * @param array|string $paths List of paths or single string path
457 * @param FileBackendStore $backend
458 * @return array|string
460 protected function substPaths( $paths, FileBackendStore $backend ) {
461 return preg_replace(
462 '!^mwstore://' . preg_quote( $this->name, '!' ) . '/!',
463 StringUtils::escapeRegexReplacement( "mwstore://{$backend->getName()}/" ),
464 $paths // string or array
469 * Substitute the backend of internal storage paths with the proxy backend's name
471 * @param array|string $paths List of paths or single string path
472 * @return array|string
474 protected function unsubstPaths( $paths ) {
475 return preg_replace(
476 '!^mwstore://([^/]+)!',
477 StringUtils::escapeRegexReplacement( "mwstore://{$this->name}" ),
478 $paths // string or array
483 * @param array $ops File operations for FileBackend::doOperations()
484 * @return bool Whether there are file path sources with outside lifetime/ownership
486 protected function hasVolatileSources( array $ops ) {
487 foreach ( $ops as $op ) {
488 if ( $op['op'] === 'store' && !isset( $op['srcRef'] ) ) {
489 return true; // source file might be deleted anytime after do*Operations()
493 return false;
496 protected function doQuickOperationsInternal( array $ops ) {
497 $status = $this->newStatus();
498 // Do the operations on the master backend; setting StatusValue fields...
499 $realOps = $this->substOpBatchPaths( $ops, $this->backends[$this->masterIndex] );
500 $masterStatus = $this->backends[$this->masterIndex]->doQuickOperations( $realOps );
501 $status->merge( $masterStatus );
502 // Propagate the operations to the clone backends...
503 foreach ( $this->backends as $index => $backend ) {
504 if ( $index === $this->masterIndex ) {
505 continue; // done already
508 $realOps = $this->substOpBatchPaths( $ops, $backend );
509 if ( $this->asyncWrites && !$this->hasVolatileSources( $ops ) ) {
510 DeferredUpdates::addCallableUpdate(
511 function() use ( $backend, $realOps ) {
512 $backend->doQuickOperations( $realOps );
515 } else {
516 $status->merge( $backend->doQuickOperations( $realOps ) );
519 // Make 'success', 'successCount', and 'failCount' fields reflect
520 // the overall operation, rather than all the batches for each backend.
521 // Do this by only using success values from the master backend's batch.
522 $status->success = $masterStatus->success;
523 $status->successCount = $masterStatus->successCount;
524 $status->failCount = $masterStatus->failCount;
526 return $status;
529 protected function doPrepare( array $params ) {
530 return $this->doDirectoryOp( 'prepare', $params );
533 protected function doSecure( array $params ) {
534 return $this->doDirectoryOp( 'secure', $params );
537 protected function doPublish( array $params ) {
538 return $this->doDirectoryOp( 'publish', $params );
541 protected function doClean( array $params ) {
542 return $this->doDirectoryOp( 'clean', $params );
546 * @param string $method One of (doPrepare,doSecure,doPublish,doClean)
547 * @param array $params Method arguments
548 * @return StatusValue
550 protected function doDirectoryOp( $method, array $params ) {
551 $status = $this->newStatus();
553 $realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] );
554 $masterStatus = $this->backends[$this->masterIndex]->$method( $realParams );
555 $status->merge( $masterStatus );
557 foreach ( $this->backends as $index => $backend ) {
558 if ( $index === $this->masterIndex ) {
559 continue; // already done
562 $realParams = $this->substOpPaths( $params, $backend );
563 if ( $this->asyncWrites ) {
564 DeferredUpdates::addCallableUpdate(
565 function() use ( $backend, $method, $realParams ) {
566 $backend->$method( $realParams );
569 } else {
570 $status->merge( $backend->$method( $realParams ) );
574 return $status;
577 public function concatenate( array $params ) {
578 $status = $this->newStatus();
579 // We are writing to an FS file, so we don't need to do this per-backend
580 $index = $this->getReadIndexFromParams( $params );
581 $realParams = $this->substOpPaths( $params, $this->backends[$index] );
583 $status->merge( $this->backends[$index]->concatenate( $realParams ) );
585 return $status;
588 public function fileExists( array $params ) {
589 $index = $this->getReadIndexFromParams( $params );
590 $realParams = $this->substOpPaths( $params, $this->backends[$index] );
592 return $this->backends[$index]->fileExists( $realParams );
595 public function getFileTimestamp( array $params ) {
596 $index = $this->getReadIndexFromParams( $params );
597 $realParams = $this->substOpPaths( $params, $this->backends[$index] );
599 return $this->backends[$index]->getFileTimestamp( $realParams );
602 public function getFileSize( array $params ) {
603 $index = $this->getReadIndexFromParams( $params );
604 $realParams = $this->substOpPaths( $params, $this->backends[$index] );
606 return $this->backends[$index]->getFileSize( $realParams );
609 public function getFileStat( array $params ) {
610 $index = $this->getReadIndexFromParams( $params );
611 $realParams = $this->substOpPaths( $params, $this->backends[$index] );
613 return $this->backends[$index]->getFileStat( $realParams );
616 public function getFileXAttributes( array $params ) {
617 $index = $this->getReadIndexFromParams( $params );
618 $realParams = $this->substOpPaths( $params, $this->backends[$index] );
620 return $this->backends[$index]->getFileXAttributes( $realParams );
623 public function getFileContentsMulti( array $params ) {
624 $index = $this->getReadIndexFromParams( $params );
625 $realParams = $this->substOpPaths( $params, $this->backends[$index] );
627 $contentsM = $this->backends[$index]->getFileContentsMulti( $realParams );
629 $contents = []; // (path => FSFile) mapping using the proxy backend's name
630 foreach ( $contentsM as $path => $data ) {
631 $contents[$this->unsubstPaths( $path )] = $data;
634 return $contents;
637 public function getFileSha1Base36( array $params ) {
638 $index = $this->getReadIndexFromParams( $params );
639 $realParams = $this->substOpPaths( $params, $this->backends[$index] );
641 return $this->backends[$index]->getFileSha1Base36( $realParams );
644 public function getFileProps( array $params ) {
645 $index = $this->getReadIndexFromParams( $params );
646 $realParams = $this->substOpPaths( $params, $this->backends[$index] );
648 return $this->backends[$index]->getFileProps( $realParams );
651 public function streamFile( array $params ) {
652 $index = $this->getReadIndexFromParams( $params );
653 $realParams = $this->substOpPaths( $params, $this->backends[$index] );
655 return $this->backends[$index]->streamFile( $realParams );
658 public function getLocalReferenceMulti( array $params ) {
659 $index = $this->getReadIndexFromParams( $params );
660 $realParams = $this->substOpPaths( $params, $this->backends[$index] );
662 $fsFilesM = $this->backends[$index]->getLocalReferenceMulti( $realParams );
664 $fsFiles = []; // (path => FSFile) mapping using the proxy backend's name
665 foreach ( $fsFilesM as $path => $fsFile ) {
666 $fsFiles[$this->unsubstPaths( $path )] = $fsFile;
669 return $fsFiles;
672 public function getLocalCopyMulti( array $params ) {
673 $index = $this->getReadIndexFromParams( $params );
674 $realParams = $this->substOpPaths( $params, $this->backends[$index] );
676 $tempFilesM = $this->backends[$index]->getLocalCopyMulti( $realParams );
678 $tempFiles = []; // (path => TempFSFile) mapping using the proxy backend's name
679 foreach ( $tempFilesM as $path => $tempFile ) {
680 $tempFiles[$this->unsubstPaths( $path )] = $tempFile;
683 return $tempFiles;
686 public function getFileHttpUrl( array $params ) {
687 $index = $this->getReadIndexFromParams( $params );
688 $realParams = $this->substOpPaths( $params, $this->backends[$index] );
690 return $this->backends[$index]->getFileHttpUrl( $realParams );
693 public function directoryExists( array $params ) {
694 $realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] );
696 return $this->backends[$this->masterIndex]->directoryExists( $realParams );
699 public function getDirectoryList( array $params ) {
700 $realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] );
702 return $this->backends[$this->masterIndex]->getDirectoryList( $realParams );
705 public function getFileList( array $params ) {
706 $realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] );
708 return $this->backends[$this->masterIndex]->getFileList( $realParams );
711 public function getFeatures() {
712 return $this->backends[$this->masterIndex]->getFeatures();
715 public function clearCache( array $paths = null ) {
716 foreach ( $this->backends as $backend ) {
717 $realPaths = is_array( $paths ) ? $this->substPaths( $paths, $backend ) : null;
718 $backend->clearCache( $realPaths );
722 public function preloadCache( array $paths ) {
723 $realPaths = $this->substPaths( $paths, $this->backends[$this->readIndex] );
724 $this->backends[$this->readIndex]->preloadCache( $realPaths );
727 public function preloadFileStat( array $params ) {
728 $index = $this->getReadIndexFromParams( $params );
729 $realParams = $this->substOpPaths( $params, $this->backends[$index] );
731 return $this->backends[$index]->preloadFileStat( $realParams );
734 public function getScopedLocksForOps( array $ops, StatusValue $status ) {
735 $realOps = $this->substOpBatchPaths( $ops, $this->backends[$this->masterIndex] );
736 $fileOps = $this->backends[$this->masterIndex]->getOperationsInternal( $realOps );
737 // Get the paths to lock from the master backend
738 $paths = $this->backends[$this->masterIndex]->getPathsToLockForOpsInternal( $fileOps );
739 // Get the paths under the proxy backend's name
740 $pbPaths = [
741 LockManager::LOCK_UW => $this->unsubstPaths( $paths[LockManager::LOCK_UW] ),
742 LockManager::LOCK_EX => $this->unsubstPaths( $paths[LockManager::LOCK_EX] )
745 // Actually acquire the locks
746 return $this->getScopedFileLocks( $pbPaths, 'mixed', $status );
750 * @param array $params
751 * @return int The master or read affinity backend index, based on $params['latest']
753 protected function getReadIndexFromParams( array $params ) {
754 return !empty( $params['latest'] ) ? $this->masterIndex : $this->readIndex;