3 use Psr\Log\LoggerInterface
;
4 use Wikimedia\Rdbms\IDatabase
;
7 * Class for fixing stale WANObjectCache keys using a purge event source
9 * This is useful for expiring keys that missed fire-and-forget purges. This uses the
10 * recentchanges table as a reliable stream to make certain keys reach consistency
11 * as soon as the underlying replica database catches up. These means that critical
12 * keys will not escape getting purged simply due to brief hiccups in the network,
13 * which are more prone to happen accross datacenters.
16 * "I was trying to cheat death. I was only trying to surmount for a little while the
17 * darkness that all my life I surely knew was going to come rolling in on me some day
18 * and obliterate me. I was only to stay alive a little brief while longer, after I was
19 * already gone. To stay in the light, to be with the living, a little while past my time."
20 * -- Notes for "Blues of a Lifetime", by [[Cornell Woolrich]]
24 class WANCacheReapUpdate
implements DeferrableUpdate
{
27 /** @var LoggerInterface */
31 * @param IDatabase $db
32 * @param LoggerInterface $logger
34 public function __construct( IDatabase
$db, LoggerInterface
$logger ) {
36 $this->logger
= $logger;
40 $reaper = new WANObjectCacheReaper(
41 ObjectCache
::getMainWANInstance(),
42 ObjectCache
::getLocalClusterInstance(),
43 [ $this, 'getTitleChangeEvents' ],
44 [ $this, 'getEventAffectedKeys' ],
46 'channel' => 'table:recentchanges:' . $this->db
->getWikiID(),
47 'logger' => $this->logger
51 $reaper->invoke( 100 );
55 * @see WANObjectCacheRepear
61 * @return TitleValue[]
63 public function getTitleChangeEvents( $start, $id, $end, $limit ) {
65 $encStart = $db->addQuotes( $db->timestamp( $start ) );
66 $encEnd = $db->addQuotes( $db->timestamp( $end ) );
67 $id = (int)$id; // cast NULL => 0 since rc_id is an integer
71 [ 'rc_namespace', 'rc_title', 'rc_timestamp', 'rc_id' ],
74 "rc_timestamp > $encStart",
75 "rc_timestamp = $encStart AND rc_id > " . $db->addQuotes( $id )
77 "rc_timestamp < $encEnd"
80 [ 'ORDER BY' => 'rc_timestamp ASC, rc_id ASC', 'LIMIT' => $limit ]
84 foreach ( $res as $row ) {
86 'id' => (int)$row->rc_id
,
87 'pos' => (int)wfTimestamp( TS_UNIX
, $row->rc_timestamp
),
88 'item' => new TitleValue( (int)$row->rc_namespace
, $row->rc_title
)
96 * Gets a list of important cache keys associated with a title
98 * @see WANObjectCacheRepear
99 * @param WANObjectCache $cache
100 * @param TitleValue $t
103 public function getEventAffectedKeys( WANObjectCache
$cache, TitleValue
$t ) {
104 /** @var WikiPage[]|LocalFile[]|User[] $entities */
107 // You can't create a WikiPage for special pages (-1) or other virtual
108 // namespaces, but special pages do appear in RC sometimes, e.g. for logs
109 // of AbuseFilter filter changes.
110 if ( $t->getNamespace() >= 0 ) {
111 $entities[] = WikiPage
::factory( Title
::newFromTitleValue( $t ) );
114 if ( $t->inNamespace( NS_FILE
) ) {
115 $entities[] = wfLocalFile( $t->getText() );
117 if ( $t->inNamespace( NS_USER
) ) {
118 $entities[] = User
::newFromName( $t->getText(), false );
122 foreach ( $entities as $entity ) {
124 $keys = array_merge( $keys, $entity->getMutableCacheKeys( $cache ) );
128 $this->logger
->debug( __CLASS__
. ': got key(s) ' . implode( ', ', $keys ) );