Merge "Change 'editfont' default preference to 'monospace'"
[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 Wikimedia\ScopedCallback;
28 use BagOStuff;
29 use EmptyBagOStuff;
30 use WANObjectCache;
31 use Exception;
32 use RuntimeException;
33
34 /**
35 * An interface for generating database load balancers
36 * @ingroup Database
37 */
38 abstract class LBFactory implements ILBFactory {
39 /** @var ChronologyProtector */
40 protected $chronProt;
41 /** @var object|string Class name or object With profileIn/profileOut methods */
42 protected $profiler;
43 /** @var TransactionProfiler */
44 protected $trxProfiler;
45 /** @var LoggerInterface */
46 protected $replLogger;
47 /** @var LoggerInterface */
48 protected $connLogger;
49 /** @var LoggerInterface */
50 protected $queryLogger;
51 /** @var LoggerInterface */
52 protected $perfLogger;
53 /** @var callable Error logger */
54 protected $errorLogger;
55 /** @var BagOStuff */
56 protected $srvCache;
57 /** @var BagOStuff */
58 protected $memStash;
59 /** @var WANObjectCache */
60 protected $wanCache;
61
62 /** @var DatabaseDomain Local domain */
63 protected $localDomain;
64 /** @var string Local hostname of the app server */
65 protected $hostname;
66 /** @var array Web request information about the client */
67 protected $requestInfo;
68
69 /** @var mixed */
70 protected $ticket;
71 /** @var string|bool String if a requested DBO_TRX transaction round is active */
72 protected $trxRoundId = false;
73 /** @var string|bool Reason all LBs are read-only or false if not */
74 protected $readOnlyReason = false;
75 /** @var callable[] */
76 protected $replicationWaitCallbacks = [];
77
78 /** @var bool Whether this PHP instance is for a CLI script */
79 protected $cliMode;
80 /** @var string Agent name for query profiling */
81 protected $agent;
82
83 private static $loggerFields =
84 [ 'replLogger', 'connLogger', 'queryLogger', 'perfLogger' ];
85
86 public function __construct( array $conf ) {
87 $this->localDomain = isset( $conf['localDomain'] )
88 ? DatabaseDomain::newFromId( $conf['localDomain'] )
89 : DatabaseDomain::newUnspecified();
90
91 if ( isset( $conf['readOnlyReason'] ) && is_string( $conf['readOnlyReason'] ) ) {
92 $this->readOnlyReason = $conf['readOnlyReason'];
93 }
94
95 $this->srvCache = isset( $conf['srvCache'] ) ? $conf['srvCache'] : new EmptyBagOStuff();
96 $this->memStash = isset( $conf['memStash'] ) ? $conf['memStash'] : new EmptyBagOStuff();
97 $this->wanCache = isset( $conf['wanCache'] )
98 ? $conf['wanCache']
99 : WANObjectCache::newEmpty();
100
101 foreach ( self::$loggerFields as $key ) {
102 $this->$key = isset( $conf[$key] ) ? $conf[$key] : new \Psr\Log\NullLogger();
103 }
104 $this->errorLogger = isset( $conf['errorLogger'] )
105 ? $conf['errorLogger']
106 : function ( Exception $e ) {
107 trigger_error( E_USER_WARNING, get_class( $e ) . ': ' . $e->getMessage() );
108 };
109
110 $this->profiler = isset( $conf['profiler'] ) ? $conf['profiler'] : null;
111 $this->trxProfiler = isset( $conf['trxProfiler'] )
112 ? $conf['trxProfiler']
113 : new TransactionProfiler();
114
115 $this->requestInfo = [
116 'IPAddress' => isset( $_SERVER[ 'REMOTE_ADDR' ] ) ? $_SERVER[ 'REMOTE_ADDR' ] : '',
117 'UserAgent' => isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : '',
118 'ChronologyProtection' => 'true'
119 ];
120
121 $this->cliMode = isset( $conf['cliMode'] ) ? $conf['cliMode'] : PHP_SAPI === 'cli';
122 $this->hostname = isset( $conf['hostname'] ) ? $conf['hostname'] : gethostname();
123 $this->agent = isset( $conf['agent'] ) ? $conf['agent'] : '';
124
125 $this->ticket = mt_rand();
126 }
127
128 public function destroy() {
129 $this->shutdown( self::SHUTDOWN_NO_CHRONPROT );
130 $this->forEachLBCallMethod( 'disable' );
131 }
132
133 public function shutdown(
134 $mode = self::SHUTDOWN_CHRONPROT_SYNC, callable $workCallback = null
135 ) {
136 $chronProt = $this->getChronologyProtector();
137 if ( $mode === self::SHUTDOWN_CHRONPROT_SYNC ) {
138 $this->shutdownChronologyProtector( $chronProt, $workCallback, 'sync' );
139 } elseif ( $mode === self::SHUTDOWN_CHRONPROT_ASYNC ) {
140 $this->shutdownChronologyProtector( $chronProt, null, 'async' );
141 }
142
143 $this->commitMasterChanges( __METHOD__ ); // sanity
144 }
145
146 /**
147 * @see ILBFactory::newMainLB()
148 * @param bool $domain
149 * @return LoadBalancer
150 */
151 abstract public function newMainLB( $domain = false );
152
153 /**
154 * @see ILBFactory::getMainLB()
155 * @param bool $domain
156 * @return LoadBalancer
157 */
158 abstract public function getMainLB( $domain = false );
159
160 /**
161 * @see ILBFactory::newExternalLB()
162 * @param string $cluster
163 * @return LoadBalancer
164 */
165 abstract public function newExternalLB( $cluster );
166
167 /**
168 * @see ILBFactory::getExternalLB()
169 * @param string $cluster
170 * @return LoadBalancer
171 */
172 abstract public function getExternalLB( $cluster );
173
174 /**
175 * Call a method of each tracked load balancer
176 *
177 * @param string $methodName
178 * @param array $args
179 */
180 protected function forEachLBCallMethod( $methodName, array $args = [] ) {
181 $this->forEachLB(
182 function ( ILoadBalancer $loadBalancer, $methodName, array $args ) {
183 call_user_func_array( [ $loadBalancer, $methodName ], $args );
184 },
185 [ $methodName, $args ]
186 );
187 }
188
189 public function flushReplicaSnapshots( $fname = __METHOD__ ) {
190 $this->forEachLBCallMethod( 'flushReplicaSnapshots', [ $fname ] );
191 }
192
193 public function commitAll( $fname = __METHOD__, array $options = [] ) {
194 $this->commitMasterChanges( $fname, $options );
195 $this->forEachLBCallMethod( 'commitAll', [ $fname ] );
196 }
197
198 public function beginMasterChanges( $fname = __METHOD__ ) {
199 if ( $this->trxRoundId !== false ) {
200 throw new DBTransactionError(
201 null,
202 "$fname: transaction round '{$this->trxRoundId}' already started."
203 );
204 }
205 $this->trxRoundId = $fname;
206 // Set DBO_TRX flags on all appropriate DBs
207 $this->forEachLBCallMethod( 'beginMasterChanges', [ $fname ] );
208 }
209
210 public function commitMasterChanges( $fname = __METHOD__, array $options = [] ) {
211 if ( $this->trxRoundId !== false && $this->trxRoundId !== $fname ) {
212 throw new DBTransactionError(
213 null,
214 "$fname: transaction round '{$this->trxRoundId}' still running."
215 );
216 }
217 /** @noinspection PhpUnusedLocalVariableInspection */
218 $scope = $this->getScopedPHPBehaviorForCommit(); // try to ignore client aborts
219 // Run pre-commit callbacks and suppress post-commit callbacks, aborting on failure
220 $this->forEachLBCallMethod( 'finalizeMasterChanges' );
221 $this->trxRoundId = false;
222 // Perform pre-commit checks, aborting on failure
223 $this->forEachLBCallMethod( 'approveMasterChanges', [ $options ] );
224 // Log the DBs and methods involved in multi-DB transactions
225 $this->logIfMultiDbTransaction();
226 // Actually perform the commit on all master DB connections and revert DBO_TRX
227 $this->forEachLBCallMethod( 'commitMasterChanges', [ $fname ] );
228 // Run all post-commit callbacks
229 /** @var Exception $e */
230 $e = null; // first callback exception
231 $this->forEachLB( function ( ILoadBalancer $lb ) use ( &$e ) {
232 $ex = $lb->runMasterPostTrxCallbacks( IDatabase::TRIGGER_COMMIT );
233 $e = $e ?: $ex;
234 } );
235 // Commit any dangling DBO_TRX transactions from callbacks on one DB to another DB
236 $this->forEachLBCallMethod( 'commitMasterChanges', [ $fname ] );
237 // Throw any last post-commit callback error
238 if ( $e instanceof Exception ) {
239 throw $e;
240 }
241 }
242
243 public function rollbackMasterChanges( $fname = __METHOD__ ) {
244 $this->trxRoundId = false;
245 $this->forEachLBCallMethod( 'suppressTransactionEndCallbacks' );
246 $this->forEachLBCallMethod( 'rollbackMasterChanges', [ $fname ] );
247 // Run all post-rollback callbacks
248 $this->forEachLB( function ( ILoadBalancer $lb ) {
249 $lb->runMasterPostTrxCallbacks( IDatabase::TRIGGER_ROLLBACK );
250 } );
251 }
252
253 public function hasTransactionRound() {
254 return ( $this->trxRoundId !== false );
255 }
256
257 /**
258 * Log query info if multi DB transactions are going to be committed now
259 */
260 private function logIfMultiDbTransaction() {
261 $callersByDB = [];
262 $this->forEachLB( function ( ILoadBalancer $lb ) use ( &$callersByDB ) {
263 $masterName = $lb->getServerName( $lb->getWriterIndex() );
264 $callers = $lb->pendingMasterChangeCallers();
265 if ( $callers ) {
266 $callersByDB[$masterName] = $callers;
267 }
268 } );
269
270 if ( count( $callersByDB ) >= 2 ) {
271 $dbs = implode( ', ', array_keys( $callersByDB ) );
272 $msg = "Multi-DB transaction [{$dbs}]:\n";
273 foreach ( $callersByDB as $db => $callers ) {
274 $msg .= "$db: " . implode( '; ', $callers ) . "\n";
275 }
276 $this->queryLogger->info( $msg );
277 }
278 }
279
280 public function hasMasterChanges() {
281 $ret = false;
282 $this->forEachLB( function ( ILoadBalancer $lb ) use ( &$ret ) {
283 $ret = $ret || $lb->hasMasterChanges();
284 } );
285
286 return $ret;
287 }
288
289 public function laggedReplicaUsed() {
290 $ret = false;
291 $this->forEachLB( function ( ILoadBalancer $lb ) use ( &$ret ) {
292 $ret = $ret || $lb->laggedReplicaUsed();
293 } );
294
295 return $ret;
296 }
297
298 public function hasOrMadeRecentMasterChanges( $age = null ) {
299 $ret = false;
300 $this->forEachLB( function ( ILoadBalancer $lb ) use ( $age, &$ret ) {
301 $ret = $ret || $lb->hasOrMadeRecentMasterChanges( $age );
302 } );
303 return $ret;
304 }
305
306 public function waitForReplication( array $opts = [] ) {
307 $opts += [
308 'domain' => false,
309 'cluster' => false,
310 'timeout' => 60,
311 'ifWritesSince' => null
312 ];
313
314 if ( $opts['domain'] === false && isset( $opts['wiki'] ) ) {
315 $opts['domain'] = $opts['wiki']; // b/c
316 }
317
318 // Figure out which clusters need to be checked
319 /** @var ILoadBalancer[] $lbs */
320 $lbs = [];
321 if ( $opts['cluster'] !== false ) {
322 $lbs[] = $this->getExternalLB( $opts['cluster'] );
323 } elseif ( $opts['domain'] !== false ) {
324 $lbs[] = $this->getMainLB( $opts['domain'] );
325 } else {
326 $this->forEachLB( function ( ILoadBalancer $lb ) use ( &$lbs ) {
327 $lbs[] = $lb;
328 } );
329 if ( !$lbs ) {
330 return; // nothing actually used
331 }
332 }
333
334 // Get all the master positions of applicable DBs right now.
335 // This can be faster since waiting on one cluster reduces the
336 // time needed to wait on the next clusters.
337 $masterPositions = array_fill( 0, count( $lbs ), false );
338 foreach ( $lbs as $i => $lb ) {
339 if ( $lb->getServerCount() <= 1 ) {
340 // T29975 - Don't try to wait for replica DBs if there are none
341 // Prevents permission error when getting master position
342 continue;
343 } elseif ( $opts['ifWritesSince']
344 && $lb->lastMasterChangeTimestamp() < $opts['ifWritesSince']
345 ) {
346 continue; // no writes since the last wait
347 }
348 $masterPositions[$i] = $lb->getMasterPos();
349 }
350
351 // Run any listener callbacks *after* getting the DB positions. The more
352 // time spent in the callbacks, the less time is spent in waitForAll().
353 foreach ( $this->replicationWaitCallbacks as $callback ) {
354 $callback();
355 }
356
357 $failed = [];
358 foreach ( $lbs as $i => $lb ) {
359 if ( $masterPositions[$i] ) {
360 // The DBMS may not support getMasterPos()
361 if ( !$lb->waitForAll( $masterPositions[$i], $opts['timeout'] ) ) {
362 $failed[] = $lb->getServerName( $lb->getWriterIndex() );
363 }
364 }
365 }
366
367 if ( $failed ) {
368 throw new DBReplicationWaitError(
369 null,
370 "Could not wait for replica DBs to catch up to " .
371 implode( ', ', $failed )
372 );
373 }
374 }
375
376 public function setWaitForReplicationListener( $name, callable $callback = null ) {
377 if ( $callback ) {
378 $this->replicationWaitCallbacks[$name] = $callback;
379 } else {
380 unset( $this->replicationWaitCallbacks[$name] );
381 }
382 }
383
384 public function getEmptyTransactionTicket( $fname ) {
385 if ( $this->hasMasterChanges() ) {
386 $this->queryLogger->error( __METHOD__ . ": $fname does not have outer scope.\n" .
387 ( new RuntimeException() )->getTraceAsString() );
388
389 return null;
390 }
391
392 return $this->ticket;
393 }
394
395 public function commitAndWaitForReplication( $fname, $ticket, array $opts = [] ) {
396 if ( $ticket !== $this->ticket ) {
397 $this->perfLogger->error( __METHOD__ . ": $fname does not have outer scope.\n" .
398 ( new RuntimeException() )->getTraceAsString() );
399
400 return;
401 }
402
403 // The transaction owner and any caller with the empty transaction ticket can commit
404 // so that getEmptyTransactionTicket() callers don't risk seeing DBTransactionError.
405 if ( $this->trxRoundId !== false && $fname !== $this->trxRoundId ) {
406 $this->queryLogger->info( "$fname: committing on behalf of {$this->trxRoundId}." );
407 $fnameEffective = $this->trxRoundId;
408 } else {
409 $fnameEffective = $fname;
410 }
411
412 $this->commitMasterChanges( $fnameEffective );
413 $this->waitForReplication( $opts );
414 // If a nested caller committed on behalf of $fname, start another empty $fname
415 // transaction, leaving the caller with the same empty transaction state as before.
416 if ( $fnameEffective !== $fname ) {
417 $this->beginMasterChanges( $fnameEffective );
418 }
419 }
420
421 public function getChronologyProtectorTouched( $dbName ) {
422 return $this->getChronologyProtector()->getTouched( $dbName );
423 }
424
425 public function disableChronologyProtection() {
426 $this->getChronologyProtector()->setEnabled( false );
427 }
428
429 /**
430 * @return ChronologyProtector
431 */
432 protected function getChronologyProtector() {
433 if ( $this->chronProt ) {
434 return $this->chronProt;
435 }
436
437 $this->chronProt = new ChronologyProtector(
438 $this->memStash,
439 [
440 'ip' => $this->requestInfo['IPAddress'],
441 'agent' => $this->requestInfo['UserAgent'],
442 ],
443 isset( $_GET['cpPosTime'] ) ? $_GET['cpPosTime'] : null
444 );
445 $this->chronProt->setLogger( $this->replLogger );
446
447 if ( $this->cliMode ) {
448 $this->chronProt->setEnabled( false );
449 } elseif ( $this->requestInfo['ChronologyProtection'] === 'false' ) {
450 // Request opted out of using position wait logic. This is useful for requests
451 // done by the job queue or background ETL that do not have a meaningful session.
452 $this->chronProt->setWaitEnabled( false );
453 }
454
455 $this->replLogger->debug( __METHOD__ . ': using request info ' .
456 json_encode( $this->requestInfo, JSON_PRETTY_PRINT ) );
457
458 return $this->chronProt;
459 }
460
461 /**
462 * Get and record all of the staged DB positions into persistent memory storage
463 *
464 * @param ChronologyProtector $cp
465 * @param callable|null $workCallback Work to do instead of waiting on syncing positions
466 * @param string $mode One of (sync, async); whether to wait on remote datacenters
467 */
468 protected function shutdownChronologyProtector(
469 ChronologyProtector $cp, $workCallback, $mode
470 ) {
471 // Record all the master positions needed
472 $this->forEachLB( function ( ILoadBalancer $lb ) use ( $cp ) {
473 $cp->shutdownLB( $lb );
474 } );
475 // Write them to the persistent stash. Try to do something useful by running $work
476 // while ChronologyProtector waits for the stash write to replicate to all DCs.
477 $unsavedPositions = $cp->shutdown( $workCallback, $mode );
478 if ( $unsavedPositions && $workCallback ) {
479 // Invoke callback in case it did not cache the result yet
480 $workCallback(); // work now to block for less time in waitForAll()
481 }
482 // If the positions failed to write to the stash, at least wait on local datacenter
483 // replica DBs to catch up before responding. Even if there are several DCs, this increases
484 // the chance that the user will see their own changes immediately afterwards. As long
485 // as the sticky DC cookie applies (same domain), this is not even an issue.
486 $this->forEachLB( function ( ILoadBalancer $lb ) use ( $unsavedPositions ) {
487 $masterName = $lb->getServerName( $lb->getWriterIndex() );
488 if ( isset( $unsavedPositions[$masterName] ) ) {
489 $lb->waitForAll( $unsavedPositions[$masterName] );
490 }
491 } );
492 }
493
494 /**
495 * Base parameters to LoadBalancer::__construct()
496 * @return array
497 */
498 final protected function baseLoadBalancerParams() {
499 return [
500 'localDomain' => $this->localDomain,
501 'readOnlyReason' => $this->readOnlyReason,
502 'srvCache' => $this->srvCache,
503 'wanCache' => $this->wanCache,
504 'profiler' => $this->profiler,
505 'trxProfiler' => $this->trxProfiler,
506 'queryLogger' => $this->queryLogger,
507 'connLogger' => $this->connLogger,
508 'replLogger' => $this->replLogger,
509 'errorLogger' => $this->errorLogger,
510 'hostname' => $this->hostname,
511 'cliMode' => $this->cliMode,
512 'agent' => $this->agent,
513 'chronologyProtector' => $this->getChronologyProtector()
514 ];
515 }
516
517 /**
518 * @param ILoadBalancer $lb
519 */
520 protected function initLoadBalancer( ILoadBalancer $lb ) {
521 if ( $this->trxRoundId !== false ) {
522 $lb->beginMasterChanges( $this->trxRoundId ); // set DBO_TRX
523 }
524 }
525
526 public function setDomainPrefix( $prefix ) {
527 $this->localDomain = new DatabaseDomain(
528 $this->localDomain->getDatabase(),
529 null,
530 $prefix
531 );
532
533 $this->forEachLB( function ( ILoadBalancer $lb ) use ( $prefix ) {
534 $lb->setDomainPrefix( $prefix );
535 } );
536 }
537
538 public function closeAll() {
539 $this->forEachLBCallMethod( 'closeAll', [] );
540 }
541
542 public function setAgentName( $agent ) {
543 $this->agent = $agent;
544 }
545
546 public function appendPreShutdownTimeAsQuery( $url, $time ) {
547 $usedCluster = 0;
548 $this->forEachLB( function ( ILoadBalancer $lb ) use ( &$usedCluster ) {
549 $usedCluster |= ( $lb->getServerCount() > 1 );
550 } );
551
552 if ( !$usedCluster ) {
553 return $url; // no master/replica clusters touched
554 }
555
556 return strpos( $url, '?' ) === false ? "$url?cpPosTime=$time" : "$url&cpPosTime=$time";
557 }
558
559 public function setRequestInfo( array $info ) {
560 $this->requestInfo = $info + $this->requestInfo;
561 }
562
563 /**
564 * Make PHP ignore user aborts/disconnects until the returned
565 * value leaves scope. This returns null and does nothing in CLI mode.
566 *
567 * @return ScopedCallback|null
568 */
569 final protected function getScopedPHPBehaviorForCommit() {
570 if ( PHP_SAPI != 'cli' ) { // https://bugs.php.net/bug.php?id=47540
571 $old = ignore_user_abort( true ); // avoid half-finished operations
572 return new ScopedCallback( function () use ( $old ) {
573 ignore_user_abort( $old );
574 } );
575 }
576
577 return null;
578 }
579
580 function __destruct() {
581 $this->destroy();
582 }
583 }
584
585 class_alias( LBFactory::class, 'LBFactory' );