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
21 * @ingroup FileBackend
24 use Wikimedia\Timestamp\ConvertibleTimestamp
;
27 * @brief Proxy backend that mirrors writes to several internal backends.
29 * This class defines a multi-write backend. Multiple backends can be
30 * registered to this proxy backend and it will act as a single backend.
31 * Use this when all access to those backends is through this proxy backend.
32 * At least one of the backends must be declared the "master" backend.
34 * Only use this class when transitioning from one storage system to another.
36 * Read operations are only done on the 'master' backend for consistency.
37 * Write operations are performed on all backends, starting with the master.
38 * This makes a best-effort to have transactional semantics, but since requests
39 * may sometimes fail, the use of "autoResync" or background scripts to fix
40 * inconsistencies is important.
42 * @ingroup FileBackend
45 class FileBackendMultiWrite
extends FileBackend
{
46 /** @var FileBackendStore[] Prioritized list of FileBackendStore objects */
47 protected $backends = [];
49 /** @var int Index of master backend */
50 protected $masterIndex = -1;
51 /** @var int Index of read affinity backend */
52 protected $readIndex = -1;
54 /** @var int Bitfield */
55 protected $syncChecks = 0;
56 /** @var string|bool */
57 protected $autoResync = false;
60 protected $asyncWrites = false;
62 /** @var int Compare file sizes among backends */
64 /** @var int Compare file mtimes among backends */
66 /** @var int Compare file hashes among backends */
70 * Construct a proxy backend that consists of several internal backends.
71 * Locking, journaling, and read-only checks are handled by the proxy backend.
73 * Additional $config params include:
74 * - backends : Array of backend config and multi-backend settings.
75 * Each value is the config used in the constructor of a
76 * FileBackendStore class, but with these additional settings:
77 * - class : The name of the backend class
78 * - isMultiMaster : This must be set for one backend.
79 * - readAffinity : Use this for reads without 'latest' set.
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.
94 * @param array $config
95 * @throws LogicException
97 public function __construct( array $config ) {
98 parent
::__construct( $config );
99 $this->syncChecks
= $config['syncChecks'] ?? self
::CHECK_SIZE
;
100 $this->autoResync
= $config['autoResync'] ??
false;
101 $this->asyncWrites
= isset( $config['replication'] ) && $config['replication'] === 'async';
102 // Construct backends here rather than via registration
103 // to keep these backends hidden from outside the proxy.
105 foreach ( $config['backends'] as $index => $config ) {
106 $name = $config['name'];
107 if ( isset( $namesUsed[$name] ) ) { // don't break FileOp predicates
108 throw new LogicException( "Two or more backends defined with the name $name." );
110 $namesUsed[$name] = 1;
111 // Alter certain sub-backend settings for sanity
112 unset( $config['readOnly'] ); // use proxy backend setting
113 unset( $config['fileJournal'] ); // use proxy backend journal
114 unset( $config['lockManager'] ); // lock under proxy backend
115 $config['domainId'] = $this->domainId
; // use the proxy backend wiki ID
116 if ( !empty( $config['isMultiMaster'] ) ) {
117 if ( $this->masterIndex
>= 0 ) {
118 throw new LogicException( 'More than one master backend defined.' );
120 $this->masterIndex
= $index; // this is the "master"
121 $config['fileJournal'] = $this->fileJournal
; // log under proxy backend
123 if ( !empty( $config['readAffinity'] ) ) {
124 $this->readIndex
= $index; // prefer this for reads
126 // Create sub-backend object
127 if ( !isset( $config['class'] ) ) {
128 throw new InvalidArgumentException( 'No class given for a backend config.' );
130 $class = $config['class'];
131 $this->backends
[$index] = new $class( $config );
133 if ( $this->masterIndex
< 0 ) { // need backends and must have a master
134 throw new LogicException( 'No master backend defined.' );
136 if ( $this->readIndex
< 0 ) {
137 $this->readIndex
= $this->masterIndex
; // default
141 final protected function doOperationsInternal( array $ops, array $opts ) {
142 $status = $this->newStatus();
144 $mbe = $this->backends
[$this->masterIndex
]; // convenience
146 // Acquire any locks as needed
147 if ( empty( $opts['nonLocking'] ) ) {
148 /** @noinspection PhpUnusedLocalVariableInspection */
149 $scopeLock = $this->getScopedLocksForOps( $ops, $status );
150 if ( !$status->isOK() ) {
151 return $status; // abort
154 // Clear any cache entries (after locks acquired)
156 $opts['preserveCache'] = true; // only locked files are cached
157 // Get the list of paths to read/write
158 $relevantPaths = $this->fileStoragePathsForOps( $ops );
159 // Check if the paths are valid and accessible on all backends
160 $status->merge( $this->accessibilityCheck( $relevantPaths ) );
161 if ( !$status->isOK() ) {
162 return $status; // abort
164 // Do a consistency check to see if the backends are consistent
165 $syncStatus = $this->consistencyCheck( $relevantPaths );
166 if ( !$syncStatus->isOK() ) {
167 $this->logger
->error(
168 __METHOD__
. ": failed sync check: " . FormatJson
::encode( $relevantPaths )
170 // Try to resync the clone backends to the master on the spot
172 $this->autoResync
=== false ||
173 !$this->resyncFiles( $relevantPaths, $this->autoResync
)->isOK()
175 $status->merge( $syncStatus );
177 return $status; // abort
180 // Actually attempt the operation batch on the master backend
181 $realOps = $this->substOpBatchPaths( $ops, $mbe );
182 $masterStatus = $mbe->doOperations( $realOps, $opts );
183 $status->merge( $masterStatus );
184 // Propagate the operations to the clone backends if there were no unexpected errors
185 // and everything didn't fail due to predicted errors. If $ops only had one operation,
186 // this might avoid backend sync inconsistencies.
187 if ( $masterStatus->isOK() && $masterStatus->successCount
> 0 ) {
188 foreach ( $this->backends
as $index => $backend ) {
189 if ( $index === $this->masterIndex
) {
190 continue; // done already
193 $realOps = $this->substOpBatchPaths( $ops, $backend );
194 if ( $this->asyncWrites
&& !$this->hasVolatileSources( $ops ) ) {
195 // Bind $scopeLock to the callback to preserve locks
196 DeferredUpdates
::addCallableUpdate(
197 function () use ( $backend, $realOps, $opts, $scopeLock, $relevantPaths ) {
198 $this->logger
->error(
199 "'{$backend->getName()}' async replication; paths: " .
200 FormatJson
::encode( $relevantPaths )
202 $backend->doOperations( $realOps, $opts );
206 $this->logger
->error(
207 "'{$backend->getName()}' sync replication; paths: " .
208 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
;
225 * Check that a set of files are consistent across all internal backends
227 * This method should only be called if the files are locked or the backend
228 * is in read-only mode
230 * @param array $paths List of storage paths
231 * @return StatusValue
233 public function consistencyCheck( array $paths ) {
234 $status = $this->newStatus();
235 if ( $this->syncChecks
== 0 ||
count( $this->backends
) <= 1 ) {
236 return $status; // skip checks
239 // Preload all of the stat info in as few round trips as possible
240 foreach ( $this->backends
as $backend ) {
241 $realPaths = $this->substPaths( $paths, $backend );
242 $backend->preloadFileStat( [ 'srcs' => $realPaths, 'latest' => true ] );
245 foreach ( $paths as $path ) {
246 $params = [ 'src' => $path, 'latest' => true ];
247 // Get the state of the file on the master backend
248 $masterBackend = $this->backends
[$this->masterIndex
];
249 $masterParams = $this->substOpPaths( $params, $masterBackend );
250 $masterStat = $masterBackend->getFileStat( $masterParams );
251 if ( $masterStat === self
::STAT_ERROR
) {
252 $status->fatal( 'backend-fail-stat', $path );
255 if ( $this->syncChecks
& self
::CHECK_SHA1
) {
256 $masterSha1 = $masterBackend->getFileSha1Base36( $masterParams );
257 if ( ( $masterSha1 !== false ) !== (bool)$masterStat ) {
258 $status->fatal( 'backend-fail-hash', $path );
262 $masterSha1 = null; // unused
265 // Check if all clone backends agree with the master...
266 foreach ( $this->backends
as $index => $cloneBackend ) {
267 if ( $index === $this->masterIndex
) {
271 // Get the state of the file on the clone backend
272 $cloneParams = $this->substOpPaths( $params, $cloneBackend );
273 $cloneStat = $cloneBackend->getFileStat( $cloneParams );
276 // File exists in the master backend
278 // File is missing from the clone backend
279 $status->fatal( 'backend-fail-synced', $path );
281 ( $this->syncChecks
& self
::CHECK_SIZE
) &&
282 $cloneStat['size'] !== $masterStat['size']
284 // File in the clone backend is different
285 $status->fatal( 'backend-fail-synced', $path );
287 ( $this->syncChecks
& self
::CHECK_TIME
) &&
289 ConvertibleTimestamp
::convert( TS_UNIX
, $masterStat['mtime'] ) -
290 ConvertibleTimestamp
::convert( TS_UNIX
, $cloneStat['mtime'] )
293 // File in the clone backend is significantly newer or older
294 $status->fatal( 'backend-fail-synced', $path );
296 ( $this->syncChecks
& self
::CHECK_SHA1
) &&
297 $cloneBackend->getFileSha1Base36( $cloneParams ) !== $masterSha1
299 // File in the clone backend is different
300 $status->fatal( 'backend-fail-synced', $path );
303 // File does not exist in the master backend
305 // Stray file exists in the clone backend
306 $status->fatal( 'backend-fail-synced', $path );
316 * Check that a set of file paths are usable across all internal backends
318 * @param array $paths List of storage paths
319 * @return StatusValue
321 public function accessibilityCheck( array $paths ) {
322 $status = $this->newStatus();
323 if ( count( $this->backends
) <= 1 ) {
324 return $status; // skip checks
327 foreach ( $paths as $path ) {
328 foreach ( $this->backends
as $backend ) {
329 $realPath = $this->substPaths( $path, $backend );
330 if ( !$backend->isPathUsableInternal( $realPath ) ) {
331 $status->fatal( 'backend-fail-usable', $path );
340 * Check that a set of files are consistent across all internal backends
341 * and re-synchronize those files against the "multi master" if needed.
343 * This method should only be called if the files are locked
345 * @param array $paths List of storage paths
346 * @param string|bool $resyncMode False, True, or "conservative"; see __construct()
347 * @return StatusValue
349 public function resyncFiles( array $paths, $resyncMode = true ) {
350 $status = $this->newStatus();
353 foreach ( $paths as $path ) {
354 $params = [ 'src' => $path, 'latest' => true ];
355 // Get the state of the file on the master backend
356 $masterBackend = $this->backends
[$this->masterIndex
];
357 $masterParams = $this->substOpPaths( $params, $masterBackend );
358 $masterPath = $masterParams['src'];
359 $masterStat = $masterBackend->getFileStat( $masterParams );
360 if ( $masterStat === self
::STAT_ERROR
) {
361 $status->fatal( 'backend-fail-stat', $path );
362 $this->logger
->error( "$fname: file '$masterPath' is not available" );
365 $masterSha1 = $masterBackend->getFileSha1Base36( $masterParams );
366 if ( ( $masterSha1 !== false ) !== (bool)$masterStat ) {
367 $status->fatal( 'backend-fail-hash', $path );
368 $this->logger
->error( "$fname: file '$masterPath' hash does not match stat" );
372 // Check of all clone backends agree with the master...
373 foreach ( $this->backends
as $index => $cloneBackend ) {
374 if ( $index === $this->masterIndex
) {
378 // Get the state of the file on the clone backend
379 $cloneParams = $this->substOpPaths( $params, $cloneBackend );
380 $clonePath = $cloneParams['src'];
381 $cloneStat = $cloneBackend->getFileStat( $cloneParams );
382 if ( $cloneStat === self
::STAT_ERROR
) {
383 $status->fatal( 'backend-fail-stat', $path );
384 $this->logger
->error( "$fname: file '$clonePath' is not available" );
387 $cloneSha1 = $cloneBackend->getFileSha1Base36( $cloneParams );
388 if ( ( $cloneSha1 !== false ) !== (bool)$cloneStat ) {
389 $status->fatal( 'backend-fail-hash', $path );
390 $this->logger
->error( "$fname: file '$clonePath' hash does not match stat" );
394 if ( $masterSha1 === $cloneSha1 ) {
395 // File is either the same in both backends or absent from both backends
396 $this->logger
->debug( "$fname: file '$clonePath' matches '$masterPath'" );
397 } elseif ( $masterSha1 !== false ) {
398 // File is either missing from or different in the clone backend
400 $resyncMode === 'conservative' &&
402 $cloneStat['mtime'] > $masterStat['mtime']
404 // Do not replace files with older ones; reduces the risk of data loss
405 $status->fatal( 'backend-fail-synced', $path );
407 // Copy the master backend file to the clone backend in overwrite mode
408 $fsFile = $masterBackend->getLocalReference( $masterParams );
409 $status->merge( $cloneBackend->quickStore( [
414 } elseif ( $masterStat === false ) {
415 // Stray file exists in the clone backend
416 if ( $resyncMode === 'conservative' ) {
417 // Do not delete stray files; reduces the risk of data loss
418 $status->fatal( 'backend-fail-synced', $path );
420 // Delete the stay file from the clone backend
421 $status->merge( $cloneBackend->quickDelete( [ 'src' => $clonePath ] ) );
427 if ( !$status->isOK() ) {
428 $this->logger
->error( "$fname: failed to resync: " . FormatJson
::encode( $paths ) );
435 * Get a list of file storage paths to read or write for a list of operations
437 * @param array $ops Same format as doOperations()
438 * @return array List of storage paths to files (does not include directories)
440 protected function fileStoragePathsForOps( array $ops ) {
442 foreach ( $ops as $op ) {
443 if ( isset( $op['src'] ) ) {
444 // For things like copy/move/delete with "ignoreMissingSource" and there
445 // is no source file, nothing should happen and there should be no errors.
446 if ( empty( $op['ignoreMissingSource'] )
447 ||
$this->fileExists( [ 'src' => $op['src'] ] )
449 $paths[] = $op['src'];
452 if ( isset( $op['srcs'] ) ) {
453 $paths = array_merge( $paths, $op['srcs'] );
455 if ( isset( $op['dst'] ) ) {
456 $paths[] = $op['dst'];
460 return array_values( array_unique( array_filter( $paths, 'FileBackend::isStoragePath' ) ) );
464 * Substitute the backend name in storage path parameters
465 * for a set of operations with that of a given internal backend.
467 * @param array $ops List of file operation arrays
468 * @param FileBackendStore $backend
471 protected function substOpBatchPaths( array $ops, FileBackendStore
$backend ) {
472 $newOps = []; // operations
473 foreach ( $ops as $op ) {
474 $newOp = $op; // operation
475 foreach ( [ 'src', 'srcs', 'dst', 'dir' ] as $par ) {
476 if ( isset( $newOp[$par] ) ) { // string or array
477 $newOp[$par] = $this->substPaths( $newOp[$par], $backend );
487 * Same as substOpBatchPaths() but for a single operation
489 * @param array $ops File operation array
490 * @param FileBackendStore $backend
493 protected function substOpPaths( array $ops, FileBackendStore
$backend ) {
494 $newOps = $this->substOpBatchPaths( [ $ops ], $backend );
500 * Substitute the backend of storage paths with an internal backend's name
502 * @param array|string $paths List of paths or single string path
503 * @param FileBackendStore $backend
504 * @return string[]|string
506 protected function substPaths( $paths, FileBackendStore
$backend ) {
508 '!^mwstore://' . preg_quote( $this->name
, '!' ) . '/!',
509 StringUtils
::escapeRegexReplacement( "mwstore://{$backend->getName()}/" ),
510 $paths // string or array
515 * Substitute the backend of internal storage paths with the proxy backend's name
517 * @param array|string $paths List of paths or single string path
518 * @param FileBackendStore $backend internal storage backend
519 * @return string[]|string
521 protected function unsubstPaths( $paths, FileBackendStore
$backend ) {
523 '!^mwstore://' . preg_quote( $backend->getName(), '!' ) . '/!',
524 StringUtils
::escapeRegexReplacement( "mwstore://{$this->name}/" ),
525 $paths // string or array
530 * @param array $ops File operations for FileBackend::doOperations()
531 * @return bool Whether there are file path sources with outside lifetime/ownership
533 protected function hasVolatileSources( array $ops ) {
534 foreach ( $ops as $op ) {
535 if ( $op['op'] === 'store' && !isset( $op['srcRef'] ) ) {
536 return true; // source file might be deleted anytime after do*Operations()
543 protected function doQuickOperationsInternal( array $ops ) {
544 $status = $this->newStatus();
545 // Do the operations on the master backend; setting StatusValue fields
546 $realOps = $this->substOpBatchPaths( $ops, $this->backends
[$this->masterIndex
] );
547 $masterStatus = $this->backends
[$this->masterIndex
]->doQuickOperations( $realOps );
548 $status->merge( $masterStatus );
549 // Propagate the operations to the clone backends...
550 foreach ( $this->backends
as $index => $backend ) {
551 if ( $index === $this->masterIndex
) {
552 continue; // done already
555 $realOps = $this->substOpBatchPaths( $ops, $backend );
556 if ( $this->asyncWrites
&& !$this->hasVolatileSources( $ops ) ) {
557 DeferredUpdates
::addCallableUpdate(
558 function () use ( $backend, $realOps ) {
559 $backend->doQuickOperations( $realOps );
563 $status->merge( $backend->doQuickOperations( $realOps ) );
566 // Make 'success', 'successCount', and 'failCount' fields reflect
567 // the overall operation, rather than all the batches for each backend.
568 // Do this by only using success values from the master backend's batch.
569 $status->success
= $masterStatus->success
;
570 $status->successCount
= $masterStatus->successCount
;
571 $status->failCount
= $masterStatus->failCount
;
576 protected function doPrepare( array $params ) {
577 return $this->doDirectoryOp( 'prepare', $params );
580 protected function doSecure( array $params ) {
581 return $this->doDirectoryOp( 'secure', $params );
584 protected function doPublish( array $params ) {
585 return $this->doDirectoryOp( 'publish', $params );
588 protected function doClean( array $params ) {
589 return $this->doDirectoryOp( 'clean', $params );
593 * @param string $method One of (doPrepare,doSecure,doPublish,doClean)
594 * @param array $params Method arguments
595 * @return StatusValue
597 protected function doDirectoryOp( $method, array $params ) {
598 $status = $this->newStatus();
600 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
601 $masterStatus = $this->backends
[$this->masterIndex
]->$method( $realParams );
602 $status->merge( $masterStatus );
604 foreach ( $this->backends
as $index => $backend ) {
605 if ( $index === $this->masterIndex
) {
606 continue; // already done
609 $realParams = $this->substOpPaths( $params, $backend );
610 if ( $this->asyncWrites
) {
611 DeferredUpdates
::addCallableUpdate(
612 function () use ( $backend, $method, $realParams ) {
613 $backend->$method( $realParams );
617 $status->merge( $backend->$method( $realParams ) );
624 public function concatenate( array $params ) {
625 $status = $this->newStatus();
626 // We are writing to an FS file, so we don't need to do this per-backend
627 $index = $this->getReadIndexFromParams( $params );
628 $realParams = $this->substOpPaths( $params, $this->backends
[$index] );
630 $status->merge( $this->backends
[$index]->concatenate( $realParams ) );
635 public function fileExists( array $params ) {
636 $index = $this->getReadIndexFromParams( $params );
637 $realParams = $this->substOpPaths( $params, $this->backends
[$index] );
639 return $this->backends
[$index]->fileExists( $realParams );
642 public function getFileTimestamp( array $params ) {
643 $index = $this->getReadIndexFromParams( $params );
644 $realParams = $this->substOpPaths( $params, $this->backends
[$index] );
646 return $this->backends
[$index]->getFileTimestamp( $realParams );
649 public function getFileSize( array $params ) {
650 $index = $this->getReadIndexFromParams( $params );
651 $realParams = $this->substOpPaths( $params, $this->backends
[$index] );
653 return $this->backends
[$index]->getFileSize( $realParams );
656 public function getFileStat( array $params ) {
657 $index = $this->getReadIndexFromParams( $params );
658 $realParams = $this->substOpPaths( $params, $this->backends
[$index] );
660 return $this->backends
[$index]->getFileStat( $realParams );
663 public function getFileXAttributes( array $params ) {
664 $index = $this->getReadIndexFromParams( $params );
665 $realParams = $this->substOpPaths( $params, $this->backends
[$index] );
667 return $this->backends
[$index]->getFileXAttributes( $realParams );
670 public function getFileContentsMulti( array $params ) {
671 $index = $this->getReadIndexFromParams( $params );
672 $realParams = $this->substOpPaths( $params, $this->backends
[$index] );
674 $contentsM = $this->backends
[$index]->getFileContentsMulti( $realParams );
676 $contents = []; // (path => FSFile) mapping using the proxy backend's name
677 foreach ( $contentsM as $path => $data ) {
678 $contents[$this->unsubstPaths( $path, $this->backends
[$index] )] = $data;
684 public function getFileSha1Base36( array $params ) {
685 $index = $this->getReadIndexFromParams( $params );
686 $realParams = $this->substOpPaths( $params, $this->backends
[$index] );
688 return $this->backends
[$index]->getFileSha1Base36( $realParams );
691 public function getFileProps( array $params ) {
692 $index = $this->getReadIndexFromParams( $params );
693 $realParams = $this->substOpPaths( $params, $this->backends
[$index] );
695 return $this->backends
[$index]->getFileProps( $realParams );
698 public function streamFile( array $params ) {
699 $index = $this->getReadIndexFromParams( $params );
700 $realParams = $this->substOpPaths( $params, $this->backends
[$index] );
702 return $this->backends
[$index]->streamFile( $realParams );
705 public function getLocalReferenceMulti( array $params ) {
706 $index = $this->getReadIndexFromParams( $params );
707 $realParams = $this->substOpPaths( $params, $this->backends
[$index] );
709 $fsFilesM = $this->backends
[$index]->getLocalReferenceMulti( $realParams );
711 $fsFiles = []; // (path => FSFile) mapping using the proxy backend's name
712 foreach ( $fsFilesM as $path => $fsFile ) {
713 $fsFiles[$this->unsubstPaths( $path, $this->backends
[$index] )] = $fsFile;
719 public function getLocalCopyMulti( array $params ) {
720 $index = $this->getReadIndexFromParams( $params );
721 $realParams = $this->substOpPaths( $params, $this->backends
[$index] );
723 $tempFilesM = $this->backends
[$index]->getLocalCopyMulti( $realParams );
725 $tempFiles = []; // (path => TempFSFile) mapping using the proxy backend's name
726 foreach ( $tempFilesM as $path => $tempFile ) {
727 $tempFiles[$this->unsubstPaths( $path, $this->backends
[$index] )] = $tempFile;
733 public function getFileHttpUrl( array $params ) {
734 $index = $this->getReadIndexFromParams( $params );
735 $realParams = $this->substOpPaths( $params, $this->backends
[$index] );
737 return $this->backends
[$index]->getFileHttpUrl( $realParams );
740 public function directoryExists( array $params ) {
741 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
743 return $this->backends
[$this->masterIndex
]->directoryExists( $realParams );
746 public function getDirectoryList( array $params ) {
747 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
749 return $this->backends
[$this->masterIndex
]->getDirectoryList( $realParams );
752 public function getFileList( array $params ) {
753 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
755 return $this->backends
[$this->masterIndex
]->getFileList( $realParams );
758 public function getFeatures() {
759 return $this->backends
[$this->masterIndex
]->getFeatures();
762 public function clearCache( array $paths = null ) {
763 foreach ( $this->backends
as $backend ) {
764 $realPaths = is_array( $paths ) ?
$this->substPaths( $paths, $backend ) : null;
765 $backend->clearCache( $realPaths );
769 public function preloadCache( array $paths ) {
770 $realPaths = $this->substPaths( $paths, $this->backends
[$this->readIndex
] );
771 $this->backends
[$this->readIndex
]->preloadCache( $realPaths );
774 public function preloadFileStat( array $params ) {
775 $index = $this->getReadIndexFromParams( $params );
776 $realParams = $this->substOpPaths( $params, $this->backends
[$index] );
778 return $this->backends
[$index]->preloadFileStat( $realParams );
781 public function getScopedLocksForOps( array $ops, StatusValue
$status ) {
782 $realOps = $this->substOpBatchPaths( $ops, $this->backends
[$this->masterIndex
] );
783 $fileOps = $this->backends
[$this->masterIndex
]->getOperationsInternal( $realOps );
784 // Get the paths to lock from the master backend
785 $paths = $this->backends
[$this->masterIndex
]->getPathsToLockForOpsInternal( $fileOps );
786 // Get the paths under the proxy backend's name
788 LockManager
::LOCK_UW
=> $this->unsubstPaths(
789 $paths[LockManager
::LOCK_UW
],
790 $this->backends
[$this->masterIndex
]
792 LockManager
::LOCK_EX
=> $this->unsubstPaths(
793 $paths[LockManager
::LOCK_EX
],
794 $this->backends
[$this->masterIndex
]
798 // Actually acquire the locks
799 return $this->getScopedFileLocks( $pbPaths, 'mixed', $status );
803 * @param array $params
804 * @return int The master or read affinity backend index, based on $params['latest']
806 protected function getReadIndexFromParams( array $params ) {
807 return !empty( $params['latest'] ) ?
$this->masterIndex
: $this->readIndex
;