MediaWiki  1.27.2
ChronologyProtector.php
Go to the documentation of this file.
1 <?php
30  protected $store;
31 
33  protected $key;
35  protected $client;
37  protected $enabled = true;
39  protected $wait = true;
40 
42  protected $initialized = false;
44  protected $startupPositions = [];
46  protected $shutdownPositions = [];
47 
53  public function __construct( BagOStuff $store, array $client ) {
54  $this->store = $store;
55  $this->client = $client;
56  $this->key = $store->makeGlobalKey(
57  'ChronologyProtector',
58  md5( $client['ip'] . "\n" . $client['agent'] )
59  );
60  }
61 
66  public function setEnabled( $enabled ) {
67  $this->enabled = $enabled;
68  }
69 
74  public function setWaitEnabled( $enabled ) {
75  $this->wait = $enabled;
76  }
77 
89  public function initLB( LoadBalancer $lb ) {
90  if ( !$this->enabled || $lb->getServerCount() <= 1 ) {
91  return; // non-replicated setup or disabled
92  }
93 
94  $this->initPositions();
95 
96  $masterName = $lb->getServerName( $lb->getWriterIndex() );
97  if ( !empty( $this->startupPositions[$masterName] ) ) {
98  $info = $lb->parentInfo();
99  $pos = $this->startupPositions[$masterName];
100  wfDebugLog( 'replication', __METHOD__ .
101  ": LB '" . $info['id'] . "' waiting for master pos $pos\n" );
102  $lb->waitFor( $pos );
103  }
104  }
105 
113  public function shutdownLB( LoadBalancer $lb ) {
114  if ( !$this->enabled || $lb->getServerCount() <= 1 ) {
115  return; // non-replicated setup or disabled
116  }
117 
118  $info = $lb->parentInfo();
119  $masterName = $lb->getServerName( $lb->getWriterIndex() );
120 
121  // Only save the position if writes have been done on the connection
122  $db = $lb->getAnyOpenConnection( $lb->getWriterIndex() );
123  if ( !$db || !$db->doneWrites() ) {
124  wfDebugLog( 'replication', __METHOD__ . ": LB {$info['id']}, no writes done\n" );
125 
126  return; // nothing to do
127  }
128 
129  $pos = $db->getMasterPos();
130  wfDebugLog( 'replication', __METHOD__ . ": LB {$info['id']} has master pos $pos\n" );
131  $this->shutdownPositions[$masterName] = $pos;
132  }
133 
140  public function shutdown() {
141  if ( !$this->enabled || !count( $this->shutdownPositions ) ) {
142  return true; // nothing to save
143  }
144 
145  wfDebugLog( 'replication',
146  __METHOD__ . ": saving master pos for " .
147  implode( ', ', array_keys( $this->shutdownPositions ) ) . "\n"
148  );
149 
151  $ok = $this->store->merge(
152  $this->key,
153  function ( $store, $key, $curValue ) use ( $shutdownPositions ) {
155  if ( $curValue === false ) {
156  $curPositions = $shutdownPositions;
157  } else {
158  $curPositions = $curValue['positions'];
159  // Use the newest positions for each DB master
160  foreach ( $shutdownPositions as $db => $pos ) {
161  if ( !isset( $curPositions[$db] )
162  || $pos->asOfTime() > $curPositions[$db]->asOfTime()
163  ) {
164  $curPositions[$db] = $pos;
165  }
166  }
167  }
168 
169  return [ 'positions' => $curPositions ];
170  },
172  10,
173  BagOStuff::WRITE_SYNC // visible in all datacenters
174  );
175 
176  if ( !$ok ) {
177  // Raced out too many times or stash is down
178  wfDebugLog( 'replication',
179  __METHOD__ . ": failed to save master pos for " .
180  implode( ', ', array_keys( $this->shutdownPositions ) ) . "\n"
181  );
182 
184  }
185 
186  return [];
187  }
188 
192  protected function initPositions() {
193  if ( $this->initialized ) {
194  return;
195  }
196 
197  $this->initialized = true;
198  if ( $this->wait ) {
199  $data = $this->store->get( $this->key );
200  $this->startupPositions = $data ? $data['positions'] : [];
201 
202  wfDebugLog( 'replication', __METHOD__ . ": key is {$this->key} (read)\n" );
203  } else {
204  $this->startupPositions = [];
205 
206  wfDebugLog( 'replication', __METHOD__ . ": key is {$this->key} (unread)\n" );
207  }
208  }
209 }
bool $initialized
Whether the client data was loaded.
the array() calling protocol came about after MediaWiki 1.4rc1.
getAnyOpenConnection($i)
Get any open connection to a given server index, local or foreign Returns false if there is no connec...
initLB(LoadBalancer $lb)
Initialise a LoadBalancer to give it appropriate chronology protection.
bool $enabled
Whether to no-op all method calls.
getServerCount()
Get the number of defined servers (not the number of open connections)
DBMasterPos[] $shutdownPositions
Map of (DB master name => position)
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
initPositions()
Load in previous master positions for the client.
MediaWiki s SiteStore can be cached and stored in a flat in a json format If the SiteStore is frequently the file cache may provide a performance benefit over a database store
Definition: sitescache.txt:1
bool $wait
Whether to check and wait on positions.
shutdownLB(LoadBalancer $lb)
Notify the ChronologyProtector that the LoadBalancer is about to shut down.
Database load balancing object.
wfDebugLog($logGroup, $text, $dest= 'all', array $context=[])
Send a line to a supplementary debug log file, if configured, or main debug log if not...
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add in any and then calling but I prefer the flexibility This should also do the output encoding The system allocates a global one in $wgOut Title Represents the title of an and does all the work of translating among various forms such as plain database key
Definition: design.txt:25
array $client
Map of (ip: , agent: )
const WRITE_SYNC
Bitfield constants for set()/merge()
Definition: BagOStuff.php:83
parentInfo($x=null)
Get or set arbitrary data used by the parent object, usually an LBFactory.
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
__construct(BagOStuff $store, array $client)
string $key
Storage key name.
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
Class for ensuring a consistent ordering of events as seen by the user, despite replication.
shutdown()
Notify the ChronologyProtector that the LBFactory is done calling shutdownLB() for now...
waitFor($pos)
Set the master wait position If a DB_SLAVE connection has been opened already, waits Otherwise sets a...
getServerName($i)
Get the host name or IP address of the server with the specified index Prefer a readable name if avai...
DBMasterPos[] $startupPositions
Map of (DB master name => position)
makeGlobalKey()
Make a global cache key.
Definition: BagOStuff.php:717