From 23f8346ce73875f8d78b07b067c19198c710eefa Mon Sep 17 00:00:00 2001 From: Tim Starling Date: Mon, 29 Aug 2011 05:04:55 +0000 Subject: [PATCH] Further updates for r90643/r95647 in core and extensions. Fixed all callers of Database::getLag(), made them call a new function LoadBalancer::safeGetLag() instead, which doesn't cause fatal errors for non-replicated installations. The core updates are tested but the extension updates aren't. --- includes/db/Database.php | 8 ++++++-- includes/db/LoadBalancer.php | 19 +++++++++++++++++++ includes/specials/SpecialContributions.php | 3 ++- .../specials/SpecialDeletedContributions.php | 3 ++- includes/specials/SpecialWatchlist.php | 2 +- 5 files changed, 30 insertions(+), 5 deletions(-) diff --git a/includes/db/Database.php b/includes/db/Database.php index 6126b3559b..c5b58e5a79 100644 --- a/includes/db/Database.php +++ b/includes/db/Database.php @@ -2980,8 +2980,12 @@ abstract class DatabaseBase implements DatabaseType { } /** - * Get slave lag. - * Currently supported only by MySQL + * Get slave lag. Currently supported only by MySQL. + * + * Note that this function will generate a fatal error on many + * installations. Most callers should use LoadBalancer::safeGetLag() + * instead. + * * @return Database replication lag in seconds */ function getLag() { diff --git a/includes/db/LoadBalancer.php b/includes/db/LoadBalancer.php index 55a6cee772..da26417768 100644 --- a/includes/db/LoadBalancer.php +++ b/includes/db/LoadBalancer.php @@ -987,6 +987,25 @@ class LoadBalancer { return $this->mLagTimes; } + /** + * Get the lag in seconds for a given connection, or zero if this load + * balancer does not have replication enabled. + * + * This should be used in preference to Database::getLag() in cases where + * replication may not be in use, since there is no way to determine if + * replication is in use at the connection level without running + * potentially restricted queries such as SHOW SLAVE STATUS. Using this + * function instead of Database::getLag() avoids a fatal error in this + * case on many installations. + */ + function safeGetLag( $conn ) { + if ( $this->getServerCount() == 1 ) { + return 0; + } else { + return $conn->getLag(); + } + } + /** * Clear the cache for getLagTimes */ diff --git a/includes/specials/SpecialContributions.php b/includes/specials/SpecialContributions.php index d724cb4fde..497bd1c846 100644 --- a/includes/specials/SpecialContributions.php +++ b/includes/specials/SpecialContributions.php @@ -167,7 +167,8 @@ class SpecialContributions extends SpecialPage { $wgOut->addWikiMsg( 'nocontribs', $target ); } else { # Show a message about slave lag, if applicable - if( ( $lag = $pager->getDatabase()->getLag() ) > 0 ) + $lag = wfGetLB()->safeGetLag( $pager->getDatabase() ); + if( $lag > 0 ) $wgOut->showLagWarning( $lag ); $wgOut->addHTML( diff --git a/includes/specials/SpecialDeletedContributions.php b/includes/specials/SpecialDeletedContributions.php index e45e85fd5b..265de421d4 100644 --- a/includes/specials/SpecialDeletedContributions.php +++ b/includes/specials/SpecialDeletedContributions.php @@ -317,7 +317,8 @@ class DeletedContributionsPage extends SpecialPage { } # Show a message about slave lag, if applicable - if( ( $lag = $pager->getDatabase()->getLag() ) > 0 ) + $lag = wfGetLB()->safeGetLag( $pager->getDatabase() ); + if( $lag > 0 ) $wgOut->showLagWarning( $lag ); $wgOut->addHTML( diff --git a/includes/specials/SpecialWatchlist.php b/includes/specials/SpecialWatchlist.php index fb57baa237..51086bb1b3 100644 --- a/includes/specials/SpecialWatchlist.php +++ b/includes/specials/SpecialWatchlist.php @@ -240,7 +240,7 @@ class SpecialWatchlist extends SpecialPage { } # Show a message about slave lag, if applicable - $lag = $dbr->getLag(); + $lag = wfGetLB()->safeGetLag( $dbr ); if( $lag > 0 ) { $output->showLagWarning( $lag ); } -- 2.20.1