Merge "Remove unused messages 'edit-externally' and 'edit-externally-help'"
[mediawiki.git] / includes / filebackend / FileOpBatch.php
blob32b65baba17460eee83f5cd791b6ec0bd095b505
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 array $performOps List of FileOp operations
53 * @param array $opts Batch operation options
54 * @param FileJournal $journal 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__ );
66 return $status;
69 $batchId = $journal->getTimestampedUUID();
70 $ignoreErrors = !empty( $opts['force'] );
71 $journaled = empty( $opts['nonJournaled'] );
72 $maxConcurrency = isset( $opts['concurrency'] ) ? $opts['concurrency'] : 1;
74 $entries = array(); // file journal entry list
75 $predicates = FileOp::newPredicates(); // account for previous ops in prechecks
76 $curBatch = array(); // concurrent FileOp sub-batch accumulation
77 $curBatchDeps = FileOp::newDependencies(); // paths used in FileOp sub-batch
78 $pPerformOps = array(); // ordered list of concurrent FileOp sub-batches
79 $lastBackend = null; // last op backend name
80 // Do pre-checks for each operation; abort on failure...
81 foreach ( $performOps as $index => $fileOp ) {
82 $backendName = $fileOp->getBackend()->getName();
83 $fileOp->setBatchId( $batchId ); // transaction ID
84 // Decide if this op can be done concurrently within this sub-batch
85 // or if a new concurrent sub-batch must be started after this one...
86 if ( $fileOp->dependsOn( $curBatchDeps )
87 || count( $curBatch ) >= $maxConcurrency
88 || ( $backendName !== $lastBackend && count( $curBatch ) )
89 ) {
90 $pPerformOps[] = $curBatch; // push this batch
91 $curBatch = array(); // start a new sub-batch
92 $curBatchDeps = FileOp::newDependencies();
94 $lastBackend = $backendName;
95 $curBatch[$index] = $fileOp; // keep index
96 // Update list of affected paths in this batch
97 $curBatchDeps = $fileOp->applyDependencies( $curBatchDeps );
98 // Simulate performing the operation...
99 $oldPredicates = $predicates;
100 $subStatus = $fileOp->precheck( $predicates ); // updates $predicates
101 $status->merge( $subStatus );
102 if ( $subStatus->isOK() ) {
103 if ( $journaled ) { // journal log entries
104 $entries = array_merge( $entries,
105 $fileOp->getJournalEntries( $oldPredicates, $predicates ) );
107 } else { // operation failed?
108 $status->success[$index] = false;
109 ++$status->failCount;
110 if ( !$ignoreErrors ) {
111 wfProfileOut( __METHOD__ );
113 return $status; // abort
117 // Push the last sub-batch
118 if ( count( $curBatch ) ) {
119 $pPerformOps[] = $curBatch;
122 // Log the operations in the file journal...
123 if ( count( $entries ) ) {
124 $subStatus = $journal->logChangeBatch( $entries, $batchId );
125 if ( !$subStatus->isOK() ) {
126 wfProfileOut( __METHOD__ );
128 return $subStatus; // abort
132 if ( $ignoreErrors ) { // treat precheck() fatals as mere warnings
133 $status->setResult( true, $status->value );
136 // Attempt each operation (in parallel if allowed and possible)...
137 self::runParallelBatches( $pPerformOps, $status );
139 wfProfileOut( __METHOD__ );
141 return $status;
145 * Attempt a list of file operations sub-batches in series.
147 * The operations *in* each sub-batch will be done in parallel.
148 * The caller is responsible for making sure the operations
149 * within any given sub-batch do not depend on each other.
150 * This will abort remaining ops on failure.
152 * @param array $pPerformOps
153 * @param Status $status
154 * @return bool Success
156 protected static function runParallelBatches( array $pPerformOps, Status $status ) {
157 $aborted = false; // set to true on unexpected errors
158 foreach ( $pPerformOps as $performOpsBatch ) {
159 if ( $aborted ) { // check batch op abort flag...
160 // We can't continue (even with $ignoreErrors) as $predicates is wrong.
161 // Log the remaining ops as failed for recovery...
162 foreach ( $performOpsBatch as $i => $fileOp ) {
163 $performOpsBatch[$i]->logFailure( 'attempt_aborted' );
165 continue;
167 $statuses = array();
168 $opHandles = array();
169 // Get the backend; all sub-batch ops belong to a single backend
170 $backend = reset( $performOpsBatch )->getBackend();
171 // Get the operation handles or actually do it if there is just one.
172 // If attemptAsync() returns a Status, it was either due to an error
173 // or the backend does not support async ops and did it synchronously.
174 foreach ( $performOpsBatch as $i => $fileOp ) {
175 if ( !$fileOp->failed() ) { // failed => already has Status
176 // If the batch is just one operation, it's faster to avoid
177 // pipelining as that can involve creating new TCP connections.
178 $subStatus = ( count( $performOpsBatch ) > 1 )
179 ? $fileOp->attemptAsync()
180 : $fileOp->attempt();
181 if ( $subStatus->value instanceof FileBackendStoreOpHandle ) {
182 $opHandles[$i] = $subStatus->value; // deferred
183 } else {
184 $statuses[$i] = $subStatus; // done already
188 // Try to do all the operations concurrently...
189 $statuses = $statuses + $backend->executeOpHandlesInternal( $opHandles );
190 // Marshall and merge all the responses (blocking)...
191 foreach ( $performOpsBatch as $i => $fileOp ) {
192 if ( !$fileOp->failed() ) { // failed => already has Status
193 $subStatus = $statuses[$i];
194 $status->merge( $subStatus );
195 if ( $subStatus->isOK() ) {
196 $status->success[$i] = true;
197 ++$status->successCount;
198 } else {
199 $status->success[$i] = false;
200 ++$status->failCount;
201 $aborted = true; // set abort flag; we can't continue
207 return $status;