Merge "Fix API output formatting (change lines delimited with * as bold)"
[mediawiki.git] / includes / filebackend / FileOpBatch.php
blob6e103ec5507c2405eb37b6def16faf7741e3489f
1 <?php
2 /**
3 * Helper class for representing batch file operations.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup FileBackend
22 * @author Aaron Schulz
25 /**
26 * Helper class for representing batch file operations.
27 * Do not use this class from places outside FileBackend.
29 * Methods should avoid throwing exceptions at all costs.
31 * @ingroup FileBackend
32 * @since 1.20
34 class FileOpBatch {
35 /* Timeout related parameters */
36 const MAX_BATCH_SIZE = 1000; // integer
38 /**
39 * Attempt to perform a series of file operations.
40 * Callers are responsible for handling file locking.
42 * $opts is an array of options, including:
43 * - force : Errors that would normally cause a rollback do not.
44 * The remaining operations are still attempted if any fail.
45 * - nonJournaled : Don't log this operation batch in the file journal.
46 * - concurrency : Try to do this many operations in parallel when possible.
48 * The resulting Status will be "OK" unless:
49 * - a) unexpected operation errors occurred (network partitions, disk full...)
50 * - b) significant operation errors occurred and 'force' was not set
52 * @param $performOps Array List of FileOp operations
53 * @param $opts Array Batch operation options
54 * @param $journal FileJournal Journal to log operations to
55 * @return Status
57 public static function attempt( array $performOps, array $opts, FileJournal $journal ) {
58 wfProfileIn( __METHOD__ );
59 $status = Status::newGood();
61 $n = count( $performOps );
62 if ( $n > self::MAX_BATCH_SIZE ) {
63 $status->fatal( 'backend-fail-batchsize', $n, self::MAX_BATCH_SIZE );
64 wfProfileOut( __METHOD__ );
65 return $status;
68 $batchId = $journal->getTimestampedUUID();
69 $ignoreErrors = !empty( $opts['force'] );
70 $journaled = empty( $opts['nonJournaled'] );
71 $maxConcurrency = isset( $opts['concurrency'] ) ? $opts['concurrency'] : 1;
73 $entries = array(); // file journal entry list
74 $predicates = FileOp::newPredicates(); // account for previous ops in prechecks
75 $curBatch = array(); // concurrent FileOp sub-batch accumulation
76 $curBatchDeps = FileOp::newDependencies(); // paths used in FileOp sub-batch
77 $pPerformOps = array(); // ordered list of concurrent FileOp sub-batches
78 $lastBackend = null; // last op backend name
79 // Do pre-checks for each operation; abort on failure...
80 foreach ( $performOps as $index => $fileOp ) {
81 $backendName = $fileOp->getBackend()->getName();
82 $fileOp->setBatchId( $batchId ); // transaction ID
83 // Decide if this op can be done concurrently within this sub-batch
84 // or if a new concurrent sub-batch must be started after this one...
85 if ( $fileOp->dependsOn( $curBatchDeps )
86 || count( $curBatch ) >= $maxConcurrency
87 || ( $backendName !== $lastBackend && count( $curBatch ) )
88 ) {
89 $pPerformOps[] = $curBatch; // push this batch
90 $curBatch = array(); // start a new sub-batch
91 $curBatchDeps = FileOp::newDependencies();
93 $lastBackend = $backendName;
94 $curBatch[$index] = $fileOp; // keep index
95 // Update list of affected paths in this batch
96 $curBatchDeps = $fileOp->applyDependencies( $curBatchDeps );
97 // Simulate performing the operation...
98 $oldPredicates = $predicates;
99 $subStatus = $fileOp->precheck( $predicates ); // updates $predicates
100 $status->merge( $subStatus );
101 if ( $subStatus->isOK() ) {
102 if ( $journaled ) { // journal log entries
103 $entries = array_merge( $entries,
104 $fileOp->getJournalEntries( $oldPredicates, $predicates ) );
106 } else { // operation failed?
107 $status->success[$index] = false;
108 ++$status->failCount;
109 if ( !$ignoreErrors ) {
110 wfProfileOut( __METHOD__ );
111 return $status; // abort
115 // Push the last sub-batch
116 if ( count( $curBatch ) ) {
117 $pPerformOps[] = $curBatch;
120 // Log the operations in the file journal...
121 if ( count( $entries ) ) {
122 $subStatus = $journal->logChangeBatch( $entries, $batchId );
123 if ( !$subStatus->isOK() ) {
124 wfProfileOut( __METHOD__ );
125 return $subStatus; // abort
129 if ( $ignoreErrors ) { // treat precheck() fatals as mere warnings
130 $status->setResult( true, $status->value );
133 // Attempt each operation (in parallel if allowed and possible)...
134 self::runParallelBatches( $pPerformOps, $status );
136 wfProfileOut( __METHOD__ );
137 return $status;
141 * Attempt a list of file operations sub-batches in series.
143 * The operations *in* each sub-batch will be done in parallel.
144 * The caller is responsible for making sure the operations
145 * within any given sub-batch do not depend on each other.
146 * This will abort remaining ops on failure.
148 * @param $pPerformOps Array
149 * @param $status Status
150 * @return bool Success
152 protected static function runParallelBatches( array $pPerformOps, Status $status ) {
153 $aborted = false; // set to true on unexpected errors
154 foreach ( $pPerformOps as $performOpsBatch ) {
155 if ( $aborted ) { // check batch op abort flag...
156 // We can't continue (even with $ignoreErrors) as $predicates is wrong.
157 // Log the remaining ops as failed for recovery...
158 foreach ( $performOpsBatch as $i => $fileOp ) {
159 $performOpsBatch[$i]->logFailure( 'attempt_aborted' );
161 continue;
163 $statuses = array();
164 $opHandles = array();
165 // Get the backend; all sub-batch ops belong to a single backend
166 $backend = reset( $performOpsBatch )->getBackend();
167 // Get the operation handles or actually do it if there is just one.
168 // If attemptAsync() returns a Status, it was either due to an error
169 // or the backend does not support async ops and did it synchronously.
170 foreach ( $performOpsBatch as $i => $fileOp ) {
171 if ( !$fileOp->failed() ) { // failed => already has Status
172 // If the batch is just one operation, it's faster to avoid
173 // pipelining as that can involve creating new TCP connections.
174 $subStatus = ( count( $performOpsBatch ) > 1 )
175 ? $fileOp->attemptAsync()
176 : $fileOp->attempt();
177 if ( $subStatus->value instanceof FileBackendStoreOpHandle ) {
178 $opHandles[$i] = $subStatus->value; // deferred
179 } else {
180 $statuses[$i] = $subStatus; // done already
184 // Try to do all the operations concurrently...
185 $statuses = $statuses + $backend->executeOpHandlesInternal( $opHandles );
186 // Marshall and merge all the responses (blocking)...
187 foreach ( $performOpsBatch as $i => $fileOp ) {
188 if ( !$fileOp->failed() ) { // failed => already has Status
189 $subStatus = $statuses[$i];
190 $status->merge( $subStatus );
191 if ( $subStatus->isOK() ) {
192 $status->success[$i] = true;
193 ++$status->successCount;
194 } else {
195 $status->success[$i] = false;
196 ++$status->failCount;
197 $aborted = true; // set abort flag; we can't continue
202 return $status;