3 * Generator and manager of database load balancing objects
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.
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.
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
24 namespace Wikimedia\Rdbms
;
26 use Psr\Log\LoggerInterface
;
27 use Psr\Log\NullLogger
;
28 use Wikimedia\ScopedCallback
;
37 * An interface for generating database load balancers
40 abstract class LBFactory
implements ILBFactory
{
41 /** @var ChronologyProtector */
43 /** @var object|string Class name or object With profileIn/profileOut methods */
45 /** @var TransactionProfiler */
47 /** @var LoggerInterface */
49 /** @var LoggerInterface */
51 /** @var LoggerInterface */
53 /** @var LoggerInterface */
55 /** @var callable Error logger */
57 /** @var callable Deprecation logger */
58 private $deprecationLogger;
64 /** @var WANObjectCache */
67 /** @var DatabaseDomain Local domain */
68 protected $localDomain;
70 /** @var string Local hostname of the app server */
72 /** @var array Web request information about the client */
74 /** @var bool Whether this PHP instance is for a CLI script */
76 /** @var string Agent name for query profiling */
78 /** @var string Secret string for HMAC hashing */
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 = [];
88 /** var int An identifier for this class instance */
90 /** @var int|null Ticket used to delegate transaction ownership */
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
;
97 /** @var string|bool Reason all LBs are read-only or false if not */
98 protected $readOnlyReason = false;
100 /** @var string|null */
101 private $defaultGroup = null;
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';
113 private static $loggerFields =
114 [ 'replLogger', 'connLogger', 'queryLogger', 'perfLogger' ];
116 public function __construct( array $conf ) {
117 $this->localDomain
= isset( $conf['localDomain'] )
118 ? DatabaseDomain
::newFromId( $conf['localDomain'] )
119 : DatabaseDomain
::newUnspecified();
121 $this->maxLag
= $conf['maxLag'] ??
null;
122 if ( isset( $conf['readOnlyReason'] ) && is_string( $conf['readOnlyReason'] ) ) {
123 $this->readOnlyReason
= $conf['readOnlyReason'];
126 $this->srvCache
= $conf['srvCache'] ??
new EmptyBagOStuff();
127 $this->memStash
= $conf['memStash'] ??
new EmptyBagOStuff();
128 $this->wanCache
= $conf['wanCache'] ?? WANObjectCache
::newEmpty();
130 foreach ( self
::$loggerFields as $key ) {
131 $this->$key = $conf[$key] ??
new NullLogger();
133 $this->errorLogger
= $conf['errorLogger'] ??
function ( Exception
$e ) {
134 trigger_error( get_class( $e ) . ': ' . $e->getMessage(), E_USER_WARNING
);
136 $this->deprecationLogger
= $conf['deprecationLogger'] ??
function ( $msg ) {
137 trigger_error( $msg, E_USER_DEPRECATED
);
140 $this->profiler
= $conf['profiler'] ??
null;
141 $this->trxProfiler
= $conf['trxProfiler'] ??
new TransactionProfiler();
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()
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'] ??
'';
158 static $nextId, $nextTicket;
159 $this->id
= $nextId = ( is_int( $nextId ) ?
$nextId++
: mt_rand() );
160 $this->ticket
= $nextTicket = ( is_int( $nextTicket ) ?
$nextTicket++
: mt_rand() );
163 public function destroy() {
164 /** @noinspection PhpUnusedLocalVariableInspection */
165 $scope = ScopedCallback
::newScopedIgnoreUserAbort();
167 $this->forEachLBCallMethod( 'disable', [ __METHOD__
, $this->id
] );
170 public function getLocalDomainID() {
171 return $this->localDomain
->getId();
174 public function resolveDomainID( $domain ) {
175 return ( $domain !== false ) ?
(string)$domain : $this->getLocalDomainID();
178 public function shutdown(
179 $mode = self
::SHUTDOWN_CHRONPROT_SYNC
,
180 callable
$workCallback = null,
184 /** @noinspection PhpUnusedLocalVariableInspection */
185 $scope = ScopedCallback
::newScopedIgnoreUserAbort();
187 $chronProt = $this->getChronologyProtector();
188 if ( $mode === self
::SHUTDOWN_CHRONPROT_SYNC
) {
189 $this->shutdownChronologyProtector( $chronProt, $workCallback, 'sync', $cpIndex );
190 } elseif ( $mode === self
::SHUTDOWN_CHRONPROT_ASYNC
) {
191 $this->shutdownChronologyProtector( $chronProt, null, 'async', $cpIndex );
194 $cpClientId = $chronProt->getClientId();
196 $this->commitMasterChanges( __METHOD__
); // sanity
200 * Call a method of each tracked load balancer
202 * @param string $methodName
205 protected function forEachLBCallMethod( $methodName, array $args = [] ) {
207 function ( ILoadBalancer
$loadBalancer, $methodName, array $args ) {
208 $loadBalancer->$methodName( ...$args );
210 [ $methodName, $args ]
214 public function flushReplicaSnapshots( $fname = __METHOD__
) {
215 if ( $this->trxRoundId
!== false && $this->trxRoundId
!== $fname ) {
216 $this->queryLogger
->warning(
217 "$fname: transaction round '{$this->trxRoundId}' still running",
218 [ 'trace' => ( new RuntimeException() )->getTraceAsString() ]
221 $this->forEachLBCallMethod( 'flushReplicaSnapshots', [ $fname, $this->id
] );
224 final public function commitAll( $fname = __METHOD__
, array $options = [] ) {
225 $this->commitMasterChanges( $fname, $options );
226 $this->forEachLBCallMethod( 'flushMasterSnapshots', [ $fname, $this->id
] );
227 $this->forEachLBCallMethod( 'flushReplicaSnapshots', [ $fname, $this->id
] );
230 final public function beginMasterChanges( $fname = __METHOD__
) {
231 $this->assertTransactionRoundStage( self
::ROUND_CURSORY
);
232 /** @noinspection PhpUnusedLocalVariableInspection */
233 $scope = ScopedCallback
::newScopedIgnoreUserAbort();
235 $this->trxRoundStage
= self
::ROUND_BEGINNING
;
236 if ( $this->trxRoundId
!== false ) {
237 throw new DBTransactionError(
239 "$fname: transaction round '{$this->trxRoundId}' already started"
242 $this->trxRoundId
= $fname;
243 // Set DBO_TRX flags on all appropriate DBs
244 $this->forEachLBCallMethod( 'beginMasterChanges', [ $fname, $this->id
] );
245 $this->trxRoundStage
= self
::ROUND_CURSORY
;
248 final public function commitMasterChanges( $fname = __METHOD__
, array $options = [] ) {
249 $this->assertTransactionRoundStage( self
::ROUND_CURSORY
);
250 /** @noinspection PhpUnusedLocalVariableInspection */
251 $scope = ScopedCallback
::newScopedIgnoreUserAbort();
253 $this->trxRoundStage
= self
::ROUND_COMMITTING
;
254 if ( $this->trxRoundId
!== false && $this->trxRoundId
!== $fname ) {
255 throw new DBTransactionError(
257 "$fname: transaction round '{$this->trxRoundId}' still running"
260 // Run pre-commit callbacks and suppress post-commit callbacks, aborting on failure
262 $count = 0; // number of callbacks executed this iteration
263 $this->forEachLB( function ( ILoadBalancer
$lb ) use ( &$count, $fname ) {
264 $count +
= $lb->finalizeMasterChanges( $fname, $this->id
);
266 } while ( $count > 0 );
267 $this->trxRoundId
= false;
268 // Perform pre-commit checks, aborting on failure
269 $this->forEachLBCallMethod( 'approveMasterChanges', [ $options, $fname, $this->id
] );
270 // Log the DBs and methods involved in multi-DB transactions
271 $this->logIfMultiDbTransaction();
272 // Actually perform the commit on all master DB connections and revert DBO_TRX
273 $this->forEachLBCallMethod( 'commitMasterChanges', [ $fname, $this->id
] );
274 // Run all post-commit callbacks in a separate step
275 $this->trxRoundStage
= self
::ROUND_COMMIT_CALLBACKS
;
276 $e = $this->executePostTransactionCallbacks();
277 $this->trxRoundStage
= self
::ROUND_CURSORY
;
278 // Throw any last post-commit callback error
279 if ( $e instanceof Exception
) {
284 final public function rollbackMasterChanges( $fname = __METHOD__
) {
285 /** @noinspection PhpUnusedLocalVariableInspection */
286 $scope = ScopedCallback
::newScopedIgnoreUserAbort();
288 $this->trxRoundStage
= self
::ROUND_ROLLING_BACK
;
289 $this->trxRoundId
= false;
290 // Actually perform the rollback on all master DB connections and revert DBO_TRX
291 $this->forEachLBCallMethod( 'rollbackMasterChanges', [ $fname, $this->id
] );
292 // Run all post-commit callbacks in a separate step
293 $this->trxRoundStage
= self
::ROUND_ROLLBACK_CALLBACKS
;
294 $this->executePostTransactionCallbacks();
295 $this->trxRoundStage
= self
::ROUND_CURSORY
;
299 * @return Exception|null
301 private function executePostTransactionCallbacks() {
303 // Run all post-commit callbacks until new ones stop getting added
304 $e = null; // first callback exception
306 $this->forEachLB( function ( ILoadBalancer
$lb ) use ( &$e, $fname ) {
307 $ex = $lb->runMasterTransactionIdleCallbacks( $fname, $this->id
);
310 } while ( $this->hasMasterChanges() );
311 // Run all listener callbacks once
312 $this->forEachLB( function ( ILoadBalancer
$lb ) use ( &$e, $fname ) {
313 $ex = $lb->runMasterTransactionListenerCallbacks( $fname, $this->id
);
320 public function hasTransactionRound() {
321 return ( $this->trxRoundId
!== false );
324 public function isReadyForRoundOperations() {
325 return ( $this->trxRoundStage
=== self
::ROUND_CURSORY
);
329 * Log query info if multi DB transactions are going to be committed now
331 private function logIfMultiDbTransaction() {
333 $this->forEachLB( function ( ILoadBalancer
$lb ) use ( &$callersByDB ) {
334 $masterName = $lb->getServerName( $lb->getWriterIndex() );
335 $callers = $lb->pendingMasterChangeCallers();
337 $callersByDB[$masterName] = $callers;
341 if ( count( $callersByDB ) >= 2 ) {
342 $dbs = implode( ', ', array_keys( $callersByDB ) );
343 $msg = "Multi-DB transaction [{$dbs}]:\n";
344 foreach ( $callersByDB as $db => $callers ) {
345 $msg .= "$db: " . implode( '; ', $callers ) . "\n";
347 $this->queryLogger
->info( $msg );
351 public function hasMasterChanges() {
353 $this->forEachLB( function ( ILoadBalancer
$lb ) use ( &$ret ) {
354 $ret = $ret ||
$lb->hasMasterChanges();
360 public function laggedReplicaUsed() {
362 $this->forEachLB( function ( ILoadBalancer
$lb ) use ( &$ret ) {
363 $ret = $ret ||
$lb->laggedReplicaUsed();
369 public function hasOrMadeRecentMasterChanges( $age = null ) {
371 $this->forEachLB( function ( ILoadBalancer
$lb ) use ( $age, &$ret ) {
372 $ret = $ret ||
$lb->hasOrMadeRecentMasterChanges( $age );
377 public function waitForReplication( array $opts = [] ) {
381 'timeout' => $this->cliMode ?
60 : 1,
382 'ifWritesSince' => null
385 if ( $opts['domain'] === false && isset( $opts['wiki'] ) ) {
386 $opts['domain'] = $opts['wiki']; // b/c
389 // Figure out which clusters need to be checked
390 /** @var ILoadBalancer[] $lbs */
392 if ( $opts['cluster'] !== false ) {
393 $lbs[] = $this->getExternalLB( $opts['cluster'] );
394 } elseif ( $opts['domain'] !== false ) {
395 $lbs[] = $this->getMainLB( $opts['domain'] );
397 $this->forEachLB( function ( ILoadBalancer
$lb ) use ( &$lbs ) {
401 return true; // nothing actually used
405 // Get all the master positions of applicable DBs right now.
406 // This can be faster since waiting on one cluster reduces the
407 // time needed to wait on the next clusters.
408 $masterPositions = array_fill( 0, count( $lbs ), false );
409 foreach ( $lbs as $i => $lb ) {
411 // No writes to wait on getting replicated
412 !$lb->hasMasterConnection() ||
413 // No replication; avoid getMasterPos() permissions errors (T29975)
414 !$lb->hasStreamingReplicaServers() ||
415 // No writes since the last replication wait
417 $opts['ifWritesSince'] &&
418 $lb->lastMasterChangeTimestamp() < $opts['ifWritesSince']
421 continue; // no need to wait
424 $masterPositions[$i] = $lb->getMasterPos();
427 // Run any listener callbacks *after* getting the DB positions. The more
428 // time spent in the callbacks, the less time is spent in waitForAll().
429 foreach ( $this->replicationWaitCallbacks
as $callback ) {
434 foreach ( $lbs as $i => $lb ) {
435 if ( $masterPositions[$i] ) {
436 // The RDBMS may not support getMasterPos()
437 if ( !$lb->waitForAll( $masterPositions[$i], $opts['timeout'] ) ) {
438 $failed[] = $lb->getServerName( $lb->getWriterIndex() );
446 public function setWaitForReplicationListener( $name, callable
$callback = null ) {
448 $this->replicationWaitCallbacks
[$name] = $callback;
450 unset( $this->replicationWaitCallbacks
[$name] );
454 public function getEmptyTransactionTicket( $fname ) {
455 if ( $this->hasMasterChanges() ) {
456 $this->queryLogger
->error(
457 __METHOD__
. ": $fname does not have outer scope",
458 [ 'trace' => ( new RuntimeException() )->getTraceAsString() ]
464 return $this->ticket
;
467 final public function commitAndWaitForReplication( $fname, $ticket, array $opts = [] ) {
468 if ( $ticket !== $this->ticket
) {
469 $this->perfLogger
->error(
470 __METHOD__
. ": $fname does not have outer scope",
471 [ 'trace' => ( new RuntimeException() )->getTraceAsString() ]
477 // The transaction owner and any caller with the empty transaction ticket can commit
478 // so that getEmptyTransactionTicket() callers don't risk seeing DBTransactionError.
479 if ( $this->trxRoundId
!== false && $fname !== $this->trxRoundId
) {
480 $this->queryLogger
->info( "$fname: committing on behalf of {$this->trxRoundId}" );
481 $fnameEffective = $this->trxRoundId
;
483 $fnameEffective = $fname;
486 $this->commitMasterChanges( $fnameEffective );
487 $waitSucceeded = $this->waitForReplication( $opts );
488 // If a nested caller committed on behalf of $fname, start another empty $fname
489 // transaction, leaving the caller with the same empty transaction state as before.
490 if ( $fnameEffective !== $fname ) {
491 $this->beginMasterChanges( $fnameEffective );
494 return $waitSucceeded;
497 public function getChronologyProtectorTouched( $dbName ) {
498 return $this->getChronologyProtector()->getTouched( $dbName );
501 public function disableChronologyProtection() {
502 $this->getChronologyProtector()->setEnabled( false );
506 * @return ChronologyProtector
508 protected function getChronologyProtector() {
509 if ( $this->chronProt
) {
510 return $this->chronProt
;
513 $this->chronProt
= new ChronologyProtector(
516 'ip' => $this->requestInfo
['IPAddress'],
517 'agent' => $this->requestInfo
['UserAgent'],
518 'clientId' => $this->requestInfo
['ChronologyClientId'] ?
: null
520 $this->requestInfo
['ChronologyPositionIndex'],
523 $this->chronProt
->setLogger( $this->replLogger
);
525 if ( $this->cliMode
) {
526 $this->chronProt
->setEnabled( false );
527 } elseif ( $this->requestInfo
['ChronologyProtection'] === 'false' ) {
528 // Request opted out of using position wait logic. This is useful for requests
529 // done by the job queue or background ETL that do not have a meaningful session.
530 $this->chronProt
->setWaitEnabled( false );
531 } elseif ( $this->memStash
instanceof EmptyBagOStuff
) {
532 // No where to store any DB positions and wait for them to appear
533 $this->chronProt
->setEnabled( false );
534 $this->replLogger
->info( 'Cannot use ChronologyProtector with EmptyBagOStuff' );
537 $this->replLogger
->debug(
538 __METHOD__
. ': request info ' .
539 json_encode( $this->requestInfo
, JSON_PRETTY_PRINT
)
542 return $this->chronProt
;
546 * Get and record all of the staged DB positions into persistent memory storage
548 * @param ChronologyProtector $cp
549 * @param callable|null $workCallback Work to do instead of waiting on syncing positions
550 * @param string $mode One of (sync, async); whether to wait on remote datacenters
551 * @param int|null &$cpIndex DB position key write counter; incremented on update
553 protected function shutdownChronologyProtector(
554 ChronologyProtector
$cp, $workCallback, $mode, &$cpIndex = null
556 // Record all the master positions needed
557 $this->forEachLB( function ( ILoadBalancer
$lb ) use ( $cp ) {
558 $cp->storeSessionReplicationPosition( $lb );
560 // Write them to the persistent stash. Try to do something useful by running $work
561 // while ChronologyProtector waits for the stash write to replicate to all DCs.
562 $unsavedPositions = $cp->shutdown( $workCallback, $mode, $cpIndex );
563 if ( $unsavedPositions && $workCallback ) {
564 // Invoke callback in case it did not cache the result yet
565 $workCallback(); // work now to block for less time in waitForAll()
567 // If the positions failed to write to the stash, at least wait on local datacenter
568 // replica DBs to catch up before responding. Even if there are several DCs, this increases
569 // the chance that the user will see their own changes immediately afterwards. As long
570 // as the sticky DC cookie applies (same domain), this is not even an issue.
571 $this->forEachLB( function ( ILoadBalancer
$lb ) use ( $unsavedPositions ) {
572 $masterName = $lb->getServerName( $lb->getWriterIndex() );
573 if ( isset( $unsavedPositions[$masterName] ) ) {
574 $lb->waitForAll( $unsavedPositions[$masterName] );
580 * Get parameters to ILoadBalancer::__construct()
582 * @param int|null $owner Use getOwnershipId() if this is for getMainLB()/getExternalLB()
585 final protected function baseLoadBalancerParams( $owner ) {
586 if ( $this->trxRoundStage
=== self
::ROUND_COMMIT_CALLBACKS
) {
587 $initStage = ILoadBalancer
::STAGE_POSTCOMMIT_CALLBACKS
;
588 } elseif ( $this->trxRoundStage
=== self
::ROUND_ROLLBACK_CALLBACKS
) {
589 $initStage = ILoadBalancer
::STAGE_POSTROLLBACK_CALLBACKS
;
595 'localDomain' => $this->localDomain
,
596 'readOnlyReason' => $this->readOnlyReason
,
597 'srvCache' => $this->srvCache
,
598 'wanCache' => $this->wanCache
,
599 'profiler' => $this->profiler
,
600 'trxProfiler' => $this->trxProfiler
,
601 'queryLogger' => $this->queryLogger
,
602 'connLogger' => $this->connLogger
,
603 'replLogger' => $this->replLogger
,
604 'errorLogger' => $this->errorLogger
,
605 'deprecationLogger' => $this->deprecationLogger
,
606 'hostname' => $this->hostname
,
607 'cliMode' => $this->cliMode
,
608 'agent' => $this->agent
,
609 'maxLag' => $this->maxLag
,
610 'defaultGroup' => $this->defaultGroup
,
611 'chronologyCallback' => function ( ILoadBalancer
$lb ) {
612 // Defer ChronologyProtector construction in case setRequestInfo() ends up
613 // being called later (but before the first connection attempt) (T192611)
614 $this->getChronologyProtector()->applySessionReplicationPosition( $lb );
616 'roundStage' => $initStage,
622 * @param ILoadBalancer $lb
624 protected function initLoadBalancer( ILoadBalancer
$lb ) {
625 if ( $this->trxRoundId
!== false ) {
626 $lb->beginMasterChanges( $this->trxRoundId
, $this->id
); // set DBO_TRX
629 $lb->setTableAliases( $this->tableAliases
);
630 $lb->setIndexAliases( $this->indexAliases
);
633 public function setTableAliases( array $aliases ) {
634 $this->tableAliases
= $aliases;
637 public function setIndexAliases( array $aliases ) {
638 $this->indexAliases
= $aliases;
641 public function setLocalDomainPrefix( $prefix ) {
642 $this->localDomain
= new DatabaseDomain(
643 $this->localDomain
->getDatabase(),
644 $this->localDomain
->getSchema(),
648 $this->forEachLB( function ( ILoadBalancer
$lb ) use ( $prefix ) {
649 $lb->setLocalDomainPrefix( $prefix );
653 public function redefineLocalDomain( $domain ) {
656 $this->localDomain
= DatabaseDomain
::newFromId( $domain );
658 $this->forEachLB( function ( ILoadBalancer
$lb ) {
659 $lb->redefineLocalDomain( $this->localDomain
);
663 public function closeAll() {
664 /** @noinspection PhpUnusedLocalVariableInspection */
665 $scope = ScopedCallback
::newScopedIgnoreUserAbort();
667 $this->forEachLBCallMethod( 'closeAll', [ __METHOD__
, $this->id
] );
670 public function setAgentName( $agent ) {
671 $this->agent
= $agent;
674 public function appendShutdownCPIndexAsQuery( $url, $index ) {
676 $this->forEachLB( function ( ILoadBalancer
$lb ) use ( &$usedCluster ) {
677 $usedCluster |
= $lb->hasStreamingReplicaServers();
680 if ( !$usedCluster ) {
681 return $url; // no master/replica clusters touched
684 return strpos( $url, '?' ) === false ?
"$url?cpPosIndex=$index" : "$url&cpPosIndex=$index";
687 public function getChronologyProtectorClientId() {
688 return $this->getChronologyProtector()->getClientId();
692 * @param int $index Write index
693 * @param int $time UNIX timestamp; can be used to detect stale cookies (T190082)
694 * @param string $clientId Agent ID hash from ILBFactory::shutdown()
695 * @return string Timestamp-qualified write index of the form "<index>@<timestamp>#<hash>"
698 public static function makeCookieValueFromCPIndex( $index, $time, $clientId ) {
699 return "$index@$time#$clientId";
703 * @param string $value Possible result of LBFactory::makeCookieValueFromCPIndex()
704 * @param int $minTimestamp Lowest UNIX timestamp that a non-expired value can have
705 * @return array (index: int or null, clientId: string or null)
708 public static function getCPInfoFromCookieValue( $value, $minTimestamp ) {
709 static $placeholder = [ 'index' => null, 'clientId' => null ];
711 if ( !preg_match( '/^(\d+)@(\d+)#([0-9a-f]{32})$/', $value, $m ) ) {
712 return $placeholder; // invalid
717 return $placeholder; // invalid
718 } elseif ( isset( $m[2] ) && $m[2] !== '' && (int)$m[2] < $minTimestamp ) {
719 return $placeholder; // expired
722 $clientId = ( isset( $m[3] ) && $m[3] !== '' ) ?
$m[3] : null;
724 return [ 'index' => $index, 'clientId' => $clientId ];
727 public function setRequestInfo( array $info ) {
728 if ( $this->chronProt
) {
729 throw new LogicException( 'ChronologyProtector already initialized' );
732 $this->requestInfo
= $info +
$this->requestInfo
;
736 * @return int Internal instance ID used to assert ownership of ILoadBalancer instances
739 final protected function getOwnershipId() {
744 * @param string $stage
746 private function assertTransactionRoundStage( $stage ) {
747 if ( $this->trxRoundStage
!== $stage ) {
748 throw new DBTransactionError(
750 "Transaction round stage must be '$stage' (not '{$this->trxRoundStage}')"
755 function __destruct() {