Use wfGetLB()->getServerCount() to get the server count, not $wgDBservers
[lhc/web/wiklou.git] / maintenance / fixSlaveDesync.php
1 <?php
2 /**
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.
7 *
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.
12 *
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
17 *
18 * @ingroup Maintenance
19 */
20
21 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
22
23 class FixSlaveDesync extends Maintenance {
24 public function __construct() {
25 parent::__construct();
26 $this->mDescription = "";
27
28 }
29
30 public function execute() {
31 $this->slaveIndexes = array();
32 for ( $i = 1; $i < wfGetLB()->getServerCount(); $i++ ) {
33 if ( wfGetLB()->isNonZeroLoad( $i ) ) {
34 $this->slaveIndexes[] = $i;
35 }
36 }
37
38 if ( $this->hasArg() ) {
39 $this->desyncFixPage( $this->getArg() );
40 } else {
41 $dbw = wfGetDB( DB_MASTER );
42 $maxPage = $dbw->selectField( 'page', 'MAX(page_id)', false, __METHOD__ );
43 $corrupt = $this->findPageLatestCorruption();
44 foreach ( $corrupt as $id => $dummy ) {
45 $this->desyncFixPage( $id );
46 }
47 }
48 }
49
50 /**
51 * Find all pages that have a corrupted page_latest
52 * @return array
53 */
54 private function findPageLatestCorruption() {
55 $desync = array();
56 $n = 0;
57 $dbw = wfGetDB( DB_MASTER );
58 $masterIDs = array();
59 $res = $dbw->select( 'page', array( 'page_id', 'page_latest' ), array( 'page_id<6054123' ), __METHOD__ );
60 $this->output( "Number of pages: " . $dbw->numRows( $res ) . "\n" );
61 foreach ( $res as $row ) {
62 $masterIDs[$row->page_id] = $row->page_latest;
63 if ( !( ++$n % 10000 ) ) {
64 $this->output( "$n\r" );
65 }
66 }
67 $this->output( "\n" );
68
69 foreach ( $this->slaveIndexes as $i ) {
70 $db = wfGetDB( $i );
71 $res = $db->select( 'page', array( 'page_id', 'page_latest' ), array( 'page_id<6054123' ), __METHOD__ );
72 foreach ( $res as $row ) {
73 if ( isset( $masterIDs[$row->page_id] ) && $masterIDs[$row->page_id] != $row->page_latest ) {
74 $desync[$row->page_id] = true;
75 $this->output( $row->page_id . "\t" );
76 }
77 }
78 }
79 $this->output( "\n" );
80 return $desync;
81 }
82
83 /**
84 * Fix a broken page entry
85 * @param $pageID int The page_id to fix
86 */
87 private function desyncFixPage( $pageID ) {
88 # Check for a corrupted page_latest
89 $dbw = wfGetDB( DB_MASTER );
90 $dbw->begin();
91 $realLatest = $dbw->selectField( 'page', 'page_latest', array( 'page_id' => $pageID ),
92 __METHOD__, 'FOR UPDATE' );
93 # list( $masterFile, $masterPos ) = $dbw->getMasterPos();
94 $found = false;
95 foreach ( $this->slaveIndexes as $i ) {
96 $db = wfGetDB( $i );
97 /*
98 if ( !$db->masterPosWait( $masterFile, $masterPos, 10 ) ) {
99 $this->output( "Slave is too lagged, aborting\n" );
100 $dbw->commit();
101 sleep(10);
102 return;
103 }*/
104 $latest = $db->selectField( 'page', 'page_latest', array( 'page_id' => $pageID ), __METHOD__ );
105 $max = $db->selectField( 'revision', 'MAX(rev_id)', false, __METHOD__ );
106 if ( $latest != $realLatest && $realLatest < $max ) {
107 $this->output( "page_latest corrupted in page $pageID, server $i\n" );
108 $found = true;
109 break;
110 }
111 }
112 if ( !$found ) {
113 $this->output( "page_id $pageID seems fine\n" );
114 $dbw->commit();
115 return;
116 }
117
118 # Find the missing revisions
119 $res = $dbw->select( 'revision', array( 'rev_id' ), array( 'rev_page' => $pageID ),
120 __METHOD__, 'FOR UPDATE' );
121 $masterIDs = array();
122 foreach ( $res as $row ) {
123 $masterIDs[] = $row->rev_id;
124 }
125
126 $res = $db->select( 'revision', array( 'rev_id' ), array( 'rev_page' => $pageID ), __METHOD__ );
127 $slaveIDs = array();
128 foreach ( $res as $row ) {
129 $slaveIDs[] = $row->rev_id;
130 }
131 if ( count( $masterIDs ) < count( $slaveIDs ) ) {
132 $missingIDs = array_diff( $slaveIDs, $masterIDs );
133 if ( count( $missingIDs ) ) {
134 $this->output( "Found " . count( $missingIDs ) . " lost in master, copying from slave... " );
135 $dbFrom = $db;
136 $found = true;
137 $toMaster = true;
138 } else {
139 $found = false;
140 }
141 } else {
142 $missingIDs = array_diff( $masterIDs, $slaveIDs );
143 if ( count( $missingIDs ) ) {
144 $this->output( "Found " . count( $missingIDs ) . " missing revision(s), copying from master... " );
145 $dbFrom = $dbw;
146 $found = true;
147 $toMaster = false;
148 } else {
149 $found = false;
150 }
151 }
152
153 if ( $found ) {
154 foreach ( $missingIDs as $rid ) {
155 $this->output( "$rid " );
156 # Revision
157 $row = $dbFrom->selectRow( 'revision', '*', array( 'rev_id' => $rid ), __METHOD__ );
158 if ( $toMaster ) {
159 $id = $dbw->selectField( 'revision', 'rev_id', array( 'rev_id' => $rid ),
160 __METHOD__, 'FOR UPDATE' );
161 if ( $id ) {
162 $this->output( "Revision already exists\n" );
163 $found = false;
164 break;
165 } else {
166 $dbw->insert( 'revision', get_object_vars( $row ), __METHOD__, 'IGNORE' );
167 }
168 } else {
169 foreach ( $this->slaveIndexes as $i ) {
170 $db = wfGetDB( $i );
171 $db->insert( 'revision', get_object_vars( $row ), __METHOD__, 'IGNORE' );
172 }
173 }
174
175 # Text
176 $row = $dbFrom->selectRow( 'text', '*', array( 'old_id' => $row->rev_text_id ), __METHOD__ );
177 if ( $toMaster ) {
178 $dbw->insert( 'text', get_object_vars( $row ), __METHOD__, 'IGNORE' );
179 } else {
180 foreach ( $this->slaveIndexes as $i ) {
181 $db = wfGetDB( $i );
182 $db->insert( 'text', get_object_vars( $row ), __METHOD__, 'IGNORE' );
183 }
184 }
185 }
186 $this->output( "done\n" );
187 }
188
189 if ( $found ) {
190 $this->output( "Fixing page_latest... " );
191 if ( $toMaster ) {
192 # $dbw->update( 'page', array( 'page_latest' => $realLatest ), array( 'page_id' => $pageID ), __METHOD__ );
193 } else {
194 foreach ( $this->slaveIndexes as $i ) {
195 $db = wfGetDB( $i );
196 $db->update( 'page', array( 'page_latest' => $realLatest ), array( 'page_id' => $pageID ), __METHOD__ );
197 }
198 }
199 $this->output( "done\n" );
200 }
201 $dbw->commit();
202 }
203 }
204
205 $maintClass = "FixSlaveDesync";
206 require_once( DO_MAINTENANCE );