MediaWiki  1.29.1
WANObjectCacheReaper.php
Go to the documentation of this file.
1 <?php
23 use Psr\Log\LoggerAwareInterface;
24 use Psr\Log\LoggerInterface;
25 use Psr\Log\NullLogger;
26 use Wikimedia\ScopedCallback;
27 
38 class WANObjectCacheReaper implements LoggerAwareInterface {
40  protected $cache;
42  protected $store;
44  protected $logChunkCallback;
46  protected $keyListCallback;
48  protected $logger;
49 
51  protected $channel;
54 
78  public function __construct(
81  callable $logCallback,
82  callable $keyCallback,
84  ) {
85  $this->cache = $cache;
86  $this->store = $store;
87 
88  $this->logChunkCallback = $logCallback;
89  $this->keyListCallback = $keyCallback;
90  if ( isset( $params['channel'] ) ) {
91  $this->channel = $params['channel'];
92  } else {
93  throw new UnexpectedValueException( "No channel specified." );
94  }
95 
96  $this->initialStartWindow = isset( $params['initialStartWindow'] )
97  ? $params['initialStartWindow']
98  : 3600;
99  $this->logger = isset( $params['logger'] )
100  ? $params['logger']
101  : new NullLogger();
102  }
103 
104  public function setLogger( LoggerInterface $logger ) {
105  $this->logger = $logger;
106  }
107 
114  final public function invoke( $n = 100 ) {
115  $posKey = $this->store->makeGlobalKey( 'WANCache', 'reaper', $this->channel );
116  $scopeLock = $this->store->getScopedLock( "$posKey:busy", 0 );
117  if ( !$scopeLock ) {
118  return 0;
119  }
120 
121  $now = time();
122  $status = $this->store->get( $posKey );
123  if ( !$status ) {
124  $status = [ 'pos' => $now - $this->initialStartWindow, 'id' => null ];
125  }
126 
127  // Get events for entities who's keys tombstones/hold-off should have expired by now
128  $events = call_user_func_array(
129  $this->logChunkCallback,
130  [ $status['pos'], $status['id'], $now - WANObjectCache::HOLDOFF_TTL - 1, $n ]
131  );
132 
133  $event = null;
134  $keyEvents = [];
135  foreach ( $events as $event ) {
136  $keys = call_user_func_array(
137  $this->keyListCallback,
138  [ $this->cache, $event['item'] ]
139  );
140  foreach ( $keys as $key ) {
141  unset( $keyEvents[$key] ); // use only the latest per key
142  $keyEvents[$key] = [
143  'pos' => $event['pos'],
144  'id' => $event['id']
145  ];
146  }
147  }
148 
149  $purgeCount = 0;
150  $lastOkEvent = null;
151  foreach ( $keyEvents as $key => $keyEvent ) {
152  if ( !$this->cache->reap( $key, $keyEvent['pos'] ) ) {
153  break;
154  }
155  ++$purgeCount;
156  $lastOkEvent = $event;
157  }
158 
159  if ( $lastOkEvent ) {
160  $ok = $this->store->merge(
161  $posKey,
162  function ( $bag, $key, $curValue ) use ( $lastOkEvent ) {
163  if ( !$curValue ) {
164  // Use new position
165  } else {
166  $curCoord = [ $curValue['pos'], $curValue['id'] ];
167  $newCoord = [ $lastOkEvent['pos'], $lastOkEvent['id'] ];
168  if ( $newCoord < $curCoord ) {
169  // Keep prior position instead of rolling it back
170  return $curValue;
171  }
172  }
173 
174  return [
175  'pos' => $lastOkEvent['pos'],
176  'id' => $lastOkEvent['id'],
177  'ctime' => $curValue ? $curValue['ctime'] : date( 'c' )
178  ];
179  },
181  );
182 
183  $pos = $lastOkEvent['pos'];
184  $id = $lastOkEvent['id'];
185  if ( $ok ) {
186  $this->logger->info( "Updated cache reap position ($pos, $id)." );
187  } else {
188  $this->logger->error( "Could not update cache reap position ($pos, $id)." );
189  }
190  }
191 
192  ScopedCallback::consume( $scopeLock );
193 
194  return $purgeCount;
195  }
196 
200  public function getState() {
201  $posKey = $this->store->makeGlobalKey( 'WANCache', 'reaper', $this->channel );
202 
203  return $this->store->get( $posKey );
204  }
205 }
WANObjectCacheReaper\$logger
LoggerInterface $logger
Definition: WANObjectCacheReaper.php:48
WANObjectCacheReaper\setLogger
setLogger(LoggerInterface $logger)
Definition: WANObjectCacheReaper.php:104
WANObjectCacheReaper\$cache
WANObjectCache $cache
Definition: WANObjectCacheReaper.php:40
$status
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist and Watchlist you will want to construct new ChangesListBooleanFilter or ChangesListStringOptionsFilter objects When constructing you specify which group they belong to You can reuse existing or create your you must register them with $special registerFilterGroup removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set $status
Definition: hooks.txt:1049
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
$params
$params
Definition: styleTest.css.php:40
WANObjectCacheReaper\$keyListCallback
callable $keyListCallback
Definition: WANObjectCacheReaper.php:46
BagOStuff
interface is intended to be more or less compatible with the PHP memcached client.
Definition: BagOStuff.php:47
cache
you have access to all of the normal MediaWiki so you can get a DB use the cache
Definition: maintenance.txt:52
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:35
WANObjectCacheReaper\$logChunkCallback
callable $logChunkCallback
Definition: WANObjectCacheReaper.php:44
store
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
IExpiringStore\TTL_INDEFINITE
const TTL_INDEFINITE
Definition: IExpiringStore.php:44
WANObjectCache\HOLDOFF_TTL
const HOLDOFF_TTL
Seconds to tombstone keys on delete()
Definition: WANObjectCache.php:106
WANObjectCacheReaper\$initialStartWindow
integer $initialStartWindow
Definition: WANObjectCacheReaper.php:53
WANObjectCache
Multi-datacenter aware caching interface.
Definition: WANObjectCache.php:81
WANObjectCacheReaper\__construct
__construct(WANObjectCache $cache, BagOStuff $store, callable $logCallback, callable $keyCallback, array $params)
Definition: WANObjectCacheReaper.php:78
WANObjectCacheReaper\invoke
invoke( $n=100)
Check and reap stale keys based on a chunk of events.
Definition: WANObjectCacheReaper.php:114
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
$keys
$keys
Definition: testCompression.php:65
WANObjectCacheReaper
Class for scanning through chronological, log-structured data or change logs and locally purging cach...
Definition: WANObjectCacheReaper.php:38
WANObjectCacheReaper\$store
BagOStuff $store
Definition: WANObjectCacheReaper.php:42
WANObjectCacheReaper\getState
getState()
Definition: WANObjectCacheReaper.php:200
WANObjectCacheReaper\$channel
string $channel
Definition: WANObjectCacheReaper.php:51
array
the array() calling protocol came about after MediaWiki 1.4rc1.