Fix use of GenderCache in ApiPageSet::processTitlesArray
[lhc/web/wiklou.git] / includes / libs / rdbms / lbfactory / LBFactory.php
1 <?php
2 /**
3 * Generator and manager 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 namespace Wikimedia\Rdbms;
25
26 use Psr\Log\LoggerInterface;
27 use Psr\Log\NullLogger;
28 use Wikimedia\ScopedCallback;
29 use BagOStuff;
30 use EmptyBagOStuff;
31 use WANObjectCache;
32 use Exception;
33 use RuntimeException;
34 use LogicException;
35
36 /**
37 * An interface for generating database load balancers
38 * @ingroup Database
39 */
40 abstract class LBFactory implements ILBFactory {
41 /** @var ChronologyProtector */
42 private $chronProt;
43 /** @var object|string Class name or object With profileIn/profileOut methods */
44 private $profiler;
45 /** @var TransactionProfiler */
46 private $trxProfiler;
47 /** @var LoggerInterface */
48 private $replLogger;
49 /** @var LoggerInterface */
50 private $connLogger;
51 /** @var LoggerInterface */
52 private $queryLogger;
53 /** @var LoggerInterface */
54 private $perfLogger;
55 /** @var callable Error logger */
56 private $errorLogger;
57 /** @var callable Deprecation logger */
58 private $deprecationLogger;
59
60 /** @var BagOStuff */
61 protected $srvCache;
62 /** @var BagOStuff */
63 protected $memStash;
64 /** @var WANObjectCache */
65 protected $wanCache;
66
67 /** @var DatabaseDomain Local domain */
68 protected $localDomain;
69
70 /** @var string Local hostname of the app server */
71 private $hostname;
72 /** @var array Web request information about the client */
73 private $requestInfo;
74 /** @var bool Whether this PHP instance is for a CLI script */
75 private $cliMode;
76 /** @var string Agent name for query profiling */
77 private $agent;
78 /** @var string Secret string for HMAC hashing */
79 private $secret;
80
81 /** @var array[] $aliases Map of (table => (dbname, schema, prefix) map) */
82 private $tableAliases = [];
83 /** @var string[] Map of (index alias => index) */
84 private $indexAliases = [];
85 /** @var callable[] */
86 private $replicationWaitCallbacks = [];
87
88 /** var int An identifier for this class instance */
89 private $id;
90 /** @var int|null Ticket used to delegate transaction ownership */
91 private $ticket;
92 /** @var string|bool String if a requested DBO_TRX transaction round is active */
93 private $trxRoundId = false;
94 /** @var string One of the ROUND_* class constants */
95 private $trxRoundStage = self::ROUND_CURSORY;
96
97 /** @var string|bool Reason all LBs are read-only or false if not */
98 protected $readOnlyReason = false;
99
100 /** @var string|null */
101 private $defaultGroup = null;
102
103 /** @var int|null */
104 protected $maxLag;
105
106 const ROUND_CURSORY = 'cursory';
107 const ROUND_BEGINNING = 'within-begin';
108 const ROUND_COMMITTING = 'within-commit';
109 const ROUND_ROLLING_BACK = 'within-rollback';
110 const ROUND_COMMIT_CALLBACKS = 'within-commit-callbacks';
111 const ROUND_ROLLBACK_CALLBACKS = 'within-rollback-callbacks';
112
113 private static $loggerFields =
114 [ 'replLogger', 'connLogger', 'queryLogger', 'perfLogger' ];
115
116 public function __construct( array $conf ) {
117 $this->localDomain = isset( $conf['localDomain'] )
118 ? DatabaseDomain::newFromId( $conf['localDomain'] )
119 : DatabaseDomain::newUnspecified();
120
121 $this->maxLag = $conf['maxLag'] ?? null;
122 if ( isset( $conf['readOnlyReason'] ) && is_string( $conf['readOnlyReason'] ) ) {
123 $this->readOnlyReason = $conf['readOnlyReason'];
124 }
125
126 $this->srvCache = $conf['srvCache'] ?? new EmptyBagOStuff();
127 $this->memStash = $conf['memStash'] ?? new EmptyBagOStuff();
128 $this->wanCache = $conf['wanCache'] ?? WANObjectCache::newEmpty();
129
130 foreach ( self::$loggerFields as $key ) {
131 $this->$key = $conf[$key] ?? new NullLogger();
132 }
133 $this->errorLogger = $conf['errorLogger'] ?? function ( Exception $e ) {
134 trigger_error( get_class( $e ) . ': ' . $e->getMessage(), E_USER_WARNING );
135 };
136 $this->deprecationLogger = $conf['deprecationLogger'] ?? function ( $msg ) {
137 trigger_error( $msg, E_USER_DEPRECATED );
138 };
139
140 $this->profiler = $conf['profiler'] ?? null;
141 $this->trxProfiler = $conf['trxProfiler'] ?? new TransactionProfiler();
142
143 $this->requestInfo = [
144 'IPAddress' => $_SERVER[ 'REMOTE_ADDR' ] ?? '',
145 'UserAgent' => $_SERVER['HTTP_USER_AGENT'] ?? '',
146 // Headers application can inject via LBFactory::setRequestInfo()
147 'ChronologyProtection' => null,
148 'ChronologyClientId' => null, // prior $cpClientId value from LBFactory::shutdown()
149 'ChronologyPositionIndex' => null // prior $cpIndex value from LBFactory::shutdown()
150 ];
151
152 $this->cliMode = $conf['cliMode'] ?? ( PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg' );
153 $this->hostname = $conf['hostname'] ?? gethostname();
154 $this->agent = $conf['agent'] ?? '';
155 $this->defaultGroup = $conf['defaultGroup'] ?? null;
156 $this->secret = $conf['secret'] ?? '';
157
158 $this->id = mt_rand();
159 $this->ticket = mt_rand();
160 }
161
162 public function destroy() {
163 /** @noinspection PhpUnusedLocalVariableInspection */
164 $scope = ScopedCallback::newScopedIgnoreUserAbort();
165
166 $this->forEachLBCallMethod( 'disable' );
167 }
168
169 public function getLocalDomainID() {
170 return $this->localDomain->getId();
171 }
172
173 public function resolveDomainID( $domain ) {
174 return ( $domain !== false ) ? (string)$domain : $this->getLocalDomainID();
175 }
176
177 public function shutdown(
178 $mode = self::SHUTDOWN_CHRONPROT_SYNC,
179 callable $workCallback = null,
180 &$cpIndex = null,
181 &$cpClientId = null
182 ) {
183 /** @noinspection PhpUnusedLocalVariableInspection */
184 $scope = ScopedCallback::newScopedIgnoreUserAbort();
185
186 $chronProt = $this->getChronologyProtector();
187 if ( $mode === self::SHUTDOWN_CHRONPROT_SYNC ) {
188 $this->shutdownChronologyProtector( $chronProt, $workCallback, 'sync', $cpIndex );
189 } elseif ( $mode === self::SHUTDOWN_CHRONPROT_ASYNC ) {
190 $this->shutdownChronologyProtector( $chronProt, null, 'async', $cpIndex );
191 }
192
193 $cpClientId = $chronProt->getClientId();
194
195 $this->commitMasterChanges( __METHOD__ ); // sanity
196 }
197
198 /**
199 * @see ILBFactory::newMainLB()
200 * @param bool $domain
201 * @return ILoadBalancer
202 */
203 abstract public function newMainLB( $domain = false );
204
205 /**
206 * @see ILBFactory::getMainLB()
207 * @param bool $domain
208 * @return ILoadBalancer
209 */
210 abstract public function getMainLB( $domain = false );
211
212 /**
213 * @see ILBFactory::newExternalLB()
214 * @param string $cluster
215 * @return ILoadBalancer
216 */
217 abstract public function newExternalLB( $cluster );
218
219 /**
220 * @see ILBFactory::getExternalLB()
221 * @param string $cluster
222 * @return ILoadBalancer
223 */
224 abstract public function getExternalLB( $cluster );
225
226 /**
227 * Call a method of each tracked load balancer
228 *
229 * @param string $methodName
230 * @param array $args
231 */
232 protected function forEachLBCallMethod( $methodName, array $args = [] ) {
233 $this->forEachLB(
234 function ( ILoadBalancer $loadBalancer, $methodName, array $args ) {
235 $loadBalancer->$methodName( ...$args );
236 },
237 [ $methodName, $args ]
238 );
239 }
240
241 public function flushReplicaSnapshots( $fname = __METHOD__ ) {
242 if ( $this->trxRoundId !== false && $this->trxRoundId !== $fname ) {
243 $this->queryLogger->warning(
244 "$fname: transaction round '{$this->trxRoundId}' still running",
245 [ 'trace' => ( new RuntimeException() )->getTraceAsString() ]
246 );
247 }
248 $this->forEachLBCallMethod( 'flushReplicaSnapshots', [ $fname ] );
249 }
250
251 final public function commitAll( $fname = __METHOD__, array $options = [] ) {
252 $this->commitMasterChanges( $fname, $options );
253 $this->forEachLBCallMethod( 'flushMasterSnapshots', [ $fname ] );
254 $this->forEachLBCallMethod( 'flushReplicaSnapshots', [ $fname ] );
255 }
256
257 final public function beginMasterChanges( $fname = __METHOD__ ) {
258 $this->assertTransactionRoundStage( self::ROUND_CURSORY );
259 /** @noinspection PhpUnusedLocalVariableInspection */
260 $scope = ScopedCallback::newScopedIgnoreUserAbort();
261
262 $this->trxRoundStage = self::ROUND_BEGINNING;
263 if ( $this->trxRoundId !== false ) {
264 throw new DBTransactionError(
265 null,
266 "$fname: transaction round '{$this->trxRoundId}' already started"
267 );
268 }
269 $this->trxRoundId = $fname;
270 // Set DBO_TRX flags on all appropriate DBs
271 $this->forEachLBCallMethod( 'beginMasterChanges', [ $fname, $this->id ] );
272 $this->trxRoundStage = self::ROUND_CURSORY;
273 }
274
275 final public function commitMasterChanges( $fname = __METHOD__, array $options = [] ) {
276 $this->assertTransactionRoundStage( self::ROUND_CURSORY );
277 /** @noinspection PhpUnusedLocalVariableInspection */
278 $scope = ScopedCallback::newScopedIgnoreUserAbort();
279
280 $this->trxRoundStage = self::ROUND_COMMITTING;
281 if ( $this->trxRoundId !== false && $this->trxRoundId !== $fname ) {
282 throw new DBTransactionError(
283 null,
284 "$fname: transaction round '{$this->trxRoundId}' still running"
285 );
286 }
287 // Run pre-commit callbacks and suppress post-commit callbacks, aborting on failure
288 do {
289 $count = 0; // number of callbacks executed this iteration
290 $this->forEachLB( function ( ILoadBalancer $lb ) use ( &$count, $fname ) {
291 $count += $lb->finalizeMasterChanges( $fname, $this->id );
292 } );
293 } while ( $count > 0 );
294 $this->trxRoundId = false;
295 // Perform pre-commit checks, aborting on failure
296 $this->forEachLBCallMethod( 'approveMasterChanges', [ $options, $fname, $this->id ] );
297 // Log the DBs and methods involved in multi-DB transactions
298 $this->logIfMultiDbTransaction();
299 // Actually perform the commit on all master DB connections and revert DBO_TRX
300 $this->forEachLBCallMethod( 'commitMasterChanges', [ $fname, $this->id ] );
301 // Run all post-commit callbacks in a separate step
302 $this->trxRoundStage = self::ROUND_COMMIT_CALLBACKS;
303 $e = $this->executePostTransactionCallbacks();
304 $this->trxRoundStage = self::ROUND_CURSORY;
305 // Throw any last post-commit callback error
306 if ( $e instanceof Exception ) {
307 throw $e;
308 }
309 }
310
311 final public function rollbackMasterChanges( $fname = __METHOD__ ) {
312 /** @noinspection PhpUnusedLocalVariableInspection */
313 $scope = ScopedCallback::newScopedIgnoreUserAbort();
314
315 $this->trxRoundStage = self::ROUND_ROLLING_BACK;
316 $this->trxRoundId = false;
317 // Actually perform the rollback on all master DB connections and revert DBO_TRX
318 $this->forEachLBCallMethod( 'rollbackMasterChanges', [ $fname, $this->id ] );
319 // Run all post-commit callbacks in a separate step
320 $this->trxRoundStage = self::ROUND_ROLLBACK_CALLBACKS;
321 $this->executePostTransactionCallbacks();
322 $this->trxRoundStage = self::ROUND_CURSORY;
323 }
324
325 /**
326 * @return Exception|null
327 */
328 private function executePostTransactionCallbacks() {
329 $fname = __METHOD__;
330 // Run all post-commit callbacks until new ones stop getting added
331 $e = null; // first callback exception
332 do {
333 $this->forEachLB( function ( ILoadBalancer $lb ) use ( &$e, $fname ) {
334 $ex = $lb->runMasterTransactionIdleCallbacks( $fname, $this->id );
335 $e = $e ?: $ex;
336 } );
337 } while ( $this->hasMasterChanges() );
338 // Run all listener callbacks once
339 $this->forEachLB( function ( ILoadBalancer $lb ) use ( &$e, $fname ) {
340 $ex = $lb->runMasterTransactionListenerCallbacks( $fname, $this->id );
341 $e = $e ?: $ex;
342 } );
343
344 return $e;
345 }
346
347 public function hasTransactionRound() {
348 return ( $this->trxRoundId !== false );
349 }
350
351 public function isReadyForRoundOperations() {
352 return ( $this->trxRoundStage === self::ROUND_CURSORY );
353 }
354
355 /**
356 * Log query info if multi DB transactions are going to be committed now
357 */
358 private function logIfMultiDbTransaction() {
359 $callersByDB = [];
360 $this->forEachLB( function ( ILoadBalancer $lb ) use ( &$callersByDB ) {
361 $masterName = $lb->getServerName( $lb->getWriterIndex() );
362 $callers = $lb->pendingMasterChangeCallers();
363 if ( $callers ) {
364 $callersByDB[$masterName] = $callers;
365 }
366 } );
367
368 if ( count( $callersByDB ) >= 2 ) {
369 $dbs = implode( ', ', array_keys( $callersByDB ) );
370 $msg = "Multi-DB transaction [{$dbs}]:\n";
371 foreach ( $callersByDB as $db => $callers ) {
372 $msg .= "$db: " . implode( '; ', $callers ) . "\n";
373 }
374 $this->queryLogger->info( $msg );
375 }
376 }
377
378 public function hasMasterChanges() {
379 $ret = false;
380 $this->forEachLB( function ( ILoadBalancer $lb ) use ( &$ret ) {
381 $ret = $ret || $lb->hasMasterChanges();
382 } );
383
384 return $ret;
385 }
386
387 public function laggedReplicaUsed() {
388 $ret = false;
389 $this->forEachLB( function ( ILoadBalancer $lb ) use ( &$ret ) {
390 $ret = $ret || $lb->laggedReplicaUsed();
391 } );
392
393 return $ret;
394 }
395
396 public function hasOrMadeRecentMasterChanges( $age = null ) {
397 $ret = false;
398 $this->forEachLB( function ( ILoadBalancer $lb ) use ( $age, &$ret ) {
399 $ret = $ret || $lb->hasOrMadeRecentMasterChanges( $age );
400 } );
401 return $ret;
402 }
403
404 public function waitForReplication( array $opts = [] ) {
405 $opts += [
406 'domain' => false,
407 'cluster' => false,
408 'timeout' => $this->cliMode ? 60 : 1,
409 'ifWritesSince' => null
410 ];
411
412 if ( $opts['domain'] === false && isset( $opts['wiki'] ) ) {
413 $opts['domain'] = $opts['wiki']; // b/c
414 }
415
416 // Figure out which clusters need to be checked
417 /** @var ILoadBalancer[] $lbs */
418 $lbs = [];
419 if ( $opts['cluster'] !== false ) {
420 $lbs[] = $this->getExternalLB( $opts['cluster'] );
421 } elseif ( $opts['domain'] !== false ) {
422 $lbs[] = $this->getMainLB( $opts['domain'] );
423 } else {
424 $this->forEachLB( function ( ILoadBalancer $lb ) use ( &$lbs ) {
425 $lbs[] = $lb;
426 } );
427 if ( !$lbs ) {
428 return true; // nothing actually used
429 }
430 }
431
432 // Get all the master positions of applicable DBs right now.
433 // This can be faster since waiting on one cluster reduces the
434 // time needed to wait on the next clusters.
435 $masterPositions = array_fill( 0, count( $lbs ), false );
436 foreach ( $lbs as $i => $lb ) {
437 if (
438 // No writes to wait on getting replicated
439 !$lb->hasMasterConnection() ||
440 // No replication; avoid getMasterPos() permissions errors (T29975)
441 !$lb->hasStreamingReplicaServers() ||
442 // No writes since the last replication wait
443 (
444 $opts['ifWritesSince'] &&
445 $lb->lastMasterChangeTimestamp() < $opts['ifWritesSince']
446 )
447 ) {
448 continue; // no need to wait
449 }
450
451 $masterPositions[$i] = $lb->getMasterPos();
452 }
453
454 // Run any listener callbacks *after* getting the DB positions. The more
455 // time spent in the callbacks, the less time is spent in waitForAll().
456 foreach ( $this->replicationWaitCallbacks as $callback ) {
457 $callback();
458 }
459
460 $failed = [];
461 foreach ( $lbs as $i => $lb ) {
462 if ( $masterPositions[$i] ) {
463 // The RDBMS may not support getMasterPos()
464 if ( !$lb->waitForAll( $masterPositions[$i], $opts['timeout'] ) ) {
465 $failed[] = $lb->getServerName( $lb->getWriterIndex() );
466 }
467 }
468 }
469
470 return !$failed;
471 }
472
473 public function setWaitForReplicationListener( $name, callable $callback = null ) {
474 if ( $callback ) {
475 $this->replicationWaitCallbacks[$name] = $callback;
476 } else {
477 unset( $this->replicationWaitCallbacks[$name] );
478 }
479 }
480
481 public function getEmptyTransactionTicket( $fname ) {
482 if ( $this->hasMasterChanges() ) {
483 $this->queryLogger->error(
484 __METHOD__ . ": $fname does not have outer scope",
485 [ 'trace' => ( new RuntimeException() )->getTraceAsString() ]
486 );
487
488 return null;
489 }
490
491 return $this->ticket;
492 }
493
494 final public function commitAndWaitForReplication( $fname, $ticket, array $opts = [] ) {
495 if ( $ticket !== $this->ticket ) {
496 $this->perfLogger->error(
497 __METHOD__ . ": $fname does not have outer scope",
498 [ 'trace' => ( new RuntimeException() )->getTraceAsString() ]
499 );
500
501 return false;
502 }
503
504 // The transaction owner and any caller with the empty transaction ticket can commit
505 // so that getEmptyTransactionTicket() callers don't risk seeing DBTransactionError.
506 if ( $this->trxRoundId !== false && $fname !== $this->trxRoundId ) {
507 $this->queryLogger->info( "$fname: committing on behalf of {$this->trxRoundId}" );
508 $fnameEffective = $this->trxRoundId;
509 } else {
510 $fnameEffective = $fname;
511 }
512
513 $this->commitMasterChanges( $fnameEffective );
514 $waitSucceeded = $this->waitForReplication( $opts );
515 // If a nested caller committed on behalf of $fname, start another empty $fname
516 // transaction, leaving the caller with the same empty transaction state as before.
517 if ( $fnameEffective !== $fname ) {
518 $this->beginMasterChanges( $fnameEffective );
519 }
520
521 return $waitSucceeded;
522 }
523
524 public function getChronologyProtectorTouched( $dbName ) {
525 return $this->getChronologyProtector()->getTouched( $dbName );
526 }
527
528 public function disableChronologyProtection() {
529 $this->getChronologyProtector()->setEnabled( false );
530 }
531
532 /**
533 * @return ChronologyProtector
534 */
535 protected function getChronologyProtector() {
536 if ( $this->chronProt ) {
537 return $this->chronProt;
538 }
539
540 $this->chronProt = new ChronologyProtector(
541 $this->memStash,
542 [
543 'ip' => $this->requestInfo['IPAddress'],
544 'agent' => $this->requestInfo['UserAgent'],
545 'clientId' => $this->requestInfo['ChronologyClientId'] ?: null
546 ],
547 $this->requestInfo['ChronologyPositionIndex'],
548 $this->secret
549 );
550 $this->chronProt->setLogger( $this->replLogger );
551
552 if ( $this->cliMode ) {
553 $this->chronProt->setEnabled( false );
554 } elseif ( $this->requestInfo['ChronologyProtection'] === 'false' ) {
555 // Request opted out of using position wait logic. This is useful for requests
556 // done by the job queue or background ETL that do not have a meaningful session.
557 $this->chronProt->setWaitEnabled( false );
558 } elseif ( $this->memStash instanceof EmptyBagOStuff ) {
559 // No where to store any DB positions and wait for them to appear
560 $this->chronProt->setEnabled( false );
561 $this->replLogger->info( 'Cannot use ChronologyProtector with EmptyBagOStuff' );
562 }
563
564 $this->replLogger->debug(
565 __METHOD__ . ': request info ' .
566 json_encode( $this->requestInfo, JSON_PRETTY_PRINT )
567 );
568
569 return $this->chronProt;
570 }
571
572 /**
573 * Get and record all of the staged DB positions into persistent memory storage
574 *
575 * @param ChronologyProtector $cp
576 * @param callable|null $workCallback Work to do instead of waiting on syncing positions
577 * @param string $mode One of (sync, async); whether to wait on remote datacenters
578 * @param int|null &$cpIndex DB position key write counter; incremented on update
579 */
580 protected function shutdownChronologyProtector(
581 ChronologyProtector $cp, $workCallback, $mode, &$cpIndex = null
582 ) {
583 // Record all the master positions needed
584 $this->forEachLB( function ( ILoadBalancer $lb ) use ( $cp ) {
585 $cp->storeSessionReplicationPosition( $lb );
586 } );
587 // Write them to the persistent stash. Try to do something useful by running $work
588 // while ChronologyProtector waits for the stash write to replicate to all DCs.
589 $unsavedPositions = $cp->shutdown( $workCallback, $mode, $cpIndex );
590 if ( $unsavedPositions && $workCallback ) {
591 // Invoke callback in case it did not cache the result yet
592 $workCallback(); // work now to block for less time in waitForAll()
593 }
594 // If the positions failed to write to the stash, at least wait on local datacenter
595 // replica DBs to catch up before responding. Even if there are several DCs, this increases
596 // the chance that the user will see their own changes immediately afterwards. As long
597 // as the sticky DC cookie applies (same domain), this is not even an issue.
598 $this->forEachLB( function ( ILoadBalancer $lb ) use ( $unsavedPositions ) {
599 $masterName = $lb->getServerName( $lb->getWriterIndex() );
600 if ( isset( $unsavedPositions[$masterName] ) ) {
601 $lb->waitForAll( $unsavedPositions[$masterName] );
602 }
603 } );
604 }
605
606 /**
607 * Base parameters to ILoadBalancer::__construct()
608 * @return array
609 */
610 final protected function baseLoadBalancerParams() {
611 if ( $this->trxRoundStage === self::ROUND_COMMIT_CALLBACKS ) {
612 $initStage = ILoadBalancer::STAGE_POSTCOMMIT_CALLBACKS;
613 } elseif ( $this->trxRoundStage === self::ROUND_ROLLBACK_CALLBACKS ) {
614 $initStage = ILoadBalancer::STAGE_POSTROLLBACK_CALLBACKS;
615 } else {
616 $initStage = null;
617 }
618
619 return [
620 'localDomain' => $this->localDomain,
621 'readOnlyReason' => $this->readOnlyReason,
622 'srvCache' => $this->srvCache,
623 'wanCache' => $this->wanCache,
624 'profiler' => $this->profiler,
625 'trxProfiler' => $this->trxProfiler,
626 'queryLogger' => $this->queryLogger,
627 'connLogger' => $this->connLogger,
628 'replLogger' => $this->replLogger,
629 'errorLogger' => $this->errorLogger,
630 'deprecationLogger' => $this->deprecationLogger,
631 'hostname' => $this->hostname,
632 'cliMode' => $this->cliMode,
633 'agent' => $this->agent,
634 'maxLag' => $this->maxLag,
635 'defaultGroup' => $this->defaultGroup,
636 'chronologyCallback' => function ( ILoadBalancer $lb ) {
637 // Defer ChronologyProtector construction in case setRequestInfo() ends up
638 // being called later (but before the first connection attempt) (T192611)
639 $this->getChronologyProtector()->applySessionReplicationPosition( $lb );
640 },
641 'roundStage' => $initStage,
642 'ownerId' => $this->id
643 ];
644 }
645
646 /**
647 * @param ILoadBalancer $lb
648 */
649 protected function initLoadBalancer( ILoadBalancer $lb ) {
650 if ( $this->trxRoundId !== false ) {
651 $lb->beginMasterChanges( $this->trxRoundId, $this->id ); // set DBO_TRX
652 }
653
654 $lb->setTableAliases( $this->tableAliases );
655 $lb->setIndexAliases( $this->indexAliases );
656 }
657
658 public function setTableAliases( array $aliases ) {
659 $this->tableAliases = $aliases;
660 }
661
662 public function setIndexAliases( array $aliases ) {
663 $this->indexAliases = $aliases;
664 }
665
666 public function setLocalDomainPrefix( $prefix ) {
667 $this->localDomain = new DatabaseDomain(
668 $this->localDomain->getDatabase(),
669 $this->localDomain->getSchema(),
670 $prefix
671 );
672
673 $this->forEachLB( function ( ILoadBalancer $lb ) use ( $prefix ) {
674 $lb->setLocalDomainPrefix( $prefix );
675 } );
676 }
677
678 public function redefineLocalDomain( $domain ) {
679 $this->closeAll();
680
681 $this->localDomain = DatabaseDomain::newFromId( $domain );
682
683 $this->forEachLB( function ( ILoadBalancer $lb ) {
684 $lb->redefineLocalDomain( $this->localDomain );
685 } );
686 }
687
688 public function closeAll() {
689 /** @noinspection PhpUnusedLocalVariableInspection */
690 $scope = ScopedCallback::newScopedIgnoreUserAbort();
691
692 $this->forEachLBCallMethod( 'closeAll' );
693 }
694
695 public function setAgentName( $agent ) {
696 $this->agent = $agent;
697 }
698
699 public function appendShutdownCPIndexAsQuery( $url, $index ) {
700 $usedCluster = 0;
701 $this->forEachLB( function ( ILoadBalancer $lb ) use ( &$usedCluster ) {
702 $usedCluster |= $lb->hasStreamingReplicaServers();
703 } );
704
705 if ( !$usedCluster ) {
706 return $url; // no master/replica clusters touched
707 }
708
709 return strpos( $url, '?' ) === false ? "$url?cpPosIndex=$index" : "$url&cpPosIndex=$index";
710 }
711
712 public function getChronologyProtectorClientId() {
713 return $this->getChronologyProtector()->getClientId();
714 }
715
716 /**
717 * @param int $index Write index
718 * @param int $time UNIX timestamp; can be used to detect stale cookies (T190082)
719 * @param string $clientId Agent ID hash from ILBFactory::shutdown()
720 * @return string Timestamp-qualified write index of the form "<index>@<timestamp>#<hash>"
721 * @since 1.32
722 */
723 public static function makeCookieValueFromCPIndex( $index, $time, $clientId ) {
724 return "$index@$time#$clientId";
725 }
726
727 /**
728 * @param string $value Possible result of LBFactory::makeCookieValueFromCPIndex()
729 * @param int $minTimestamp Lowest UNIX timestamp that a non-expired value can have
730 * @return array (index: int or null, clientId: string or null)
731 * @since 1.32
732 */
733 public static function getCPInfoFromCookieValue( $value, $minTimestamp ) {
734 static $placeholder = [ 'index' => null, 'clientId' => null ];
735
736 if ( !preg_match( '/^(\d+)@(\d+)#([0-9a-f]{32})$/', $value, $m ) ) {
737 return $placeholder; // invalid
738 }
739
740 $index = (int)$m[1];
741 if ( $index <= 0 ) {
742 return $placeholder; // invalid
743 } elseif ( isset( $m[2] ) && $m[2] !== '' && (int)$m[2] < $minTimestamp ) {
744 return $placeholder; // expired
745 }
746
747 $clientId = ( isset( $m[3] ) && $m[3] !== '' ) ? $m[3] : null;
748
749 return [ 'index' => $index, 'clientId' => $clientId ];
750 }
751
752 public function setRequestInfo( array $info ) {
753 if ( $this->chronProt ) {
754 throw new LogicException( 'ChronologyProtector already initialized' );
755 }
756
757 $this->requestInfo = $info + $this->requestInfo;
758 }
759
760 /**
761 * @param string $stage
762 */
763 private function assertTransactionRoundStage( $stage ) {
764 if ( $this->trxRoundStage !== $stage ) {
765 throw new DBTransactionError(
766 null,
767 "Transaction round stage must be '$stage' (not '{$this->trxRoundStage}')"
768 );
769 }
770 }
771
772 function __destruct() {
773 $this->destroy();
774 }
775 }