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