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