1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Generator of database load balancing objects. |
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 Database |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
use MediaWiki\MediaWikiServices; |
25
|
|
|
use MediaWiki\Services\DestructibleService; |
26
|
|
|
use Psr\Log\LoggerInterface; |
27
|
|
|
use MediaWiki\Logger\LoggerFactory; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* An interface for generating database load balancers |
31
|
|
|
* @ingroup Database |
32
|
|
|
*/ |
33
|
|
|
abstract class LBFactory implements DestructibleService { |
34
|
|
|
/** @var ChronologyProtector */ |
35
|
|
|
protected $chronProt; |
36
|
|
|
/** @var TransactionProfiler */ |
37
|
|
|
protected $trxProfiler; |
38
|
|
|
/** @var LoggerInterface */ |
39
|
|
|
protected $trxLogger; |
40
|
|
|
/** @var BagOStuff */ |
41
|
|
|
protected $srvCache; |
42
|
|
|
/** @var WANObjectCache */ |
43
|
|
|
protected $wanCache; |
44
|
|
|
|
45
|
|
|
/** @var mixed */ |
46
|
|
|
protected $ticket; |
47
|
|
|
/** @var string|bool String if a requested DBO_TRX transaction round is active */ |
48
|
|
|
protected $trxRoundId = false; |
49
|
|
|
/** @var string|bool Reason all LBs are read-only or false if not */ |
50
|
|
|
protected $readOnlyReason = false; |
51
|
|
|
/** @var callable[] */ |
52
|
|
|
protected $replicationWaitCallbacks = []; |
53
|
|
|
|
54
|
|
|
const SHUTDOWN_NO_CHRONPROT = 0; // don't save DB positions at all |
55
|
|
|
const SHUTDOWN_CHRONPROT_ASYNC = 1; // save DB positions, but don't wait on remote DCs |
56
|
|
|
const SHUTDOWN_CHRONPROT_SYNC = 2; // save DB positions, waiting on all DCs |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Construct a factory based on a configuration array (typically from $wgLBFactoryConf) |
60
|
|
|
* @param array $conf |
61
|
|
|
* @TODO: inject objects via dependency framework |
62
|
|
|
*/ |
63
|
|
|
public function __construct( array $conf ) { |
64
|
|
View Code Duplication |
if ( isset( $conf['readOnlyReason'] ) && is_string( $conf['readOnlyReason'] ) ) { |
65
|
|
|
$this->readOnlyReason = $conf['readOnlyReason']; |
66
|
|
|
} |
67
|
|
|
$this->chronProt = $this->newChronologyProtector(); |
68
|
|
|
$this->trxProfiler = Profiler::instance()->getTransactionProfiler(); |
69
|
|
|
// Use APC/memcached style caching, but avoids loops with CACHE_DB (T141804) |
70
|
|
|
$cache = ObjectCache::getLocalServerInstance(); |
71
|
|
|
if ( $cache->getQoS( $cache::ATTR_EMULATION ) > $cache::QOS_EMULATION_SQL ) { |
72
|
|
|
$this->srvCache = $cache; |
73
|
|
|
} else { |
74
|
|
|
$this->srvCache = new EmptyBagOStuff(); |
75
|
|
|
} |
76
|
|
|
$wCache = ObjectCache::getMainWANInstance(); |
77
|
|
|
if ( $wCache->getQoS( $wCache::ATTR_EMULATION ) > $wCache::QOS_EMULATION_SQL ) { |
78
|
|
|
$this->wanCache = $wCache; |
79
|
|
|
} else { |
80
|
|
|
$this->wanCache = WANObjectCache::newEmpty(); |
81
|
|
|
} |
82
|
|
|
$this->trxLogger = LoggerFactory::getInstance( 'DBTransaction' ); |
83
|
|
|
$this->ticket = mt_rand(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Disables all load balancers. All connections are closed, and any attempt to |
88
|
|
|
* open a new connection will result in a DBAccessError. |
89
|
|
|
* @see LoadBalancer::disable() |
90
|
|
|
*/ |
91
|
|
|
public function destroy() { |
92
|
|
|
$this->shutdown( self::SHUTDOWN_NO_CHRONPROT ); |
93
|
|
|
$this->forEachLBCallMethod( 'disable' ); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Disables all access to the load balancer, will cause all database access |
98
|
|
|
* to throw a DBAccessError |
99
|
|
|
*/ |
100
|
|
|
public static function disableBackend() { |
101
|
|
|
MediaWikiServices::disableStorageBackend(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get an LBFactory instance |
106
|
|
|
* |
107
|
|
|
* @deprecated since 1.27, use MediaWikiServices::getDBLoadBalancerFactory() instead. |
108
|
|
|
* |
109
|
|
|
* @return LBFactory |
110
|
|
|
*/ |
111
|
|
|
public static function singleton() { |
112
|
|
|
return MediaWikiServices::getInstance()->getDBLoadBalancerFactory(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Returns the LBFactory class to use and the load balancer configuration. |
117
|
|
|
* |
118
|
|
|
* @todo instead of this, use a ServiceContainer for managing the different implementations. |
119
|
|
|
* |
120
|
|
|
* @param array $config (e.g. $wgLBFactoryConf) |
121
|
|
|
* @return string Class name |
122
|
|
|
*/ |
123
|
|
|
public static function getLBFactoryClass( array $config ) { |
124
|
|
|
// For configuration backward compatibility after removing |
125
|
|
|
// underscores from class names in MediaWiki 1.23. |
126
|
|
|
$bcClasses = [ |
127
|
|
|
'LBFactory_Simple' => 'LBFactorySimple', |
128
|
|
|
'LBFactory_Single' => 'LBFactorySingle', |
129
|
|
|
'LBFactory_Multi' => 'LBFactoryMulti', |
130
|
|
|
'LBFactory_Fake' => 'LBFactoryFake', |
131
|
|
|
]; |
132
|
|
|
|
133
|
|
|
$class = $config['class']; |
134
|
|
|
|
135
|
|
|
if ( isset( $bcClasses[$class] ) ) { |
136
|
|
|
$class = $bcClasses[$class]; |
137
|
|
|
wfDeprecated( |
138
|
|
|
'$wgLBFactoryConf must be updated. See RELEASE-NOTES for details', |
139
|
|
|
'1.23' |
140
|
|
|
); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return $class; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Shut down, close connections and destroy the cached instance. |
148
|
|
|
* |
149
|
|
|
* @deprecated since 1.27, use LBFactory::destroy() |
150
|
|
|
*/ |
151
|
|
|
public static function destroyInstance() { |
152
|
|
|
self::singleton()->destroy(); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Create a new load balancer object. The resulting object will be untracked, |
157
|
|
|
* not chronology-protected, and the caller is responsible for cleaning it up. |
158
|
|
|
* |
159
|
|
|
* @param bool|string $wiki Wiki ID, or false for the current wiki |
160
|
|
|
* @return LoadBalancer |
161
|
|
|
*/ |
162
|
|
|
abstract public function newMainLB( $wiki = false ); |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Get a cached (tracked) load balancer object. |
166
|
|
|
* |
167
|
|
|
* @param bool|string $wiki Wiki ID, or false for the current wiki |
168
|
|
|
* @return LoadBalancer |
169
|
|
|
*/ |
170
|
|
|
abstract public function getMainLB( $wiki = false ); |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Create a new load balancer for external storage. The resulting object will be |
174
|
|
|
* untracked, not chronology-protected, and the caller is responsible for |
175
|
|
|
* cleaning it up. |
176
|
|
|
* |
177
|
|
|
* @param string $cluster External storage cluster, or false for core |
178
|
|
|
* @param bool|string $wiki Wiki ID, or false for the current wiki |
179
|
|
|
* @return LoadBalancer |
180
|
|
|
*/ |
181
|
|
|
abstract protected function newExternalLB( $cluster, $wiki = false ); |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Get a cached (tracked) load balancer for external storage |
185
|
|
|
* |
186
|
|
|
* @param string $cluster External storage cluster, or false for core |
187
|
|
|
* @param bool|string $wiki Wiki ID, or false for the current wiki |
188
|
|
|
* @return LoadBalancer |
189
|
|
|
*/ |
190
|
|
|
abstract public function getExternalLB( $cluster, $wiki = false ); |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Execute a function for each tracked load balancer |
194
|
|
|
* The callback is called with the load balancer as the first parameter, |
195
|
|
|
* and $params passed as the subsequent parameters. |
196
|
|
|
* |
197
|
|
|
* @param callable $callback |
198
|
|
|
* @param array $params |
199
|
|
|
*/ |
200
|
|
|
abstract public function forEachLB( $callback, array $params = [] ); |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Prepare all tracked load balancers for shutdown |
204
|
|
|
* @param integer $mode One of the class SHUTDOWN_* constants |
205
|
|
|
* @param callable|null $workCallback Work to mask ChronologyProtector writes |
206
|
|
|
*/ |
207
|
|
|
public function shutdown( |
208
|
|
|
$mode = self::SHUTDOWN_CHRONPROT_SYNC, callable $workCallback = null |
209
|
|
|
) { |
210
|
|
|
if ( $mode === self::SHUTDOWN_CHRONPROT_SYNC ) { |
211
|
|
|
$this->shutdownChronologyProtector( $this->chronProt, $workCallback, 'sync' ); |
212
|
|
|
} elseif ( $mode === self::SHUTDOWN_CHRONPROT_ASYNC ) { |
213
|
|
|
$this->shutdownChronologyProtector( $this->chronProt, null, 'async' ); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
$this->commitMasterChanges( __METHOD__ ); // sanity |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Call a method of each tracked load balancer |
221
|
|
|
* |
222
|
|
|
* @param string $methodName |
223
|
|
|
* @param array $args |
224
|
|
|
*/ |
225
|
|
|
private function forEachLBCallMethod( $methodName, array $args = [] ) { |
226
|
|
|
$this->forEachLB( |
227
|
|
|
function ( LoadBalancer $loadBalancer, $methodName, array $args ) { |
228
|
|
|
call_user_func_array( [ $loadBalancer, $methodName ], $args ); |
229
|
|
|
}, |
230
|
|
|
[ $methodName, $args ] |
231
|
|
|
); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Commit all replica DB transactions so as to flush any REPEATABLE-READ or SSI snapshot |
236
|
|
|
* |
237
|
|
|
* @param string $fname Caller name |
238
|
|
|
* @since 1.28 |
239
|
|
|
*/ |
240
|
|
|
public function flushReplicaSnapshots( $fname = __METHOD__ ) { |
241
|
|
|
$this->forEachLBCallMethod( 'flushReplicaSnapshots', [ $fname ] ); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Commit on all connections. Done for two reasons: |
246
|
|
|
* 1. To commit changes to the masters. |
247
|
|
|
* 2. To release the snapshot on all connections, master and replica DB. |
248
|
|
|
* @param string $fname Caller name |
249
|
|
|
* @param array $options Options map: |
250
|
|
|
* - maxWriteDuration: abort if more than this much time was spent in write queries |
251
|
|
|
*/ |
252
|
|
|
public function commitAll( $fname = __METHOD__, array $options = [] ) { |
253
|
|
|
$this->commitMasterChanges( $fname, $options ); |
254
|
|
|
$this->forEachLBCallMethod( 'commitAll', [ $fname ] ); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Flush any master transaction snapshots and set DBO_TRX (if DBO_DEFAULT is set) |
259
|
|
|
* |
260
|
|
|
* The DBO_TRX setting will be reverted to the default in each of these methods: |
261
|
|
|
* - commitMasterChanges() |
262
|
|
|
* - rollbackMasterChanges() |
263
|
|
|
* - commitAll() |
264
|
|
|
* |
265
|
|
|
* This allows for custom transaction rounds from any outer transaction scope. |
266
|
|
|
* |
267
|
|
|
* @param string $fname |
268
|
|
|
* @throws DBTransactionError |
269
|
|
|
* @since 1.28 |
270
|
|
|
*/ |
271
|
|
|
public function beginMasterChanges( $fname = __METHOD__ ) { |
272
|
|
|
if ( $this->trxRoundId !== false ) { |
273
|
|
|
throw new DBTransactionError( |
274
|
|
|
null, |
275
|
|
|
"$fname: transaction round '{$this->trxRoundId}' already started." |
276
|
|
|
); |
277
|
|
|
} |
278
|
|
|
$this->trxRoundId = $fname; |
279
|
|
|
// Set DBO_TRX flags on all appropriate DBs |
280
|
|
|
$this->forEachLBCallMethod( 'beginMasterChanges', [ $fname ] ); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Commit changes on all master connections |
285
|
|
|
* @param string $fname Caller name |
286
|
|
|
* @param array $options Options map: |
287
|
|
|
* - maxWriteDuration: abort if more than this much time was spent in write queries |
288
|
|
|
* @throws Exception |
289
|
|
|
*/ |
290
|
|
|
public function commitMasterChanges( $fname = __METHOD__, array $options = [] ) { |
291
|
|
|
if ( $this->trxRoundId !== false && $this->trxRoundId !== $fname ) { |
292
|
|
|
throw new DBTransactionError( |
293
|
|
|
null, |
294
|
|
|
"$fname: transaction round '{$this->trxRoundId}' still running." |
295
|
|
|
); |
296
|
|
|
} |
297
|
|
|
// Run pre-commit callbacks and suppress post-commit callbacks, aborting on failure |
298
|
|
|
$this->forEachLBCallMethod( 'finalizeMasterChanges' ); |
299
|
|
|
$this->trxRoundId = false; |
300
|
|
|
// Perform pre-commit checks, aborting on failure |
301
|
|
|
$this->forEachLBCallMethod( 'approveMasterChanges', [ $options ] ); |
302
|
|
|
// Log the DBs and methods involved in multi-DB transactions |
303
|
|
|
$this->logIfMultiDbTransaction(); |
304
|
|
|
// Actually perform the commit on all master DB connections and revert DBO_TRX |
305
|
|
|
$this->forEachLBCallMethod( 'commitMasterChanges', [ $fname ] ); |
306
|
|
|
// Run all post-commit callbacks |
307
|
|
|
/** @var Exception $e */ |
308
|
|
|
$e = null; // first callback exception |
309
|
|
|
$this->forEachLB( function ( LoadBalancer $lb ) use ( &$e ) { |
310
|
|
|
$ex = $lb->runMasterPostTrxCallbacks( IDatabase::TRIGGER_COMMIT ); |
311
|
|
|
$e = $e ?: $ex; |
312
|
|
|
} ); |
313
|
|
|
// Commit any dangling DBO_TRX transactions from callbacks on one DB to another DB |
314
|
|
|
$this->forEachLBCallMethod( 'commitMasterChanges', [ $fname ] ); |
315
|
|
|
// Throw any last post-commit callback error |
316
|
|
|
if ( $e instanceof Exception ) { |
317
|
|
|
throw $e; |
318
|
|
|
} |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* Rollback changes on all master connections |
323
|
|
|
* @param string $fname Caller name |
324
|
|
|
* @since 1.23 |
325
|
|
|
*/ |
326
|
|
|
public function rollbackMasterChanges( $fname = __METHOD__ ) { |
327
|
|
|
$this->trxRoundId = false; |
328
|
|
|
$this->forEachLBCallMethod( 'suppressTransactionEndCallbacks' ); |
329
|
|
|
$this->forEachLBCallMethod( 'rollbackMasterChanges', [ $fname ] ); |
330
|
|
|
// Run all post-rollback callbacks |
331
|
|
|
$this->forEachLB( function ( LoadBalancer $lb ) { |
332
|
|
|
$lb->runMasterPostTrxCallbacks( IDatabase::TRIGGER_ROLLBACK ); |
333
|
|
|
} ); |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* Log query info if multi DB transactions are going to be committed now |
338
|
|
|
*/ |
339
|
|
|
private function logIfMultiDbTransaction() { |
340
|
|
|
$callersByDB = []; |
341
|
|
|
$this->forEachLB( function ( LoadBalancer $lb ) use ( &$callersByDB ) { |
342
|
|
|
$masterName = $lb->getServerName( $lb->getWriterIndex() ); |
343
|
|
|
$callers = $lb->pendingMasterChangeCallers(); |
344
|
|
|
if ( $callers ) { |
345
|
|
|
$callersByDB[$masterName] = $callers; |
346
|
|
|
} |
347
|
|
|
} ); |
348
|
|
|
|
349
|
|
|
if ( count( $callersByDB ) >= 2 ) { |
350
|
|
|
$dbs = implode( ', ', array_keys( $callersByDB ) ); |
351
|
|
|
$msg = "Multi-DB transaction [{$dbs}]:\n"; |
352
|
|
|
foreach ( $callersByDB as $db => $callers ) { |
353
|
|
|
$msg .= "$db: " . implode( '; ', $callers ) . "\n"; |
354
|
|
|
} |
355
|
|
|
$this->trxLogger->info( $msg ); |
356
|
|
|
} |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
/** |
360
|
|
|
* Determine if any master connection has pending changes |
361
|
|
|
* @return bool |
362
|
|
|
* @since 1.23 |
363
|
|
|
*/ |
364
|
|
|
public function hasMasterChanges() { |
365
|
|
|
$ret = false; |
366
|
|
|
$this->forEachLB( function ( LoadBalancer $lb ) use ( &$ret ) { |
367
|
|
|
$ret = $ret || $lb->hasMasterChanges(); |
368
|
|
|
} ); |
369
|
|
|
|
370
|
|
|
return $ret; |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
/** |
374
|
|
|
* Detemine if any lagged replica DB connection was used |
375
|
|
|
* @return bool |
376
|
|
|
* @since 1.28 |
377
|
|
|
*/ |
378
|
|
|
public function laggedReplicaUsed() { |
379
|
|
|
$ret = false; |
380
|
|
|
$this->forEachLB( function ( LoadBalancer $lb ) use ( &$ret ) { |
381
|
|
|
$ret = $ret || $lb->laggedReplicaUsed(); |
382
|
|
|
} ); |
383
|
|
|
|
384
|
|
|
return $ret; |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
/** |
388
|
|
|
* @return bool |
389
|
|
|
* @since 1.27 |
390
|
|
|
* @deprecated Since 1.28; use laggedReplicaUsed() |
391
|
|
|
*/ |
392
|
|
|
public function laggedSlaveUsed() { |
393
|
|
|
return $this->laggedReplicaUsed(); |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
/** |
397
|
|
|
* Determine if any master connection has pending/written changes from this request |
398
|
|
|
* @param float $age How many seconds ago is "recent" [defaults to LB lag wait timeout] |
399
|
|
|
* @return bool |
400
|
|
|
* @since 1.27 |
401
|
|
|
*/ |
402
|
|
|
public function hasOrMadeRecentMasterChanges( $age = null ) { |
403
|
|
|
$ret = false; |
404
|
|
|
$this->forEachLB( function ( LoadBalancer $lb ) use ( $age, &$ret ) { |
405
|
|
|
$ret = $ret || $lb->hasOrMadeRecentMasterChanges( $age ); |
406
|
|
|
} ); |
407
|
|
|
return $ret; |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
/** |
411
|
|
|
* Waits for the replica DBs to catch up to the current master position |
412
|
|
|
* |
413
|
|
|
* Use this when updating very large numbers of rows, as in maintenance scripts, |
414
|
|
|
* to avoid causing too much lag. Of course, this is a no-op if there are no replica DBs. |
415
|
|
|
* |
416
|
|
|
* By default this waits on all DB clusters actually used in this request. |
417
|
|
|
* This makes sense when lag being waiting on is caused by the code that does this check. |
418
|
|
|
* In that case, setting "ifWritesSince" can avoid the overhead of waiting for clusters |
419
|
|
|
* that were not changed since the last wait check. To forcefully wait on a specific cluster |
420
|
|
|
* for a given wiki, use the 'wiki' parameter. To forcefully wait on an "external" cluster, |
421
|
|
|
* use the "cluster" parameter. |
422
|
|
|
* |
423
|
|
|
* Never call this function after a large DB write that is *still* in a transaction. |
424
|
|
|
* It only makes sense to call this after the possible lag inducing changes were committed. |
425
|
|
|
* |
426
|
|
|
* @param array $opts Optional fields that include: |
427
|
|
|
* - wiki : wait on the load balancer DBs that handles the given wiki |
428
|
|
|
* - cluster : wait on the given external load balancer DBs |
429
|
|
|
* - timeout : Max wait time. Default: ~60 seconds |
430
|
|
|
* - ifWritesSince: Only wait if writes were done since this UNIX timestamp |
431
|
|
|
* @throws DBReplicationWaitError If a timeout or error occured waiting on a DB cluster |
432
|
|
|
* @since 1.27 |
433
|
|
|
*/ |
434
|
|
|
public function waitForReplication( array $opts = [] ) { |
435
|
|
|
$opts += [ |
436
|
|
|
'wiki' => false, |
437
|
|
|
'cluster' => false, |
438
|
|
|
'timeout' => 60, |
439
|
|
|
'ifWritesSince' => null |
440
|
|
|
]; |
441
|
|
|
|
442
|
|
|
// Figure out which clusters need to be checked |
443
|
|
|
/** @var LoadBalancer[] $lbs */ |
444
|
|
|
$lbs = []; |
445
|
|
|
if ( $opts['cluster'] !== false ) { |
446
|
|
|
$lbs[] = $this->getExternalLB( $opts['cluster'] ); |
447
|
|
|
} elseif ( $opts['wiki'] !== false ) { |
448
|
|
|
$lbs[] = $this->getMainLB( $opts['wiki'] ); |
449
|
|
|
} else { |
450
|
|
|
$this->forEachLB( function ( LoadBalancer $lb ) use ( &$lbs ) { |
451
|
|
|
$lbs[] = $lb; |
452
|
|
|
} ); |
453
|
|
|
if ( !$lbs ) { |
454
|
|
|
return; // nothing actually used |
455
|
|
|
} |
456
|
|
|
} |
457
|
|
|
|
458
|
|
|
// Get all the master positions of applicable DBs right now. |
459
|
|
|
// This can be faster since waiting on one cluster reduces the |
460
|
|
|
// time needed to wait on the next clusters. |
461
|
|
|
$masterPositions = array_fill( 0, count( $lbs ), false ); |
462
|
|
|
foreach ( $lbs as $i => $lb ) { |
463
|
|
|
if ( $lb->getServerCount() <= 1 ) { |
464
|
|
|
// Bug 27975 - Don't try to wait for replica DBs if there are none |
465
|
|
|
// Prevents permission error when getting master position |
466
|
|
|
continue; |
467
|
|
|
} elseif ( $opts['ifWritesSince'] |
468
|
|
|
&& $lb->lastMasterChangeTimestamp() < $opts['ifWritesSince'] |
469
|
|
|
) { |
470
|
|
|
continue; // no writes since the last wait |
471
|
|
|
} |
472
|
|
|
$masterPositions[$i] = $lb->getMasterPos(); |
473
|
|
|
} |
474
|
|
|
|
475
|
|
|
// Run any listener callbacks *after* getting the DB positions. The more |
476
|
|
|
// time spent in the callbacks, the less time is spent in waitForAll(). |
477
|
|
|
foreach ( $this->replicationWaitCallbacks as $callback ) { |
478
|
|
|
$callback(); |
479
|
|
|
} |
480
|
|
|
|
481
|
|
|
$failed = []; |
482
|
|
|
foreach ( $lbs as $i => $lb ) { |
483
|
|
|
if ( $masterPositions[$i] ) { |
484
|
|
|
// The DBMS may not support getMasterPos() or the whole |
485
|
|
|
// load balancer might be fake (e.g. $wgAllDBsAreLocalhost). |
486
|
|
|
if ( !$lb->waitForAll( $masterPositions[$i], $opts['timeout'] ) ) { |
487
|
|
|
$failed[] = $lb->getServerName( $lb->getWriterIndex() ); |
488
|
|
|
} |
489
|
|
|
} |
490
|
|
|
} |
491
|
|
|
|
492
|
|
|
if ( $failed ) { |
493
|
|
|
throw new DBReplicationWaitError( |
|
|
|
|
494
|
|
|
"Could not wait for replica DBs to catch up to " . |
495
|
|
|
implode( ', ', $failed ) |
496
|
|
|
); |
497
|
|
|
} |
498
|
|
|
} |
499
|
|
|
|
500
|
|
|
/** |
501
|
|
|
* Add a callback to be run in every call to waitForReplication() before waiting |
502
|
|
|
* |
503
|
|
|
* Callbacks must clear any transactions that they start |
504
|
|
|
* |
505
|
|
|
* @param string $name Callback name |
506
|
|
|
* @param callable|null $callback Use null to unset a callback |
507
|
|
|
* @since 1.28 |
508
|
|
|
*/ |
509
|
|
|
public function setWaitForReplicationListener( $name, callable $callback = null ) { |
510
|
|
|
if ( $callback ) { |
511
|
|
|
$this->replicationWaitCallbacks[$name] = $callback; |
512
|
|
|
} else { |
513
|
|
|
unset( $this->replicationWaitCallbacks[$name] ); |
514
|
|
|
} |
515
|
|
|
} |
516
|
|
|
|
517
|
|
|
/** |
518
|
|
|
* Get a token asserting that no transaction writes are active |
519
|
|
|
* |
520
|
|
|
* @param string $fname Caller name (e.g. __METHOD__) |
521
|
|
|
* @return mixed A value to pass to commitAndWaitForReplication() |
522
|
|
|
* @since 1.28 |
523
|
|
|
*/ |
524
|
|
|
public function getEmptyTransactionTicket( $fname ) { |
525
|
|
|
if ( $this->hasMasterChanges() ) { |
526
|
|
|
$this->trxLogger->error( __METHOD__ . ": $fname does not have outer scope." ); |
527
|
|
|
return null; |
528
|
|
|
} |
529
|
|
|
|
530
|
|
|
return $this->ticket; |
531
|
|
|
} |
532
|
|
|
|
533
|
|
|
/** |
534
|
|
|
* Convenience method for safely running commitMasterChanges()/waitForReplication() |
535
|
|
|
* |
536
|
|
|
* This will commit and wait unless $ticket indicates it is unsafe to do so |
537
|
|
|
* |
538
|
|
|
* @param string $fname Caller name (e.g. __METHOD__) |
539
|
|
|
* @param mixed $ticket Result of getEmptyTransactionTicket() |
540
|
|
|
* @param array $opts Options to waitForReplication() |
541
|
|
|
* @throws DBReplicationWaitError |
542
|
|
|
* @since 1.28 |
543
|
|
|
*/ |
544
|
|
|
public function commitAndWaitForReplication( $fname, $ticket, array $opts = [] ) { |
545
|
|
|
if ( $ticket !== $this->ticket ) { |
546
|
|
|
$logger = LoggerFactory::getInstance( 'DBPerformance' ); |
547
|
|
|
$logger->error( __METHOD__ . ": cannot commit; $fname does not have outer scope." ); |
548
|
|
|
return; |
549
|
|
|
} |
550
|
|
|
|
551
|
|
|
// The transaction owner and any caller with the empty transaction ticket can commit |
552
|
|
|
// so that getEmptyTransactionTicket() callers don't risk seeing DBTransactionError. |
553
|
|
|
if ( $this->trxRoundId !== false && $fname !== $this->trxRoundId ) { |
554
|
|
|
$this->trxLogger->info( "$fname: committing on behalf of {$this->trxRoundId}." ); |
555
|
|
|
$fnameEffective = $this->trxRoundId; |
556
|
|
|
} else { |
557
|
|
|
$fnameEffective = $fname; |
558
|
|
|
} |
559
|
|
|
|
560
|
|
|
$this->commitMasterChanges( $fnameEffective ); |
|
|
|
|
561
|
|
|
$this->waitForReplication( $opts ); |
562
|
|
|
// If a nested caller committed on behalf of $fname, start another empty $fname |
563
|
|
|
// transaction, leaving the caller with the same empty transaction state as before. |
564
|
|
|
if ( $fnameEffective !== $fname ) { |
565
|
|
|
$this->beginMasterChanges( $fnameEffective ); |
|
|
|
|
566
|
|
|
} |
567
|
|
|
} |
568
|
|
|
|
569
|
|
|
/** |
570
|
|
|
* @param string $dbName DB master name (e.g. "db1052") |
571
|
|
|
* @return float|bool UNIX timestamp when client last touched the DB or false if not recent |
572
|
|
|
* @since 1.28 |
573
|
|
|
*/ |
574
|
|
|
public function getChronologyProtectorTouched( $dbName ) { |
575
|
|
|
return $this->chronProt->getTouched( $dbName ); |
576
|
|
|
} |
577
|
|
|
|
578
|
|
|
/** |
579
|
|
|
* Disable the ChronologyProtector for all load balancers |
580
|
|
|
* |
581
|
|
|
* This can be called at the start of special API entry points |
582
|
|
|
* |
583
|
|
|
* @since 1.27 |
584
|
|
|
*/ |
585
|
|
|
public function disableChronologyProtection() { |
586
|
|
|
$this->chronProt->setEnabled( false ); |
587
|
|
|
} |
588
|
|
|
|
589
|
|
|
/** |
590
|
|
|
* @return ChronologyProtector |
591
|
|
|
*/ |
592
|
|
|
protected function newChronologyProtector() { |
593
|
|
|
$request = RequestContext::getMain()->getRequest(); |
594
|
|
|
$chronProt = new ChronologyProtector( |
595
|
|
|
ObjectCache::getMainStashInstance(), |
596
|
|
|
[ |
597
|
|
|
'ip' => $request->getIP(), |
598
|
|
|
'agent' => $request->getHeader( 'User-Agent' ), |
599
|
|
|
], |
600
|
|
|
$request->getFloat( 'cpPosTime', null ) |
601
|
|
|
); |
602
|
|
|
if ( PHP_SAPI === 'cli' ) { |
603
|
|
|
$chronProt->setEnabled( false ); |
604
|
|
|
} elseif ( $request->getHeader( 'ChronologyProtection' ) === 'false' ) { |
605
|
|
|
// Request opted out of using position wait logic. This is useful for requests |
606
|
|
|
// done by the job queue or background ETL that do not have a meaningful session. |
607
|
|
|
$chronProt->setWaitEnabled( false ); |
608
|
|
|
} |
609
|
|
|
|
610
|
|
|
return $chronProt; |
611
|
|
|
} |
612
|
|
|
|
613
|
|
|
/** |
614
|
|
|
* Get and record all of the staged DB positions into persistent memory storage |
615
|
|
|
* |
616
|
|
|
* @param ChronologyProtector $cp |
617
|
|
|
* @param callable|null $workCallback Work to do instead of waiting on syncing positions |
618
|
|
|
* @param string $mode One of (sync, async); whether to wait on remote datacenters |
619
|
|
|
*/ |
620
|
|
|
protected function shutdownChronologyProtector( |
621
|
|
|
ChronologyProtector $cp, $workCallback, $mode |
622
|
|
|
) { |
623
|
|
|
// Record all the master positions needed |
624
|
|
|
$this->forEachLB( function ( LoadBalancer $lb ) use ( $cp ) { |
625
|
|
|
$cp->shutdownLB( $lb ); |
626
|
|
|
} ); |
627
|
|
|
// Write them to the persistent stash. Try to do something useful by running $work |
628
|
|
|
// while ChronologyProtector waits for the stash write to replicate to all DCs. |
629
|
|
|
$unsavedPositions = $cp->shutdown( $workCallback, $mode ); |
630
|
|
|
if ( $unsavedPositions && $workCallback ) { |
631
|
|
|
// Invoke callback in case it did not cache the result yet |
632
|
|
|
$workCallback(); // work now to block for less time in waitForAll() |
633
|
|
|
} |
634
|
|
|
// If the positions failed to write to the stash, at least wait on local datacenter |
635
|
|
|
// replica DBs to catch up before responding. Even if there are several DCs, this increases |
636
|
|
|
// the chance that the user will see their own changes immediately afterwards. As long |
637
|
|
|
// as the sticky DC cookie applies (same domain), this is not even an issue. |
638
|
|
|
$this->forEachLB( function ( LoadBalancer $lb ) use ( $unsavedPositions ) { |
639
|
|
|
$masterName = $lb->getServerName( $lb->getWriterIndex() ); |
640
|
|
|
if ( isset( $unsavedPositions[$masterName] ) ) { |
641
|
|
|
$lb->waitForAll( $unsavedPositions[$masterName] ); |
642
|
|
|
} |
643
|
|
|
} ); |
644
|
|
|
} |
645
|
|
|
|
646
|
|
|
/** |
647
|
|
|
* @param LoadBalancer $lb |
648
|
|
|
*/ |
649
|
|
|
protected function initLoadBalancer( LoadBalancer $lb ) { |
650
|
|
|
if ( $this->trxRoundId !== false ) { |
651
|
|
|
$lb->beginMasterChanges( $this->trxRoundId ); // set DBO_TRX |
|
|
|
|
652
|
|
|
} |
653
|
|
|
} |
654
|
|
|
|
655
|
|
|
/** |
656
|
|
|
* Append ?cpPosTime parameter to a URL for ChronologyProtector purposes if needed |
657
|
|
|
* |
658
|
|
|
* Note that unlike cookies, this works accross domains |
659
|
|
|
* |
660
|
|
|
* @param string $url |
661
|
|
|
* @param float $time UNIX timestamp just before shutdown() was called |
662
|
|
|
* @return string |
663
|
|
|
* @since 1.28 |
664
|
|
|
*/ |
665
|
|
|
public function appendPreShutdownTimeAsQuery( $url, $time ) { |
666
|
|
|
$usedCluster = 0; |
667
|
|
|
$this->forEachLB( function ( LoadBalancer $lb ) use ( &$usedCluster ) { |
668
|
|
|
$usedCluster |= ( $lb->getServerCount() > 1 ); |
669
|
|
|
} ); |
670
|
|
|
|
671
|
|
|
if ( !$usedCluster ) { |
672
|
|
|
return $url; // no master/replica clusters touched |
673
|
|
|
} |
674
|
|
|
|
675
|
|
|
return wfAppendQuery( $url, [ 'cpPosTime' => $time ] ); |
676
|
|
|
} |
677
|
|
|
|
678
|
|
|
/** |
679
|
|
|
* Close all open database connections on all open load balancers. |
680
|
|
|
* @since 1.28 |
681
|
|
|
*/ |
682
|
|
|
public function closeAll() { |
683
|
|
|
$this->forEachLBCallMethod( 'closeAll', [] ); |
684
|
|
|
} |
685
|
|
|
} |
686
|
|
|
|
This check looks for function calls that miss required arguments.