r47360 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r47359‎ | r47360 | r47361 >
Date:14:06, 17 February 2009
Author:tstarling
Status:ok (Comments)
Tags:
Comment:
* Skip COMMIT query when no write queries have been done.
* Improvement to ChronologyProtector: only record the master position if a write query was done. This should help to avoid the worst of the ChronologyProtector side-effects, such as having action=raw CSS requests block for a time equivalent to the slave lag on every page view, while maintaining the benefits, like preventing a 404 from being displayed after page creation.
Modified paths:
  • /trunk/phase3/includes/db/Database.php (modified) (history)
  • /trunk/phase3/includes/db/LBFactory.php (modified) (history)
  • /trunk/phase3/includes/db/LoadBalancer.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/db/Database.php
@@ -26,6 +26,7 @@
2727 #------------------------------------------------------------------------------
2828
2929 protected $mLastQuery = '';
 30+ protected $mDoneWrites = false;
3031 protected $mPHPError = false;
3132
3233 protected $mServer, $mUser, $mPassword, $mConn = null, $mDBname;
@@ -210,8 +211,15 @@
211212 * @return String
212213 */
213214 function lastQuery() { return $this->mLastQuery; }
214 -
 215+
 216+
215217 /**
 218+ * Returns true if the connection may have been used for write queries.
 219+ * Should return true if unsure.
 220+ */
 221+ function doneWrites() { return $this->mDoneWrites; }
 222+
 223+ /**
216224 * Is a connection to the database open?
217225 * @return Boolean
218226 */
@@ -493,6 +501,14 @@
494502 }
495503
496504 /**
 505+ * Determine whether a query writes to the DB.
 506+ * Should return true if unsure.
 507+ */
 508+ function isWriteQuery( $sql ) {
 509+ return !preg_match( '/^(?:SELECT|BEGIN|COMMIT|SET|SHOW)\b/i', $sql );
 510+ }
 511+
 512+ /**
497513 * Usually aborts on failure. If errors are explicitly ignored, returns success.
498514 *
499515 * @param $sql String: SQL query
@@ -527,6 +543,11 @@
528544 }
529545
530546 $this->mLastQuery = $sql;
 547+ if ( !$this->mDoneWrites && $this->isWriteQuery( $sql ) ) {
 548+ // Set a flag indicating that writes have been done
 549+ wfDebug( __METHOD__.": Writes done: $sql\n" );
 550+ $this->mDoneWrites = true;
 551+ }
531552
532553 # Add a comment for easy SHOW PROCESSLIST interpretation
533554 #if ( $fname ) {
Index: trunk/phase3/includes/db/LBFactory.php
@@ -236,15 +236,25 @@
237237 * @param LoadBalancer $lb
238238 */
239239 function shutdownLB( $lb ) {
240 - if ( session_id() != '' && $lb->getServerCount() > 1 ) {
241 - $masterName = $lb->getServerName( 0 );
242 - if ( !isset( $this->shutdownPos[$masterName] ) ) {
243 - $pos = $lb->getMasterPos();
244 - $info = $lb->parentInfo();
245 - wfDebug( __METHOD__.": LB " . $info['id'] . " has master pos $pos\n" );
246 - $this->shutdownPos[$masterName] = $pos;
247 - }
 240+ // Don't start a session, don't bother with non-replicated setups
 241+ if ( strval( session_id() ) == '' || $lb->getServerCount() <= 1 ) {
 242+ return;
248243 }
 244+ $masterName = $lb->getServerName( 0 );
 245+ if ( isset( $this->shutdownPos[$masterName] ) ) {
 246+ // Already done
 247+ return;
 248+ }
 249+ // Only save the position if writes have been done on the connection
 250+ $db = $lb->getAnyOpenConnection( 0 );
 251+ $info = $lb->parentInfo();
 252+ if ( !$db || !$db->doneWrites() ) {
 253+ wfDebug( __METHOD__.": LB {$info['id']}, no writes done\n" );
 254+ return;
 255+ }
 256+ $pos = $db->getMasterPos();
 257+ wfDebug( __METHOD__.": LB {$info['id']} has master pos $pos\n" );
 258+ $this->shutdownPos[$masterName] = $pos;
249259 }
250260
251261 /**
Index: trunk/phase3/includes/db/LoadBalancer.php
@@ -824,7 +824,7 @@
825825 continue;
826826 }
827827 foreach ( $conns2[$masterIndex] as $conn ) {
828 - if ( $conn->lastQuery() != '' ) {
 828+ if ( $conn->doneWrites() ) {
829829 $conn->commit();
830830 }
831831 }

Comments

#Comment by Brion VIBBER (talk | contribs)   19:01, 17 February 2009

Yay!

Status & tagging log