Use consistent caching strategy in Revision storage classes
[lhc/web/wiklou.git] / includes / Storage / NameTableStore.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21 namespace MediaWiki\Storage;
22
23 use IExpiringStore;
24 use Psr\Log\LoggerInterface;
25 use WANObjectCache;
26 use Wikimedia\Assert\Assert;
27 use Wikimedia\Rdbms\Database;
28 use Wikimedia\Rdbms\IDatabase;
29 use Wikimedia\Rdbms\LoadBalancer;
30
31 /**
32 * @author Addshore
33 * @since 1.31
34 */
35 class NameTableStore {
36
37 /** @var LoadBalancer */
38 private $loadBalancer;
39
40 /** @var WANObjectCache */
41 private $cache;
42
43 /** @var LoggerInterface */
44 private $logger;
45
46 /** @var string[] */
47 private $tableCache = null;
48
49 /** @var bool|string */
50 private $wikiId = false;
51
52 /** @var int */
53 private $cacheTTL;
54
55 /** @var string */
56 private $table;
57 /** @var string */
58 private $idField;
59 /** @var string */
60 private $nameField;
61 /** @var null|callable */
62 private $normalizationCallback = null;
63 /** @var null|callable */
64 private $insertCallback = null;
65
66 /**
67 * @param LoadBalancer $dbLoadBalancer A load balancer for acquiring database connections
68 * @param WANObjectCache $cache A cache manager for caching data
69 * @param LoggerInterface $logger
70 * @param string $table
71 * @param string $idField
72 * @param string $nameField
73 * @param callable $normalizationCallback Normalization to be applied to names before being
74 * saved or queried. This should be a callback that accepts and returns a single string.
75 * @param bool|string $wikiId The ID of the target wiki database. Use false for the local wiki.
76 * @param callable $insertCallback Callback to change insert fields accordingly.
77 * This parameter was introduced in 1.32
78 */
79 public function __construct(
80 LoadBalancer $dbLoadBalancer,
81 WANObjectCache $cache,
82 LoggerInterface $logger,
83 $table,
84 $idField,
85 $nameField,
86 callable $normalizationCallback = null,
87 $wikiId = false,
88 callable $insertCallback = null
89 ) {
90 $this->loadBalancer = $dbLoadBalancer;
91 $this->cache = $cache;
92 $this->logger = $logger;
93 $this->table = $table;
94 $this->idField = $idField;
95 $this->nameField = $nameField;
96 $this->normalizationCallback = $normalizationCallback;
97 $this->wikiId = $wikiId;
98 $this->cacheTTL = IExpiringStore::TTL_MONTH;
99 $this->insertCallback = $insertCallback;
100 }
101
102 /**
103 * @param int $index A database index, like DB_MASTER or DB_REPLICA
104 * @param int $flags Database connection flags
105 *
106 * @return IDatabase
107 */
108 private function getDBConnection( $index, $flags = 0 ) {
109 return $this->loadBalancer->getConnection( $index, [], $this->wikiId, $flags );
110 }
111
112 /**
113 * Gets the cache key for names.
114 *
115 * The cache key is constructed based on the wiki ID passed to the constructor, and allows
116 * sharing of name tables cached for a specific database between wikis.
117 *
118 * @return string
119 */
120 private function getCacheKey() {
121 return $this->cache->makeGlobalKey(
122 'NameTableSqlStore',
123 $this->table,
124 $this->loadBalancer->resolveDomainID( $this->wikiId )
125 );
126 }
127
128 /**
129 * @param string $name
130 * @return string
131 */
132 private function normalizeName( $name ) {
133 if ( $this->normalizationCallback === null ) {
134 return $name;
135 }
136 return call_user_func( $this->normalizationCallback, $name );
137 }
138
139 /**
140 * Acquire the id of the given name.
141 * This creates a row in the table if it doesn't already exist.
142 *
143 * @param string $name
144 * @throws NameTableAccessException
145 * @return int
146 */
147 public function acquireId( $name ) {
148 Assert::parameterType( 'string', $name, '$name' );
149 $name = $this->normalizeName( $name );
150
151 $table = $this->getTableFromCachesOrReplica();
152 $searchResult = array_search( $name, $table, true );
153 if ( $searchResult === false ) {
154 $id = $this->store( $name );
155 if ( $id === null ) {
156 // RACE: $name was already in the db, probably just inserted, so load from master
157 // Use DBO_TRX to avoid missing inserts due to other threads or REPEATABLE-READs
158 $table = $this->loadTable(
159 $this->getDBConnection( DB_MASTER, LoadBalancer::CONN_TRX_AUTOCOMMIT )
160 );
161 $searchResult = array_search( $name, $table, true );
162 if ( $searchResult === false ) {
163 // Insert failed due to IGNORE flag, but DB_MASTER didn't give us the data
164 $m = "No insert possible but master didn't give us a record for " .
165 "'{$name}' in '{$this->table}'";
166 $this->logger->error( $m );
167 throw new NameTableAccessException( $m );
168 }
169 $this->purgeWANCache(
170 function () {
171 $this->cache->reap( $this->getCacheKey(), INF );
172 }
173 );
174 } else {
175 $table[$id] = $name;
176 $searchResult = $id;
177 // As store returned an ID we know we inserted so delete from WAN cache
178 $this->purgeWANCache(
179 function () {
180 $this->cache->delete( $this->getCacheKey() );
181 }
182 );
183 }
184 $this->tableCache = $table;
185 }
186
187 return $searchResult;
188 }
189
190 /**
191 * Get the id of the given name.
192 * If the name doesn't exist this will throw.
193 * This should be used in cases where we believe the name already exists or want to check for
194 * existence.
195 *
196 * @param string $name
197 * @throws NameTableAccessException The name does not exist
198 * @return int Id
199 */
200 public function getId( $name ) {
201 Assert::parameterType( 'string', $name, '$name' );
202 $name = $this->normalizeName( $name );
203
204 $table = $this->getTableFromCachesOrReplica();
205 $searchResult = array_search( $name, $table, true );
206
207 if ( $searchResult !== false ) {
208 return $searchResult;
209 }
210
211 throw NameTableAccessException::newFromDetails( $this->table, 'name', $name );
212 }
213
214 /**
215 * Get the name of the given id.
216 * If the id doesn't exist this will throw.
217 * This should be used in cases where we believe the id already exists.
218 *
219 * Note: Calls to this method will result in a master select for non existing IDs.
220 *
221 * @param int $id
222 * @throws NameTableAccessException The id does not exist
223 * @return string name
224 */
225 public function getName( $id ) {
226 Assert::parameterType( 'integer', $id, '$id' );
227
228 $table = $this->getTableFromCachesOrReplica();
229 if ( array_key_exists( $id, $table ) ) {
230 return $table[$id];
231 }
232
233 $table = $this->cache->getWithSetCallback(
234 $this->getCacheKey(),
235 $this->cacheTTL,
236 function ( $oldValue, &$ttl, &$setOpts ) use ( $id ) {
237 // Check if cached value is up-to-date enough to have $id
238 if ( is_array( $oldValue ) && array_key_exists( $id, $oldValue ) ) {
239 // Completely leave the cache key alone
240 $ttl = WANObjectCache::TTL_UNCACHEABLE;
241 // Use the old value
242 return $oldValue;
243 }
244 // Regenerate from replica DB, and master DB if needed
245 foreach ( [ DB_REPLICA, DB_MASTER ] as $source ) {
246 // Log a fallback to master
247 if ( $source === DB_MASTER ) {
248 $this->logger->info(
249 __METHOD__ . 'falling back to master select from ' .
250 $this->table . ' with id ' . $id
251 );
252 }
253 $db = $this->getDBConnection( $source );
254 $cacheSetOpts = Database::getCacheSetOptions( $db );
255 $table = $this->loadTable( $db );
256 if ( array_key_exists( $id, $table ) ) {
257 break; // found it
258 }
259 }
260 // Use the value from last source checked
261 $setOpts += $cacheSetOpts;
262
263 return $table;
264 },
265 [ 'minAsOf' => INF ] // force callback run
266 );
267
268 $this->tableCache = $table;
269
270 if ( array_key_exists( $id, $table ) ) {
271 return $table[$id];
272 }
273
274 throw NameTableAccessException::newFromDetails( $this->table, 'id', $id );
275 }
276
277 /**
278 * Get the whole table, in no particular order as a map of ids to names.
279 * This method could be subject to DB or cache lag.
280 *
281 * @return string[] keys are the name ids, values are the names themselves
282 * Example: [ 1 => 'foo', 3 => 'bar' ]
283 */
284 public function getMap() {
285 return $this->getTableFromCachesOrReplica();
286 }
287
288 /**
289 * @return string[]
290 */
291 private function getTableFromCachesOrReplica() {
292 if ( $this->tableCache !== null ) {
293 return $this->tableCache;
294 }
295
296 $table = $this->cache->getWithSetCallback(
297 $this->getCacheKey(),
298 $this->cacheTTL,
299 function ( $oldValue, &$ttl, &$setOpts ) {
300 $dbr = $this->getDBConnection( DB_REPLICA );
301 $setOpts += Database::getCacheSetOptions( $dbr );
302 return $this->loadTable( $dbr );
303 }
304 );
305
306 $this->tableCache = $table;
307
308 return $table;
309 }
310
311 /**
312 * Reap the WANCache entry for this table.
313 *
314 * @param callable $purgeCallback callback to 'purge' the WAN cache
315 */
316 private function purgeWANCache( $purgeCallback ) {
317 // If the LB has no DB changes don't both with onTransactionPreCommitOrIdle
318 if ( !$this->loadBalancer->hasOrMadeRecentMasterChanges() ) {
319 $purgeCallback();
320 return;
321 }
322
323 $this->getDBConnection( DB_MASTER )
324 ->onTransactionPreCommitOrIdle( $purgeCallback, __METHOD__ );
325 }
326
327 /**
328 * Gets the table from the db
329 *
330 * @param IDatabase $db
331 *
332 * @return string[]
333 */
334 private function loadTable( IDatabase $db ) {
335 $result = $db->select(
336 $this->table,
337 [
338 'id' => $this->idField,
339 'name' => $this->nameField
340 ],
341 [],
342 __METHOD__,
343 [ 'ORDER BY' => 'id' ]
344 );
345
346 $assocArray = [];
347 foreach ( $result as $row ) {
348 $assocArray[$row->id] = $row->name;
349 }
350
351 return $assocArray;
352 }
353
354 /**
355 * Stores the given name in the DB, returning the ID when an insert occurs.
356 *
357 * @param string $name
358 * @return int|null int if we know the ID, null if we don't
359 */
360 private function store( $name ) {
361 Assert::parameterType( 'string', $name, '$name' );
362 Assert::parameter( $name !== '', '$name', 'should not be an empty string' );
363 // Note: this is only called internally so normalization of $name has already occurred.
364
365 $dbw = $this->getDBConnection( DB_MASTER );
366
367 $dbw->insert(
368 $this->table,
369 $this->getFieldsToStore( $name ),
370 __METHOD__,
371 [ 'IGNORE' ]
372 );
373
374 if ( $dbw->affectedRows() === 0 ) {
375 $this->logger->info(
376 'Tried to insert name into table ' . $this->table . ', but value already existed.'
377 );
378 return null;
379 }
380
381 return $dbw->insertId();
382 }
383
384 /**
385 * @param string $name
386 * @return array
387 */
388 private function getFieldsToStore( $name ) {
389 $fields = [ $this->nameField => $name ];
390 if ( $this->insertCallback !== null ) {
391 $fields = call_user_func( $this->insertCallback, $fields );
392 }
393 return $fields;
394 }
395
396 }