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