r59996 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r59995‎ | r59996 | r59997 >
Date:15:38, 12 December 2009
Author:catrope
Status:ok
Tags:
Comment:
Add maintenance script for running queries in batches. This is especially useful for executing large write queries (e.g. fixing user renames, clearing huge watchlists) on the WMF cluster; before, everyone wrote their own script for this when they needed it.
Modified paths:
  • /trunk/phase3/maintenance/runBatchedQuery.php (added) (history)

Diff [purge]

Index: trunk/phase3/maintenance/runBatchedQuery.php
@@ -0,0 +1,60 @@
 2+<?php
 3+/**
 4+ * Run a database query in batches and wait for slaves. This is used on large
 5+ * wikis to prevent replication lag from going through the roof when executing
 6+ * large write queries.
 7+ *
 8+ * This program is free software; you can redistribute it and/or modify
 9+ * it under the terms of the GNU General Public License as published by
 10+ * the Free Software Foundation; either version 2 of the License, or
 11+ * (at your option) any later version.
 12+ *
 13+ * This program is distributed in the hope that it will be useful,
 14+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 16+ * GNU General Public License for more details.
 17+ *
 18+ * You should have received a copy of the GNU General Public License along
 19+ * with this program; if not, write to the Free Software Foundation, Inc.,
 20+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 21+ * http://www.gnu.org/copyleft/gpl.html
 22+ *
 23+ * @ingroup Maintenance
 24+ */
 25+
 26+require_once( dirname(__FILE__) . '/Maintenance.php' );
 27+
 28+class BatchedQueryRunner extends Maintenance {
 29+ public function __construct() {
 30+ parent::__construct();
 31+ $this->mDescription = "Run a query repeatedly until it affects 0 rows, and wait for slaves in between.\n" .
 32+ "NOTE: You need to set a LIMIT clause yourself.";
 33+ $this->addOption( 'wait', "Wait for replication lag to go down to this value. Default: 5", false, true );
 34+ }
 35+
 36+ public function execute() {
 37+ if ( !$this->hasArg() )
 38+ $this->error( "No query specified. Specify the query as a command line parameter.", true );
 39+
 40+ $query = $this->getArg();
 41+ $wait = $this->getOption( 'wait', 5 );
 42+ $n = 1;
 43+ $dbw = wfGetDb( DB_MASTER );
 44+ do {
 45+ $this->output( "Batch $n: " );
 46+ $n++;
 47+ $dbw->query( $query );
 48+ $affected = $dbw->affectedRows();
 49+ $this->output( "$affected rows\n" );
 50+ wfWaitForSlaves( $wait );
 51+ } while ( $affected > 0 );
 52+ }
 53+
 54+ protected function getDbType() {
 55+ return Maintenance::DB_ADMIN;
 56+ }
 57+}
 58+
 59+
 60+$maintClass = "BatchedQueryRunner";
 61+require_once( DO_MAINTENANCE );
Property changes on: trunk/phase3/maintenance/runBatchedQuery.php
___________________________________________________________________
Name: svn:eol-style
162 + native

Status & tagging log