r94404 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r94403‎ | r94404 | r94405 >
Date:10:46, 13 August 2011
Author:catrope
Status:resolved (Comments)
Tags:
Comment:
Hopefully prevent race conditions in ArticleFeedback by reading from the master rather than the slave and using LOCK IN SHARE MODE. These race conditions probably caused bug 30227. Hopefully this will prevent that from happening again.
Modified paths:
  • /trunk/extensions/ArticleFeedback/api/ApiArticleFeedback.php (modified) (history)
  • /trunk/extensions/ArticleFeedback/populateAFRevisions.php (modified) (history)

Diff [purge]

Index: trunk/extensions/ArticleFeedback/populateAFRevisions.php
@@ -40,12 +40,11 @@
4141
4242 $lastRevID = 0;
4343 $i = 0;
44 - $dbr = wfGetDB( DB_SLAVE );
4544 $dbw = wfGetDB( DB_MASTER );
4645 $this->output( "Reading data from article_feedback ...\n" );
4746 while ( true ) {
4847 // Get the next revision ID
49 - $row = $dbr->selectRow( 'article_feedback', array( 'aa_revision', 'aa_page_id' ),
 48+ $row = $dbw->selectRow( 'article_feedback', array( 'aa_revision', 'aa_page_id' ),
5049 "aa_revision > $lastRevID", __METHOD__,
5150 array( 'ORDER BY' => 'aa_revision', 'LIMIT' => 1 )
5251 );
@@ -57,7 +56,7 @@
5857 $pageid = intval( $row->aa_page_id );
5958
6059 // Get all article_feedback rows for this revision
61 - $res = $dbr->select( 'article_feedback',
 60+ $res = $dbw->select( 'article_feedback',
6261 array( 'aa_rating_id', 'aa_rating_value', 'aa_user_id', 'aa_user_anon_token' ),
6362 array( 'aa_revision' => $revid ),
6463 __METHOD__
Index: trunk/extensions/ArticleFeedback/api/ApiArticleFeedback.php
@@ -37,11 +37,12 @@
3838 $this->dieUsage( 'ArticleFeedback is not enabled on this page', 'invalidpage' );
3939 }
4040
41 - $dbr = wfGetDB( DB_SLAVE );
 41+ $dbw = wfGetDB( DB_MASTER );
4242
4343 // Query the latest ratings by this user for this page,
4444 // possibly for an older revision
45 - $res = $dbr->select(
 45+ // Select from the master to prevent replag-induced bugs
 46+ $res = $dbw->select(
4647 'article_feedback',
4748 array( 'aa_rating_id', 'aa_rating_value', 'aa_revision' ),
4849 array(
@@ -55,6 +56,7 @@
5657 array(
5758 'ORDER BY' => 'aa_revision DESC',
5859 'LIMIT' => count( $wgArticleFeedbackRatings ),
 60+ 'LOCK IN SHARE MODE', // Prevent race conditions
5961 )
6062 );
6163

Follow-up revisions

RevisionCommit summaryAuthorDate
r94509Followup r94404: remove LOCK IN SHARE MODE because locking reads are evil, pe...catrope13:06, 15 August 2011
r945621.17wmf1: MFT r93711, r94346, r94369, r94376, r94404, r94502, r94509, r94511catrope20:30, 15 August 2011
r95628MFT to REL1_18 (extensions)...hashar15:32, 28 August 2011
r96722Epic followup to r94404, r94509: change the ArticleFeedback schema such that ...catrope15:26, 10 September 2011

Comments

#Comment by Tim Starling (talk | contribs)   12:45, 15 August 2011

Years ago, I tested LOCK IN SHARE MODE on InnoDB and found that it generates a deadlock pretty much every time there are two threads doing the same thing at the same time. I was unable to find a use for it apart from error generation.

Maybe it's better in MySQL 5.1, but I wouldn't count on it. I think it's better to design your schema in a way that avoids locking reads, that's what I said in docs/database.txt.

Status & tagging log