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
22 * @author Aaron Schulz
26 * @brief Proxy backend that mirrors writes to several internal backends.
28 * This class defines a multi-write backend. Multiple backends can be
29 * registered to this proxy backend and it will act as a single backend.
30 * Use this when all access to those backends is through this proxy backend.
31 * At least one of the backends must be declared the "master" backend.
33 * Only use this class when transitioning from one storage system to another.
35 * Read operations are only done on the 'master' backend for consistency.
36 * Write operations are performed on all backends, starting with the master.
37 * This makes a best-effort to have transactional semantics, but since requests
38 * may sometimes fail, the use of "autoResync" or background scripts to fix
39 * inconsistencies is important.
41 * @ingroup FileBackend
44 class FileBackendMultiWrite
extends FileBackend
{
45 /** @var FileBackendStore[] Prioritized list of FileBackendStore objects */
46 protected $backends = array();
48 /** @var int Index of master backend */
49 protected $masterIndex = -1;
50 /** @var int Index of read affinity backend */
51 protected $readIndex = -1;
53 /** @var int Bitfield */
54 protected $syncChecks = 0;
56 /** @var string|bool */
57 protected $autoResync = false;
59 /* Possible internal backend consistency checks */
65 * Construct a proxy backend that consists of several internal backends.
66 * Locking, journaling, and read-only checks are handled by the proxy backend.
68 * Additional $config params include:
69 * - backends : Array of backend config and multi-backend settings.
70 * Each value is the config used in the constructor of a
71 * FileBackendStore class, but with these additional settings:
72 * - class : The name of the backend class
73 * - isMultiMaster : This must be set for one backend.
74 * - readAffinity : Use this for reads without 'latest' set.
75 * - template: : If given a backend name, this will use
76 * the config of that backend as a template.
77 * Values specified here take precedence.
78 * - syncChecks : Integer bitfield of internal backend sync checks to perform.
79 * Possible bits include the FileBackendMultiWrite::CHECK_* constants.
80 * There are constants for SIZE, TIME, and SHA1.
81 * The checks are done before allowing any file operations.
82 * - autoResync : Automatically resync the clone backends to the master backend
83 * when pre-operation sync checks fail. This should only be used
84 * if the master backend is stable and not missing any files.
85 * Use "conservative" to limit resyncing to copying newer master
86 * backend files over older (or non-existing) clone backend files.
87 * Cases that cannot be handled will result in operation abortion.
89 * @param array $config
90 * @throws FileBackendError
92 public function __construct( array $config ) {
93 parent
::__construct( $config );
94 $this->syncChecks
= isset( $config['syncChecks'] )
95 ?
$config['syncChecks']
97 $this->autoResync
= isset( $config['autoResync'] )
98 ?
$config['autoResync']
100 // Construct backends here rather than via registration
101 // to keep these backends hidden from outside the proxy.
102 $namesUsed = array();
103 foreach ( $config['backends'] as $index => $config ) {
104 if ( isset( $config['template'] ) ) {
105 // Config is just a modified version of a registered backend's.
106 // This should only be used when that config is used only by this backend.
107 $config = $config + FileBackendGroup
::singleton()->config( $config['template'] );
109 $name = $config['name'];
110 if ( isset( $namesUsed[$name] ) ) { // don't break FileOp predicates
111 throw new FileBackendError( "Two or more backends defined with the name $name." );
113 $namesUsed[$name] = 1;
114 // Alter certain sub-backend settings for sanity
115 unset( $config['readOnly'] ); // use proxy backend setting
116 unset( $config['fileJournal'] ); // use proxy backend journal
117 unset( $config['lockManager'] ); // lock under proxy backend
118 $config['wikiId'] = $this->wikiId
; // use the proxy backend wiki ID
119 if ( !empty( $config['isMultiMaster'] ) ) {
120 if ( $this->masterIndex
>= 0 ) {
121 throw new FileBackendError( 'More than one master backend defined.' );
123 $this->masterIndex
= $index; // this is the "master"
124 $config['fileJournal'] = $this->fileJournal
; // log under proxy backend
126 if ( !empty( $config['readAffinity'] ) ) {
127 $this->readIndex
= $index; // prefer this for reads
129 // Create sub-backend object
130 if ( !isset( $config['class'] ) ) {
131 throw new FileBackendError( 'No class given for a backend config.' );
133 $class = $config['class'];
134 $this->backends
[$index] = new $class( $config );
136 if ( $this->masterIndex
< 0 ) { // need backends and must have a master
137 throw new FileBackendError( 'No master backend defined.' );
139 if ( $this->readIndex
< 0 ) {
140 $this->readIndex
= $this->masterIndex
; // default
144 final protected function doOperationsInternal( array $ops, array $opts ) {
145 $status = Status
::newGood();
147 $mbe = $this->backends
[$this->masterIndex
]; // convenience
149 // Try to lock those files for the scope of this function...
150 if ( empty( $opts['nonLocking'] ) ) {
151 // Try to lock those files for the scope of this function...
152 /** @noinspection PhpUnusedLocalVariableInspection */
153 $scopeLock = $this->getScopedLocksForOps( $ops, $status );
154 if ( !$status->isOK() ) {
155 return $status; // abort
158 // Clear any cache entries (after locks acquired)
160 $opts['preserveCache'] = true; // only locked files are cached
161 // Get the list of paths to read/write...
162 $relevantPaths = $this->fileStoragePathsForOps( $ops );
163 // Check if the paths are valid and accessible on all backends...
164 $status->merge( $this->accessibilityCheck( $relevantPaths ) );
165 if ( !$status->isOK() ) {
166 return $status; // abort
168 // Do a consistency check to see if the backends are consistent...
169 $syncStatus = $this->consistencyCheck( $relevantPaths );
170 if ( !$syncStatus->isOK() ) {
171 wfDebugLog( 'FileOperation', get_class( $this ) .
172 " failed sync check: " . FormatJson
::encode( $relevantPaths ) );
173 // Try to resync the clone backends to the master on the spot...
174 if ( !$this->autoResync ||
!$this->resyncFiles( $relevantPaths )->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 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
) { // not done already
191 $realOps = $this->substOpBatchPaths( $ops, $backend );
192 $status->merge( $backend->doOperations( $realOps, $opts ) );
196 // Make 'success', 'successCount', and 'failCount' fields reflect
197 // the overall operation, rather than all the batches for each backend.
198 // Do this by only using success values from the master backend's batch.
199 $status->success
= $masterStatus->success
;
200 $status->successCount
= $masterStatus->successCount
;
201 $status->failCount
= $masterStatus->failCount
;
207 * Check that a set of files are consistent across all internal backends
209 * @param array $paths List of storage paths
212 public function consistencyCheck( array $paths ) {
213 $status = Status
::newGood();
214 if ( $this->syncChecks
== 0 ||
count( $this->backends
) <= 1 ) {
215 return $status; // skip checks
218 $mBackend = $this->backends
[$this->masterIndex
];
219 foreach ( $paths as $path ) {
220 $params = array( 'src' => $path, 'latest' => true );
221 $mParams = $this->substOpPaths( $params, $mBackend );
222 // Stat the file on the 'master' backend
223 $mStat = $mBackend->getFileStat( $mParams );
224 if ( $this->syncChecks
& self
::CHECK_SHA1
) {
225 $mSha1 = $mBackend->getFileSha1Base36( $mParams );
229 // Check if all clone backends agree with the master...
230 foreach ( $this->backends
as $index => $cBackend ) {
231 if ( $index === $this->masterIndex
) {
234 $cParams = $this->substOpPaths( $params, $cBackend );
235 $cStat = $cBackend->getFileStat( $cParams );
236 if ( $mStat ) { // file is in master
237 if ( !$cStat ) { // file should exist
238 $status->fatal( 'backend-fail-synced', $path );
241 if ( $this->syncChecks
& self
::CHECK_SIZE
) {
242 if ( $cStat['size'] != $mStat['size'] ) { // wrong size
243 $status->fatal( 'backend-fail-synced', $path );
247 if ( $this->syncChecks
& self
::CHECK_TIME
) {
248 $mTs = wfTimestamp( TS_UNIX
, $mStat['mtime'] );
249 $cTs = wfTimestamp( TS_UNIX
, $cStat['mtime'] );
250 if ( abs( $mTs - $cTs ) > 30 ) { // outdated file somewhere
251 $status->fatal( 'backend-fail-synced', $path );
255 if ( $this->syncChecks
& self
::CHECK_SHA1
) {
256 if ( $cBackend->getFileSha1Base36( $cParams ) !== $mSha1 ) { // wrong SHA1
257 $status->fatal( 'backend-fail-synced', $path );
261 } else { // file is not in master
262 if ( $cStat ) { // file should not exist
263 $status->fatal( 'backend-fail-synced', $path );
273 * Check that a set of file paths are usable across all internal backends
275 * @param array $paths List of storage paths
278 public function accessibilityCheck( array $paths ) {
279 $status = Status
::newGood();
280 if ( count( $this->backends
) <= 1 ) {
281 return $status; // skip checks
284 foreach ( $paths as $path ) {
285 foreach ( $this->backends
as $backend ) {
286 $realPath = $this->substPaths( $path, $backend );
287 if ( !$backend->isPathUsableInternal( $realPath ) ) {
288 $status->fatal( 'backend-fail-usable', $path );
297 * Check that a set of files are consistent across all internal backends
298 * and re-synchronize those files against the "multi master" if needed.
300 * @param array $paths List of storage paths
303 public function resyncFiles( array $paths ) {
304 $status = Status
::newGood();
306 $mBackend = $this->backends
[$this->masterIndex
];
307 foreach ( $paths as $path ) {
308 $mPath = $this->substPaths( $path, $mBackend );
309 $mSha1 = $mBackend->getFileSha1Base36( array( 'src' => $mPath, 'latest' => true ) );
310 $mStat = $mBackend->getFileStat( array( 'src' => $mPath, 'latest' => true ) );
311 if ( $mStat === null ||
( $mSha1 !== false && !$mStat ) ) { // sanity
312 $status->fatal( 'backend-fail-internal', $this->name
);
313 wfDebugLog( 'FileOperation', __METHOD__
314 . ': File is not available on the master backend' );
315 continue; // file is not available on the master backend...
317 // Check of all clone backends agree with the master...
318 foreach ( $this->backends
as $index => $cBackend ) {
319 if ( $index === $this->masterIndex
) {
322 $cPath = $this->substPaths( $path, $cBackend );
323 $cSha1 = $cBackend->getFileSha1Base36( array( 'src' => $cPath, 'latest' => true ) );
324 $cStat = $cBackend->getFileStat( array( 'src' => $cPath, 'latest' => true ) );
325 if ( $cStat === null ||
( $cSha1 !== false && !$cStat ) ) { // sanity
326 $status->fatal( 'backend-fail-internal', $cBackend->getName() );
327 wfDebugLog( 'FileOperation', __METHOD__
.
328 ': File is not available on the clone backend' );
329 continue; // file is not available on the clone backend...
331 if ( $mSha1 === $cSha1 ) {
332 // already synced; nothing to do
333 } elseif ( $mSha1 !== false ) { // file is in master
334 if ( $this->autoResync
=== 'conservative'
335 && $cStat && $cStat['mtime'] > $mStat['mtime']
337 $status->fatal( 'backend-fail-synced', $path );
338 continue; // don't rollback data
340 $fsFile = $mBackend->getLocalReference(
341 array( 'src' => $mPath, 'latest' => true ) );
342 $status->merge( $cBackend->quickStore(
343 array( 'src' => $fsFile->getPath(), 'dst' => $cPath )
345 } elseif ( $mStat === false ) { // file is not in master
346 if ( $this->autoResync
=== 'conservative' ) {
347 $status->fatal( 'backend-fail-synced', $path );
348 continue; // don't delete data
350 $status->merge( $cBackend->quickDelete( array( 'src' => $cPath ) ) );
359 * Get a list of file storage paths to read or write for a list of operations
361 * @param array $ops Same format as doOperations()
362 * @return array List of storage paths to files (does not include directories)
364 protected function fileStoragePathsForOps( array $ops ) {
366 foreach ( $ops as $op ) {
367 if ( isset( $op['src'] ) ) {
368 // For things like copy/move/delete with "ignoreMissingSource" and there
369 // is no source file, nothing should happen and there should be no errors.
370 if ( empty( $op['ignoreMissingSource'] )
371 ||
$this->fileExists( array( 'src' => $op['src'] ) )
373 $paths[] = $op['src'];
376 if ( isset( $op['srcs'] ) ) {
377 $paths = array_merge( $paths, $op['srcs'] );
379 if ( isset( $op['dst'] ) ) {
380 $paths[] = $op['dst'];
384 return array_values( array_unique( array_filter( $paths, 'FileBackend::isStoragePath' ) ) );
388 * Substitute the backend name in storage path parameters
389 * for a set of operations with that of a given internal backend.
391 * @param array $ops List of file operation arrays
392 * @param FileBackendStore $backend
395 protected function substOpBatchPaths( array $ops, FileBackendStore
$backend ) {
396 $newOps = array(); // operations
397 foreach ( $ops as $op ) {
398 $newOp = $op; // operation
399 foreach ( array( 'src', 'srcs', 'dst', 'dir' ) as $par ) {
400 if ( isset( $newOp[$par] ) ) { // string or array
401 $newOp[$par] = $this->substPaths( $newOp[$par], $backend );
411 * Same as substOpBatchPaths() but for a single operation
413 * @param array $ops File operation array
414 * @param FileBackendStore $backend
417 protected function substOpPaths( array $ops, FileBackendStore
$backend ) {
418 $newOps = $this->substOpBatchPaths( array( $ops ), $backend );
424 * Substitute the backend of storage paths with an internal backend's name
426 * @param array|string $paths List of paths or single string path
427 * @param FileBackendStore $backend
428 * @return array|string
430 protected function substPaths( $paths, FileBackendStore
$backend ) {
432 '!^mwstore://' . preg_quote( $this->name
, '!' ) . '/!',
433 StringUtils
::escapeRegexReplacement( "mwstore://{$backend->getName()}/" ),
434 $paths // string or array
439 * Substitute the backend of internal storage paths with the proxy backend's name
441 * @param array|string $paths List of paths or single string path
442 * @return array|string
444 protected function unsubstPaths( $paths ) {
446 '!^mwstore://([^/]+)!',
447 StringUtils
::escapeRegexReplacement( "mwstore://{$this->name}" ),
448 $paths // string or array
452 protected function doQuickOperationsInternal( array $ops ) {
453 $status = Status
::newGood();
454 // Do the operations on the master backend; setting Status fields...
455 $realOps = $this->substOpBatchPaths( $ops, $this->backends
[$this->masterIndex
] );
456 $masterStatus = $this->backends
[$this->masterIndex
]->doQuickOperations( $realOps );
457 $status->merge( $masterStatus );
458 // Propagate the operations to the clone backends...
459 foreach ( $this->backends
as $index => $backend ) {
460 if ( $index !== $this->masterIndex
) { // not done already
461 $realOps = $this->substOpBatchPaths( $ops, $backend );
462 $status->merge( $backend->doQuickOperations( $realOps ) );
465 // Make 'success', 'successCount', and 'failCount' fields reflect
466 // the overall operation, rather than all the batches for each backend.
467 // Do this by only using success values from the master backend's batch.
468 $status->success
= $masterStatus->success
;
469 $status->successCount
= $masterStatus->successCount
;
470 $status->failCount
= $masterStatus->failCount
;
475 protected function doPrepare( array $params ) {
476 $status = Status
::newGood();
477 foreach ( $this->backends
as $index => $backend ) {
478 $realParams = $this->substOpPaths( $params, $backend );
479 $status->merge( $backend->doPrepare( $realParams ) );
485 protected function doSecure( array $params ) {
486 $status = Status
::newGood();
487 foreach ( $this->backends
as $index => $backend ) {
488 $realParams = $this->substOpPaths( $params, $backend );
489 $status->merge( $backend->doSecure( $realParams ) );
495 protected function doPublish( array $params ) {
496 $status = Status
::newGood();
497 foreach ( $this->backends
as $index => $backend ) {
498 $realParams = $this->substOpPaths( $params, $backend );
499 $status->merge( $backend->doPublish( $realParams ) );
505 protected function doClean( array $params ) {
506 $status = Status
::newGood();
507 foreach ( $this->backends
as $index => $backend ) {
508 $realParams = $this->substOpPaths( $params, $backend );
509 $status->merge( $backend->doClean( $realParams ) );
515 public function concatenate( array $params ) {
516 // We are writing to an FS file, so we don't need to do this per-backend
517 $index = $this->getReadIndexFromParams( $params );
518 $realParams = $this->substOpPaths( $params, $this->backends
[$index] );
520 return $this->backends
[$index]->concatenate( $realParams );
523 public function fileExists( array $params ) {
524 $index = $this->getReadIndexFromParams( $params );
525 $realParams = $this->substOpPaths( $params, $this->backends
[$index] );
527 return $this->backends
[$index]->fileExists( $realParams );
530 public function getFileTimestamp( array $params ) {
531 $index = $this->getReadIndexFromParams( $params );
532 $realParams = $this->substOpPaths( $params, $this->backends
[$index] );
534 return $this->backends
[$index]->getFileTimestamp( $realParams );
537 public function getFileSize( array $params ) {
538 $index = $this->getReadIndexFromParams( $params );
539 $realParams = $this->substOpPaths( $params, $this->backends
[$index] );
541 return $this->backends
[$index]->getFileSize( $realParams );
544 public function getFileStat( array $params ) {
545 $index = $this->getReadIndexFromParams( $params );
546 $realParams = $this->substOpPaths( $params, $this->backends
[$index] );
548 return $this->backends
[$index]->getFileStat( $realParams );
551 public function getFileXAttributes( array $params ) {
552 $index = $this->getReadIndexFromParams( $params );
553 $realParams = $this->substOpPaths( $params, $this->backends
[$index] );
555 return $this->backends
[$index]->getFileXAttributes( $realParams );
558 public function getFileContentsMulti( array $params ) {
559 $index = $this->getReadIndexFromParams( $params );
560 $realParams = $this->substOpPaths( $params, $this->backends
[$index] );
562 $contentsM = $this->backends
[$index]->getFileContentsMulti( $realParams );
564 $contents = array(); // (path => FSFile) mapping using the proxy backend's name
565 foreach ( $contentsM as $path => $data ) {
566 $contents[$this->unsubstPaths( $path )] = $data;
572 public function getFileSha1Base36( array $params ) {
573 $index = $this->getReadIndexFromParams( $params );
574 $realParams = $this->substOpPaths( $params, $this->backends
[$index] );
576 return $this->backends
[$index]->getFileSha1Base36( $realParams );
579 public function getFileProps( array $params ) {
580 $index = $this->getReadIndexFromParams( $params );
581 $realParams = $this->substOpPaths( $params, $this->backends
[$index] );
583 return $this->backends
[$index]->getFileProps( $realParams );
586 public function streamFile( array $params ) {
587 $index = $this->getReadIndexFromParams( $params );
588 $realParams = $this->substOpPaths( $params, $this->backends
[$index] );
590 return $this->backends
[$index]->streamFile( $realParams );
593 public function getLocalReferenceMulti( array $params ) {
594 $index = $this->getReadIndexFromParams( $params );
595 $realParams = $this->substOpPaths( $params, $this->backends
[$index] );
597 $fsFilesM = $this->backends
[$index]->getLocalReferenceMulti( $realParams );
599 $fsFiles = array(); // (path => FSFile) mapping using the proxy backend's name
600 foreach ( $fsFilesM as $path => $fsFile ) {
601 $fsFiles[$this->unsubstPaths( $path )] = $fsFile;
607 public function getLocalCopyMulti( array $params ) {
608 $index = $this->getReadIndexFromParams( $params );
609 $realParams = $this->substOpPaths( $params, $this->backends
[$index] );
611 $tempFilesM = $this->backends
[$index]->getLocalCopyMulti( $realParams );
613 $tempFiles = array(); // (path => TempFSFile) mapping using the proxy backend's name
614 foreach ( $tempFilesM as $path => $tempFile ) {
615 $tempFiles[$this->unsubstPaths( $path )] = $tempFile;
621 public function getFileHttpUrl( array $params ) {
622 $index = $this->getReadIndexFromParams( $params );
623 $realParams = $this->substOpPaths( $params, $this->backends
[$index] );
625 return $this->backends
[$index]->getFileHttpUrl( $realParams );
628 public function directoryExists( array $params ) {
629 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
631 return $this->backends
[$this->masterIndex
]->directoryExists( $realParams );
634 public function getDirectoryList( array $params ) {
635 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
637 return $this->backends
[$this->masterIndex
]->getDirectoryList( $realParams );
640 public function getFileList( array $params ) {
641 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
643 return $this->backends
[$this->masterIndex
]->getFileList( $realParams );
646 public function getFeatures() {
647 return $this->backends
[$this->masterIndex
]->getFeatures();
650 public function clearCache( array $paths = null ) {
651 foreach ( $this->backends
as $backend ) {
652 $realPaths = is_array( $paths ) ?
$this->substPaths( $paths, $backend ) : null;
653 $backend->clearCache( $realPaths );
657 public function preloadCache( array $paths ) {
658 $realPaths = $this->substPaths( $paths, $this->backends
[$this->readIndex
] );
659 $this->backends
[$this->readIndex
]->preloadCache( $realPaths );
662 public function preloadFileStat( array $params ) {
663 $index = $this->getReadIndexFromParams( $params );
664 $realParams = $this->substOpPaths( $params, $this->backends
[$index] );
666 return $this->backends
[$index]->preloadFileStat( $realParams );
669 public function getScopedLocksForOps( array $ops, Status
$status ) {
670 $realOps = $this->substOpBatchPaths( $ops, $this->backends
[$this->masterIndex
] );
671 $fileOps = $this->backends
[$this->masterIndex
]->getOperationsInternal( $realOps );
672 // Get the paths to lock from the master backend
673 $paths = $this->backends
[$this->masterIndex
]->getPathsToLockForOpsInternal( $fileOps );
674 // Get the paths under the proxy backend's name
676 LockManager
::LOCK_UW
=> $this->unsubstPaths( $paths[LockManager
::LOCK_UW
] ),
677 LockManager
::LOCK_EX
=> $this->unsubstPaths( $paths[LockManager
::LOCK_EX
] )
680 // Actually acquire the locks
681 return $this->getScopedFileLocks( $pbPaths, 'mixed', $status );
685 * @param array $params
686 * @return int The master or read affinity backend index, based on $params['latest']
688 protected function getReadIndexFromParams( array $params ) {
689 return !empty( $params['latest'] ) ?
$this->masterIndex
: $this->readIndex
;