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