Merge "Add option to rebuild message files on a different folder. It also creates...
[lhc/web/wiklou.git] / includes / filerepo / backend / FileBackendMultiWrite.php
1 <?php
2 /**
3 * @file
4 * @ingroup FileBackend
5 * @author Aaron Schulz
6 */
7
8 /**
9 * @brief Proxy backend that mirrors writes to several internal backends.
10 *
11 * This class defines a multi-write backend. Multiple backends can be
12 * registered to this proxy backend and it will act as a single backend.
13 * Use this when all access to those backends is through this proxy backend.
14 * At least one of the backends must be declared the "master" backend.
15 *
16 * Only use this class when transitioning from one storage system to another.
17 *
18 * Read operations are only done on the 'master' backend for consistency.
19 * Write operations are performed on all backends, in the order defined.
20 * If an operation fails on one backend it will be rolled back from the others.
21 *
22 * @ingroup FileBackend
23 * @since 1.19
24 */
25 class FileBackendMultiWrite extends FileBackend {
26 /** @var Array Prioritized list of FileBackendStore objects */
27 protected $backends = array(); // array of (backend index => backends)
28 protected $masterIndex = -1; // integer; index of master backend
29 protected $syncChecks = 0; // integer bitfield
30
31 /* Possible internal backend consistency checks */
32 const CHECK_SIZE = 1;
33 const CHECK_TIME = 2;
34
35 /**
36 * Construct a proxy backend that consists of several internal backends.
37 * Additional $config params include:
38 * 'backends' : Array of backend config and multi-backend settings.
39 * Each value is the config used in the constructor of a
40 * FileBackendStore class, but with these additional settings:
41 * 'class' : The name of the backend class
42 * 'isMultiMaster' : This must be set for one backend.
43 * 'syncChecks' : Integer bitfield of internal backend sync checks to perform.
44 * Possible bits include self::CHECK_SIZE and self::CHECK_TIME.
45 * The checks are done before allowing any file operations.
46 * @param $config Array
47 */
48 public function __construct( array $config ) {
49 parent::__construct( $config );
50 $namesUsed = array();
51 // Construct backends here rather than via registration
52 // to keep these backends hidden from outside the proxy.
53 foreach ( $config['backends'] as $index => $config ) {
54 $name = $config['name'];
55 if ( isset( $namesUsed[$name] ) ) { // don't break FileOp predicates
56 throw new MWException( "Two or more backends defined with the name $name." );
57 }
58 $namesUsed[$name] = 1;
59 if ( !isset( $config['class'] ) ) {
60 throw new MWException( 'No class given for a backend config.' );
61 }
62 $class = $config['class'];
63 $this->backends[$index] = new $class( $config );
64 if ( !empty( $config['isMultiMaster'] ) ) {
65 if ( $this->masterIndex >= 0 ) {
66 throw new MWException( 'More than one master backend defined.' );
67 }
68 $this->masterIndex = $index;
69 }
70 }
71 if ( $this->masterIndex < 0 ) { // need backends and must have a master
72 throw new MWException( 'No master backend defined.' );
73 }
74 $this->syncChecks = isset( $config['syncChecks'] )
75 ? $config['syncChecks']
76 : self::CHECK_SIZE;
77 }
78
79 /**
80 * @see FileBackend::doOperationsInternal()
81 * @return Status
82 */
83 final protected function doOperationsInternal( array $ops, array $opts ) {
84 $status = Status::newGood();
85
86 $performOps = array(); // list of FileOp objects
87 $filesRead = array(); // storage paths read from
88 $filesChanged = array(); // storage paths written to
89 // Build up a list of FileOps. The list will have all the ops
90 // for one backend, then all the ops for the next, and so on.
91 // These batches of ops are all part of a continuous array.
92 // Also build up a list of files read/changed...
93 foreach ( $this->backends as $index => $backend ) {
94 $backendOps = $this->substOpBatchPaths( $ops, $backend );
95 // Add on the operation batch for this backend
96 $performOps = array_merge( $performOps, $backend->getOperations( $backendOps ) );
97 if ( $index == 0 ) { // first batch
98 // Get the files used for these operations. Each backend has a batch of
99 // the same operations, so we only need to get them from the first batch.
100 foreach ( $performOps as $fileOp ) {
101 $filesRead = array_merge( $filesRead, $fileOp->storagePathsRead() );
102 $filesChanged = array_merge( $filesChanged, $fileOp->storagePathsChanged() );
103 }
104 // Get the paths under the proxy backend's name
105 $filesRead = $this->unsubstPaths( $filesRead );
106 $filesChanged = $this->unsubstPaths( $filesChanged );
107 }
108 }
109
110 // Try to lock those files for the scope of this function...
111 if ( empty( $opts['nonLocking'] ) ) {
112 $filesLockSh = array_diff( $filesRead, $filesChanged ); // optimization
113 $filesLockEx = $filesChanged;
114 // Get a shared lock on the parent directory of each path changed
115 $filesLockSh = array_merge( $filesLockSh, array_map( 'dirname', $filesLockEx ) );
116 // Try to lock those files for the scope of this function...
117 $scopeLockS = $this->getScopedFileLocks( $filesLockSh, LockManager::LOCK_UW, $status );
118 $scopeLockE = $this->getScopedFileLocks( $filesLockEx, LockManager::LOCK_EX, $status );
119 if ( !$status->isOK() ) {
120 return $status; // abort
121 }
122 }
123
124 // Clear any cache entries (after locks acquired)
125 $this->clearCache();
126
127 // Do a consistency check to see if the backends agree
128 if ( count( $this->backends ) > 1 ) {
129 $status->merge( $this->consistencyCheck( array_merge( $filesRead, $filesChanged ) ) );
130 if ( !$status->isOK() ) {
131 return $status; // abort
132 }
133 }
134
135 // Actually attempt the operation batch...
136 $subStatus = FileOp::attemptBatch( $performOps, $opts, $this->fileJournal );
137
138 $success = array();
139 $failCount = 0;
140 $successCount = 0;
141 // Make 'success', 'successCount', and 'failCount' fields reflect
142 // the overall operation, rather than all the batches for each backend.
143 // Do this by only using success values from the master backend's batch.
144 $batchStart = $this->masterIndex * count( $ops );
145 $batchEnd = $batchStart + count( $ops ) - 1;
146 for ( $i = $batchStart; $i <= $batchEnd; $i++ ) {
147 if ( !isset( $subStatus->success[$i] ) ) {
148 break; // failed out before trying this op
149 } elseif ( $subStatus->success[$i] ) {
150 ++$successCount;
151 } else {
152 ++$failCount;
153 }
154 $success[] = $subStatus->success[$i];
155 }
156 $subStatus->success = $success;
157 $subStatus->successCount = $successCount;
158 $subStatus->failCount = $failCount;
159
160 // Merge errors into status fields
161 $status->merge( $subStatus );
162 $status->success = $subStatus->success; // not done in merge()
163
164 return $status;
165 }
166
167 /**
168 * Check that a set of files are consistent across all internal backends
169 *
170 * @param $paths Array
171 * @return Status
172 */
173 public function consistencyCheck( array $paths ) {
174 $status = Status::newGood();
175 if ( $this->syncChecks == 0 ) {
176 return $status; // skip checks
177 }
178
179 $mBackend = $this->backends[$this->masterIndex];
180 foreach ( array_unique( $paths ) as $path ) {
181 $params = array( 'src' => $path, 'latest' => true );
182 // Stat the file on the 'master' backend
183 $mStat = $mBackend->getFileStat( $this->substOpPaths( $params, $mBackend ) );
184 // Check of all clone backends agree with the master...
185 foreach ( $this->backends as $index => $cBackend ) {
186 if ( $index === $this->masterIndex ) {
187 continue; // master
188 }
189 $cStat = $cBackend->getFileStat( $this->substOpPaths( $params, $cBackend ) );
190 if ( $mStat ) { // file is in master
191 if ( !$cStat ) { // file should exist
192 $status->fatal( 'backend-fail-synced', $path );
193 continue;
194 }
195 if ( $this->syncChecks & self::CHECK_SIZE ) {
196 if ( $cStat['size'] != $mStat['size'] ) { // wrong size
197 $status->fatal( 'backend-fail-synced', $path );
198 continue;
199 }
200 }
201 if ( $this->syncChecks & self::CHECK_TIME ) {
202 $mTs = wfTimestamp( TS_UNIX, $mStat['mtime'] );
203 $cTs = wfTimestamp( TS_UNIX, $cStat['mtime'] );
204 if ( abs( $mTs - $cTs ) > 30 ) { // outdated file somewhere
205 $status->fatal( 'backend-fail-synced', $path );
206 continue;
207 }
208 }
209 } else { // file is not in master
210 if ( $cStat ) { // file should not exist
211 $status->fatal( 'backend-fail-synced', $path );
212 }
213 }
214 }
215 }
216
217 return $status;
218 }
219
220 /**
221 * Substitute the backend name in storage path parameters
222 * for a set of operations with that of a given internal backend.
223 *
224 * @param $ops Array List of file operation arrays
225 * @param $backend FileBackendStore
226 * @return Array
227 */
228 protected function substOpBatchPaths( array $ops, FileBackendStore $backend ) {
229 $newOps = array(); // operations
230 foreach ( $ops as $op ) {
231 $newOp = $op; // operation
232 foreach ( array( 'src', 'srcs', 'dst', 'dir' ) as $par ) {
233 if ( isset( $newOp[$par] ) ) { // string or array
234 $newOp[$par] = $this->substPaths( $newOp[$par], $backend );
235 }
236 }
237 $newOps[] = $newOp;
238 }
239 return $newOps;
240 }
241
242 /**
243 * Same as substOpBatchPaths() but for a single operation
244 *
245 * @param $op File operation array
246 * @param $backend FileBackendStore
247 * @return Array
248 */
249 protected function substOpPaths( array $ops, FileBackendStore $backend ) {
250 $newOps = $this->substOpBatchPaths( array( $ops ), $backend );
251 return $newOps[0];
252 }
253
254 /**
255 * Substitute the backend of storage paths with an internal backend's name
256 *
257 * @param $paths Array|string List of paths or single string path
258 * @param $backend FileBackendStore
259 * @return Array|string
260 */
261 protected function substPaths( $paths, FileBackendStore $backend ) {
262 return preg_replace(
263 '!^mwstore://' . preg_quote( $this->name ) . '/!',
264 StringUtils::escapeRegexReplacement( "mwstore://{$backend->getName()}/" ),
265 $paths // string or array
266 );
267 }
268
269 /**
270 * Substitute the backend of internal storage paths with the proxy backend's name
271 *
272 * @param $paths Array|string List of paths or single string path
273 * @return Array|string
274 */
275 protected function unsubstPaths( $paths ) {
276 return preg_replace(
277 '!^mwstore://([^/]+)!',
278 StringUtils::escapeRegexReplacement( "mwstore://{$this->name}" ),
279 $paths // string or array
280 );
281 }
282
283 /**
284 * @see FileBackend::doPrepare()
285 * @return Status
286 */
287 protected function doPrepare( array $params ) {
288 $status = Status::newGood();
289 foreach ( $this->backends as $backend ) {
290 $realParams = $this->substOpPaths( $params, $backend );
291 $status->merge( $backend->doPrepare( $realParams ) );
292 }
293 return $status;
294 }
295
296 /**
297 * @see FileBackend::doSecure()
298 * @return Status
299 */
300 protected function doSecure( array $params ) {
301 $status = Status::newGood();
302 foreach ( $this->backends as $backend ) {
303 $realParams = $this->substOpPaths( $params, $backend );
304 $status->merge( $backend->doSecure( $realParams ) );
305 }
306 return $status;
307 }
308
309 /**
310 * @see FileBackend::doClean()
311 * @return Status
312 */
313 protected function doClean( array $params ) {
314 $status = Status::newGood();
315 foreach ( $this->backends as $backend ) {
316 $realParams = $this->substOpPaths( $params, $backend );
317 $status->merge( $backend->doClean( $realParams ) );
318 }
319 return $status;
320 }
321
322 /**
323 * @see FileBackend::getFileList()
324 */
325 public function concatenate( array $params ) {
326 // We are writing to an FS file, so we don't need to do this per-backend
327 $realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] );
328 return $this->backends[$this->masterIndex]->concatenate( $realParams );
329 }
330
331 /**
332 * @see FileBackend::fileExists()
333 */
334 public function fileExists( array $params ) {
335 $realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] );
336 return $this->backends[$this->masterIndex]->fileExists( $realParams );
337 }
338
339 /**
340 * @see FileBackend::getFileTimestamp()
341 */
342 public function getFileTimestamp( array $params ) {
343 $realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] );
344 return $this->backends[$this->masterIndex]->getFileTimestamp( $realParams );
345 }
346
347 /**
348 * @see FileBackend::getFileSize()
349 */
350 public function getFileSize( array $params ) {
351 $realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] );
352 return $this->backends[$this->masterIndex]->getFileSize( $realParams );
353 }
354
355 /**
356 * @see FileBackend::getFileStat()
357 */
358 public function getFileStat( array $params ) {
359 $realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] );
360 return $this->backends[$this->masterIndex]->getFileStat( $realParams );
361 }
362
363 /**
364 * @see FileBackend::getFileContents()
365 */
366 public function getFileContents( array $params ) {
367 $realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] );
368 return $this->backends[$this->masterIndex]->getFileContents( $realParams );
369 }
370
371 /**
372 * @see FileBackend::getFileSha1Base36()
373 */
374 public function getFileSha1Base36( array $params ) {
375 $realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] );
376 return $this->backends[$this->masterIndex]->getFileSha1Base36( $realParams );
377 }
378
379 /**
380 * @see FileBackend::getFileProps()
381 */
382 public function getFileProps( array $params ) {
383 $realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] );
384 return $this->backends[$this->masterIndex]->getFileProps( $realParams );
385 }
386
387 /**
388 * @see FileBackend::streamFile()
389 */
390 public function streamFile( array $params ) {
391 $realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] );
392 return $this->backends[$this->masterIndex]->streamFile( $realParams );
393 }
394
395 /**
396 * @see FileBackend::getLocalReference()
397 */
398 public function getLocalReference( array $params ) {
399 $realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] );
400 return $this->backends[$this->masterIndex]->getLocalReference( $realParams );
401 }
402
403 /**
404 * @see FileBackend::getLocalCopy()
405 */
406 public function getLocalCopy( array $params ) {
407 $realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] );
408 return $this->backends[$this->masterIndex]->getLocalCopy( $realParams );
409 }
410
411 /**
412 * @see FileBackend::getFileList()
413 */
414 public function getFileList( array $params ) {
415 $realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] );
416 return $this->backends[$this->masterIndex]->getFileList( $realParams );
417 }
418
419 /**
420 * @see FileBackend::clearCache()
421 */
422 public function clearCache( array $paths = null ) {
423 foreach ( $this->backends as $backend ) {
424 $realPaths = is_array( $paths ) ? $this->substPaths( $paths, $backend ) : null;
425 $backend->clearCache( $realPaths );
426 }
427 }
428 }