MediaWiki  master
ChronologyProtector.php
Go to the documentation of this file.
1 <?php
24 namespace Wikimedia\Rdbms;
25 
31 
39 class ChronologyProtector implements LoggerAwareInterface {
41  protected $store;
43  protected $logger;
44 
46  protected $key;
48  protected $clientId;
50  protected $clientLogInfo;
52  protected $waitForPosIndex;
54  protected $waitForPosStoreTimeout = self::POS_STORE_WAIT_TIMEOUT;
56  protected $enabled = true;
58  protected $wait = true;
59 
61  protected $initialized = false;
63  protected $startupPositions = [];
65  protected $shutdownPositions = [];
67  protected $shutdownTouchDBs = [];
68 
70  const POSITION_TTL = 60;
72  const POSITION_COOKIE_TTL = 10;
74  const POS_STORE_WAIT_TIMEOUT = 5;
75 
83  public function __construct( BagOStuff $store, array $client, $posIndex, $secret = '' ) {
84  $this->store = $store;
85  if ( isset( $client['clientId'] ) ) {
86  $this->clientId = $client['clientId'];
87  } else {
88  $this->clientId = ( $secret != '' )
89  ? hash_hmac( 'md5', $client['ip'] . "\n" . $client['agent'], $secret )
90  : md5( $client['ip'] . "\n" . $client['agent'] );
91  }
92  $this->key = $store->makeGlobalKey( __CLASS__, $this->clientId, 'v2' );
93  $this->waitForPosIndex = $posIndex;
94 
95  $this->clientLogInfo = [
96  'clientIP' => $client['ip'],
97  'clientAgent' => $client['agent'],
98  'clientId' => $client['clientId'] ?? null
99  ];
100 
101  $this->logger = new NullLogger();
102  }
103 
104  public function setLogger( LoggerInterface $logger ) {
105  $this->logger = $logger;
106  }
107 
112  public function getClientId() {
113  return $this->clientId;
114  }
115 
120  public function setEnabled( $enabled ) {
121  $this->enabled = $enabled;
122  }
123 
128  public function setWaitEnabled( $enabled ) {
129  $this->wait = $enabled;
130  }
131 
146  if ( !$this->enabled ) {
147  return; // disabled
148  }
149 
150  $masterName = $lb->getServerName( $lb->getWriterIndex() );
152 
153  $pos = $startupPositions[$masterName] ?? null;
154  if ( $pos instanceof DBMasterPos ) {
155  $this->logger->debug( __METHOD__ . ": pos for DB '$masterName' set to '$pos'\n" );
156  $lb->waitFor( $pos );
157  }
158  }
159 
171  if ( !$this->enabled ) {
172  return; // disabled
173  } elseif ( !$lb->hasOrMadeRecentMasterChanges( INF ) ) {
174  // Only save the position if writes have been done on the connection
175  return;
176  }
177 
178  $masterName = $lb->getServerName( $lb->getWriterIndex() );
179  if ( $lb->hasStreamingReplicaServers() ) {
180  $pos = $lb->getReplicaResumePos();
181  if ( $pos ) {
182  $this->logger->debug( __METHOD__ . ": LB for '$masterName' has pos $pos\n" );
183  $this->shutdownPositions[$masterName] = $pos;
184  }
185  } else {
186  $this->logger->debug( __METHOD__ . ": DB '$masterName' touched\n" );
187  }
188  $this->shutdownTouchDBs[$masterName] = 1;
189  }
190 
200  public function shutdown( callable $workCallback = null, $mode = 'sync', &$cpIndex = null ) {
201  if ( !$this->enabled ) {
202  return [];
203  }
204 
206  // Some callers might want to know if a user recently touched a DB.
207  // These writes do not need to block on all datacenters receiving them.
208  foreach ( $this->shutdownTouchDBs as $dbName => $unused ) {
209  $store->set(
210  $this->getTouchedKey( $this->store, $dbName ),
211  microtime( true ),
212  $store::TTL_DAY
213  );
214  }
215 
216  if ( $this->shutdownPositions === [] ) {
217  $this->logger->debug( __METHOD__ . ": no master positions to save\n" );
218 
219  return []; // nothing to save
220  }
221 
222  $this->logger->debug(
223  __METHOD__ . ": saving master pos for " .
224  implode( ', ', array_keys( $this->shutdownPositions ) ) . "\n"
225  );
226 
227  // CP-protected writes should overwhelmingly go to the master datacenter, so use a
228  // DC-local lock to merge the values. Use a DC-local get() and a synchronous all-DC
229  // set(). This makes it possible for the BagOStuff class to write in parallel to all
230  // DCs with one RTT. The use of WRITE_SYNC avoids needing READ_LATEST for the get().
231  if ( $store->lock( $this->key, 3 ) ) {
232  if ( $workCallback ) {
233  // Let the store run the work before blocking on a replication sync barrier.
234  // If replication caught up while the work finished, the barrier will be fast.
235  $store->addBusyCallback( $workCallback );
236  }
237  $ok = $store->set(
238  $this->key,
239  $this->mergePositions(
240  $store->get( $this->key ),
242  $cpIndex
243  ),
244  self::POSITION_TTL,
245  ( $mode === 'sync' ) ? $store::WRITE_SYNC : 0
246  );
247  $store->unlock( $this->key );
248  } else {
249  $ok = false;
250  }
251 
252  if ( !$ok ) {
253  $cpIndex = null; // nothing saved
254  $bouncedPositions = $this->shutdownPositions;
255  // Raced out too many times or stash is down
256  $this->logger->warning( __METHOD__ . ": failed to save master pos for " .
257  implode( ', ', array_keys( $this->shutdownPositions ) ) . "\n"
258  );
259  } elseif ( $mode === 'sync' &&
260  $store->getQoS( $store::ATTR_SYNCWRITES ) < $store::QOS_SYNCWRITES_BE
261  ) {
262  // Positions may not be in all datacenters, force LBFactory to play it safe
263  $this->logger->info( __METHOD__ . ": store may not support synchronous writes." );
264  $bouncedPositions = $this->shutdownPositions;
265  } else {
266  $bouncedPositions = [];
267  }
268 
269  return $bouncedPositions;
270  }
271 
277  public function getTouched( $dbName ) {
278  return $this->store->get( $this->getTouchedKey( $this->store, $dbName ) );
279  }
280 
286  private function getTouchedKey( BagOStuff $store, $dbName ) {
287  return $store->makeGlobalKey( __CLASS__, 'mtime', $this->clientId, $dbName );
288  }
289 
293  protected function getStartupMasterPositions() {
294  if ( $this->initialized ) {
296  }
297 
298  $this->initialized = true;
299  $this->logger->debug( __METHOD__ . ": client ID is {$this->clientId} (read)\n" );
300 
301  if ( $this->wait ) {
302  // If there is an expectation to see master positions from a certain write
303  // index or higher, then block until it appears, or until a timeout is reached.
304  // Since the write index restarts each time the key is created, it is possible that
305  // a lagged store has a matching key write index. However, in that case, it should
306  // already be expired and thus treated as non-existing, maintaining correctness.
307  if ( $this->waitForPosIndex > 0 ) {
308  $data = null;
309  $indexReached = null; // highest index reached in the position store
310  $loop = new WaitConditionLoop(
311  function () use ( &$data, &$indexReached ) {
312  $data = $this->store->get( $this->key );
313  if ( !is_array( $data ) ) {
314  return WaitConditionLoop::CONDITION_CONTINUE; // not found yet
315  } elseif ( !isset( $data['writeIndex'] ) ) {
316  return WaitConditionLoop::CONDITION_REACHED; // b/c
317  }
318  $indexReached = max( $data['writeIndex'], $indexReached );
319 
320  return ( $data['writeIndex'] >= $this->waitForPosIndex )
321  ? WaitConditionLoop::CONDITION_REACHED
322  : WaitConditionLoop::CONDITION_CONTINUE;
323  },
325  );
326  $result = $loop->invoke();
327  $waitedMs = $loop->getLastWaitTime() * 1e3;
328 
329  if ( $result == $loop::CONDITION_REACHED ) {
330  $this->logger->debug(
331  __METHOD__ . ": expected and found position index.",
332  [
333  'cpPosIndex' => $this->waitForPosIndex,
334  'waitTimeMs' => $waitedMs
335  ] + $this->clientLogInfo
336  );
337  } else {
338  $this->logger->warning(
339  __METHOD__ . ": expected but failed to find position index.",
340  [
341  'cpPosIndex' => $this->waitForPosIndex,
342  'indexReached' => $indexReached,
343  'waitTimeMs' => $waitedMs
344  ] + $this->clientLogInfo
345  );
346  }
347  } else {
348  $data = $this->store->get( $this->key );
349  }
350 
351  $this->startupPositions = $data ? $data['positions'] : [];
352  $this->logger->debug( __METHOD__ . ": key is {$this->key} (read)\n" );
353  } else {
354  $this->startupPositions = [];
355  $this->logger->debug( __METHOD__ . ": key is {$this->key} (unread)\n" );
356  }
357 
359  }
360 
367  protected function mergePositions( $curValue, array $shutdownPositions, &$cpIndex = null ) {
369  $curPositions = $curValue['positions'] ?? [];
370  // Use the newest positions for each DB master
371  foreach ( $shutdownPositions as $db => $pos ) {
372  if (
373  !isset( $curPositions[$db] ) ||
374  !( $curPositions[$db] instanceof DBMasterPos ) ||
375  $pos->asOfTime() > $curPositions[$db]->asOfTime()
376  ) {
377  $curPositions[$db] = $pos;
378  }
379  }
380 
381  $cpIndex = $curValue['writeIndex'] ?? 0;
382 
383  return [
384  'positions' => $curPositions,
385  'writeIndex' => ++$cpIndex
386  ];
387  }
388 }
lock( $key, $timeout=6, $expiry=6, $rclass='')
Acquire an advisory lock on a key string.
waitFor( $pos)
Set the master position to reach before the next generic group DB handle query.
shutdown(callable $workCallback=null, $mode='sync', &$cpIndex=null)
Notify the ChronologyProtector that the LBFactory is done calling shutdownLB() for now...
unlock( $key)
Release an advisory lock on a key string.
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
string [] $clientLogInfo
Map of client information fields for logging.
either a unescaped string or a HtmlArmor object after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation use $formDescriptor instead default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message key
Definition: hooks.txt:2139
DBMasterPos [] $startupPositions
Map of (DB master name => position)
bool $wait
Whether to check and wait on positions.
The index of the header message $result[1]=The index of the body text message $result[2 through n]=Parameters passed to body text message. Please note the header message cannot receive/use parameters. 'ImgAuthModifyHeaders':Executed just before a file is streamed to a user via img_auth.php, allowing headers to be modified beforehand. $title:LinkTarget object & $headers:HTTP headers(name=> value, names are case insensitive). Two headers get special handling:If-Modified-Since(value must be a valid HTTP date) and Range(must be of the form "bytes=(\*-\*)") will be honored when streaming the file. 'ImportHandleLogItemXMLTag':When parsing a XML tag in a log item. Return false to stop further processing of the tag $reader:XMLReader object $logInfo:Array of information 'ImportHandlePageXMLTag':When parsing a XML tag in a page. Return false to stop further processing of the tag $reader:XMLReader object & $pageInfo:Array of information 'ImportHandleRevisionXMLTag':When parsing a XML tag in a page revision. Return false to stop further processing of the tag $reader:XMLReader object $pageInfo:Array of page information $revisionInfo:Array of revision information 'ImportHandleToplevelXMLTag':When parsing a top level XML tag. Return false to stop further processing of the tag $reader:XMLReader object 'ImportHandleUnknownUser':When a user doesn 't exist locally, this hook is called to give extensions an opportunity to auto-create it. If the auto-creation is successful, return false. $name:User name 'ImportHandleUploadXMLTag':When parsing a XML tag in a file upload. Return false to stop further processing of the tag $reader:XMLReader object $revisionInfo:Array of information 'ImportLogInterwikiLink':Hook to change the interwiki link used in log entries and edit summaries for transwiki imports. & $fullInterwikiPrefix:Interwiki prefix, may contain colons. & $pageTitle:String that contains page title. 'ImportSources':Called when reading from the $wgImportSources configuration variable. Can be used to lazy-load the import sources list. & $importSources:The value of $wgImportSources. Modify as necessary. See the comment in DefaultSettings.php for the detail of how to structure this array. 'InfoAction':When building information to display on the action=info page. $context:IContextSource object & $pageInfo:Array of information 'InitializeArticleMaybeRedirect':MediaWiki check to see if title is a redirect. & $title:Title object for the current page & $request:WebRequest & $ignoreRedirect:boolean to skip redirect check & $target:Title/string of redirect target & $article:Article object 'InternalParseBeforeLinks':during Parser 's internalParse method before links but after nowiki/noinclude/includeonly/onlyinclude and other processings. & $parser:Parser object & $text:string containing partially parsed text & $stripState:Parser 's internal StripState object 'InternalParseBeforeSanitize':during Parser 's internalParse method just before the parser removes unwanted/dangerous HTML tags and after nowiki/noinclude/includeonly/onlyinclude and other processings. Ideal for syntax-extensions after template/parser function execution which respect nowiki and HTML-comments. & $parser:Parser object & $text:string containing partially parsed text & $stripState:Parser 's internal StripState object 'InterwikiLoadPrefix':When resolving if a given prefix is an interwiki or not. Return true without providing an interwiki to continue interwiki search. $prefix:interwiki prefix we are looking for. & $iwData:output array describing the interwiki with keys iw_url, iw_local, iw_trans and optionally iw_api and iw_wikiid. 'InvalidateEmailComplete':Called after a user 's email has been invalidated successfully. $user:user(object) whose email is being invalidated 'IRCLineURL':When constructing the URL to use in an IRC notification. Callee may modify $url and $query, URL will be constructed as $url . $query & $url:URL to index.php & $query:Query string $rc:RecentChange object that triggered url generation 'IsFileCacheable':Override the result of Article::isFileCacheable()(if true) & $article:article(object) being checked 'IsTrustedProxy':Override the result of IP::isTrustedProxy() & $ip:IP being check & $result:Change this value to override the result of IP::isTrustedProxy() 'IsUploadAllowedFromUrl':Override the result of UploadFromUrl::isAllowedUrl() $url:URL used to upload from & $allowed:Boolean indicating if uploading is allowed for given URL 'isValidEmailAddr':Override the result of Sanitizer::validateEmail(), for instance to return false if the domain name doesn 't match your organization. $addr:The e-mail address entered by the user & $result:Set this and return false to override the internal checks 'isValidPassword':Override the result of User::isValidPassword() $password:The password entered by the user & $result:Set this and return false to override the internal checks $user:User the password is being validated for 'Language::getMessagesFileName':$code:The language code or the language we 're looking for a messages file for & $file:The messages file path, you can override this to change the location. 'LanguageGetNamespaces':Provide custom ordering for namespaces or remove namespaces. Do not use this hook to add namespaces. Use CanonicalNamespaces for that. & $namespaces:Array of namespaces indexed by their numbers 'LanguageGetTranslatedLanguageNames':Provide translated language names. & $names:array of language code=> language name $code:language of the preferred translations 'LanguageLinks':Manipulate a page 's language links. This is called in various places to allow extensions to define the effective language links for a page. $title:The page 's Title. & $links:Array with elements of the form "language:title" in the order that they will be output. & $linkFlags:Associative array mapping prefixed links to arrays of flags. Currently unused, but planned to provide support for marking individual language links in the UI, e.g. for featured articles. 'LanguageSelector':Hook to change the language selector available on a page. $out:The output page. $cssClassName:CSS class name of the language selector. 'LinkBegin':DEPRECATED since 1.28! Use HtmlPageLinkRendererBegin instead. Used when generating internal and interwiki links in Linker::link(), before processing starts. Return false to skip default processing and return $ret. See documentation for Linker::link() for details on the expected meanings of parameters. $skin:the Skin object $target:the Title that the link is pointing to & $html:the contents that the< a > tag should have(raw HTML) $result
Definition: hooks.txt:1970
float [] $shutdownTouchDBs
Map of (DB master name => 1)
applySessionReplicationPosition(ILoadBalancer $lb)
Apply the "session consistency" DB replication position to a new ILoadBalancer.
getServerName( $i)
Get the host name or IP address of the server with the specified index.
Helper class for mitigating DB replication lag in order to provide "session consistency".
bool $enabled
Whether to no-op all method calls.
set( $key, $value, $exptime=0, $flags=0)
Set an item.
__construct(BagOStuff $store, array $client, $posIndex, $secret='')
An object representing a master or replica DB position in a replicated setup.
Definition: DBMasterPos.php:12
mergePositions( $curValue, array $shutdownPositions, &$cpIndex=null)
getTouchedKey(BagOStuff $store, $dbName)
int $waitForPosStoreTimeout
Max seconds to wait on positions to appear.
storeSessionReplicationPosition(ILoadBalancer $lb)
Save the "session consistency" DB replication position for an end-of-life ILoadBalancer.
hasStreamingReplicaServers()
Whether any replica servers use streaming replication from the master server.
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:767
bool $initialized
Whether the client data was loaded.
int null $waitForPosIndex
Expected minimum index of the last write to the position store.
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
makeGlobalKey( $class,... $components)
Make a global cache key.
getQoS( $flag)
Definition: BagOStuff.php:463
hasOrMadeRecentMasterChanges( $age=null)
Check if this load balancer object had any recent or still pending writes issued against it by this P...
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:35
getWriterIndex()
Get the server index of the master server.
string $clientId
Hash of client parameters.
Database cluster connection, tracking, load balancing, and transaction manager interface.
get( $key, $flags=0)
Get an item with the given key.
DBMasterPos [] $shutdownPositions
Map of (DB master name => position)
addBusyCallback(callable $workCallback)
Let a callback be run to avoid wasting time on special blocking calls.
getStartupMasterPositions()
Load in previous master positions for the client.
getReplicaResumePos()
Get the highest DB replication position for chronology control purposes.