MediaWiki  1.23.8
mcc.php
Go to the documentation of this file.
1 <?php
26 require_once __DIR__ . '/commandLine.inc';
27 
28 $options = getopt( '', array( 'debug', 'help', 'cache:' ) );
29 
30 $debug = isset( $options['debug'] );
31 $help = isset( $options['help'] );
32 $cache = isset( $options['cache'] ) ? $options['cache'] : null;
33 
34 if ( $help ) {
35  mccShowUsage();
36  exit( 0 );
37 }
39  'persistent' => true,
40  'debug' => $debug,
41 ) );
42 
43 if ( $cache ) {
44  if ( !isset( $wgObjectCaches[$cache] ) ) {
45  print "MediaWiki isn't configured with a cache named '$cache'";
46  exit( 1 );
47  }
48  $servers = $wgObjectCaches[$cache]['servers'];
49 } elseif ( $wgMainCacheType === CACHE_MEMCACHED ) {
50  $mcc->set_servers( $wgMemCachedServers );
51 } elseif ( isset( $wgObjectCaches[$wgMainCacheType]['servers'] ) ) {
52  $mcc->set_servers( $wgObjectCaches[$wgMainCacheType]['servers'] );
53 } else {
54  print "MediaWiki isn't configured for Memcached usage\n";
55  exit( 1 );
56 }
57 
61 function mccShowUsage() {
62  echo <<<EOF
63 Usage:
64  mcc.php [--debug]
65  mcc.php --help
66 
67 MemCached Command (mcc) is an interactive command tool that let you interact
68 with the MediaWiki memcached cache.
69 
70 Options:
71  --debug Set debug mode on the memcached connection.
72  --help This help screen.
73 
74 Interactive commands:
75 
76 EOF;
77  print "\t";
78  print str_replace( "\n", "\n\t", mccGetHelp( false ) );
79  print "\n";
80 }
81 
82 function mccGetHelp( $command ) {
83  $output = '';
84  $commandList = array(
85  'get' => 'grabs something',
86  'getsock' => 'lists sockets',
87  'set' => 'changes something',
88  'delete' => 'deletes something',
89  'history' => 'show command line history',
90  'server' => 'show current memcached server',
91  'dumpmcc' => 'shows the whole thing',
92  'exit' => 'exit mcc',
93  'quit' => 'exit mcc',
94  'help' => 'help about a command',
95  );
96  if ( !$command ) {
97  $command = 'fullhelp';
98  }
99  if ( $command === 'fullhelp' ) {
100  $max_cmd_len = max( array_map( 'strlen', array_keys( $commandList ) ) );
101  foreach ( $commandList as $cmd => $desc ) {
102  $output .= sprintf( "%-{$max_cmd_len}s: %s\n", $cmd, $desc );
103  }
104  } elseif ( isset( $commandList[$command] ) ) {
105  $output .= "$command: $commandList[$command]\n";
106  } else {
107  $output .= "$command: command does not exist or no help for it\n";
108  }
109 
110  return $output;
111 }
112 
113 do {
114  $bad = false;
115  $showhelp = false;
116  $quit = false;
117 
119  if ( $line === false ) {
120  exit;
121  }
122 
123  $args = explode( ' ', $line );
124  $command = array_shift( $args );
125 
126  // process command
127  switch ( $command ) {
128  case 'help':
129  // show an help message
130  print mccGetHelp( array_shift( $args ) );
131  break;
132 
133  case 'get':
134  $sub = '';
135  if ( array_key_exists( 1, $args ) ) {
136  $sub = $args[1];
137  }
138  print "Getting {$args[0]}[$sub]\n";
139  $res = $mcc->get( $args[0] );
140  if ( array_key_exists( 1, $args ) ) {
141  $res = $res[$args[1]];
142  }
143  if ( $res === false ) {
144  # print 'Error: ' . $mcc->error_string() . "\n";
145  print "MemCached error\n";
146  } elseif ( is_string( $res ) ) {
147  print "$res\n";
148  } else {
149  var_dump( $res );
150  }
151  break;
152 
153  case 'getsock':
154  $res = $mcc->get( $args[0] );
155  $sock = $mcc->get_sock( $args[0] );
156  var_dump( $sock );
157  break;
158 
159  case 'server':
160  if ( $mcc->_single_sock !== null ) {
161  print $mcc->_single_sock . "\n";
162  break;
163  }
164  $res = $mcc->get( $args[0] );
165  $hv = $mcc->_hashfunc( $args[0] );
166  for ( $i = 0; $i < 3; $i++ ) {
167  print $mcc->_buckets[$hv % $mcc->_bucketcount] . "\n";
168  $hv += $mcc->_hashfunc( $i . $args[0] );
169  }
170  break;
171 
172  case 'set':
173  $key = array_shift( $args );
174  if ( $args[0] == "#" && is_numeric( $args[1] ) ) {
175  $value = str_repeat( '*', $args[1] );
176  } else {
177  $value = implode( ' ', $args );
178  }
179  if ( !$mcc->set( $key, $value, 0 ) ) {
180  # print 'Error: ' . $mcc->error_string() . "\n";
181  print "MemCached error\n";
182  }
183  break;
184 
185  case 'delete':
186  $key = implode( ' ', $args );
187  if ( !$mcc->delete( $key ) ) {
188  # print 'Error: ' . $mcc->error_string() . "\n";
189  print "MemCached error\n";
190  }
191  break;
192 
193  case 'history':
194  if ( function_exists( 'readline_list_history' ) ) {
195  foreach ( readline_list_history() as $num => $line ) {
196  print "$num: $line\n";
197  }
198  } else {
199  print "readline_list_history() not available\n";
200  }
201  break;
202 
203  case 'dumpmcc':
204  var_dump( $mcc );
205  break;
206 
207  case 'quit':
208  case 'exit':
209  $quit = true;
210  break;
211 
212  default:
213  $bad = true;
214  } // switch() end
215 
216  if ( $bad ) {
217  if ( $command ) {
218  print "Bad command\n";
219  }
220  } else {
221  if ( function_exists( 'readline_add_history' ) ) {
222  readline_add_history( $line );
223  }
224  }
225 } while ( !$quit );
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
MWMemcached
This is the PHP client for memcached - a distributed memory cache daemon.
Definition: MemcachedClient.php:76
is
We use the convention $dbr for read and $dbw for write to help you keep track of whether the database object is a the world will explode Or to be a subsequent write query which succeeded on the master may fail when replicated to the slave due to a unique key collision Replication on the slave will stop and it may take hours to repair the database and get it back online Setting read_only in my cnf on the slave will avoid this but given the dire we prefer to have as many checks as possible We provide a but the wrapper functions like please read the documentation for except in special pages derived from QueryPage It s a common pitfall for new developers to submit code containing SQL queries which examine huge numbers of rows Remember that COUNT * is(N), counting rows in atable is like counting beans in a bucket.------------------------------------------------------------------------ Replication------------------------------------------------------------------------The largest installation of MediaWiki, Wikimedia, uses a large set ofslave MySQL servers replicating writes made to a master MySQL server. Itis important to understand the issues associated with this setup if youwant to write code destined for Wikipedia.It 's often the case that the best algorithm to use for a given taskdepends on whether or not replication is in use. Due to our unabashedWikipedia-centrism, we often just use the replication-friendly version, but if you like, you can use wfGetLB() ->getServerCount() > 1 tocheck to see if replication is in use.===Lag===Lag primarily occurs when large write queries are sent to the master.Writes on the master are executed in parallel, but they are executed inserial when they are replicated to the slaves. The master writes thequery to the binlog when the transaction is committed. The slaves pollthe binlog and start executing the query as soon as it appears. They canservice reads while they are performing a write query, but will not readanything more from the binlog and thus will perform no more writes. Thismeans that if the write query runs for a long time, the slaves will lagbehind the master for the time it takes for the write query to complete.Lag can be exacerbated by high read load. MediaWiki 's load balancer willstop sending reads to a slave when it is lagged by more than 30 seconds.If the load ratios are set incorrectly, or if there is too much loadgenerally, this may lead to a slave permanently hovering around 30seconds lag.If all slaves are lagged by more than 30 seconds, MediaWiki will stopwriting to the database. All edits and other write operations will berefused, with an error returned to the user. This gives the slaves achance to catch up. Before we had this mechanism, the slaves wouldregularly lag by several minutes, making review of recent editsdifficult.In addition to this, MediaWiki attempts to ensure that the user seesevents occurring on the wiki in chronological order. A few seconds of lagcan be tolerated, as long as the user sees a consistent picture fromsubsequent requests. This is done by saving the master binlog positionin the session, and then at the start of each request, waiting for theslave to catch up to that position before doing any reads from it. Ifthis wait times out, reads are allowed anyway, but the request isconsidered to be in "lagged slave mode". Lagged slave mode can bechecked by calling wfGetLB() ->getLaggedSlaveMode(). The onlypractical consequence at present is a warning displayed in the pagefooter.===Lag avoidance===To avoid excessive lag, queries which write large numbers of rows shouldbe split up, generally to write one row at a time. Multi-row INSERT ...SELECT queries are the worst offenders should be avoided altogether.Instead do the select first and then the insert.===Working with lag===Despite our best efforts, it 's not practical to guarantee a low-lagenvironment. Lag will usually be less than one second, but mayoccasionally be up to 30 seconds. For scalability, it 's very importantto keep load on the master low, so simply sending all your queries tothe master is not the answer. So when you have a genuine need forup-to-date data, the following approach is advised:1) Do a quick query to the master for a sequence number or timestamp 2) Run the full query on the slave and check if it matches the data you gotfrom the master 3) If it doesn 't, run the full query on the masterTo avoid swamping the master every time the slaves lag, use of thisapproach should be kept to a minimum. In most cases you should just readfrom the slave and let the user deal with the delay.------------------------------------------------------------------------ Lock contention------------------------------------------------------------------------Due to the high write rate on Wikipedia(and some other wikis), MediaWiki developers need to be very careful to structure their writesto avoid long-lasting locks. By default, MediaWiki opens a transactionat the first query, and commits it before the output is sent. Locks willbe held from the time when the query is done until the commit. So youcan reduce lock time by doing as much processing as possible before youdo your write queries.Often this approach is not good enough, and it becomes necessary toenclose small groups of queries in their own transaction. Use thefollowing syntax:$dbw=wfGetDB(DB_MASTER
Maintenance\readconsole
static readconsole( $prompt='> ')
Prompt the console for input.
Definition: Maintenance.php:1134
CACHE_MEMCACHED
const CACHE_MEMCACHED
Definition: Defines.php:114
$mcc
if( $help) $mcc
Definition: mcc.php:38
cache
you have access to all of the normal MediaWiki so you can get a DB use the cache
Definition: maintenance.txt:52
$debug
$debug
Definition: mcc.php:30
memcached
MediaWiki has optional support for memcached
Definition: memcached.txt:1
MediaWiki
$args
if( $line===false) $args
Definition: mcc.php:123
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
$wgMemCachedServers
$wgMemCachedServers
Definition: memcached.txt:64
$value
$value
Definition: styleTest.css.php:45
mccShowUsage
mccShowUsage()
Show this command line tool usage.
Definition: mcc.php:61
$line
$line
Definition: mcc.php:118
$options
$options
Definition: mcc.php:28
$command
$command
Definition: mcc.php:124
$cache
$cache
Definition: mcc.php:32
mccGetHelp
mccGetHelp( $command)
Definition: mcc.php:82
$quit
$quit
Definition: mcc.php:116
on
We ve cleaned up the code here by removing clumps of infrequently used code and moving them off somewhere else It s much easier for someone working with this code to see what s _really_ going on
Definition: hooks.txt:86
$output
& $output
Definition: hooks.txt:375
$wgMainCacheType
CACHE_MEMCACHED $wgMainCacheType
Definition: memcached.txt:63
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
connection
you have access to all of the normal MediaWiki so you can get a DB connection
Definition: maintenance.txt:52
$showhelp
$showhelp
Definition: mcc.php:115
$help
$help
Definition: mcc.php:31
that
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global then executing the whole list after the page is displayed We don t do anything smart like collating updates to the same table or such because the list is almost always going to have just one item on if that
Definition: deferred.txt:11
$res
$res
Definition: database.txt:21