Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 60
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
PurgeParserCache
0.00% covered (danger)
0.00%
0 / 57
0.00% covered (danger)
0.00%
0 / 3
72
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 1
30
 showProgressAndWait
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21require_once __DIR__ . '/Maintenance.php';
22
23use MediaWiki\MainConfigNames;
24use Wikimedia\Timestamp\ConvertibleTimestamp;
25
26/**
27 * Remove expired objects from the parser cache database.
28 *
29 * By default, this does not need to be run. The default parser cache
30 * backend is CACHE_DB (SqlBagOStuff), and by default that automatically
31 * performs incremental purges in the background of write requests.
32 *
33 * @see {@link MediaWiki\MainConfigSchema::ParserCacheType}
34 * @ingroup Maintenance
35 */
36class PurgeParserCache extends Maintenance {
37
38    /** @var null|string */
39    private $lastProgress;
40
41    /** @var null|float */
42    private $lastTimestamp;
43
44    private $tmpCount = 0;
45    private $usleep = 0;
46
47    public function __construct() {
48        parent::__construct();
49        $this->addDescription( "Remove old objects from the parser cache. " .
50            "This only works when the parser cache is in an SQL database." );
51        $this->addOption( 'expiredate', 'Delete objects expiring before this date.', false, true );
52        $this->addOption(
53            'age',
54            'Delete objects created more than this many seconds ago, assuming ' .
55                '$wgParserCacheExpireTime has remained consistent.',
56            false,
57            true );
58        $this->addOption( 'dry-run', 'Perform a dry run, to verify age and date calculation.' );
59        $this->addOption( 'msleep', 'Milliseconds to sleep between purge chunks of $wgUpdateRowsPerQuery.',
60            false,
61            true );
62        $this->addOption(
63            'tag',
64            'Purge a single server only. This feature is designed for use by large wiki farms where ' .
65                'one has to purge multiple servers concurrently in order to keep up with new writes. ' .
66                'This requires using the SqlBagOStuff "servers" option in $wgObjectCaches.',
67            false,
68            true );
69    }
70
71    public function execute() {
72        $inputDate = $this->getOption( 'expiredate' );
73        $inputAge = $this->getOption( 'age' );
74
75        if ( $inputDate !== null ) {
76            $timestamp = strtotime( $inputDate );
77        } elseif ( $inputAge !== null ) {
78            $expireTime = (int)$this->getConfig()->get( MainConfigNames::ParserCacheExpireTime );
79            $timestamp = time() + $expireTime - intval( $inputAge );
80        } else {
81            $this->fatalError( "Must specify either --expiredate or --age" );
82        }
83        $this->usleep = 1e3 * $this->getOption( 'msleep', 0 );
84        $this->lastTimestamp = microtime( true );
85
86        $humanDate = ConvertibleTimestamp::convert( TS_RFC2822, $timestamp );
87        if ( $this->hasOption( 'dry-run' ) ) {
88            $this->fatalError( "\nDry run mode, would delete objects having an expiry before " . $humanDate . "\n" );
89        }
90
91        $this->output( "Deleting objects expiring before " . $humanDate . "\n" );
92
93        $pc = $this->getServiceContainer()->getParserCache()->getCacheStorage();
94        $success = $pc->deleteObjectsExpiringBefore(
95            $timestamp,
96            [ $this, 'showProgressAndWait' ],
97            INF,
98            // Note that "0" can be a valid server tag, and must not be discarded or changed to null.
99            $this->getOption( 'tag', null )
100        );
101        if ( !$success ) {
102            $this->fatalError( "\nCannot purge this kind of parser cache." );
103        }
104        $this->showProgressAndWait( 100 );
105        $this->output( "\nDone\n" );
106    }
107
108    public function showProgressAndWait( $percent ) {
109        // Parser caches involve mostly-unthrottled writes of large blobs. This is sometimes prone
110        // to replication lag. As such, while our purge queries are simple primary key deletes,
111        // we want to avoid adding significant load to the replication stream, by being
112        // proactively graceful with these sleeps between each batch.
113        // The reason we don't explicitly wait for replication is that that would require the script
114        // to be aware of cross-dc replicas, which we prefer not to, and waiting for replication
115        // and confirmation latency might actually be *too* graceful and take so long that the
116        // purge script would not be able to finish within 24 hours for large wiki farms.
117        // (T150124).
118        usleep( $this->usleep );
119        $this->tmpCount++;
120
121        $percentString = sprintf( "%.1f", $percent );
122        if ( $percentString === $this->lastProgress ) {
123            // Only print a line if we've progressed >= 0.1% since the last printed line.
124            // This does not mean every 0.1% step is printed since we only run this callback
125            // once after a deletion batch. How often and how many lines we print depends on the
126            // batch size (SqlBagOStuff::deleteObjectsExpiringBefore, $wgUpdateRowsPerQuery),
127            // and on how many table rows there are.
128            return;
129        }
130        $now = microtime( true );
131        $sec = sprintf( "%.1f", $now - $this->lastTimestamp );
132
133        // Give a sense of how much time is spent in the delete operations vs the sleep time,
134        // by recording the number of iterations we've completed since the last progress update.
135        $this->output( "... {$percentString}% done (+{$this->tmpCount} iterations in {$sec}s)\n" );
136
137        $this->lastProgress = $percentString;
138        $this->tmpCount = 0;
139        $this->lastTimestamp = $now;
140    }
141}
142
143$maintClass = PurgeParserCache::class;
144require_once RUN_MAINTENANCE_IF_MAIN;