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, in the order defined.
37 * If an operation fails on one backend it will be rolled back from the others.
39 * @ingroup FileBackend
42 class FileBackendMultiWrite
extends FileBackend
{
43 /** @var Array Prioritized list of FileBackendStore objects */
44 protected $backends = array(); // array of (backend index => backends)
45 protected $masterIndex = -1; // integer; index of master backend
46 protected $syncChecks = 0; // integer; bitfield
47 protected $autoResync = false; // boolean
50 protected $noPushDirConts = array();
51 protected $noPushQuickOps = false; // boolean
53 /* Possible internal backend consistency checks */
59 * Construct a proxy backend that consists of several internal backends.
60 * Locking, journaling, and read-only checks are handled by the proxy backend.
62 * Additional $config params include:
63 * - backends : Array of backend config and multi-backend settings.
64 * Each value is the config used in the constructor of a
65 * FileBackendStore class, but with these additional settings:
66 * - class : The name of the backend class
67 * - isMultiMaster : This must be set for one backend.
68 * - template: : If given a backend name, this will use
69 * the config of that backend as a template.
70 * Values specified here take precedence.
71 * - syncChecks : Integer bitfield of internal backend sync checks to perform.
72 * Possible bits include the FileBackendMultiWrite::CHECK_* constants.
73 * There are constants for SIZE, TIME, and SHA1.
74 * The checks are done before allowing any file operations.
75 * - autoResync : Automatically resync the clone backends to the master backend
76 * when pre-operation sync checks fail. This should only be used
77 * if the master backend is stable and not missing any files.
78 * Use "conservative" to limit resyncing to copying newer master
79 * backend files over older (or non-existing) clone backend files.
80 * Cases that cannot be handled will result in operation abortion.
81 * - noPushQuickOps : (hack) Only apply doQuickOperations() to the master backend.
82 * - noPushDirConts : (hack) Only apply directory functions to the master backend.
84 * @param Array $config
87 public function __construct( array $config ) {
88 parent
::__construct( $config );
89 $this->syncChecks
= isset( $config['syncChecks'] )
90 ?
$config['syncChecks']
92 $this->autoResync
= isset( $config['autoResync'] )
93 ?
$config['autoResync']
95 $this->noPushQuickOps
= isset( $config['noPushQuickOps'] )
96 ?
$config['noPushQuickOps']
98 $this->noPushDirConts
= isset( $config['noPushDirConts'] )
99 ?
$config['noPushDirConts']
101 // Construct backends here rather than via registration
102 // to keep these backends hidden from outside the proxy.
103 $namesUsed = array();
104 foreach ( $config['backends'] as $index => $config ) {
105 if ( isset( $config['template'] ) ) {
106 // Config is just a modified version of a registered backend's.
107 // This should only be used when that config is used only by this backend.
108 $config = $config + FileBackendGroup
::singleton()->config( $config['template'] );
110 $name = $config['name'];
111 if ( isset( $namesUsed[$name] ) ) { // don't break FileOp predicates
112 throw new MWException( "Two or more backends defined with the name $name." );
114 $namesUsed[$name] = 1;
115 // Alter certain sub-backend settings for sanity
116 unset( $config['readOnly'] ); // use proxy backend setting
117 unset( $config['fileJournal'] ); // use proxy backend journal
118 $config['wikiId'] = $this->wikiId
; // use the proxy backend wiki ID
119 $config['lockManager'] = 'nullLockManager'; // lock under proxy backend
120 if ( !empty( $config['isMultiMaster'] ) ) {
121 if ( $this->masterIndex
>= 0 ) {
122 throw new MWException( 'More than one master backend defined.' );
124 $this->masterIndex
= $index; // this is the "master"
125 $config['fileJournal'] = $this->fileJournal
; // log under proxy backend
127 // Create sub-backend object
128 if ( !isset( $config['class'] ) ) {
129 throw new MWException( 'No class given for a backend config.' );
131 $class = $config['class'];
132 $this->backends
[$index] = new $class( $config );
134 if ( $this->masterIndex
< 0 ) { // need backends and must have a master
135 throw new MWException( 'No master backend defined.' );
139 final protected function doOperationsInternal( array $ops, array $opts ) {
140 $status = Status
::newGood();
142 $mbe = $this->backends
[$this->masterIndex
]; // convenience
144 // Get the paths to lock from the master backend
145 $realOps = $this->substOpBatchPaths( $ops, $mbe );
146 $paths = $mbe->getPathsToLockForOpsInternal( $mbe->getOperationsInternal( $realOps ) );
147 // Get the paths under the proxy backend's name
148 $paths['sh'] = $this->unsubstPaths( $paths['sh'] );
149 $paths['ex'] = $this->unsubstPaths( $paths['ex'] );
150 // Try to lock those files for the scope of this function...
151 if ( empty( $opts['nonLocking'] ) ) {
152 // Try to lock those files for the scope of this function...
153 $scopeLockS = $this->getScopedFileLocks( $paths['sh'], LockManager
::LOCK_UW
, $status );
154 $scopeLockE = $this->getScopedFileLocks( $paths['ex'], LockManager
::LOCK_EX
, $status );
155 if ( !$status->isOK() ) {
156 return $status; // abort
159 // Clear any cache entries (after locks acquired)
161 $opts['preserveCache'] = true; // only locked files are cached
162 // Get the list of paths to read/write...
163 $relevantPaths = $this->fileStoragePathsForOps( $ops );
164 // Check if the paths are valid and accessible on all backends...
165 $status->merge( $this->accessibilityCheck( $relevantPaths ) );
166 if ( !$status->isOK() ) {
167 return $status; // abort
169 // Do a consistency check to see if the backends are consistent...
170 $syncStatus = $this->consistencyCheck( $relevantPaths );
171 if ( !$syncStatus->isOK() ) {
172 wfDebugLog( 'FileOperation', get_class( $this ) .
173 " failed sync check: " . FormatJson
::encode( $relevantPaths ) );
174 // Try to resync the clone backends to the master on the spot...
175 if ( !$this->autoResync ||
!$this->resyncFiles( $relevantPaths )->isOK() ) {
176 $status->merge( $syncStatus );
177 return $status; // abort
180 // Actually attempt the operation batch on the master backend...
181 $masterStatus = $mbe->doOperations( $realOps, $opts );
182 $status->merge( $masterStatus );
183 // Propagate the operations to the clone backends if there were no unexpected errors
184 // and if there were either no expected errors or if the 'force' option was used.
185 // However, if nothing succeeded at all, then don't replicate any of the operations.
186 // If $ops only had one operation, this might avoid backend sync inconsistencies.
187 if ( $masterStatus->isOK() && $masterStatus->successCount
> 0 ) {
188 foreach ( $this->backends
as $index => $backend ) {
189 if ( $index !== $this->masterIndex
) { // not done already
190 $realOps = $this->substOpBatchPaths( $ops, $backend );
191 $status->merge( $backend->doOperations( $realOps, $opts ) );
195 // Make 'success', 'successCount', and 'failCount' fields reflect
196 // the overall operation, rather than all the batches for each backend.
197 // Do this by only using success values from the master backend's batch.
198 $status->success
= $masterStatus->success
;
199 $status->successCount
= $masterStatus->successCount
;
200 $status->failCount
= $masterStatus->failCount
;
206 * Check that a set of files are consistent across all internal backends
208 * @param array $paths List of storage paths
211 public function consistencyCheck( array $paths ) {
212 $status = Status
::newGood();
213 if ( $this->syncChecks
== 0 ||
count( $this->backends
) <= 1 ) {
214 return $status; // skip checks
217 $mBackend = $this->backends
[$this->masterIndex
];
218 foreach ( $paths as $path ) {
219 $params = array( 'src' => $path, 'latest' => true );
220 $mParams = $this->substOpPaths( $params, $mBackend );
221 // Stat the file on the 'master' backend
222 $mStat = $mBackend->getFileStat( $mParams );
223 if ( $this->syncChecks
& self
::CHECK_SHA1
) {
224 $mSha1 = $mBackend->getFileSha1Base36( $mParams );
228 // Check if all clone backends agree with the master...
229 foreach ( $this->backends
as $index => $cBackend ) {
230 if ( $index === $this->masterIndex
) {
233 $cParams = $this->substOpPaths( $params, $cBackend );
234 $cStat = $cBackend->getFileStat( $cParams );
235 if ( $mStat ) { // file is in master
236 if ( !$cStat ) { // file should exist
237 $status->fatal( 'backend-fail-synced', $path );
240 if ( $this->syncChecks
& self
::CHECK_SIZE
) {
241 if ( $cStat['size'] != $mStat['size'] ) { // wrong size
242 $status->fatal( 'backend-fail-synced', $path );
246 if ( $this->syncChecks
& self
::CHECK_TIME
) {
247 $mTs = wfTimestamp( TS_UNIX
, $mStat['mtime'] );
248 $cTs = wfTimestamp( TS_UNIX
, $cStat['mtime'] );
249 if ( abs( $mTs - $cTs ) > 30 ) { // outdated file somewhere
250 $status->fatal( 'backend-fail-synced', $path );
254 if ( $this->syncChecks
& self
::CHECK_SHA1
) {
255 if ( $cBackend->getFileSha1Base36( $cParams ) !== $mSha1 ) { // wrong SHA1
256 $status->fatal( 'backend-fail-synced', $path );
260 } else { // file is not in master
261 if ( $cStat ) { // file should not exist
262 $status->fatal( 'backend-fail-synced', $path );
272 * Check that a set of file paths are usable across all internal backends
274 * @param array $paths List of storage paths
277 public function accessibilityCheck( array $paths ) {
278 $status = Status
::newGood();
279 if ( count( $this->backends
) <= 1 ) {
280 return $status; // skip checks
283 foreach ( $paths as $path ) {
284 foreach ( $this->backends
as $backend ) {
285 $realPath = $this->substPaths( $path, $backend );
286 if ( !$backend->isPathUsableInternal( $realPath ) ) {
287 $status->fatal( 'backend-fail-usable', $path );
296 * Check that a set of files are consistent across all internal backends
297 * and re-synchronize those files againt the "multi master" if needed.
299 * @param array $paths List of storage paths
302 public function resyncFiles( array $paths ) {
303 $status = Status
::newGood();
305 $mBackend = $this->backends
[$this->masterIndex
];
306 foreach ( $paths as $path ) {
307 $mPath = $this->substPaths( $path, $mBackend );
308 $mSha1 = $mBackend->getFileSha1Base36( array( 'src' => $mPath, 'latest' => true ) );
309 $mStat = $mBackend->getFileStat( array( 'src' => $mPath, 'latest' => true ) );
310 if ( $mStat === null ||
( $mSha1 !== false && !$mStat ) ) { // sanity
311 $status->fatal( 'backend-fail-internal', $this->name
);
312 continue; // file is not available on the master backend...
314 // Check of all clone backends agree with the master...
315 foreach ( $this->backends
as $index => $cBackend ) {
316 if ( $index === $this->masterIndex
) {
319 $cPath = $this->substPaths( $path, $cBackend );
320 $cSha1 = $cBackend->getFileSha1Base36( array( 'src' => $cPath, 'latest' => true ) );
321 $cStat = $cBackend->getFileStat( array( 'src' => $cPath, 'latest' => true ) );
322 if ( $cStat === null ||
( $cSha1 !== false && !$cStat ) ) { // sanity
323 $status->fatal( 'backend-fail-internal', $cBackend->getName() );
324 continue; // file is not available on the clone backend...
326 if ( $mSha1 === $cSha1 ) {
327 // already synced; nothing to do
328 } elseif ( $mSha1 !== false ) { // file is in master
329 if ( $this->autoResync
=== 'conservative'
330 && $cStat && $cStat['mtime'] > $mStat['mtime'] )
332 $status->fatal( 'backend-fail-synced', $path );
333 continue; // don't rollback data
335 $fsFile = $mBackend->getLocalReference(
336 array( 'src' => $mPath, 'latest' => true ) );
337 $status->merge( $cBackend->quickStore(
338 array( 'src' => $fsFile->getPath(), 'dst' => $cPath )
340 } elseif ( $mStat === false ) { // file is not in master
341 if ( $this->autoResync
=== 'conservative' ) {
342 $status->fatal( 'backend-fail-synced', $path );
343 continue; // don't delete data
345 $status->merge( $cBackend->quickDelete( array( 'src' => $cPath ) ) );
354 * Get a list of file storage paths to read or write for a list of operations
356 * @param array $ops Same format as doOperations()
357 * @return Array List of storage paths to files (does not include directories)
359 protected function fileStoragePathsForOps( array $ops ) {
361 foreach ( $ops as $op ) {
362 if ( isset( $op['src'] ) ) {
363 // For things like copy/move/delete with "ignoreMissingSource" and there
364 // is no source file, nothing should happen and there should be no errors.
365 if ( empty( $op['ignoreMissingSource'] )
366 ||
$this->fileExists( array( 'src' => $op['src'] ) ) )
368 $paths[] = $op['src'];
371 if ( isset( $op['srcs'] ) ) {
372 $paths = array_merge( $paths, $op['srcs'] );
374 if ( isset( $op['dst'] ) ) {
375 $paths[] = $op['dst'];
378 return array_values( array_unique( array_filter( $paths, 'FileBackend::isStoragePath' ) ) );
382 * Substitute the backend name in storage path parameters
383 * for a set of operations with that of a given internal backend.
385 * @param array $ops List of file operation arrays
386 * @param FileBackendStore $backend
389 protected function substOpBatchPaths( array $ops, FileBackendStore
$backend ) {
390 $newOps = array(); // operations
391 foreach ( $ops as $op ) {
392 $newOp = $op; // operation
393 foreach ( array( 'src', 'srcs', 'dst', 'dir' ) as $par ) {
394 if ( isset( $newOp[$par] ) ) { // string or array
395 $newOp[$par] = $this->substPaths( $newOp[$par], $backend );
404 * Same as substOpBatchPaths() but for a single operation
406 * @param array $ops File operation array
407 * @param FileBackendStore $backend
410 protected function substOpPaths( array $ops, FileBackendStore
$backend ) {
411 $newOps = $this->substOpBatchPaths( array( $ops ), $backend );
416 * Substitute the backend of storage paths with an internal backend's name
418 * @param array|string $paths List of paths or single string path
419 * @param FileBackendStore $backend
420 * @return Array|string
422 protected function substPaths( $paths, FileBackendStore
$backend ) {
424 '!^mwstore://' . preg_quote( $this->name
) . '/!',
425 StringUtils
::escapeRegexReplacement( "mwstore://{$backend->getName()}/" ),
426 $paths // string or array
431 * Substitute the backend of internal storage paths with the proxy backend's name
433 * @param array|string $paths List of paths or single string path
434 * @return Array|string
436 protected function unsubstPaths( $paths ) {
438 '!^mwstore://([^/]+)!',
439 StringUtils
::escapeRegexReplacement( "mwstore://{$this->name}" ),
440 $paths // string or array
444 protected function doQuickOperationsInternal( array $ops ) {
445 $status = Status
::newGood();
446 // Do the operations on the master backend; setting Status fields...
447 $realOps = $this->substOpBatchPaths( $ops, $this->backends
[$this->masterIndex
] );
448 $masterStatus = $this->backends
[$this->masterIndex
]->doQuickOperations( $realOps );
449 $status->merge( $masterStatus );
450 // Propagate the operations to the clone backends...
451 if ( !$this->noPushQuickOps
) {
452 foreach ( $this->backends
as $index => $backend ) {
453 if ( $index !== $this->masterIndex
) { // not done already
454 $realOps = $this->substOpBatchPaths( $ops, $backend );
455 $status->merge( $backend->doQuickOperations( $realOps ) );
459 // Make 'success', 'successCount', and 'failCount' fields reflect
460 // the overall operation, rather than all the batches for each backend.
461 // Do this by only using success values from the master backend's batch.
462 $status->success
= $masterStatus->success
;
463 $status->successCount
= $masterStatus->successCount
;
464 $status->failCount
= $masterStatus->failCount
;
469 * @param string $path Storage path
470 * @return bool Path container should have dir changes pushed to all backends
472 protected function replicateContainerDirChanges( $path ) {
473 list( , $shortCont, ) = self
::splitStoragePath( $path );
474 return !in_array( $shortCont, $this->noPushDirConts
);
477 protected function doPrepare( array $params ) {
478 $status = Status
::newGood();
479 $replicate = $this->replicateContainerDirChanges( $params['dir'] );
480 foreach ( $this->backends
as $index => $backend ) {
481 if ( $replicate ||
$index == $this->masterIndex
) {
482 $realParams = $this->substOpPaths( $params, $backend );
483 $status->merge( $backend->doPrepare( $realParams ) );
489 protected function doSecure( array $params ) {
490 $status = Status
::newGood();
491 $replicate = $this->replicateContainerDirChanges( $params['dir'] );
492 foreach ( $this->backends
as $index => $backend ) {
493 if ( $replicate ||
$index == $this->masterIndex
) {
494 $realParams = $this->substOpPaths( $params, $backend );
495 $status->merge( $backend->doSecure( $realParams ) );
501 protected function doPublish( array $params ) {
502 $status = Status
::newGood();
503 $replicate = $this->replicateContainerDirChanges( $params['dir'] );
504 foreach ( $this->backends
as $index => $backend ) {
505 if ( $replicate ||
$index == $this->masterIndex
) {
506 $realParams = $this->substOpPaths( $params, $backend );
507 $status->merge( $backend->doPublish( $realParams ) );
513 protected function doClean( array $params ) {
514 $status = Status
::newGood();
515 $replicate = $this->replicateContainerDirChanges( $params['dir'] );
516 foreach ( $this->backends
as $index => $backend ) {
517 if ( $replicate ||
$index == $this->masterIndex
) {
518 $realParams = $this->substOpPaths( $params, $backend );
519 $status->merge( $backend->doClean( $realParams ) );
525 public function concatenate( array $params ) {
526 // We are writing to an FS file, so we don't need to do this per-backend
527 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
528 return $this->backends
[$this->masterIndex
]->concatenate( $realParams );
531 public function fileExists( array $params ) {
532 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
533 return $this->backends
[$this->masterIndex
]->fileExists( $realParams );
536 public function getFileTimestamp( array $params ) {
537 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
538 return $this->backends
[$this->masterIndex
]->getFileTimestamp( $realParams );
541 public function getFileSize( array $params ) {
542 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
543 return $this->backends
[$this->masterIndex
]->getFileSize( $realParams );
546 public function getFileStat( array $params ) {
547 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
548 return $this->backends
[$this->masterIndex
]->getFileStat( $realParams );
551 public function getFileContentsMulti( array $params ) {
552 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
553 $contentsM = $this->backends
[$this->masterIndex
]->getFileContentsMulti( $realParams );
555 $contents = array(); // (path => FSFile) mapping using the proxy backend's name
556 foreach ( $contentsM as $path => $data ) {
557 $contents[$this->unsubstPaths( $path )] = $data;
562 public function getFileSha1Base36( array $params ) {
563 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
564 return $this->backends
[$this->masterIndex
]->getFileSha1Base36( $realParams );
567 public function getFileProps( array $params ) {
568 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
569 return $this->backends
[$this->masterIndex
]->getFileProps( $realParams );
572 public function streamFile( array $params ) {
573 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
574 return $this->backends
[$this->masterIndex
]->streamFile( $realParams );
577 public function getLocalReferenceMulti( array $params ) {
578 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
579 $fsFilesM = $this->backends
[$this->masterIndex
]->getLocalReferenceMulti( $realParams );
581 $fsFiles = array(); // (path => FSFile) mapping using the proxy backend's name
582 foreach ( $fsFilesM as $path => $fsFile ) {
583 $fsFiles[$this->unsubstPaths( $path )] = $fsFile;
588 public function getLocalCopyMulti( array $params ) {
589 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
590 $tempFilesM = $this->backends
[$this->masterIndex
]->getLocalCopyMulti( $realParams );
592 $tempFiles = array(); // (path => TempFSFile) mapping using the proxy backend's name
593 foreach ( $tempFilesM as $path => $tempFile ) {
594 $tempFiles[$this->unsubstPaths( $path )] = $tempFile;
599 public function getFileHttpUrl( array $params ) {
600 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
601 return $this->backends
[$this->masterIndex
]->getFileHttpUrl( $realParams );
604 public function directoryExists( array $params ) {
605 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
606 return $this->backends
[$this->masterIndex
]->directoryExists( $realParams );
609 public function getDirectoryList( array $params ) {
610 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
611 return $this->backends
[$this->masterIndex
]->getDirectoryList( $realParams );
614 public function getFileList( array $params ) {
615 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
616 return $this->backends
[$this->masterIndex
]->getFileList( $realParams );
619 public function clearCache( array $paths = null ) {
620 foreach ( $this->backends
as $backend ) {
621 $realPaths = is_array( $paths ) ?
$this->substPaths( $paths, $backend ) : null;
622 $backend->clearCache( $realPaths );
626 public function getScopedLocksForOps( array $ops, Status
$status ) {
627 $fileOps = $this->backends
[$this->masterIndex
]->getOperationsInternal( $ops );
628 // Get the paths to lock from the master backend
629 $paths = $this->backends
[$this->masterIndex
]->getPathsToLockForOpsInternal( $fileOps );
630 // Get the paths under the proxy backend's name
631 $paths['sh'] = $this->unsubstPaths( $paths['sh'] );
632 $paths['ex'] = $this->unsubstPaths( $paths['ex'] );
634 $this->getScopedFileLocks( $paths['sh'], LockManager
::LOCK_UW
, $status ),
635 $this->getScopedFileLocks( $paths['ex'], LockManager
::LOCK_EX
, $status )