95e0eeb0b85f3b1f390e993106a19ca8f4128bb3
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 FileBackend
{
24 /** @var Array Prioritized list of FileBackendStore objects */
25 protected $backends = array(); // array of (backend index => backends)
26 protected $masterIndex = -1; // integer; index of master backend
27 protected $syncChecks = 0; // integer bitfield
29 /* Possible internal backend consistency checks */
34 * Construct a proxy backend that consists of several internal backends.
35 * Additional $config params include:
36 * 'backends' : Array of backend config and multi-backend settings.
37 * Each value is the config used in the constructor of a
38 * FileBackendStore class, but with these additional settings:
39 * 'class' : The name of the backend class
40 * 'isMultiMaster' : This must be set for one backend.
41 * 'syncChecks' : Integer bitfield of internal backend sync checks to perform.
42 * Possible bits include self::CHECK_SIZE and self::CHECK_TIME.
43 * The checks are done before allowing any file operations.
44 * @param $config Array
46 public function __construct( array $config ) {
47 parent
::__construct( $config );
49 // Construct backends here rather than via registration
50 // to keep these backends hidden from outside the proxy.
51 foreach ( $config['backends'] as $index => $config ) {
52 $name = $config['name'];
53 if ( isset( $namesUsed[$name] ) ) { // don't break FileOp predicates
54 throw new MWException( "Two or more backends defined with the name $name." );
56 $namesUsed[$name] = 1;
57 if ( !isset( $config['class'] ) ) {
58 throw new MWException( 'No class given for a backend config.' );
60 $class = $config['class'];
61 $this->backends
[$index] = new $class( $config );
62 if ( !empty( $config['isMultiMaster'] ) ) {
63 if ( $this->masterIndex
>= 0 ) {
64 throw new MWException( 'More than one master backend defined.' );
66 $this->masterIndex
= $index;
69 if ( $this->masterIndex
< 0 ) { // need backends and must have a master
70 throw new MWException( 'No master backend defined.' );
72 $this->syncChecks
= isset( $config['syncChecks'] )
73 ?
$config['syncChecks']
78 * @see FileBackend::doOperationsInternal()
81 final protected function doOperationsInternal( array $ops, array $opts ) {
82 $status = Status
::newGood();
84 $performOps = array(); // list of FileOp objects
85 $filesRead = $filesChanged = array(); // storage paths used
86 // Build up a list of FileOps. The list will have all the ops
87 // for one backend, then all the ops for the next, and so on.
88 // These batches of ops are all part of a continuous array.
89 // Also build up a list of files read/changed...
90 foreach ( $this->backends
as $index => $backend ) {
91 $backendOps = $this->substOpBatchPaths( $ops, $backend );
92 // Add on the operation batch for this backend
93 $performOps = array_merge( $performOps, $backend->getOperations( $backendOps ) );
94 if ( $index == 0 ) { // first batch
95 // Get the files used for these operations. Each backend has a batch of
96 // the same operations, so we only need to get them from the first batch.
97 foreach ( $performOps as $fileOp ) {
98 $filesRead = array_merge( $filesRead, $fileOp->storagePathsRead() );
99 $filesChanged = array_merge( $filesChanged, $fileOp->storagePathsChanged() );
101 // Get the paths under the proxy backend's name
102 $filesRead = $this->unsubstPaths( $filesRead );
103 $filesChanged = $this->unsubstPaths( $filesChanged );
107 // Try to lock those files for the scope of this function...
108 if ( empty( $opts['nonLocking'] ) ) {
109 $filesLockSh = array_diff( $filesRead, $filesChanged ); // optimization
110 $filesLockEx = $filesChanged;
111 // Get a shared lock on the parent directory of each path changed
112 $filesLockSh = array_merge( $filesLockSh, array_map( 'dirname', $filesLockEx ) );
113 // Try to lock those files for the scope of this function...
114 $scopeLockS = $this->getScopedFileLocks( $filesLockSh, LockManager
::LOCK_UW
, $status );
115 $scopeLockE = $this->getScopedFileLocks( $filesLockEx, LockManager
::LOCK_EX
, $status );
116 if ( !$status->isOK() ) {
117 return $status; // abort
121 // Clear any cache entries (after locks acquired)
124 // Do a consistency check to see if the backends agree
125 if ( count( $this->backends
) > 1 ) {
126 $status->merge( $this->consistencyCheck( array_merge( $filesRead, $filesChanged ) ) );
127 if ( !$status->isOK() ) {
128 return $status; // abort
132 // Actually attempt the operation batch...
133 $subStatus = FileOp
::attemptBatch( $performOps, $opts );
136 $failCount = $successCount = 0;
137 // Make 'success', 'successCount', and 'failCount' fields reflect
138 // the overall operation, rather than all the batches for each backend.
139 // Do this by only using success values from the master backend's batch.
140 $batchStart = $this->masterIndex
* count( $ops );
141 $batchEnd = $batchStart +
count( $ops ) - 1;
142 for ( $i = $batchStart; $i <= $batchEnd; $i++
) {
143 if ( !isset( $subStatus->success
[$i] ) ) {
144 break; // failed out before trying this op
145 } elseif ( $subStatus->success
[$i] ) {
150 $success[] = $subStatus->success
[$i];
152 $subStatus->success
= $success;
153 $subStatus->successCount
= $successCount;
154 $subStatus->failCount
= $failCount;
156 // Merge errors into status fields
157 $status->merge( $subStatus );
158 $status->success
= $subStatus->success
; // not done in merge()
164 * Check that a set of files are consistent across all internal backends
166 * @param $paths Array
169 public function consistencyCheck( array $paths ) {
170 $status = Status
::newGood();
171 if ( $this->syncChecks
== 0 ) {
172 return $status; // skip checks
175 $mBackend = $this->backends
[$this->masterIndex
];
176 foreach ( array_unique( $paths ) as $path ) {
177 $params = array( 'src' => $path, 'latest' => true );
178 // Stat the file on the 'master' backend
179 $mStat = $mBackend->getFileStat( $this->substOpPaths( $params, $mBackend ) );
180 // Check of all clone backends agree with the master...
181 foreach ( $this->backends
as $index => $cBackend ) {
182 if ( $index === $this->masterIndex
) {
185 $cStat = $cBackend->getFileStat( $this->substOpPaths( $params, $cBackend ) );
186 if ( $mStat ) { // file is in master
187 if ( !$cStat ) { // file should exist
188 $status->fatal( 'backend-fail-synced', $path );
191 if ( $this->syncChecks
& self
::CHECK_SIZE
) {
192 if ( $cStat['size'] != $mStat['size'] ) { // wrong size
193 $status->fatal( 'backend-fail-synced', $path );
197 if ( $this->syncChecks
& self
::CHECK_TIME
) {
198 $mTs = wfTimestamp( TS_UNIX
, $mStat['mtime'] );
199 $cTs = wfTimestamp( TS_UNIX
, $cStat['mtime'] );
200 if ( abs( $mTs - $cTs ) > 30 ) { // outdated file somewhere
201 $status->fatal( 'backend-fail-synced', $path );
205 } else { // file is not in master
206 if ( $cStat ) { // file should not exist
207 $status->fatal( 'backend-fail-synced', $path );
217 * Substitute the backend name in storage path parameters
218 * for a set of operations with that of a given internal backend.
220 * @param $ops Array List of file operation arrays
221 * @param $backend FileBackendStore
224 protected function substOpBatchPaths( array $ops, FileBackendStore
$backend ) {
225 $newOps = array(); // operations
226 foreach ( $ops as $op ) {
227 $newOp = $op; // operation
228 foreach ( array( 'src', 'srcs', 'dst', 'dir' ) as $par ) {
229 if ( isset( $newOp[$par] ) ) { // string or array
230 $newOp[$par] = $this->substPaths( $newOp[$par], $backend );
239 * Same as substOpBatchPaths() but for a single operation
241 * @param $op File operation array
242 * @param $backend FileBackendStore
245 protected function substOpPaths( array $ops, FileBackendStore
$backend ) {
246 $newOps = $this->substOpBatchPaths( array( $ops ), $backend );
251 * Substitute the backend of storage paths with an internal backend's name
253 * @param $paths Array|string List of paths or single string path
254 * @param $backend FileBackendStore
255 * @return Array|string
257 protected function substPaths( $paths, FileBackendStore
$backend ) {
259 '!^mwstore://' . preg_quote( $this->name
) . '/!',
260 StringUtils
::escapeRegexReplacement( "mwstore://{$backend->getName()}/" ),
261 $paths // string or array
266 * Substitute the backend of internal storage paths with the proxy backend's name
268 * @param $paths Array|string List of paths or single string path
269 * @return Array|string
271 protected function unsubstPaths( $paths ) {
273 '!^mwstore://([^/]+)!',
274 StringUtils
::escapeRegexReplacement( "mwstore://{$this->name}" ),
275 $paths // string or array
280 * @see FileBackend::doPrepare()
283 public function doPrepare( array $params ) {
284 $status = Status
::newGood();
285 foreach ( $this->backends
as $backend ) {
286 $realParams = $this->substOpPaths( $params, $backend );
287 $status->merge( $backend->doPrepare( $realParams ) );
293 * @see FileBackend::doSecure()
296 public function doSecure( array $params ) {
297 $status = Status
::newGood();
298 foreach ( $this->backends
as $backend ) {
299 $realParams = $this->substOpPaths( $params, $backend );
300 $status->merge( $backend->doSecure( $realParams ) );
306 * @see FileBackend::doClean()
309 public function doClean( array $params ) {
310 $status = Status
::newGood();
311 foreach ( $this->backends
as $backend ) {
312 $realParams = $this->substOpPaths( $params, $backend );
313 $status->merge( $backend->doClean( $realParams ) );
319 * @see FileBackend::getFileList()
321 public function concatenate( array $params ) {
322 // We are writing to an FS file, so we don't need to do this per-backend
323 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
324 return $this->backends
[$this->masterIndex
]->concatenate( $realParams );
328 * @see FileBackend::fileExists()
330 public function fileExists( array $params ) {
331 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
332 return $this->backends
[$this->masterIndex
]->fileExists( $realParams );
336 * @see FileBackend::getFileTimestamp()
338 public function getFileTimestamp( array $params ) {
339 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
340 return $this->backends
[$this->masterIndex
]->getFileTimestamp( $realParams );
344 * @see FileBackend::getFileSize()
346 public function getFileSize( array $params ) {
347 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
348 return $this->backends
[$this->masterIndex
]->getFileSize( $realParams );
352 * @see FileBackend::getFileStat()
354 public function getFileStat( array $params ) {
355 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
356 return $this->backends
[$this->masterIndex
]->getFileStat( $realParams );
360 * @see FileBackend::getFileContents()
362 public function getFileContents( array $params ) {
363 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
364 return $this->backends
[$this->masterIndex
]->getFileContents( $realParams );
368 * @see FileBackend::getFileSha1Base36()
370 public function getFileSha1Base36( array $params ) {
371 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
372 return $this->backends
[$this->masterIndex
]->getFileSha1Base36( $realParams );
376 * @see FileBackend::getFileProps()
378 public function getFileProps( array $params ) {
379 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
380 return $this->backends
[$this->masterIndex
]->getFileProps( $realParams );
384 * @see FileBackend::streamFile()
386 public function streamFile( array $params ) {
387 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
388 return $this->backends
[$this->masterIndex
]->streamFile( $realParams );
392 * @see FileBackend::getLocalReference()
394 public function getLocalReference( array $params ) {
395 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
396 return $this->backends
[$this->masterIndex
]->getLocalReference( $realParams );
400 * @see FileBackend::getLocalCopy()
402 public function getLocalCopy( array $params ) {
403 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
404 return $this->backends
[$this->masterIndex
]->getLocalCopy( $realParams );
408 * @see FileBackend::getFileList()
410 public function getFileList( array $params ) {
411 $realParams = $this->substOpPaths( $params, $this->backends
[$this->masterIndex
] );
412 return $this->backends
[$this->masterIndex
]->getFileList( $realParams );
416 * @see FileBackend::clearCache()
418 public function clearCache( array $paths = null ) {
419 foreach ( $this->backends
as $backend ) {
420 $realPaths = is_array( $paths ) ?
$this->substPaths( $paths, $backend ) : null;
421 $backend->clearCache( $realPaths );