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
{
34 private $slaveIndexes;
36 public function __construct() {
37 parent
::__construct();
38 $this->mDescription
= "";
41 public function getDbType() {
42 return Maintenance
::DB_ADMIN
;
45 public function execute() {
46 $this->slaveIndexes
= array();
47 $serverCount = wfGetLB()->getServerCount();
48 for ( $i = 1; $i < $serverCount; $i++
) {
49 if ( wfGetLB()->isNonZeroLoad( $i ) ) {
50 $this->slaveIndexes
[] = $i;
54 if ( $this->hasArg() ) {
55 $this->desyncFixPage( $this->getArg() );
57 $corrupt = $this->findPageLatestCorruption();
58 foreach ( $corrupt as $id => $dummy ) {
59 $this->desyncFixPage( $id );
65 * Find all pages that have a corrupted page_latest
68 private function findPageLatestCorruption() {
71 $dbw = wfGetDB( DB_MASTER
);
75 array( 'page_id', 'page_latest' ),
76 array( 'page_id<6054123' ),
79 $this->output( "Number of pages: " . $res->numRows() . "\n" );
80 foreach ( $res as $row ) {
81 $masterIDs[$row->page_id
] = $row->page_latest
;
82 if ( !( ++
$n %
10000 ) ) {
83 $this->output( "$n\r" );
86 $this->output( "\n" );
88 foreach ( $this->slaveIndexes
as $i ) {
92 array( 'page_id', 'page_latest' ),
93 array( 'page_id<6054123' ),
96 foreach ( $res as $row ) {
97 if ( isset( $masterIDs[$row->page_id
] ) && $masterIDs[$row->page_id
] != $row->page_latest
) {
98 $desync[$row->page_id
] = true;
99 $this->output( $row->page_id
. "\t" );
103 $this->output( "\n" );
109 * Fix a broken page entry
110 * @param int $pageID The page_id to fix
112 private function desyncFixPage( $pageID ) {
113 # Check for a corrupted page_latest
114 $dbw = wfGetDB( DB_MASTER
);
115 $dbw->begin( __METHOD__
);
116 $realLatest = $dbw->selectField( 'page', 'page_latest', array( 'page_id' => $pageID ),
117 __METHOD__
, 'FOR UPDATE' );
118 # list( $masterFile, $masterPos ) = $dbw->getMasterPos();
120 foreach ( $this->slaveIndexes
as $i ) {
123 if ( !$db->masterPosWait( $masterFile, $masterPos, 10 ) ) {
124 $this->output( "Slave is too lagged, aborting\n" );
125 $dbw->commit( __METHOD__ );
129 $latest = $db->selectField( 'page', 'page_latest', array( 'page_id' => $pageID ), __METHOD__
);
130 $max = $db->selectField( 'revision', 'MAX(rev_id)', false, __METHOD__
);
131 if ( $latest != $realLatest && $realLatest < $max ) {
132 $this->output( "page_latest corrupted in page $pageID, server $i\n" );
138 $this->output( "page_id $pageID seems fine\n" );
139 $dbw->commit( __METHOD__
);
144 # Find the missing revisions
145 $res = $dbw->select( 'revision', array( 'rev_id' ), array( 'rev_page' => $pageID ),
146 __METHOD__
, 'FOR UPDATE' );
147 $masterIDs = array();
148 foreach ( $res as $row ) {
149 $masterIDs[] = $row->rev_id
;
152 $res = $dbw->select( 'revision', array( 'rev_id' ), array( 'rev_page' => $pageID ), __METHOD__
);
154 foreach ( $res as $row ) {
155 $slaveIDs[] = $row->rev_id
;
157 if ( count( $masterIDs ) < count( $slaveIDs ) ) {
158 $missingIDs = array_diff( $slaveIDs, $masterIDs );
159 if ( count( $missingIDs ) ) {
160 $this->output( "Found " . count( $missingIDs )
161 . " lost in master, copying from slave... " );
169 $missingIDs = array_diff( $masterIDs, $slaveIDs );
170 if ( count( $missingIDs ) ) {
171 $this->output( "Found " . count( $missingIDs )
172 . " missing revision(s), copying from master... " );
182 foreach ( $missingIDs as $rid ) {
183 $this->output( "$rid " );
185 $row = $dbFrom->selectRow( 'revision', '*', array( 'rev_id' => $rid ), __METHOD__
);
187 $id = $dbw->selectField( 'revision', 'rev_id', array( 'rev_id' => $rid ),
188 __METHOD__
, 'FOR UPDATE' );
190 $this->output( "Revision already exists\n" );
194 $dbw->insert( 'revision', get_object_vars( $row ), __METHOD__
, 'IGNORE' );
197 foreach ( $this->slaveIndexes
as $i ) {
199 $db->insert( 'revision', get_object_vars( $row ), __METHOD__
, 'IGNORE' );
204 $row = $dbFrom->selectRow( 'text', '*', array( 'old_id' => $row->rev_text_id
), __METHOD__
);
206 $dbw->insert( 'text', get_object_vars( $row ), __METHOD__
, 'IGNORE' );
208 foreach ( $this->slaveIndexes
as $i ) {
210 $db->insert( 'text', get_object_vars( $row ), __METHOD__
, 'IGNORE' );
214 $this->output( "done\n" );
218 $this->output( "Fixing page_latest... " );
223 array( 'page_latest' => $realLatest ),
224 array( 'page_id' => $pageID ),
229 foreach ( $this->slaveIndexes
as $i ) {
233 array( 'page_latest' => $realLatest ),
234 array( 'page_id' => $pageID ),
239 $this->output( "done\n" );
241 $dbw->commit( __METHOD__
);
245 $maintClass = "FixSlaveDesync";
246 require_once RUN_MAINTENANCE_IF_MAIN
;