MediaWiki  REL1_31
mcc.php
Go to the documentation of this file.
1 <?php
25 $optionsWithArgs = [ 'cache' ];
27  'debug', 'help'
28 ];
29 require_once __DIR__ . '/commandLine.inc';
30 
31 $debug = isset( $options['debug'] );
32 $help = isset( $options['help'] );
33 $cache = isset( $options['cache'] ) ? $options['cache'] : null;
34 
35 if ( $help ) {
36  mccShowUsage();
37  exit( 0 );
38 }
40  'persistent' => true,
41  'debug' => $debug,
42 ] );
43 
44 if ( $cache ) {
45  if ( !isset( $wgObjectCaches[$cache] ) ) {
46  print "MediaWiki isn't configured with a cache named '$cache'";
47  exit( 1 );
48  }
49  $servers = $wgObjectCaches[$cache]['servers'];
50 } elseif ( $wgMainCacheType === CACHE_MEMCACHED ) {
51  $mcc->set_servers( $wgMemCachedServers );
52 } elseif ( isset( $wgObjectCaches[$wgMainCacheType]['servers'] ) ) {
53  $mcc->set_servers( $wgObjectCaches[$wgMainCacheType]['servers'] );
54 } else {
55  print "MediaWiki isn't configured for Memcached usage\n";
56  exit( 1 );
57 }
58 
62 function mccShowUsage() {
63  echo <<<EOF
64 Usage:
65  mcc.php [--debug]
66  mcc.php --help
67 
68 MemCached Command (mcc) is an interactive command tool that let you interact
69 with the MediaWiki memcached cache.
70 
71 Options:
72  --debug Set debug mode on the memcached connection.
73  --help This help screen.
74 
75 Interactive commands:
76 
77 EOF;
78  print "\t";
79  print str_replace( "\n", "\n\t", mccGetHelp( false ) );
80  print "\n";
81 }
82 
83 function mccGetHelp( $command ) {
84  $output = '';
85  $commandList = [
86  'get' => 'grabs something',
87  'getsock' => 'lists sockets',
88  'set' => 'changes something',
89  'delete' => 'deletes something',
90  'history' => 'show command line history',
91  'server' => 'show current memcached server',
92  'dumpmcc' => 'shows the whole thing',
93  'exit' => 'exit mcc',
94  'quit' => 'exit mcc',
95  'help' => 'help about a command',
96  ];
97  if ( !$command ) {
98  $command = 'fullhelp';
99  }
100  if ( $command === 'fullhelp' ) {
101  $max_cmd_len = max( array_map( 'strlen', array_keys( $commandList ) ) );
102  foreach ( $commandList as $cmd => $desc ) {
103  $output .= sprintf( "%-{$max_cmd_len}s: %s\n", $cmd, $desc );
104  }
105  } elseif ( isset( $commandList[$command] ) ) {
106  $output .= "$command: $commandList[$command]\n";
107  } else {
108  $output .= "$command: command does not exist or no help for it\n";
109  }
110 
111  return $output;
112 }
113 
114 do {
115  $bad = false;
116  $showhelp = false;
117  $quit = false;
118 
120  if ( $line === false ) {
121  exit;
122  }
123 
124  $args = explode( ' ', $line );
125  $command = array_shift( $args );
126 
127  // process command
128  switch ( $command ) {
129  case 'help':
130  // show an help message
131  print mccGetHelp( array_shift( $args ) );
132  break;
133 
134  case 'get':
135  $sub = '';
136  if ( array_key_exists( 1, $args ) ) {
137  $sub = $args[1];
138  }
139  print "Getting {$args[0]}[$sub]\n";
140  $res = $mcc->get( $args[0] );
141  if ( array_key_exists( 1, $args ) ) {
142  $res = $res[$args[1]];
143  }
144  if ( $res === false ) {
145  # print 'Error: ' . $mcc->error_string() . "\n";
146  print "MemCached error\n";
147  } elseif ( is_string( $res ) ) {
148  print "$res\n";
149  } else {
150  var_dump( $res );
151  }
152  break;
153 
154  case 'getsock':
155  $res = $mcc->get( $args[0] );
156  $sock = $mcc->get_sock( $args[0] );
157  var_dump( $sock );
158  break;
159 
160  case 'server':
161  if ( $mcc->_single_sock !== null ) {
162  print $mcc->_single_sock . "\n";
163  break;
164  }
165  $res = $mcc->get( $args[0] );
166  $hv = $mcc->_hashfunc( $args[0] );
167  for ( $i = 0; $i < 3; $i++ ) {
168  print $mcc->_buckets[$hv % $mcc->_bucketcount] . "\n";
169  $hv += $mcc->_hashfunc( $i . $args[0] );
170  }
171  break;
172 
173  case 'set':
174  $key = array_shift( $args );
175  if ( $args[0] == "#" && is_numeric( $args[1] ) ) {
176  $value = str_repeat( '*', $args[1] );
177  } else {
178  $value = implode( ' ', $args );
179  }
180  if ( !$mcc->set( $key, $value, 0 ) ) {
181  # print 'Error: ' . $mcc->error_string() . "\n";
182  print "MemCached error\n";
183  }
184  break;
185 
186  case 'delete':
187  $key = implode( ' ', $args );
188  if ( !$mcc->delete( $key ) ) {
189  # print 'Error: ' . $mcc->error_string() . "\n";
190  print "MemCached error\n";
191  }
192  break;
193 
194  case 'history':
195  if ( function_exists( 'readline_list_history' ) ) {
196  foreach ( readline_list_history() as $num => $line ) {
197  print "$num: $line\n";
198  }
199  } else {
200  print "readline_list_history() not available\n";
201  }
202  break;
203 
204  case 'dumpmcc':
205  var_dump( $mcc );
206  break;
207 
208  case 'quit':
209  case 'exit':
210  $quit = true;
211  break;
212 
213  default:
214  $bad = true;
215  } // switch() end
216 
217  if ( $bad ) {
218  if ( $command ) {
219  print "Bad command\n";
220  }
221  } else {
222  if ( function_exists( 'readline_add_history' ) ) {
223  readline_add_history( $line );
224  }
225  }
226 } while ( !$quit );
$optionsWithArgs
$optionsWithArgs
Definition: mcc.php:25
$output
static configuration should be added through ResourceLoaderGetConfigVars instead can be used to get the real title after the basic globals have been set but before ordinary actions take place $output
Definition: hooks.txt:2255
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:1524
CACHE_MEMCACHED
const CACHE_MEMCACHED
Definition: Defines.php:114
$res
$res
Definition: database.txt:21
$mcc
if( $help) $mcc
Definition: mcc.php:39
cache
you have access to all of the normal MediaWiki so you can get a DB use the cache
Definition: maintenance.txt:55
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:37
MemcachedClient
memcached client class implemented using (p)fsockopen()
Definition: MemcachedClient.php:79
$debug
$debug
Definition: mcc.php:31
memcached
MediaWiki has optional support for memcached
Definition: memcached.txt:1
MediaWiki
A helper class for throttling authentication attempts.
$wgObjectCaches
$wgObjectCaches
Advanced object cache configuration.
Definition: DefaultSettings.php:2319
$args
if( $line===false) $args
Definition: mcc.php:124
$wgMemCachedServers
$wgMemCachedServers
Definition: memcached.txt:64
$options
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options
Definition: hooks.txt:2001
$value
$value
Definition: styleTest.css.php:45
mccShowUsage
mccShowUsage()
Show this command line tool usage.
Definition: mcc.php:62
$line
$line
Definition: mcc.php:119
print
print
Definition: opensearch_desc.php:46
$command
$command
Definition: mcc.php:125
$cache
$cache
Definition: mcc.php:33
mccGetHelp
mccGetHelp( $command)
Definition: mcc.php:83
$quit
$quit
Definition: mcc.php:117
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
$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:22
connection
you have access to all of the normal MediaWiki so you can get a DB connection
Definition: maintenance.txt:55
$showhelp
$showhelp
Definition: mcc.php:116
$help
$help
Definition: mcc.php:32
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:13
$optionsWithoutArgs
$optionsWithoutArgs
Definition: mcc.php:26