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