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
48 /* Possible internal backend consistency checks */
54 * Construct a proxy backend that consists of several internal backends.
55 * Locking, journaling, and read-only checks are handled by the proxy backend.
57 * Additional $config params include:
58 * - backends : Array of backend config and multi-backend settings.
59 * Each value is the config used in the constructor of a
60 * FileBackendStore class, but with these additional settings:
61 * - class : The name of the backend class
62 * - isMultiMaster : This must be set for one backend.
63 * - template: : If given a backend name, this will use
64 * the config of that backend as a template.
65 * Values specified here take precedence.
66 * - syncChecks : Integer bitfield of internal backend sync checks to perform.
67 * Possible bits include the FileBackendMultiWrite::CHECK_* constants.
68 * There are constants for SIZE, TIME, and SHA1.
69 * The checks are done before allowing any file operations.
70 * @param $config Array
73 public function __construct( array $config ) {
74 parent
::__construct( $config );
76 // Construct backends here rather than via registration
77 // to keep these backends hidden from outside the proxy.
78 foreach ( $config['backends'] as $index => $config ) {
79 if ( isset( $config['template'] ) ) {
80 // Config is just a modified version of a registered backend's.
81 // This should only be used when that config is used only by this backend.
82 $config = $config + FileBackendGroup
::singleton()->config( $config['template'] );
84 $name = $config['name'];
85 if ( isset( $namesUsed[$name] ) ) { // don't break FileOp predicates
86 throw new MWException( "Two or more backends defined with the name $name." );
88 $namesUsed[$name] = 1;
89 // Alter certain sub-backend settings for sanity
90 unset( $config['readOnly'] ); // use proxy backend setting
91 unset( $config['fileJournal'] ); // use proxy backend journal
92 $config['lockManager'] = 'nullLockManager'; // lock under proxy backend
93 if ( !empty( $config['isMultiMaster'] ) ) {
94 if ( $this->masterIndex
>= 0 ) {
95 throw new MWException( 'More than one master backend defined.' );
97 $this->masterIndex
= $index; // this is the "master"
98 $config['fileJournal'] = $this->fileJournal
; // log under proxy backend
100 // Create sub-backend object
101 if ( !isset( $config['class'] ) ) {
102 throw new MWException( 'No class given for a backend config.' );
104 $class = $config['class'];
105 $this->backends
[$index] = new $class( $config );
107 if ( $this->masterIndex
< 0 ) { // need backends and must have a master
108 throw new MWException( 'No master backend defined.' );
110 $this->syncChecks
= isset( $config['syncChecks'] )
111 ?
$config['syncChecks']
116 * @see FileBackend::doOperationsInternal()
119 final protected function doOperationsInternal( array $ops, array $opts ) {
120 $status = Status
::newGood();
122 $mbe = $this->backends
[$this->masterIndex
]; // convenience
124 // Get the paths to lock from the master backend
125 $realOps = $this->substOpBatchPaths( $ops, $mbe );
126 $paths = $mbe->getPathsToLockForOpsInternal( $mbe->getOperationsInternal( $realOps ) );
127 // Get the paths under the proxy backend's name
128 $paths['sh'] = $this->unsubstPaths( $paths['sh'] );
129 $paths['ex'] = $this->unsubstPaths( $paths['ex'] );
130 // Try to lock those files for the scope of this function...
131 if ( empty( $opts['nonLocking'] ) ) {
132 // Try to lock those files for the scope of this function...
133 $scopeLockS = $this->getScopedFileLocks( $paths['sh'], LockManager
::LOCK_UW
, $status );
134 $scopeLockE = $this->getScopedFileLocks( $paths['ex'], LockManager
::LOCK_EX
, $status );
135 if ( !$status->isOK() ) {
136 return $status; // abort
139 // Clear any cache entries (after locks acquired)
141 // Do a consistency check to see if the backends agree
142 $status->merge( $this->consistencyCheck( array_merge( $paths['sh'], $paths['ex'] ) ) );
143 if ( !$status->isOK() ) {
144 return $status; // abort
146 // Actually attempt the operation batch on the master backend...
147 $masterStatus = $mbe->doOperations( $realOps, $opts );
148 $status->merge( $masterStatus );
149 // Propagate the operations to the clone backends...
150 foreach ( $this->backends
as $index => $backend ) {
151 if ( $index !== $this->masterIndex
) { // not done already
152 $realOps = $this->substOpBatchPaths( $ops, $backend );
153 $status->merge( $backend->doOperations( $realOps, $opts ) );
156 // Make 'success', 'successCount', and 'failCount' fields reflect
157 // the overall operation, rather than all the batches for each backend.
158 // Do this by only using success values from the master backend's batch.
159 $status->success
= $masterStatus->success
;
160 $status->successCount
= $masterStatus->successCount
;
161 $status->failCount
= $masterStatus->failCount
;
167 * Check that a set of files are consistent across all internal backends
169 * @param $paths Array
172 public function consistencyCheck( array $paths ) {
173 $status = Status
::newGood();
174 if ( $this->syncChecks
== 0 ||
count( $this->backends
) <= 1 ) {
175 return $status; // skip checks
178 $mBackend = $this->backends
[$this->masterIndex
];
179 foreach ( array_unique( $paths ) as $path ) {
180 $params = array( 'src' => $path, 'latest' => true );
181 $mParams = $this->substOpPaths( $params, $mBackend );
182 // Stat the file on the 'master' backend
183 $mStat = $mBackend->getFileStat( $mParams );
184 if ( $this->syncChecks
& self
::CHECK_SHA1
) {
185 $mSha1 = $mBackend->getFileSha1( $mParams );
189 $mUsable = $mBackend->isPathUsableInternal( $mParams['src'] );
190 // Check of all clone backends agree with the master...
191 foreach ( $this->backends
as $index => $cBackend ) {
192 if ( $index === $this->masterIndex
) {
195 $cParams = $this->substOpPaths( $params, $cBackend );
196 $cStat = $cBackend->getFileStat( $cParams );
197 if ( $mStat ) { // file is in master
198 if ( !$cStat ) { // file should exist
199 $status->fatal( 'backend-fail-synced', $path );
202 if ( $this->syncChecks
& self
::CHECK_SIZE
) {
203 if ( $cStat['size'] != $mStat['size'] ) { // wrong size
204 $status->fatal( 'backend-fail-synced', $path );
208 if ( $this->syncChecks
& self
::CHECK_TIME
) {
209 $mTs = wfTimestamp( TS_UNIX
, $mStat['mtime'] );
210 $cTs = wfTimestamp( TS_UNIX
, $cStat['mtime'] );
211 if ( abs( $mTs - $cTs ) > 30 ) { // outdated file somewhere
212 $status->fatal( 'backend-fail-synced', $path );
216 if ( $this->syncChecks
& self
::CHECK_SHA1
) {
217 if ( $cBackend->getFileSha1( $cParams ) !== $mSha1 ) { // wrong SHA1
218 $status->fatal( 'backend-fail-synced', $path );
222 } else { // file is not in master
223 if ( $cStat ) { // file should not exist
224 $status->fatal( 'backend-fail-synced', $path );
227 if ( $mUsable !== $cBackend->isPathUsableInternal( $cParams['src'] ) ) {
228 $status->fatal( 'backend-fail-synced', $path );
237 * Substitute the backend name in storage path parameters
238 * for a set of operations with that of a given internal backend.
240 * @param $ops Array List of file operation arrays
241 * @param $backend FileBackendStore
244 protected function substOpBatchPaths( array $ops, FileBackendStore
$backend ) {
245 $newOps = array(); // operations
246 foreach ( $ops as $op ) {
247 $newOp = $op; // operation
248 foreach ( array( 'src', 'srcs', 'dst', 'dir' ) as $par ) {
249 if ( isset( $newOp[$par] ) ) { // string or array
250 $newOp[$par] = $this->substPaths( $newOp[$par], $backend );
259 * Same as substOpBatchPaths() but for a single operation
261 * @param $ops array File operation array
262 * @param $backend FileBackendStore
265 protected function substOpPaths( array $ops, FileBackendStore
$backend ) {
266 $newOps = $this->substOpBatchPaths( array( $ops ), $backend );
271 * Substitute the backend of storage paths with an internal backend's name
273 * @param $paths Array|string List of paths or single string path
274 * @param $backend FileBackendStore
275 * @return Array|string
277 protected function substPaths( $paths, FileBackendStore
$backend ) {
279 '!^mwstore://' . preg_quote( $this->name
) . '/!',
280 StringUtils
::escapeRegexReplacement( "mwstore://{$backend->getName()}/" ),
281 $paths // string or array
286 * Substitute the backend of internal storage paths with the proxy backend's name
288 * @param $paths Array|string List of paths or single string path
289 * @return Array|string
291 protected function unsubstPaths( $paths ) {
293 '!^mwstore://([^/]+)!',
294 StringUtils
::escapeRegexReplacement( "mwstore://{$this->name}" ),
295 $paths // string or array
300 * @see FileBackend::doQuickOperationsInternal()
303 protected function doQuickOperationsInternal( array $ops ) {
304 $status = Status
::newGood();
305 // Do the operations on the master backend; setting Status fields...
306 $realOps = $this->substOpBatchPaths( $ops, $this->backends
[$this->masterIndex
] );
307 $masterStatus = $this->backends
[$this->masterIndex
]->doQuickOperations( $realOps );
308 $status->merge( $masterStatus );
309 // Propagate the operations to the clone backends...
310 foreach ( $this->backends
as $index => $backend ) {
311 if ( $index !== $this->masterIndex
) { // not done already
312 $realOps = $this->substOpBatchPaths( $ops, $backend );
313 $status->merge( $backend->doQuickOperations( $realOps ) );
316 // Make 'success', 'successCount', and 'failCount' fields reflect
317 // the overall operation, rather than all the batches for each backend.
318 // Do this by only using success values from the master backend's batch.
319 $status->success
= $masterStatus->success
;
320 $status->successCount
= $masterStatus->successCount
;
321 $status->failCount
= $masterStatus->failCount
;
326 * @see FileBackend::doPrepare()
329 protected function doPrepare( array $params ) {
330 $status = Status
::newGood();
331 foreach ( $this->backends
as $backend ) {
332 $realParams = $this->substOpPaths( $params, $backend );
333 $status->merge( $backend->doPrepare( $realParams ) );
339 * @see FileBackend::doSecure()
340 * @param $params array
343 protected function doSecure( array $params ) {
344 $status = Status
::newGood();
345 foreach ( $this->backends
as $backend ) {
346 $realParams = $this->substOpPaths( $params, $backend );
347 $status->merge( $backend->doSecure( $realParams ) );
353 * @see FileBackend::doPublish()
354 * @param $params array
357 protected function doPublish( array $params ) {
358 $status = Status
::newGood();
359 foreach ( $this->backends
as $backend ) {
360 $realParams = $this->substOpPaths( $params, $backend );
361 $status->merge( $backend->doPublish( $realParams ) );
367 * @see FileBackend::doClean()
368 * @param $params array
371 protected function doClean( array $params ) {
372 $status = Status
::newGood();
373 foreach ( $this->backends
as $backend ) {
374 $realParams = $this->substOpPaths( $params, $backend );
375 $status->merge( $backend->doClean( $realParams ) );
381 * @see FileBackend::concatenate()
382 * @param $params array
385 public function concatenate( array $params ) {
386 // We are writing to an FS file, so we don't need to do this per-backend
387 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
388 return $this->backends
[$this->masterIndex
]->concatenate( $realParams );
392 * @see FileBackend::fileExists()
393 * @param $params array
395 public function fileExists( array $params ) {
396 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
397 return $this->backends
[$this->masterIndex
]->fileExists( $realParams );
401 * @see FileBackend::getFileTimestamp()
402 * @param $params array
403 * @return bool|string
405 public function getFileTimestamp( array $params ) {
406 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
407 return $this->backends
[$this->masterIndex
]->getFileTimestamp( $realParams );
411 * @see FileBackend::getFileSize()
412 * @param $params array
415 public function getFileSize( array $params ) {
416 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
417 return $this->backends
[$this->masterIndex
]->getFileSize( $realParams );
421 * @see FileBackend::getFileStat()
422 * @param $params array
423 * @return Array|bool|null
425 public function getFileStat( array $params ) {
426 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
427 return $this->backends
[$this->masterIndex
]->getFileStat( $realParams );
431 * @see FileBackend::getFileContents()
432 * @param $params array
433 * @return bool|string
435 public function getFileContents( array $params ) {
436 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
437 return $this->backends
[$this->masterIndex
]->getFileContents( $realParams );
441 * @see FileBackend::getFileSha1Base36()
442 * @param $params array
443 * @return bool|string
445 public function getFileSha1Base36( array $params ) {
446 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
447 return $this->backends
[$this->masterIndex
]->getFileSha1Base36( $realParams );
451 * @see FileBackend::getFileProps()
452 * @param $params array
455 public function getFileProps( array $params ) {
456 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
457 return $this->backends
[$this->masterIndex
]->getFileProps( $realParams );
461 * @see FileBackend::streamFile()
462 * @param $params array
465 public function streamFile( array $params ) {
466 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
467 return $this->backends
[$this->masterIndex
]->streamFile( $realParams );
471 * @see FileBackend::getLocalReference()
472 * @param $params array
473 * @return FSFile|null
475 public function getLocalReference( array $params ) {
476 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
477 return $this->backends
[$this->masterIndex
]->getLocalReference( $realParams );
481 * @see FileBackend::getLocalCopy()
482 * @param $params array
483 * @return null|TempFSFile
485 public function getLocalCopy( array $params ) {
486 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
487 return $this->backends
[$this->masterIndex
]->getLocalCopy( $realParams );
491 * @see FileBackend::directoryExists()
492 * @param $params array
495 public function directoryExists( array $params ) {
496 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
497 return $this->backends
[$this->masterIndex
]->directoryExists( $realParams );
501 * @see FileBackend::getSubdirectoryList()
502 * @param $params array
503 * @return Array|null|Traversable
505 public function getDirectoryList( array $params ) {
506 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
507 return $this->backends
[$this->masterIndex
]->getDirectoryList( $realParams );
511 * @see FileBackend::getFileList()
512 * @param $params array
513 * @return Array|null|\Traversable
515 public function getFileList( array $params ) {
516 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
517 return $this->backends
[$this->masterIndex
]->getFileList( $realParams );
521 * @see FileBackend::clearCache()
523 public function clearCache( array $paths = null ) {
524 foreach ( $this->backends
as $backend ) {
525 $realPaths = is_array( $paths ) ?
$this->substPaths( $paths, $backend ) : null;
526 $backend->clearCache( $realPaths );
531 * @see FileBackend::getScopedLocksForOps()
533 public function getScopedLocksForOps( array $ops, Status
$status ) {
534 $fileOps = $this->backends
[$this->masterIndex
]->getOperationsInternal( $ops );
535 // Get the paths to lock from the master backend
536 $paths = $this->backends
[$this->masterIndex
]->getPathsToLockForOpsInternal( $fileOps );
537 // Get the paths under the proxy backend's name
538 $paths['sh'] = $this->unsubstPaths( $paths['sh'] );
539 $paths['ex'] = $this->unsubstPaths( $paths['ex'] );
541 $this->getScopedFileLocks( $paths['sh'], LockManager
::LOCK_UW
, $status ),
542 $this->getScopedFileLocks( $paths['ex'], LockManager
::LOCK_EX
, $status )