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
21 * @ingroup Maintenance
24 require_once __DIR__
. '/Maintenance.php';
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() );
53 $corrupt = $this->findPageLatestCorruption();
54 foreach ( $corrupt as $id => $dummy ) {
55 $this->desyncFixPage( $id );
61 * Find all pages that have a corrupted page_latest
64 private function findPageLatestCorruption() {
67 $dbw = wfGetDB( DB_MASTER
);
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 ) {
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" );
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();
105 foreach ( $this->slaveIndexes
as $i ) {
108 if ( !$db->masterPosWait( $masterFile, $masterPos, 10 ) ) {
109 $this->output( "Slave is too lagged, aborting\n" );
110 $dbw->commit( __METHOD__ );
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" );
123 $this->output( "page_id $pageID seems fine\n" );
124 $dbw->commit( __METHOD__
);
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__
);
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... " );
152 $missingIDs = array_diff( $masterIDs, $slaveIDs );
153 if ( count( $missingIDs ) ) {
154 $this->output( "Found " . count( $missingIDs ) . " missing revision(s), copying from master... " );
164 foreach ( $missingIDs as $rid ) {
165 $this->output( "$rid " );
167 $row = $dbFrom->selectRow( 'revision', '*', array( 'rev_id' => $rid ), __METHOD__
);
169 $id = $dbw->selectField( 'revision', 'rev_id', array( 'rev_id' => $rid ),
170 __METHOD__
, 'FOR UPDATE' );
172 $this->output( "Revision already exists\n" );
176 $dbw->insert( 'revision', get_object_vars( $row ), __METHOD__
, 'IGNORE' );
179 foreach ( $this->slaveIndexes
as $i ) {
181 $db->insert( 'revision', get_object_vars( $row ), __METHOD__
, 'IGNORE' );
186 $row = $dbFrom->selectRow( 'text', '*', array( 'old_id' => $row->rev_text_id
), __METHOD__
);
188 $dbw->insert( 'text', get_object_vars( $row ), __METHOD__
, 'IGNORE' );
190 foreach ( $this->slaveIndexes
as $i ) {
192 $db->insert( 'text', get_object_vars( $row ), __METHOD__
, 'IGNORE' );
196 $this->output( "done\n" );
200 $this->output( "Fixing page_latest... " );
202 # $dbw->update( 'page', array( 'page_latest' => $realLatest ), array( 'page_id' => $pageID ), __METHOD__ );
204 foreach ( $this->slaveIndexes
as $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
;