3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
18 * @ingroup Maintenance
21 require_once( dirname( __FILE__
) . '/Maintenance.php' );
23 class FixSlaveDesync
extends Maintenance
{
24 public function __construct() {
25 parent
::__construct();
26 $this->mDescription
= "";
29 public function getDbType() {
30 return self
::DB_ADMIN
;
33 public function execute() {
34 $this->slaveIndexes
= array();
35 for ( $i = 1; $i < wfGetLB()->getServerCount(); $i++
) {
36 if ( wfGetLB()->isNonZeroLoad( $i ) ) {
37 $this->slaveIndexes
[] = $i;
41 if ( $this->hasArg() ) {
42 $this->desyncFixPage( $this->getArg() );
44 $dbw = wfGetDB( DB_MASTER
);
45 $maxPage = $dbw->selectField( 'page', 'MAX(page_id)', false, __METHOD__
);
46 $corrupt = $this->findPageLatestCorruption();
47 foreach ( $corrupt as $id => $dummy ) {
48 $this->desyncFixPage( $id );
54 * Find all pages that have a corrupted page_latest
57 private function findPageLatestCorruption() {
60 $dbw = wfGetDB( DB_MASTER
);
62 $res = $dbw->select( 'page', array( 'page_id', 'page_latest' ), array( 'page_id<6054123' ), __METHOD__
);
63 $this->output( "Number of pages: " . $dbw->numRows( $res ) . "\n" );
64 foreach ( $res as $row ) {
65 $masterIDs[$row->page_id
] = $row->page_latest
;
66 if ( !( ++
$n %
10000 ) ) {
67 $this->output( "$n\r" );
70 $this->output( "\n" );
72 foreach ( $this->slaveIndexes
as $i ) {
74 $res = $db->select( 'page', array( 'page_id', 'page_latest' ), array( 'page_id<6054123' ), __METHOD__
);
75 foreach ( $res as $row ) {
76 if ( isset( $masterIDs[$row->page_id
] ) && $masterIDs[$row->page_id
] != $row->page_latest
) {
77 $desync[$row->page_id
] = true;
78 $this->output( $row->page_id
. "\t" );
82 $this->output( "\n" );
87 * Fix a broken page entry
88 * @param $pageID int The page_id to fix
90 private function desyncFixPage( $pageID ) {
91 # Check for a corrupted page_latest
92 $dbw = wfGetDB( DB_MASTER
);
94 $realLatest = $dbw->selectField( 'page', 'page_latest', array( 'page_id' => $pageID ),
95 __METHOD__
, 'FOR UPDATE' );
96 # list( $masterFile, $masterPos ) = $dbw->getMasterPos();
98 foreach ( $this->slaveIndexes
as $i ) {
101 if ( !$db->masterPosWait( $masterFile, $masterPos, 10 ) ) {
102 $this->output( "Slave is too lagged, aborting\n" );
107 $latest = $db->selectField( 'page', 'page_latest', array( 'page_id' => $pageID ), __METHOD__
);
108 $max = $db->selectField( 'revision', 'MAX(rev_id)', false, __METHOD__
);
109 if ( $latest != $realLatest && $realLatest < $max ) {
110 $this->output( "page_latest corrupted in page $pageID, server $i\n" );
116 $this->output( "page_id $pageID seems fine\n" );
121 # Find the missing revisions
122 $res = $dbw->select( 'revision', array( 'rev_id' ), array( 'rev_page' => $pageID ),
123 __METHOD__
, 'FOR UPDATE' );
124 $masterIDs = array();
125 foreach ( $res as $row ) {
126 $masterIDs[] = $row->rev_id
;
129 $res = $db->select( 'revision', array( 'rev_id' ), array( 'rev_page' => $pageID ), __METHOD__
);
131 foreach ( $res as $row ) {
132 $slaveIDs[] = $row->rev_id
;
134 if ( count( $masterIDs ) < count( $slaveIDs ) ) {
135 $missingIDs = array_diff( $slaveIDs, $masterIDs );
136 if ( count( $missingIDs ) ) {
137 $this->output( "Found " . count( $missingIDs ) . " lost in master, copying from slave... " );
145 $missingIDs = array_diff( $masterIDs, $slaveIDs );
146 if ( count( $missingIDs ) ) {
147 $this->output( "Found " . count( $missingIDs ) . " missing revision(s), copying from master... " );
157 foreach ( $missingIDs as $rid ) {
158 $this->output( "$rid " );
160 $row = $dbFrom->selectRow( 'revision', '*', array( 'rev_id' => $rid ), __METHOD__
);
162 $id = $dbw->selectField( 'revision', 'rev_id', array( 'rev_id' => $rid ),
163 __METHOD__
, 'FOR UPDATE' );
165 $this->output( "Revision already exists\n" );
169 $dbw->insert( 'revision', get_object_vars( $row ), __METHOD__
, 'IGNORE' );
172 foreach ( $this->slaveIndexes
as $i ) {
174 $db->insert( 'revision', get_object_vars( $row ), __METHOD__
, 'IGNORE' );
179 $row = $dbFrom->selectRow( 'text', '*', array( 'old_id' => $row->rev_text_id
), __METHOD__
);
181 $dbw->insert( 'text', get_object_vars( $row ), __METHOD__
, 'IGNORE' );
183 foreach ( $this->slaveIndexes
as $i ) {
185 $db->insert( 'text', get_object_vars( $row ), __METHOD__
, 'IGNORE' );
189 $this->output( "done\n" );
193 $this->output( "Fixing page_latest... " );
195 # $dbw->update( 'page', array( 'page_latest' => $realLatest ), array( 'page_id' => $pageID ), __METHOD__ );
197 foreach ( $this->slaveIndexes
as $i ) {
199 $db->update( 'page', array( 'page_latest' => $realLatest ), array( 'page_id' => $pageID ), __METHOD__
);
202 $this->output( "done\n" );
208 $maintClass = "FixSlaveDesync";
209 require_once( DO_MAINTENANCE
);