Merge "maintenance: Script to rename titles for Unicode uppercasing changes"
[lhc/web/wiklou.git] / includes / libs / objectcache / MultiWriteBagOStuff.php
1 <?php
2 /**
3 * Wrapper for object caching in different caches.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Cache
22 */
23 use Wikimedia\ObjectFactory;
24
25 /**
26 * A cache class that replicates all writes to multiple child caches. Reads
27 * are implemented by reading from the caches in the order they are given in
28 * the configuration until a cache gives a positive result.
29 *
30 * Note that cache key construction will use the first cache backend in the list,
31 * so make sure that the other backends can handle such keys (e.g. via encoding).
32 *
33 * @ingroup Cache
34 */
35 class MultiWriteBagOStuff extends BagOStuff {
36 /** @var BagOStuff[] */
37 protected $caches;
38 /** @var bool Use async secondary writes */
39 protected $asyncWrites = false;
40 /** @var int[] List of all backing cache indexes */
41 protected $cacheIndexes = [];
42
43 const UPGRADE_TTL = 3600; // TTL when a key is copied to a higher cache tier
44
45 /**
46 * $params include:
47 * - caches: A numbered array of either ObjectFactory::getObjectFromSpec
48 * arrays yeilding BagOStuff objects or direct BagOStuff objects.
49 * If using the former, the 'args' field *must* be set.
50 * The first cache is the primary one, being the first to
51 * be read in the fallback chain. Writes happen to all stores
52 * in the order they are defined. However, lock()/unlock() calls
53 * only use the primary store.
54 * - replication: Either 'sync' or 'async'. This controls whether writes
55 * to secondary stores are deferred when possible. Async writes
56 * require setting 'asyncHandler'. HHVM register_postsend_function() function.
57 * Async writes can increase the chance of some race conditions
58 * or cause keys to expire seconds later than expected. It is
59 * safe to use for modules when cached values: are immutable,
60 * invalidation uses logical TTLs, invalidation uses etag/timestamp
61 * validation against the DB, or merge() is used to handle races.
62 * @param array $params
63 * @throws InvalidArgumentException
64 */
65 public function __construct( $params ) {
66 parent::__construct( $params );
67
68 if ( empty( $params['caches'] ) || !is_array( $params['caches'] ) ) {
69 throw new InvalidArgumentException(
70 __METHOD__ . ': "caches" parameter must be an array of caches'
71 );
72 }
73
74 $this->caches = [];
75 foreach ( $params['caches'] as $cacheInfo ) {
76 if ( $cacheInfo instanceof BagOStuff ) {
77 $this->caches[] = $cacheInfo;
78 } else {
79 if ( !isset( $cacheInfo['args'] ) ) {
80 // B/C for when $cacheInfo was for ObjectCache::newFromParams().
81 // Callers intenting this to be for ObjectFactory::getObjectFromSpec
82 // should have set "args" per the docs above. Doings so avoids extra
83 // (likely harmless) params (factory/class/calls) ending up in "args".
84 $cacheInfo['args'] = [ $cacheInfo ];
85 }
86 $this->caches[] = ObjectFactory::getObjectFromSpec( $cacheInfo );
87 }
88 }
89 $this->mergeFlagMaps( $this->caches );
90
91 $this->asyncWrites = (
92 isset( $params['replication'] ) &&
93 $params['replication'] === 'async' &&
94 is_callable( $this->asyncHandler )
95 );
96
97 $this->cacheIndexes = array_keys( $this->caches );
98 }
99
100 public function setDebug( $debug ) {
101 foreach ( $this->caches as $cache ) {
102 $cache->setDebug( $debug );
103 }
104 }
105
106 public function get( $key, $flags = 0 ) {
107 if ( ( $flags & self::READ_LATEST ) == self::READ_LATEST ) {
108 // If the latest write was a delete(), we do NOT want to fallback
109 // to the other tiers and possibly see the old value. Also, this
110 // is used by merge(), which only needs to hit the primary.
111 return $this->caches[0]->get( $key, $flags );
112 }
113
114 $value = false;
115 $missIndexes = []; // backends checked
116 foreach ( $this->caches as $i => $cache ) {
117 $value = $cache->get( $key, $flags );
118 if ( $value !== false ) {
119 break;
120 }
121 $missIndexes[] = $i;
122 }
123
124 if ( $value !== false
125 && $missIndexes
126 && ( $flags & self::READ_VERIFIED ) == self::READ_VERIFIED
127 ) {
128 // Backfill the value to the higher (and often faster/smaller) cache tiers
129 $this->doWrite(
130 $missIndexes,
131 $this->asyncWrites,
132 'set',
133 // @TODO: consider using self::WRITE_ALLOW_SEGMENTS here?
134 [ $key, $value, self::UPGRADE_TTL ]
135 );
136 }
137
138 return $value;
139 }
140
141 public function set( $key, $value, $exptime = 0, $flags = 0 ) {
142 return $this->doWrite(
143 $this->cacheIndexes,
144 $this->usesAsyncWritesGivenFlags( $flags ),
145 __FUNCTION__,
146 func_get_args()
147 );
148 }
149
150 public function delete( $key, $flags = 0 ) {
151 return $this->doWrite(
152 $this->cacheIndexes,
153 $this->usesAsyncWritesGivenFlags( $flags ),
154 __FUNCTION__,
155 func_get_args()
156 );
157 }
158
159 public function add( $key, $value, $exptime = 0, $flags = 0 ) {
160 // Try the write to the top-tier cache
161 $ok = $this->doWrite(
162 [ 0 ],
163 $this->usesAsyncWritesGivenFlags( $flags ),
164 __FUNCTION__,
165 func_get_args()
166 );
167
168 if ( $ok ) {
169 // Relay the add() using set() if it succeeded. This is meant to handle certain
170 // migration scenarios where the same store might get written to twice for certain
171 // keys. In that case, it does not make sense to return false due to "self-conflicts".
172 return $this->doWrite(
173 array_slice( $this->cacheIndexes, 1 ),
174 $this->usesAsyncWritesGivenFlags( $flags ),
175 'set',
176 [ $key, $value, $exptime, $flags ]
177 );
178 }
179
180 return false;
181 }
182
183 public function merge( $key, callable $callback, $exptime = 0, $attempts = 10, $flags = 0 ) {
184 return $this->doWrite(
185 $this->cacheIndexes,
186 $this->usesAsyncWritesGivenFlags( $flags ),
187 __FUNCTION__,
188 func_get_args()
189 );
190 }
191
192 public function changeTTL( $key, $exptime = 0, $flags = 0 ) {
193 return $this->doWrite(
194 $this->cacheIndexes,
195 $this->usesAsyncWritesGivenFlags( $flags ),
196 __FUNCTION__,
197 func_get_args()
198 );
199 }
200
201 public function lock( $key, $timeout = 6, $expiry = 6, $rclass = '' ) {
202 // Only need to lock the first cache; also avoids deadlocks
203 return $this->caches[0]->lock( $key, $timeout, $expiry, $rclass );
204 }
205
206 public function unlock( $key ) {
207 // Only the first cache is locked
208 return $this->caches[0]->unlock( $key );
209 }
210
211 public function deleteObjectsExpiringBefore(
212 $timestamp,
213 callable $progressCallback = null,
214 $limit = INF
215 ) {
216 $ret = false;
217 foreach ( $this->caches as $cache ) {
218 if ( $cache->deleteObjectsExpiringBefore( $timestamp, $progressCallback, $limit ) ) {
219 $ret = true;
220 }
221 }
222
223 return $ret;
224 }
225
226 public function getMulti( array $keys, $flags = 0 ) {
227 // Just iterate over each key in order to handle all the backfill logic
228 $res = [];
229 foreach ( $keys as $key ) {
230 $val = $this->get( $key, $flags );
231 if ( $val !== false ) {
232 $res[$key] = $val;
233 }
234 }
235
236 return $res;
237 }
238
239 public function setMulti( array $data, $exptime = 0, $flags = 0 ) {
240 return $this->doWrite(
241 $this->cacheIndexes,
242 $this->usesAsyncWritesGivenFlags( $flags ),
243 __FUNCTION__,
244 func_get_args()
245 );
246 }
247
248 public function deleteMulti( array $data, $flags = 0 ) {
249 return $this->doWrite(
250 $this->cacheIndexes,
251 $this->usesAsyncWritesGivenFlags( $flags ),
252 __FUNCTION__,
253 func_get_args()
254 );
255 }
256
257 public function incr( $key, $value = 1 ) {
258 return $this->doWrite(
259 $this->cacheIndexes,
260 $this->asyncWrites,
261 __FUNCTION__,
262 func_get_args()
263 );
264 }
265
266 public function decr( $key, $value = 1 ) {
267 return $this->doWrite(
268 $this->cacheIndexes,
269 $this->asyncWrites,
270 __FUNCTION__,
271 func_get_args()
272 );
273 }
274
275 public function incrWithInit( $key, $ttl, $value = 1, $init = 1 ) {
276 return $this->doWrite(
277 $this->cacheIndexes,
278 $this->asyncWrites,
279 __FUNCTION__,
280 func_get_args()
281 );
282 }
283
284 public function getLastError() {
285 return $this->caches[0]->getLastError();
286 }
287
288 public function clearLastError() {
289 $this->caches[0]->clearLastError();
290 }
291
292 /**
293 * Apply a write method to the backing caches specified by $indexes (in order)
294 *
295 * @param int[] $indexes List of backing cache indexes
296 * @param bool $asyncWrites
297 * @param string $method Method name of backing caches
298 * @param array $args Arguments to the method of backing caches
299 * @return bool
300 */
301 protected function doWrite( $indexes, $asyncWrites, $method, array $args ) {
302 $ret = true;
303
304 if ( array_diff( $indexes, [ 0 ] ) && $asyncWrites && $method !== 'merge' ) {
305 // Deep-clone $args to prevent misbehavior when something writes an
306 // object to the BagOStuff then modifies it afterwards, e.g. T168040.
307 $args = unserialize( serialize( $args ) );
308 }
309
310 foreach ( $indexes as $i ) {
311 $cache = $this->caches[$i];
312 if ( $i == 0 || !$asyncWrites ) {
313 // First store or in sync mode: write now and get result
314 if ( !$cache->$method( ...$args ) ) {
315 $ret = false;
316 }
317 } else {
318 // Secondary write in async mode: do not block this HTTP request
319 $logger = $this->logger;
320 ( $this->asyncHandler )(
321 function () use ( $cache, $method, $args, $logger ) {
322 if ( !$cache->$method( ...$args ) ) {
323 $logger->warning( "Async $method op failed" );
324 }
325 }
326 );
327 }
328 }
329
330 return $ret;
331 }
332
333 /**
334 * @param int $flags
335 * @return bool
336 */
337 protected function usesAsyncWritesGivenFlags( $flags ) {
338 return ( ( $flags & self::WRITE_SYNC ) == self::WRITE_SYNC ) ? false : $this->asyncWrites;
339 }
340
341 public function makeKeyInternal( $keyspace, $args ) {
342 return $this->caches[0]->makeKeyInternal( ...func_get_args() );
343 }
344
345 public function makeKey( $class, $component = null ) {
346 return $this->caches[0]->makeKey( ...func_get_args() );
347 }
348
349 public function makeGlobalKey( $class, $component = null ) {
350 return $this->caches[0]->makeGlobalKey( ...func_get_args() );
351 }
352
353 protected function doGet( $key, $flags = 0, &$casToken = null ) {
354 throw new LogicException( __METHOD__ . ': proxy class does not need this method.' );
355 }
356
357 protected function doSet( $key, $value, $exptime = 0, $flags = 0 ) {
358 throw new LogicException( __METHOD__ . ': proxy class does not need this method.' );
359 }
360
361 protected function doDelete( $key, $flags = 0 ) {
362 throw new LogicException( __METHOD__ . ': proxy class does not need this method.' );
363 }
364
365 protected function doGetMulti( array $keys, $flags = 0 ) {
366 throw new LogicException( __METHOD__ . ': proxy class does not need this method.' );
367 }
368
369 protected function serialize( $value ) {
370 throw new LogicException( __METHOD__ . ': proxy class does not need this method.' );
371 }
372
373 protected function unserialize( $value ) {
374 throw new LogicException( __METHOD__ . ': proxy class does not need this method.' );
375 }
376 }