3 use MediaWiki\Linker\LinkTarget
;
4 use Psr\Log\LoggerInterface
;
5 use Wikimedia\Rdbms\IDatabase
;
8 * Class for fixing stale WANObjectCache keys using a purge event source
10 * This is useful for expiring keys that missed fire-and-forget purges. This uses the
11 * recentchanges table as a reliable stream to make certain keys reach consistency
12 * as soon as the underlying replica database catches up. These means that critical
13 * keys will not escape getting purged simply due to brief hiccups in the network,
14 * which are more prone to happen across datacenters.
17 * "I was trying to cheat death. I was only trying to surmount for a little while the
18 * darkness that all my life I surely knew was going to come rolling in on me some day
19 * and obliterate me. I was only to stay alive a little brief while longer, after I was
20 * already gone. To stay in the light, to be with the living, a little while past my time."
21 * -- Notes for "Blues of a Lifetime", by [[Cornell Woolrich]]
25 class WANCacheReapUpdate
implements DeferrableUpdate
{
28 /** @var LoggerInterface */
32 * @param IDatabase $db
33 * @param LoggerInterface $logger
35 public function __construct( IDatabase
$db, LoggerInterface
$logger ) {
37 $this->logger
= $logger;
41 $reaper = new WANObjectCacheReaper(
42 ObjectCache
::getMainWANInstance(),
43 ObjectCache
::getLocalClusterInstance(),
44 [ $this, 'getTitleChangeEvents' ],
45 [ $this, 'getEventAffectedKeys' ],
47 'channel' => 'table:recentchanges:' . $this->db
->getDomainID(),
48 'logger' => $this->logger
52 $reaper->invoke( 100 );
56 * @see WANObjectCacheRepear
62 * @return TitleValue[]
64 public function getTitleChangeEvents( $start, $id, $end, $limit ) {
66 $encStart = $db->addQuotes( $db->timestamp( $start ) );
67 $encEnd = $db->addQuotes( $db->timestamp( $end ) );
68 $id = (int)$id; // cast NULL => 0 since rc_id is an integer
72 [ 'rc_namespace', 'rc_title', 'rc_timestamp', 'rc_id' ],
75 "rc_timestamp > $encStart",
76 "rc_timestamp = $encStart AND rc_id > " . $db->addQuotes( $id )
78 "rc_timestamp < $encEnd"
81 [ 'ORDER BY' => 'rc_timestamp ASC, rc_id ASC', 'LIMIT' => $limit ]
85 foreach ( $res as $row ) {
87 'id' => (int)$row->rc_id
,
88 'pos' => (int)wfTimestamp( TS_UNIX
, $row->rc_timestamp
),
89 'item' => new TitleValue( (int)$row->rc_namespace
, $row->rc_title
)
97 * Gets a list of important cache keys associated with a title
99 * @see WANObjectCacheRepear
100 * @param WANObjectCache $cache
101 * @param LinkTarget $t
104 public function getEventAffectedKeys( WANObjectCache
$cache, LinkTarget
$t ) {
105 /** @var WikiPage[]|LocalFile[]|User[] $entities */
108 // You can't create a WikiPage for special pages (-1) or other virtual
109 // namespaces, but special pages do appear in RC sometimes, e.g. for logs
110 // of AbuseFilter filter changes.
111 if ( $t->getNamespace() >= 0 ) {
112 $entities[] = WikiPage
::factory( Title
::newFromLinkTarget( $t ) );
115 if ( $t->inNamespace( NS_FILE
) ) {
116 $entities[] = wfLocalFile( $t->getText() );
118 if ( $t->inNamespace( NS_USER
) ) {
119 $entities[] = User
::newFromName( $t->getText(), false );
123 foreach ( $entities as $entity ) {
125 $keys = array_merge( $keys, $entity->getMutableCacheKeys( $cache ) );
129 $this->logger
->debug( __CLASS__
. ': got key(s) ' . implode( ', ', $keys ) );