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 */
53 * Construct a proxy backend that consists of several internal backends.
54 * Additional $config params include:
55 * 'backends' : Array of backend config and multi-backend settings.
56 * Each value is the config used in the constructor of a
57 * FileBackendStore class, but with these additional settings:
58 * 'class' : The name of the backend class
59 * 'isMultiMaster' : This must be set for one backend.
60 * 'syncChecks' : Integer bitfield of internal backend sync checks to perform.
61 * Possible bits include self::CHECK_SIZE and self::CHECK_TIME.
62 * The checks are done before allowing any file operations.
63 * @param $config Array
65 public function __construct( array $config ) {
66 parent
::__construct( $config );
68 // Construct backends here rather than via registration
69 // to keep these backends hidden from outside the proxy.
70 foreach ( $config['backends'] as $index => $config ) {
71 $name = $config['name'];
72 if ( isset( $namesUsed[$name] ) ) { // don't break FileOp predicates
73 throw new MWException( "Two or more backends defined with the name $name." );
75 $namesUsed[$name] = 1;
76 if ( !isset( $config['class'] ) ) {
77 throw new MWException( 'No class given for a backend config.' );
79 $class = $config['class'];
80 $this->backends
[$index] = new $class( $config );
81 if ( !empty( $config['isMultiMaster'] ) ) {
82 if ( $this->masterIndex
>= 0 ) {
83 throw new MWException( 'More than one master backend defined.' );
85 $this->masterIndex
= $index;
88 if ( $this->masterIndex
< 0 ) { // need backends and must have a master
89 throw new MWException( 'No master backend defined.' );
91 $this->syncChecks
= isset( $config['syncChecks'] )
92 ?
$config['syncChecks']
97 * @see FileBackend::doOperationsInternal()
100 final protected function doOperationsInternal( array $ops, array $opts ) {
101 $status = Status
::newGood();
103 $performOps = array(); // list of FileOp objects
104 $paths = array(); // storage paths read from or written to
105 // Build up a list of FileOps. The list will have all the ops
106 // for one backend, then all the ops for the next, and so on.
107 // These batches of ops are all part of a continuous array.
108 // Also build up a list of files read/changed...
109 foreach ( $this->backends
as $index => $backend ) {
110 $backendOps = $this->substOpBatchPaths( $ops, $backend );
111 // Add on the operation batch for this backend
112 $performOps = array_merge( $performOps,
113 $backend->getOperationsInternal( $backendOps ) );
114 if ( $index == 0 ) { // first batch
115 // Get the files used for these operations. Each backend has a batch of
116 // the same operations, so we only need to get them from the first batch.
117 $paths = $backend->getPathsToLockForOpsInternal( $performOps );
118 // Get the paths under the proxy backend's name
119 $paths['sh'] = $this->unsubstPaths( $paths['sh'] );
120 $paths['ex'] = $this->unsubstPaths( $paths['ex'] );
124 // Try to lock those files for the scope of this function...
125 if ( empty( $opts['nonLocking'] ) ) {
126 // Try to lock those files for the scope of this function...
127 $scopeLockS = $this->getScopedFileLocks( $paths['sh'], LockManager
::LOCK_UW
, $status );
128 $scopeLockE = $this->getScopedFileLocks( $paths['ex'], LockManager
::LOCK_EX
, $status );
129 if ( !$status->isOK() ) {
130 return $status; // abort
134 // Clear any cache entries (after locks acquired)
137 // Do a consistency check to see if the backends agree
138 if ( count( $this->backends
) > 1 ) {
139 $status->merge( $this->consistencyCheck( array_merge( $paths['sh'], $paths['ex'] ) ) );
140 if ( !$status->isOK() ) {
141 return $status; // abort
145 // Actually attempt the operation batch...
146 $subStatus = FileOpBatch
::attempt( $performOps, $opts, $this->fileJournal
);
151 // Make 'success', 'successCount', and 'failCount' fields reflect
152 // the overall operation, rather than all the batches for each backend.
153 // Do this by only using success values from the master backend's batch.
154 $batchStart = $this->masterIndex
* count( $ops );
155 $batchEnd = $batchStart +
count( $ops ) - 1;
156 for ( $i = $batchStart; $i <= $batchEnd; $i++
) {
157 if ( !isset( $subStatus->success
[$i] ) ) {
158 break; // failed out before trying this op
159 } elseif ( $subStatus->success
[$i] ) {
164 $success[] = $subStatus->success
[$i];
166 $subStatus->success
= $success;
167 $subStatus->successCount
= $successCount;
168 $subStatus->failCount
= $failCount;
170 // Merge errors into status fields
171 $status->merge( $subStatus );
172 $status->success
= $subStatus->success
; // not done in merge()
178 * Check that a set of files are consistent across all internal backends
180 * @param $paths Array
183 public function consistencyCheck( array $paths ) {
184 $status = Status
::newGood();
185 if ( $this->syncChecks
== 0 ) {
186 return $status; // skip checks
189 $mBackend = $this->backends
[$this->masterIndex
];
190 foreach ( array_unique( $paths ) as $path ) {
191 $params = array( 'src' => $path, 'latest' => true );
192 // Stat the file on the 'master' backend
193 $mStat = $mBackend->getFileStat( $this->substOpPaths( $params, $mBackend ) );
194 // Check of all clone backends agree with the master...
195 foreach ( $this->backends
as $index => $cBackend ) {
196 if ( $index === $this->masterIndex
) {
199 $cStat = $cBackend->getFileStat( $this->substOpPaths( $params, $cBackend ) );
200 if ( $mStat ) { // file is in master
201 if ( !$cStat ) { // file should exist
202 $status->fatal( 'backend-fail-synced', $path );
205 if ( $this->syncChecks
& self
::CHECK_SIZE
) {
206 if ( $cStat['size'] != $mStat['size'] ) { // wrong size
207 $status->fatal( 'backend-fail-synced', $path );
211 if ( $this->syncChecks
& self
::CHECK_TIME
) {
212 $mTs = wfTimestamp( TS_UNIX
, $mStat['mtime'] );
213 $cTs = wfTimestamp( TS_UNIX
, $cStat['mtime'] );
214 if ( abs( $mTs - $cTs ) > 30 ) { // outdated file somewhere
215 $status->fatal( 'backend-fail-synced', $path );
219 } else { // file is not in master
220 if ( $cStat ) { // file should not exist
221 $status->fatal( 'backend-fail-synced', $path );
231 * Substitute the backend name in storage path parameters
232 * for a set of operations with that of a given internal backend.
234 * @param $ops Array List of file operation arrays
235 * @param $backend FileBackendStore
238 protected function substOpBatchPaths( array $ops, FileBackendStore
$backend ) {
239 $newOps = array(); // operations
240 foreach ( $ops as $op ) {
241 $newOp = $op; // operation
242 foreach ( array( 'src', 'srcs', 'dst', 'dir' ) as $par ) {
243 if ( isset( $newOp[$par] ) ) { // string or array
244 $newOp[$par] = $this->substPaths( $newOp[$par], $backend );
253 * Same as substOpBatchPaths() but for a single operation
255 * @param $op File operation array
256 * @param $backend FileBackendStore
259 protected function substOpPaths( array $ops, FileBackendStore
$backend ) {
260 $newOps = $this->substOpBatchPaths( array( $ops ), $backend );
265 * Substitute the backend of storage paths with an internal backend's name
267 * @param $paths Array|string List of paths or single string path
268 * @param $backend FileBackendStore
269 * @return Array|string
271 protected function substPaths( $paths, FileBackendStore
$backend ) {
273 '!^mwstore://' . preg_quote( $this->name
) . '/!',
274 StringUtils
::escapeRegexReplacement( "mwstore://{$backend->getName()}/" ),
275 $paths // string or array
280 * Substitute the backend of internal storage paths with the proxy backend's name
282 * @param $paths Array|string List of paths or single string path
283 * @return Array|string
285 protected function unsubstPaths( $paths ) {
287 '!^mwstore://([^/]+)!',
288 StringUtils
::escapeRegexReplacement( "mwstore://{$this->name}" ),
289 $paths // string or array
294 * @see FileBackend::doPrepare()
297 protected function doPrepare( array $params ) {
298 $status = Status
::newGood();
299 foreach ( $this->backends
as $backend ) {
300 $realParams = $this->substOpPaths( $params, $backend );
301 $status->merge( $backend->doPrepare( $realParams ) );
307 * @see FileBackend::doSecure()
310 protected function doSecure( array $params ) {
311 $status = Status
::newGood();
312 foreach ( $this->backends
as $backend ) {
313 $realParams = $this->substOpPaths( $params, $backend );
314 $status->merge( $backend->doSecure( $realParams ) );
320 * @see FileBackend::doClean()
323 protected function doClean( array $params ) {
324 $status = Status
::newGood();
325 foreach ( $this->backends
as $backend ) {
326 $realParams = $this->substOpPaths( $params, $backend );
327 $status->merge( $backend->doClean( $realParams ) );
333 * @see FileBackend::concatenate()
335 public function concatenate( array $params ) {
336 // We are writing to an FS file, so we don't need to do this per-backend
337 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
338 return $this->backends
[$this->masterIndex
]->concatenate( $realParams );
342 * @see FileBackend::fileExists()
344 public function fileExists( array $params ) {
345 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
346 return $this->backends
[$this->masterIndex
]->fileExists( $realParams );
350 * @see FileBackend::getFileTimestamp()
352 public function getFileTimestamp( array $params ) {
353 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
354 return $this->backends
[$this->masterIndex
]->getFileTimestamp( $realParams );
358 * @see FileBackend::getFileSize()
360 public function getFileSize( array $params ) {
361 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
362 return $this->backends
[$this->masterIndex
]->getFileSize( $realParams );
366 * @see FileBackend::getFileStat()
368 public function getFileStat( array $params ) {
369 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
370 return $this->backends
[$this->masterIndex
]->getFileStat( $realParams );
374 * @see FileBackend::getFileContents()
376 public function getFileContents( array $params ) {
377 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
378 return $this->backends
[$this->masterIndex
]->getFileContents( $realParams );
382 * @see FileBackend::getFileSha1Base36()
384 public function getFileSha1Base36( array $params ) {
385 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
386 return $this->backends
[$this->masterIndex
]->getFileSha1Base36( $realParams );
390 * @see FileBackend::getFileProps()
392 public function getFileProps( array $params ) {
393 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
394 return $this->backends
[$this->masterIndex
]->getFileProps( $realParams );
398 * @see FileBackend::streamFile()
400 public function streamFile( array $params ) {
401 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
402 return $this->backends
[$this->masterIndex
]->streamFile( $realParams );
406 * @see FileBackend::getLocalReference()
408 public function getLocalReference( array $params ) {
409 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
410 return $this->backends
[$this->masterIndex
]->getLocalReference( $realParams );
414 * @see FileBackend::getLocalCopy()
416 public function getLocalCopy( array $params ) {
417 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
418 return $this->backends
[$this->masterIndex
]->getLocalCopy( $realParams );
422 * @see FileBackend::directoryExists()
424 public function directoryExists( array $params ) {
425 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
426 return $this->backends
[$this->masterIndex
]->directoryExists( $realParams );
430 * @see FileBackend::getSubdirectoryList()
432 public function getDirectoryList( array $params ) {
433 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
434 return $this->backends
[$this->masterIndex
]->getDirectoryList( $realParams );
438 * @see FileBackend::getFileList()
440 public function getFileList( array $params ) {
441 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
442 return $this->backends
[$this->masterIndex
]->getFileList( $realParams );
446 * @see FileBackend::clearCache()
448 public function clearCache( array $paths = null ) {
449 foreach ( $this->backends
as $backend ) {
450 $realPaths = is_array( $paths ) ?
$this->substPaths( $paths, $backend ) : null;
451 $backend->clearCache( $realPaths );