9 * This class defines a multi-write backend. Multiple backends can be
10 * registered to this proxy backend and it will act as a single backend.
11 * Use this when all access to those backends is through this proxy backend.
12 * At least one of the backends must be declared the "master" backend.
14 * Only use this class when transitioning from one storage system to another.
16 * Read operations are only done on the 'master' backend for consistency.
17 * Write operations are performed on all backends, in the order defined.
18 * If an operation fails on one backend it will be rolled back from the others.
20 * @ingroup FileBackend
23 class FileBackendMultiWrite
extends FileBackendBase
{
24 /** @var Array Prioritized list of FileBackend objects */
25 protected $backends = array(); // array of (backend index => backends)
26 protected $masterIndex = -1; // index of master backend
29 * Construct a proxy backend that consists of several internal backends.
30 * Additional $config params include:
31 * 'backends' : Array of backend config and multi-backend settings.
32 * Each value is the config used in the constructor of a
33 * FileBackend class, but with these additional settings:
34 * 'class' : The name of the backend class
35 * 'isMultiMaster' : This must be set for one backend.
36 * @param $config Array
38 public function __construct( array $config ) {
39 parent
::__construct( $config );
41 // Construct backends here rather than via registration
42 // to keep these backends hidden from outside the proxy.
43 foreach ( $config['backends'] as $index => $config ) {
44 $name = $config['name'];
45 if ( isset( $namesUsed[$name] ) ) { // don't break FileOp predicates
46 throw new MWException( "Two or more backends defined with the name $name." );
48 $namesUsed[$name] = 1;
49 if ( !isset( $config['class'] ) ) {
50 throw new MWException( 'No class given for a backend config.' );
52 $class = $config['class'];
53 $this->backends
[$index] = new $class( $config );
54 if ( !empty( $config['isMultiMaster'] ) ) {
55 if ( $this->masterIndex
>= 0 ) {
56 throw new MWException( 'More than one master backend defined.' );
58 $this->masterIndex
= $index;
61 if ( $this->masterIndex
< 0 ) { // need backends and must have a master
62 throw new MWException( 'No master backend defined.' );
67 * @see FileBackendBase::doOperationsInternal()
69 final protected function doOperationsInternal( array $ops, array $opts ) {
70 $status = Status
::newGood();
72 $performOps = array(); // list of FileOp objects
73 $filesRead = $filesChanged = array(); // storage paths used
74 // Build up a list of FileOps. The list will have all the ops
75 // for one backend, then all the ops for the next, and so on.
76 // These batches of ops are all part of a continuous array.
77 // Also build up a list of files read/changed...
78 foreach ( $this->backends
as $index => $backend ) {
79 $backendOps = $this->substOpBatchPaths( $ops, $backend );
80 // Add on the operation batch for this backend
81 $performOps = array_merge( $performOps, $backend->getOperations( $backendOps ) );
82 if ( $index == 0 ) { // first batch
83 // Get the files used for these operations. Each backend has a batch of
84 // the same operations, so we only need to get them from the first batch.
85 foreach ( $performOps as $fileOp ) {
86 $filesRead = array_merge( $filesRead, $fileOp->storagePathsRead() );
87 $filesChanged = array_merge( $filesChanged, $fileOp->storagePathsChanged() );
89 // Get the paths under the proxy backend's name
90 $filesRead = $this->unsubstPaths( $filesRead );
91 $filesChanged = $this->unsubstPaths( $filesChanged );
95 // Try to lock those files for the scope of this function...
96 if ( empty( $opts['nonLocking'] ) ) {
97 $filesLockSh = array_diff( $filesRead, $filesChanged ); // optimization
98 $filesLockEx = $filesChanged;
99 // Get a shared lock on the parent directory of each path changed
100 $filesLockSh = array_merge( $filesLockSh, array_map( 'dirname', $filesLockEx ) );
101 // Try to lock those files for the scope of this function...
102 $scopeLockS = $this->getScopedFileLocks( $filesLockSh, LockManager
::LOCK_UW
, $status );
103 $scopeLockE = $this->getScopedFileLocks( $filesLockEx, LockManager
::LOCK_EX
, $status );
104 if ( !$status->isOK() ) {
105 return $status; // abort
109 // Clear any cache entries (after locks acquired)
112 // Do a consistency check to see if the backends agree
113 if ( count( $this->backends
) > 1 ) {
114 $status->merge( $this->consistencyCheck( array_merge( $filesRead, $filesChanged ) ) );
115 if ( !$status->isOK() ) {
116 return $status; // abort
120 // Actually attempt the operation batch...
121 $subStatus = FileOp
::attemptBatch( $performOps, $opts );
124 $failCount = $successCount = 0;
125 // Make 'success', 'successCount', and 'failCount' fields reflect
126 // the overall operation, rather than all the batches for each backend.
127 // Do this by only using success values from the master backend's batch.
128 $batchStart = $this->masterIndex
* count( $ops );
129 $batchEnd = $batchStart +
count( $ops ) - 1;
130 for ( $i = $batchStart; $i <= $batchEnd; $i++
) {
131 if ( !isset( $subStatus->success
[$i] ) ) {
132 break; // failed out before trying this op
133 } elseif ( $subStatus->success
[$i] ) {
138 $success[] = $subStatus->success
[$i];
140 $subStatus->success
= $success;
141 $subStatus->successCount
= $successCount;
142 $subStatus->failCount
= $failCount;
144 // Merge errors into status fields
145 $status->merge( $subStatus );
146 $status->success
= $subStatus->success
; // not done in merge()
152 * Check that a set of files are consistent across all internal backends
154 * @param $paths Array
157 public function consistencyCheck( array $paths ) {
158 $status = Status
::newGood();
160 $mBackend = $this->backends
[$this->masterIndex
];
161 foreach ( array_unique( $paths ) as $path ) {
162 $params = array( 'src' => $path, 'latest' => true );
163 // Stat the file on the 'master' backend
164 $mStat = $mBackend->getFileStat( $this->substOpPaths( $params, $mBackend ) );
165 // Check of all clone backends agree with the master...
166 foreach ( $this->backends
as $index => $cBackend ) {
167 if ( $index === $this->masterIndex
) {
170 $cStat = $cBackend->getFileStat( $this->substOpPaths( $params, $cBackend ) );
171 if ( $mStat ) { // file is in master
172 if ( !$cStat ) { // file should exist
173 $status->fatal( 'backend-fail-synced', $path );
174 } elseif ( $cStat['size'] != $mStat['size'] ) { // wrong size
175 $status->fatal( 'backend-fail-synced', $path );
177 $mTs = wfTimestamp( TS_UNIX
, $mStat['mtime'] );
178 $cTs = wfTimestamp( TS_UNIX
, $cStat['mtime'] );
179 if ( abs( $mTs - $cTs ) > 30 ) { // outdated file somewhere
180 $status->fatal( 'backend-fail-synced', $path );
183 } else { // file is not in master
184 if ( $cStat ) { // file should not exist
185 $status->fatal( 'backend-fail-synced', $path );
195 * Substitute the backend name in storage path parameters
196 * for a set of operations with that of a given internal backend.
198 * @param $ops Array List of file operation arrays
199 * @param $backend FileBackend
202 protected function substOpBatchPaths( array $ops, FileBackend
$backend ) {
203 $newOps = array(); // operations
204 foreach ( $ops as $op ) {
205 $newOp = $op; // operation
206 foreach ( array( 'src', 'srcs', 'dst', 'dir' ) as $par ) {
207 if ( isset( $newOp[$par] ) ) { // string or array
208 $newOp[$par] = $this->substPaths( $newOp[$par], $backend );
217 * Same as substOpBatchPaths() but for a single operation
219 * @param $op File operation array
220 * @param $backend FileBackend
223 protected function substOpPaths( array $ops, FileBackend
$backend ) {
224 $newOps = $this->substOpBatchPaths( array( $ops ), $backend );
229 * Substitute the backend of storage paths with an internal backend's name
231 * @param $paths Array|string List of paths or single string path
232 * @param $backend FileBackend
233 * @return Array|string
235 protected function substPaths( $paths, FileBackend
$backend ) {
237 '!^mwstore://' . preg_quote( $this->name
) . '/!',
238 'mwstore://' . $backend->getName() . '/',
239 $paths // string or array
244 * Substitute the backend of internal storage paths with the proxy backend's name
246 * @param $paths Array|string List of paths or single string path
247 * @return Array|string
249 protected function unsubstPaths( $paths ) {
251 '!^mwstore://([^/]+)!',
252 "mwstore://{$this->name}",
253 $paths // string or array
258 * @see FileBackendBase::doPrepare()
260 public function doPrepare( array $params ) {
261 $status = Status
::newGood();
262 foreach ( $this->backends
as $backend ) {
263 $realParams = $this->substOpPaths( $params, $backend );
264 $status->merge( $backend->doPrepare( $realParams ) );
270 * @see FileBackendBase::doSecure()
272 public function doSecure( array $params ) {
273 $status = Status
::newGood();
274 foreach ( $this->backends
as $backend ) {
275 $realParams = $this->substOpPaths( $params, $backend );
276 $status->merge( $backend->doSecure( $realParams ) );
282 * @see FileBackendBase::doClean()
284 public function doClean( array $params ) {
285 $status = Status
::newGood();
286 foreach ( $this->backends
as $backend ) {
287 $realParams = $this->substOpPaths( $params, $backend );
288 $status->merge( $backend->doClean( $realParams ) );
294 * @see FileBackendBase::getFileList()
296 public function concatenate( array $params ) {
297 // We are writing to an FS file, so we don't need to do this per-backend
298 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
299 return $this->backends
[$this->masterIndex
]->concatenate( $realParams );
303 * @see FileBackendBase::fileExists()
305 public function fileExists( array $params ) {
306 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
307 return $this->backends
[$this->masterIndex
]->fileExists( $realParams );
311 * @see FileBackendBase::getFileTimestamp()
313 public function getFileTimestamp( array $params ) {
314 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
315 return $this->backends
[$this->masterIndex
]->getFileTimestamp( $realParams );
319 * @see FileBackendBase::getFileSize()
321 public function getFileSize( array $params ) {
322 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
323 return $this->backends
[$this->masterIndex
]->getFileSize( $realParams );
327 * @see FileBackendBase::getFileStat()
329 public function getFileStat( array $params ) {
330 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
331 return $this->backends
[$this->masterIndex
]->getFileStat( $realParams );
335 * @see FileBackendBase::getFileContents()
337 public function getFileContents( array $params ) {
338 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
339 return $this->backends
[$this->masterIndex
]->getFileContents( $realParams );
343 * @see FileBackendBase::getFileSha1Base36()
345 public function getFileSha1Base36( array $params ) {
346 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
347 return $this->backends
[$this->masterIndex
]->getFileSha1Base36( $realParams );
351 * @see FileBackendBase::getFileProps()
353 public function getFileProps( array $params ) {
354 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
355 return $this->backends
[$this->masterIndex
]->getFileProps( $realParams );
359 * @see FileBackendBase::streamFile()
361 public function streamFile( array $params ) {
362 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
363 return $this->backends
[$this->masterIndex
]->streamFile( $realParams );
367 * @see FileBackendBase::getLocalReference()
369 public function getLocalReference( array $params ) {
370 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
371 return $this->backends
[$this->masterIndex
]->getLocalReference( $realParams );
375 * @see FileBackendBase::getLocalCopy()
377 public function getLocalCopy( array $params ) {
378 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
379 return $this->backends
[$this->masterIndex
]->getLocalCopy( $realParams );
383 * @see FileBackendBase::getFileList()
385 public function getFileList( array $params ) {
386 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
387 return $this->backends
[$this->masterIndex
]->getFileList( $realParams );
391 * @see FileBackendBase::clearCache()
393 public function clearCache( array $paths = null ) {
394 foreach ( $this->backends
as $backend ) {
395 $realPaths = is_array( $paths ) ?
$this->substPaths( $paths ) : null;
396 $backend->clearCache( $realPaths );