Localisation updates from http://translatewiki.net.
[mediawiki.git] / maintenance / fixSlaveDesync.php
blobe4e557fe532f7d16c3010ed256e29964a0c32b7b
1 <?php
2 /**
3 * Fix erroneous page_latest values due to slave desynchronisation.
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.
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.
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
20 * @file
21 * @ingroup Maintenance
24 require_once __DIR__ . '/Maintenance.php';
26 /**
27 * Maintenance script that fixes erroneous page_latest values
28 * due to slave desynchronisation.
30 * @ingroup Maintenance
32 class FixSlaveDesync extends Maintenance {
33 public function __construct() {
34 parent::__construct();
35 $this->mDescription = "";
38 public function getDbType() {
39 return Maintenance::DB_ADMIN;
42 public function execute() {
43 $this->slaveIndexes = array();
44 for ( $i = 1; $i < wfGetLB()->getServerCount(); $i++ ) {
45 if ( wfGetLB()->isNonZeroLoad( $i ) ) {
46 $this->slaveIndexes[] = $i;
50 if ( $this->hasArg() ) {
51 $this->desyncFixPage( $this->getArg() );
52 } else {
53 $corrupt = $this->findPageLatestCorruption();
54 foreach ( $corrupt as $id => $dummy ) {
55 $this->desyncFixPage( $id );
60 /**
61 * Find all pages that have a corrupted page_latest
62 * @return array
64 private function findPageLatestCorruption() {
65 $desync = array();
66 $n = 0;
67 $dbw = wfGetDB( DB_MASTER );
68 $masterIDs = array();
69 $res = $dbw->select( 'page', array( 'page_id', 'page_latest' ), array( 'page_id<6054123' ), __METHOD__ );
70 $this->output( "Number of pages: " . $res->numRows() . "\n" );
71 foreach ( $res as $row ) {
72 $masterIDs[$row->page_id] = $row->page_latest;
73 if ( !( ++$n % 10000 ) ) {
74 $this->output( "$n\r" );
77 $this->output( "\n" );
79 foreach ( $this->slaveIndexes as $i ) {
80 $db = wfGetDB( $i );
81 $res = $db->select( 'page', array( 'page_id', 'page_latest' ), array( 'page_id<6054123' ), __METHOD__ );
82 foreach ( $res as $row ) {
83 if ( isset( $masterIDs[$row->page_id] ) && $masterIDs[$row->page_id] != $row->page_latest ) {
84 $desync[$row->page_id] = true;
85 $this->output( $row->page_id . "\t" );
89 $this->output( "\n" );
90 return $desync;
93 /**
94 * Fix a broken page entry
95 * @param $pageID int The page_id to fix
97 private function desyncFixPage( $pageID ) {
98 # Check for a corrupted page_latest
99 $dbw = wfGetDB( DB_MASTER );
100 $dbw->begin( __METHOD__ );
101 $realLatest = $dbw->selectField( 'page', 'page_latest', array( 'page_id' => $pageID ),
102 __METHOD__, 'FOR UPDATE' );
103 # list( $masterFile, $masterPos ) = $dbw->getMasterPos();
104 $found = false;
105 foreach ( $this->slaveIndexes as $i ) {
106 $db = wfGetDB( $i );
108 if ( !$db->masterPosWait( $masterFile, $masterPos, 10 ) ) {
109 $this->output( "Slave is too lagged, aborting\n" );
110 $dbw->commit( __METHOD__ );
111 sleep(10);
112 return;
114 $latest = $db->selectField( 'page', 'page_latest', array( 'page_id' => $pageID ), __METHOD__ );
115 $max = $db->selectField( 'revision', 'MAX(rev_id)', false, __METHOD__ );
116 if ( $latest != $realLatest && $realLatest < $max ) {
117 $this->output( "page_latest corrupted in page $pageID, server $i\n" );
118 $found = true;
119 break;
122 if ( !$found ) {
123 $this->output( "page_id $pageID seems fine\n" );
124 $dbw->commit( __METHOD__ );
125 return;
128 # Find the missing revisions
129 $res = $dbw->select( 'revision', array( 'rev_id' ), array( 'rev_page' => $pageID ),
130 __METHOD__, 'FOR UPDATE' );
131 $masterIDs = array();
132 foreach ( $res as $row ) {
133 $masterIDs[] = $row->rev_id;
136 $res = $dbw->select( 'revision', array( 'rev_id' ), array( 'rev_page' => $pageID ), __METHOD__ );
137 $slaveIDs = array();
138 foreach ( $res as $row ) {
139 $slaveIDs[] = $row->rev_id;
141 if ( count( $masterIDs ) < count( $slaveIDs ) ) {
142 $missingIDs = array_diff( $slaveIDs, $masterIDs );
143 if ( count( $missingIDs ) ) {
144 $this->output( "Found " . count( $missingIDs ) . " lost in master, copying from slave... " );
145 $dbFrom = $dbw;
146 $found = true;
147 $toMaster = true;
148 } else {
149 $found = false;
151 } else {
152 $missingIDs = array_diff( $masterIDs, $slaveIDs );
153 if ( count( $missingIDs ) ) {
154 $this->output( "Found " . count( $missingIDs ) . " missing revision(s), copying from master... " );
155 $dbFrom = $dbw;
156 $found = true;
157 $toMaster = false;
158 } else {
159 $found = false;
163 if ( $found ) {
164 foreach ( $missingIDs as $rid ) {
165 $this->output( "$rid " );
166 # Revision
167 $row = $dbFrom->selectRow( 'revision', '*', array( 'rev_id' => $rid ), __METHOD__ );
168 if ( $toMaster ) {
169 $id = $dbw->selectField( 'revision', 'rev_id', array( 'rev_id' => $rid ),
170 __METHOD__, 'FOR UPDATE' );
171 if ( $id ) {
172 $this->output( "Revision already exists\n" );
173 $found = false;
174 break;
175 } else {
176 $dbw->insert( 'revision', get_object_vars( $row ), __METHOD__, 'IGNORE' );
178 } else {
179 foreach ( $this->slaveIndexes as $i ) {
180 $db = wfGetDB( $i );
181 $db->insert( 'revision', get_object_vars( $row ), __METHOD__, 'IGNORE' );
185 # Text
186 $row = $dbFrom->selectRow( 'text', '*', array( 'old_id' => $row->rev_text_id ), __METHOD__ );
187 if ( $toMaster ) {
188 $dbw->insert( 'text', get_object_vars( $row ), __METHOD__, 'IGNORE' );
189 } else {
190 foreach ( $this->slaveIndexes as $i ) {
191 $db = wfGetDB( $i );
192 $db->insert( 'text', get_object_vars( $row ), __METHOD__, 'IGNORE' );
196 $this->output( "done\n" );
199 if ( $found ) {
200 $this->output( "Fixing page_latest... " );
201 if ( $toMaster ) {
202 # $dbw->update( 'page', array( 'page_latest' => $realLatest ), array( 'page_id' => $pageID ), __METHOD__ );
203 } else {
204 foreach ( $this->slaveIndexes as $i ) {
205 $db = wfGetDB( $i );
206 $db->update( 'page', array( 'page_latest' => $realLatest ), array( 'page_id' => $pageID ), __METHOD__ );
209 $this->output( "done\n" );
211 $dbw->commit( __METHOD__ );
215 $maintClass = "FixSlaveDesync";
216 require_once RUN_MAINTENANCE_IF_MAIN;