9 * Helper class for representing operations with transaction support.
10 * Do not use this class from places outside FileBackend.
12 * Methods called from attemptBatch() should avoid throwing exceptions at all costs.
13 * FileOp objects should be lightweight in order to support large arrays in memory.
15 * @ingroup FileBackend
18 abstract class FileOp
{
20 protected $params = array();
21 /** @var FileBackendStore */
24 protected $state = self
::STATE_NEW
; // integer
25 protected $failed = false; // boolean
26 protected $useLatest = true; // boolean
27 protected $batchId; // string
29 protected $sourceSha1; // string
30 protected $destSameAsSource; // boolean
32 /* Object life-cycle */
34 const STATE_CHECKED
= 2;
35 const STATE_ATTEMPTED
= 3;
37 /* Timeout related parameters */
38 const MAX_BATCH_SIZE
= 1000;
39 const TIME_LIMIT_SEC
= 300; // 5 minutes
42 * Build a new file operation transaction
44 * @param $backend FileBackendStore
45 * @param $params Array
48 final public function __construct( FileBackendStore
$backend, array $params ) {
49 $this->backend
= $backend;
50 list( $required, $optional ) = $this->allowedParams();
51 foreach ( $required as $name ) {
52 if ( isset( $params[$name] ) ) {
53 $this->params
[$name] = $params[$name];
55 throw new MWException( "File operation missing parameter '$name'." );
58 foreach ( $optional as $name ) {
59 if ( isset( $params[$name] ) ) {
60 $this->params
[$name] = $params[$name];
63 $this->params
= $params;
67 * Set the batch UUID this operation belongs to
69 * @param $batchId string
72 final protected function setBatchId( $batchId ) {
73 $this->batchId
= $batchId;
77 * Whether to allow stale data for file reads and stat checks
79 * @param $allowStale bool
82 final protected function allowStaleReads( $allowStale ) {
83 $this->useLatest
= !$allowStale;
87 * Attempt to perform a series of file operations.
88 * Callers are responsible for handling file locking.
90 * $opts is an array of options, including:
91 * 'force' : Errors that would normally cause a rollback do not.
92 * The remaining operations are still attempted if any fail.
93 * 'allowStale' : Don't require the latest available data.
94 * This can increase performance for non-critical writes.
95 * This has no effect unless the 'force' flag is set.
96 * 'nonJournaled' : Don't log this operation batch in the file journal.
98 * The resulting Status will be "OK" unless:
99 * a) unexpected operation errors occurred (network partitions, disk full...)
100 * b) significant operation errors occured and 'force' was not set
102 * @param $performOps Array List of FileOp operations
103 * @param $opts Array Batch operation options
104 * @param $journal FileJournal Journal to log operations to
107 final public static function attemptBatch(
108 array $performOps, array $opts, FileJournal
$journal
110 $status = Status
::newGood();
112 $n = count( $performOps );
113 if ( $n > self
::MAX_BATCH_SIZE
) {
114 $status->fatal( 'backend-fail-batchsize', $n, self
::MAX_BATCH_SIZE
);
118 $batchId = $journal->getTimestampedUUID();
119 $allowStale = !empty( $opts['allowStale'] );
120 $ignoreErrors = !empty( $opts['force'] );
121 $journaled = empty( $opts['nonJournaled'] );
123 $entries = array(); // file journal entries
124 $predicates = FileOp
::newPredicates(); // account for previous op in prechecks
125 // Do pre-checks for each operation; abort on failure...
126 foreach ( $performOps as $index => $fileOp ) {
127 $fileOp->setBatchId( $batchId );
128 $fileOp->allowStaleReads( $allowStale );
129 $oldPredicates = $predicates;
130 $subStatus = $fileOp->precheck( $predicates ); // updates $predicates
131 $status->merge( $subStatus );
132 if ( $subStatus->isOK() ) {
133 if ( $journaled ) { // journal log entry
134 $entries = array_merge( $entries,
135 self
::getJournalEntries( $fileOp, $oldPredicates, $predicates ) );
137 } else { // operation failed?
138 $status->success
[$index] = false;
139 ++
$status->failCount
;
140 if ( !$ignoreErrors ) {
141 return $status; // abort
146 // Log the operations in file journal...
147 if ( count( $entries ) ) {
148 $subStatus = $journal->logChangeBatch( $entries, $batchId );
149 if ( !$subStatus->isOK() ) {
150 return $subStatus; // abort
154 if ( $ignoreErrors ) { // treat precheck() fatals as mere warnings
155 $status->setResult( true, $status->value
);
158 // Restart PHP's execution timer and set the timeout to safe amount.
159 // This handles cases where the operations take a long time or where we are
160 // already running low on time left. The old timeout is restored afterwards.
161 # @TODO: re-enable this for when the number of batches is high.
162 #$scopedTimeLimit = new FileOpScopedPHPTimeout( self::TIME_LIMIT_SEC );
164 // Attempt each operation...
165 foreach ( $performOps as $index => $fileOp ) {
166 if ( $fileOp->failed() ) {
167 continue; // nothing to do
169 $subStatus = $fileOp->attempt();
170 $status->merge( $subStatus );
171 if ( $subStatus->isOK() ) {
172 $status->success
[$index] = true;
173 ++
$status->successCount
;
175 $status->success
[$index] = false;
176 ++
$status->failCount
;
177 // We can't continue (even with $ignoreErrors) as $predicates is wrong.
178 // Log the remaining ops as failed for recovery...
179 for ( $i = ($index +
1); $i < count( $performOps ); $i++
) {
180 $performOps[$i]->logFailure( 'attempt_aborted' );
182 return $status; // bail out
190 * Get the file journal entries for a single file operation
192 * @param $fileOp FileOp
193 * @param $oPredicates Array Pre-op information about files
194 * @param $nPredicates Array Post-op information about files
197 final protected static function getJournalEntries(
198 FileOp
$fileOp, array $oPredicates, array $nPredicates
200 $nullEntries = array();
201 $updateEntries = array();
202 $deleteEntries = array();
203 $pathsUsed = array_merge( $fileOp->storagePathsRead(), $fileOp->storagePathsChanged() );
204 foreach ( $pathsUsed as $path ) {
205 $nullEntries[] = array( // assertion for recovery
208 'newSha1' => $fileOp->fileSha1( $path, $oPredicates )
211 foreach ( $fileOp->storagePathsChanged() as $path ) {
212 if ( $nPredicates['sha1'][$path] === false ) { // deleted
213 $deleteEntries[] = array(
218 } else { // created/updated
219 $updateEntries[] = array(
220 'op' => $fileOp->fileExists( $path, $oPredicates ) ?
'update' : 'create',
222 'newSha1' => $nPredicates['sha1'][$path]
226 return array_merge( $nullEntries, $updateEntries, $deleteEntries );
230 * Get the value of the parameter with the given name
232 * @param $name string
233 * @return mixed Returns null if the parameter is not set
235 final public function getParam( $name ) {
236 return isset( $this->params
[$name] ) ?
$this->params
[$name] : null;
240 * Check if this operation failed precheck() or attempt()
244 final public function failed() {
245 return $this->failed
;
249 * Get a new empty predicates array for precheck()
253 final public static function newPredicates() {
254 return array( 'exists' => array(), 'sha1' => array() );
258 * Check preconditions of the operation without writing anything
260 * @param $predicates Array
263 final public function precheck( array &$predicates ) {
264 if ( $this->state
!== self
::STATE_NEW
) {
265 return Status
::newFatal( 'fileop-fail-state', self
::STATE_NEW
, $this->state
);
267 $this->state
= self
::STATE_CHECKED
;
268 $status = $this->doPrecheck( $predicates );
269 if ( !$status->isOK() ) {
270 $this->failed
= true;
276 * Attempt the operation, backing up files as needed; this must be reversible
280 final public function attempt() {
281 if ( $this->state
!== self
::STATE_CHECKED
) {
282 return Status
::newFatal( 'fileop-fail-state', self
::STATE_CHECKED
, $this->state
);
283 } elseif ( $this->failed
) { // failed precheck
284 return Status
::newFatal( 'fileop-fail-attempt-precheck' );
286 $this->state
= self
::STATE_ATTEMPTED
;
287 $status = $this->doAttempt();
288 if ( !$status->isOK() ) {
289 $this->failed
= true;
290 $this->logFailure( 'attempt' );
296 * Get the file operation parameters
298 * @return Array (required params list, optional params list)
300 protected function allowedParams() {
301 return array( array(), array() );
305 * Get a list of storage paths read from for this operation
309 public function storagePathsRead() {
314 * Get a list of storage paths written to for this operation
318 public function storagePathsChanged() {
325 protected function doPrecheck( array &$predicates ) {
326 return Status
::newGood();
332 protected function doAttempt() {
333 return Status
::newGood();
337 * Check for errors with regards to the destination file already existing.
338 * This also updates the destSameAsSource and sourceSha1 member variables.
339 * A bad status will be returned if there is no chance it can be overwritten.
341 * @param $predicates Array
344 protected function precheckDestExistence( array $predicates ) {
345 $status = Status
::newGood();
346 // Get hash of source file/string and the destination file
347 $this->sourceSha1
= $this->getSourceSha1Base36(); // FS file or data string
348 if ( $this->sourceSha1
=== null ) { // file in storage?
349 $this->sourceSha1
= $this->fileSha1( $this->params
['src'], $predicates );
351 $this->destSameAsSource
= false;
352 if ( $this->fileExists( $this->params
['dst'], $predicates ) ) {
353 if ( $this->getParam( 'overwrite' ) ) {
354 return $status; // OK
355 } elseif ( $this->getParam( 'overwriteSame' ) ) {
356 $dhash = $this->fileSha1( $this->params
['dst'], $predicates );
357 // Check if hashes are valid and match each other...
358 if ( !strlen( $this->sourceSha1
) ||
!strlen( $dhash ) ) {
359 $status->fatal( 'backend-fail-hashes' );
360 } elseif ( $this->sourceSha1
!== $dhash ) {
361 // Give an error if the files are not identical
362 $status->fatal( 'backend-fail-notsame', $this->params
['dst'] );
364 $this->destSameAsSource
= true; // OK
366 return $status; // do nothing; either OK or bad status
368 $status->fatal( 'backend-fail-alreadyexists', $this->params
['dst'] );
376 * precheckDestExistence() helper function to get the source file SHA-1.
377 * Subclasses should overwride this iff the source is not in storage.
379 * @return string|bool Returns false on failure
381 protected function getSourceSha1Base36() {
386 * Check if a file will exist in storage when this operation is attempted
388 * @param $source string Storage path
389 * @param $predicates Array
392 final protected function fileExists( $source, array $predicates ) {
393 if ( isset( $predicates['exists'][$source] ) ) {
394 return $predicates['exists'][$source]; // previous op assures this
396 $params = array( 'src' => $source, 'latest' => $this->useLatest
);
397 return $this->backend
->fileExists( $params );
402 * Get the SHA-1 of a file in storage when this operation is attempted
404 * @param $source string Storage path
405 * @param $predicates Array
406 * @return string|bool False on failure
408 final protected function fileSha1( $source, array $predicates ) {
409 if ( isset( $predicates['sha1'][$source] ) ) {
410 return $predicates['sha1'][$source]; // previous op assures this
412 $params = array( 'src' => $source, 'latest' => $this->useLatest
);
413 return $this->backend
->getFileSha1Base36( $params );
418 * Log a file operation failure and preserve any temp files
420 * @param $action string
423 final protected function logFailure( $action ) {
424 $params = $this->params
;
425 $params['failedAction'] = $action;
427 wfDebugLog( 'FileOperation', get_class( $this ) .
428 " failed (batch #{$this->batchId}): " . FormatJson
::encode( $params ) );
429 } catch ( Exception
$e ) {
430 // bad config? debug log error?
436 * FileOp helper class to expand PHP execution time for a function.
437 * On construction, set_time_limit() is called and set to $seconds.
438 * When the object goes out of scope, the timer is restarted, with
439 * the original time limit minus the time the object existed.
441 class FileOpScopedPHPTimeout
{
442 protected $startTime; // float; seconds
443 protected $oldTimeout; // integer; seconds
445 protected static $stackDepth = 0; // integer
446 protected static $totalCalls = 0; // integer
447 protected static $totalElapsed = 0; // float; seconds
449 /* Prevent callers in infinite loops from running forever */
450 const MAX_TOTAL_CALLS
= 1000000;
451 const MAX_TOTAL_TIME
= 300; // seconds
454 * @param $seconds integer
456 public function __construct( $seconds ) {
457 if ( ini_get( 'max_execution_time' ) > 0 ) { // CLI uses 0
458 if ( self
::$totalCalls >= self
::MAX_TOTAL_CALLS
) {
459 trigger_error( "Maximum invocations of " . __CLASS__
. " exceeded." );
460 } elseif ( self
::$totalElapsed >= self
::MAX_TOTAL_TIME
) {
461 trigger_error( "Time limit within invocations of " . __CLASS__
. " exceeded." );
462 } elseif ( self
::$stackDepth > 0 ) { // recursion guard
463 trigger_error( "Resursive invocation of " . __CLASS__
. " attempted." );
465 $this->oldTimeout
= ini_set( 'max_execution_time', $seconds );
466 $this->startTime
= microtime( true );
468 ++self
::$totalCalls; // proof against < 1us scopes
474 * Restore the original timeout.
475 * This does not account for the timer value on __construct().
477 public function __destruct() {
478 if ( $this->oldTimeout
) {
479 $elapsed = microtime( true ) - $this->startTime
;
480 // Note: a limit of 0 is treated as "forever"
481 set_time_limit( max( 1, $this->oldTimeout
- (int)$elapsed ) );
482 // If each scoped timeout is for less than one second, we end up
483 // restoring the original timeout without any decrease in value.
484 // Thus web scripts in an infinite loop can run forever unless we
485 // take some measures to prevent this. Track total time and calls.
486 self
::$totalElapsed +
= $elapsed;
493 * Store a file into the backend from a file on the file system.
494 * Parameters similar to FileBackendStore::storeInternal(), which include:
495 * src : source path on file system
496 * dst : destination storage path
497 * overwrite : do nothing and pass if an identical file exists at destination
498 * overwriteSame : override any existing file at destination
500 class StoreFileOp
extends FileOp
{
501 protected function allowedParams() {
502 return array( array( 'src', 'dst' ), array( 'overwrite', 'overwriteSame' ) );
505 protected function doPrecheck( array &$predicates ) {
506 $status = Status
::newGood();
507 // Check if the source file exists on the file system
508 if ( !is_file( $this->params
['src'] ) ) {
509 $status->fatal( 'backend-fail-notexists', $this->params
['src'] );
511 // Check if the source file is too big
512 } elseif ( filesize( $this->params
['src'] ) > $this->backend
->maxFileSizeInternal() ) {
513 $status->fatal( 'backend-fail-store', $this->params
['src'], $this->params
['dst'] );
515 // Check if a file can be placed at the destination
516 } elseif ( !$this->backend
->isPathUsableInternal( $this->params
['dst'] ) ) {
517 $status->fatal( 'backend-fail-store', $this->params
['src'], $this->params
['dst'] );
520 // Check if destination file exists
521 $status->merge( $this->precheckDestExistence( $predicates ) );
522 if ( $status->isOK() ) {
523 // Update file existence predicates
524 $predicates['exists'][$this->params
['dst']] = true;
525 $predicates['sha1'][$this->params
['dst']] = $this->sourceSha1
;
527 return $status; // safe to call attempt()
530 protected function doAttempt() {
531 $status = Status
::newGood();
532 // Store the file at the destination
533 if ( !$this->destSameAsSource
) {
534 $status->merge( $this->backend
->storeInternal( $this->params
) );
539 protected function getSourceSha1Base36() {
540 wfSuppressWarnings();
541 $hash = sha1_file( $this->params
['src'] );
543 if ( $hash !== false ) {
544 $hash = wfBaseConvert( $hash, 16, 36, 31 );
549 public function storagePathsChanged() {
550 return array( $this->params
['dst'] );
555 * Create a file in the backend with the given content.
556 * Parameters similar to FileBackendStore::createInternal(), which include:
557 * content : the raw file contents
558 * dst : destination storage path
559 * overwrite : do nothing and pass if an identical file exists at destination
560 * overwriteSame : override any existing file at destination
562 class CreateFileOp
extends FileOp
{
563 protected function allowedParams() {
564 return array( array( 'content', 'dst' ), array( 'overwrite', 'overwriteSame' ) );
567 protected function doPrecheck( array &$predicates ) {
568 $status = Status
::newGood();
569 // Check if the source data is too big
570 if ( strlen( $this->getParam( 'content' ) ) > $this->backend
->maxFileSizeInternal() ) {
571 $status->fatal( 'backend-fail-create', $this->params
['dst'] );
573 // Check if a file can be placed at the destination
574 } elseif ( !$this->backend
->isPathUsableInternal( $this->params
['dst'] ) ) {
575 $status->fatal( 'backend-fail-create', $this->params
['dst'] );
578 // Check if destination file exists
579 $status->merge( $this->precheckDestExistence( $predicates ) );
580 if ( $status->isOK() ) {
581 // Update file existence predicates
582 $predicates['exists'][$this->params
['dst']] = true;
583 $predicates['sha1'][$this->params
['dst']] = $this->sourceSha1
;
585 return $status; // safe to call attempt()
588 protected function doAttempt() {
589 $status = Status
::newGood();
590 // Create the file at the destination
591 if ( !$this->destSameAsSource
) {
592 $status->merge( $this->backend
->createInternal( $this->params
) );
597 protected function getSourceSha1Base36() {
598 return wfBaseConvert( sha1( $this->params
['content'] ), 16, 36, 31 );
601 public function storagePathsChanged() {
602 return array( $this->params
['dst'] );
607 * Copy a file from one storage path to another in the backend.
608 * Parameters similar to FileBackendStore::copyInternal(), which include:
609 * src : source storage path
610 * dst : destination storage path
611 * overwrite : do nothing and pass if an identical file exists at destination
612 * overwriteSame : override any existing file at destination
614 class CopyFileOp
extends FileOp
{
615 protected function allowedParams() {
616 return array( array( 'src', 'dst' ), array( 'overwrite', 'overwriteSame' ) );
619 protected function doPrecheck( array &$predicates ) {
620 $status = Status
::newGood();
621 // Check if the source file exists
622 if ( !$this->fileExists( $this->params
['src'], $predicates ) ) {
623 $status->fatal( 'backend-fail-notexists', $this->params
['src'] );
625 // Check if a file can be placed at the destination
626 } elseif ( !$this->backend
->isPathUsableInternal( $this->params
['dst'] ) ) {
627 $status->fatal( 'backend-fail-copy', $this->params
['src'], $this->params
['dst'] );
630 // Check if destination file exists
631 $status->merge( $this->precheckDestExistence( $predicates ) );
632 if ( $status->isOK() ) {
633 // Update file existence predicates
634 $predicates['exists'][$this->params
['dst']] = true;
635 $predicates['sha1'][$this->params
['dst']] = $this->sourceSha1
;
637 return $status; // safe to call attempt()
640 protected function doAttempt() {
641 $status = Status
::newGood();
642 // Do nothing if the src/dst paths are the same
643 if ( $this->params
['src'] !== $this->params
['dst'] ) {
644 // Copy the file into the destination
645 if ( !$this->destSameAsSource
) {
646 $status->merge( $this->backend
->copyInternal( $this->params
) );
652 public function storagePathsRead() {
653 return array( $this->params
['src'] );
656 public function storagePathsChanged() {
657 return array( $this->params
['dst'] );
662 * Move a file from one storage path to another in the backend.
663 * Parameters similar to FileBackendStore::moveInternal(), which include:
664 * src : source storage path
665 * dst : destination storage path
666 * overwrite : do nothing and pass if an identical file exists at destination
667 * overwriteSame : override any existing file at destination
669 class MoveFileOp
extends FileOp
{
670 protected function allowedParams() {
671 return array( array( 'src', 'dst' ), array( 'overwrite', 'overwriteSame' ) );
674 protected function doPrecheck( array &$predicates ) {
675 $status = Status
::newGood();
676 // Check if the source file exists
677 if ( !$this->fileExists( $this->params
['src'], $predicates ) ) {
678 $status->fatal( 'backend-fail-notexists', $this->params
['src'] );
680 // Check if a file can be placed at the destination
681 } elseif ( !$this->backend
->isPathUsableInternal( $this->params
['dst'] ) ) {
682 $status->fatal( 'backend-fail-move', $this->params
['src'], $this->params
['dst'] );
685 // Check if destination file exists
686 $status->merge( $this->precheckDestExistence( $predicates ) );
687 if ( $status->isOK() ) {
688 // Update file existence predicates
689 $predicates['exists'][$this->params
['src']] = false;
690 $predicates['sha1'][$this->params
['src']] = false;
691 $predicates['exists'][$this->params
['dst']] = true;
692 $predicates['sha1'][$this->params
['dst']] = $this->sourceSha1
;
694 return $status; // safe to call attempt()
697 protected function doAttempt() {
698 $status = Status
::newGood();
699 // Do nothing if the src/dst paths are the same
700 if ( $this->params
['src'] !== $this->params
['dst'] ) {
701 if ( !$this->destSameAsSource
) {
702 // Move the file into the destination
703 $status->merge( $this->backend
->moveInternal( $this->params
) );
705 // Just delete source as the destination needs no changes
706 $params = array( 'src' => $this->params
['src'] );
707 $status->merge( $this->backend
->deleteInternal( $params ) );
713 public function storagePathsRead() {
714 return array( $this->params
['src'] );
717 public function storagePathsChanged() {
718 return array( $this->params
['dst'] );
723 * Delete a file at the given storage path from the backend.
724 * Parameters similar to FileBackendStore::deleteInternal(), which include:
725 * src : source storage path
726 * ignoreMissingSource : don't return an error if the file does not exist
728 class DeleteFileOp
extends FileOp
{
729 protected function allowedParams() {
730 return array( array( 'src' ), array( 'ignoreMissingSource' ) );
733 protected $needsDelete = true;
735 protected function doPrecheck( array &$predicates ) {
736 $status = Status
::newGood();
737 // Check if the source file exists
738 if ( !$this->fileExists( $this->params
['src'], $predicates ) ) {
739 if ( !$this->getParam( 'ignoreMissingSource' ) ) {
740 $status->fatal( 'backend-fail-notexists', $this->params
['src'] );
743 $this->needsDelete
= false;
745 // Update file existence predicates
746 $predicates['exists'][$this->params
['src']] = false;
747 $predicates['sha1'][$this->params
['src']] = false;
748 return $status; // safe to call attempt()
751 protected function doAttempt() {
752 $status = Status
::newGood();
753 if ( $this->needsDelete
) {
754 // Delete the source file
755 $status->merge( $this->backend
->deleteInternal( $this->params
) );
760 public function storagePathsChanged() {
761 return array( $this->params
['src'] );
766 * Placeholder operation that has no params and does nothing
768 class NullFileOp
extends FileOp
{}