MediaWiki  REL1_20
FileOpBatch.php
Go to the documentation of this file.
00001 <?php
00034 class FileOpBatch {
00035         /* Timeout related parameters */
00036         const MAX_BATCH_SIZE = 1000; // integer
00037 
00060         public static function attempt( array $performOps, array $opts, FileJournal $journal ) {
00061                 wfProfileIn( __METHOD__ );
00062                 $status = Status::newGood();
00063 
00064                 $n = count( $performOps );
00065                 if ( $n > self::MAX_BATCH_SIZE ) {
00066                         $status->fatal( 'backend-fail-batchsize', $n, self::MAX_BATCH_SIZE );
00067                         wfProfileOut( __METHOD__ );
00068                         return $status;
00069                 }
00070 
00071                 $batchId = $journal->getTimestampedUUID();
00072                 $allowStale = !empty( $opts['allowStale'] );
00073                 $ignoreErrors = !empty( $opts['force'] );
00074                 $journaled = empty( $opts['nonJournaled'] );
00075                 $maxConcurrency = isset( $opts['concurrency'] ) ? $opts['concurrency'] : 1;
00076 
00077                 $entries = array(); // file journal entry list
00078                 $predicates = FileOp::newPredicates(); // account for previous ops in prechecks
00079                 $curBatch = array(); // concurrent FileOp sub-batch accumulation
00080                 $curBatchDeps = FileOp::newDependencies(); // paths used in FileOp sub-batch
00081                 $pPerformOps = array(); // ordered list of concurrent FileOp sub-batches
00082                 $lastBackend = null; // last op backend name
00083                 // Do pre-checks for each operation; abort on failure...
00084                 foreach ( $performOps as $index => $fileOp ) {
00085                         $backendName = $fileOp->getBackend()->getName();
00086                         $fileOp->setBatchId( $batchId ); // transaction ID
00087                         $fileOp->allowStaleReads( $allowStale ); // consistency level
00088                         // Decide if this op can be done concurrently within this sub-batch
00089                         // or if a new concurrent sub-batch must be started after this one...
00090                         if ( $fileOp->dependsOn( $curBatchDeps )
00091                                 || count( $curBatch ) >= $maxConcurrency
00092                                 || ( $backendName !== $lastBackend && count( $curBatch ) )
00093                         ) {
00094                                 $pPerformOps[] = $curBatch; // push this batch
00095                                 $curBatch = array(); // start a new sub-batch
00096                                 $curBatchDeps = FileOp::newDependencies();
00097                         }
00098                         $lastBackend = $backendName;
00099                         $curBatch[$index] = $fileOp; // keep index
00100                         // Update list of affected paths in this batch
00101                         $curBatchDeps = $fileOp->applyDependencies( $curBatchDeps );
00102                         // Simulate performing the operation...
00103                         $oldPredicates = $predicates;
00104                         $subStatus = $fileOp->precheck( $predicates ); // updates $predicates
00105                         $status->merge( $subStatus );
00106                         if ( $subStatus->isOK() ) {
00107                                 if ( $journaled ) { // journal log entries
00108                                         $entries = array_merge( $entries,
00109                                                 $fileOp->getJournalEntries( $oldPredicates, $predicates ) );
00110                                 }
00111                         } else { // operation failed?
00112                                 $status->success[$index] = false;
00113                                 ++$status->failCount;
00114                                 if ( !$ignoreErrors ) {
00115                                         wfProfileOut( __METHOD__ );
00116                                         return $status; // abort
00117                                 }
00118                         }
00119                 }
00120                 // Push the last sub-batch
00121                 if ( count( $curBatch ) ) {
00122                         $pPerformOps[] = $curBatch;
00123                 }
00124 
00125                 // Log the operations in the file journal...
00126                 if ( count( $entries ) ) {
00127                         $subStatus = $journal->logChangeBatch( $entries, $batchId );
00128                         if ( !$subStatus->isOK() ) {
00129                                 wfProfileOut( __METHOD__ );
00130                                 return $subStatus; // abort
00131                         }
00132                 }
00133 
00134                 if ( $ignoreErrors ) { // treat precheck() fatals as mere warnings
00135                         $status->setResult( true, $status->value );
00136                 }
00137 
00138                 // Attempt each operation (in parallel if allowed and possible)...
00139                 if ( count( $pPerformOps ) < count( $performOps ) ) {
00140                         self::runBatchParallel( $pPerformOps, $status );
00141                 } else {
00142                         self::runBatchSeries( $performOps, $status );
00143                 }
00144 
00145                 wfProfileOut( __METHOD__ );
00146                 return $status;
00147         }
00148 
00157         protected static function runBatchSeries( array $performOps, Status $status ) {
00158                 foreach ( $performOps as $index => $fileOp ) {
00159                         if ( $fileOp->failed() ) {
00160                                 continue; // nothing to do
00161                         }
00162                         $subStatus = $fileOp->attempt();
00163                         $status->merge( $subStatus );
00164                         if ( $subStatus->isOK() ) {
00165                                 $status->success[$index] = true;
00166                                 ++$status->successCount;
00167                         } else {
00168                                 $status->success[$index] = false;
00169                                 ++$status->failCount;
00170                                 // We can't continue (even with $ignoreErrors) as $predicates is wrong.
00171                                 // Log the remaining ops as failed for recovery...
00172                                 for ( $i = ($index + 1); $i < count( $performOps ); $i++ ) {
00173                                         $performOps[$i]->logFailure( 'attempt_aborted' );
00174                                 }
00175                                 return false; // bail out
00176                         }
00177                 }
00178                 return true;
00179         }
00180 
00193         protected static function runBatchParallel( array $pPerformOps, Status $status ) {
00194                 $aborted = false;
00195                 foreach ( $pPerformOps as $performOpsBatch ) {
00196                         if ( $aborted ) { // check batch op abort flag...
00197                                 // We can't continue (even with $ignoreErrors) as $predicates is wrong.
00198                                 // Log the remaining ops as failed for recovery...
00199                                 foreach ( $performOpsBatch as $i => $fileOp ) {
00200                                         $performOpsBatch[$i]->logFailure( 'attempt_aborted' );
00201                                 }
00202                                 continue;
00203                         }
00204                         $statuses = array();
00205                         $opHandles = array();
00206                         // Get the backend; all sub-batch ops belong to a single backend
00207                         $backend = reset( $performOpsBatch )->getBackend();
00208                         // If attemptAsync() returns synchronously, it was either an
00209                         // error Status or the backend just doesn't support async ops.
00210                         foreach ( $performOpsBatch as $i => $fileOp ) {
00211                                 if ( !$fileOp->failed() ) { // failed => already has Status
00212                                         $subStatus = $fileOp->attemptAsync();
00213                                         if ( $subStatus->value instanceof FileBackendStoreOpHandle ) {
00214                                                 $opHandles[$i] = $subStatus->value; // deferred
00215                                         } else {
00216                                                 $statuses[$i] = $subStatus; // done already
00217                                         }
00218                                 }
00219                         }
00220                         // Try to do all the operations concurrently...
00221                         $statuses = $statuses + $backend->executeOpHandlesInternal( $opHandles );
00222                         // Marshall and merge all the responses (blocking)...
00223                         foreach ( $performOpsBatch as $i => $fileOp ) {
00224                                 if ( !$fileOp->failed() ) { // failed => already has Status
00225                                         $subStatus = $statuses[$i];
00226                                         $status->merge( $subStatus );
00227                                         if ( $subStatus->isOK() ) {
00228                                                 $status->success[$i] = true;
00229                                                 ++$status->successCount;
00230                                         } else {
00231                                                 $status->success[$i] = false;
00232                                                 ++$status->failCount;
00233                                                 $aborted = true; // set abort flag; we can't continue
00234                                         }
00235                                 }
00236                         }
00237                 }
00238                 return $status;
00239         }
00240 }