LBFactory.php 24 KB

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