MediaWiki
master
Toggle main menu visibility
purgeParserCache.php
Go to the documentation of this file.
1
<?php
6
7
// @codeCoverageIgnoreStart
8
require_once __DIR__ .
'/Maintenance.php'
;
9
// @codeCoverageIgnoreEnd
10
11
use
MediaWiki\MainConfigNames
;
12
use
MediaWiki\Maintenance\Maintenance
;
13
use
MediaWiki\Parser\ParserOptions
;
14
use Wikimedia\Timestamp\ConvertibleTimestamp;
15
use Wikimedia\Timestamp\TimestampFormat as TS;
16
27
class
PurgeParserCache
extends
Maintenance
{
28
30
private
$lastProgress;
31
33
private
$lastTimestamp;
34
36
private
$tmpCount = 0;
38
private
$usleep = 0;
39
40
public
function
__construct
() {
41
parent::__construct();
42
$this->addDescription(
"Remove old objects from the parser cache. "
.
43
"This only works when the parser cache is in an SQL database."
);
44
$this->addOption(
'expiredate'
,
'Delete objects expiring before this date.'
,
false
,
true
);
45
$this->addOption(
46
'age'
,
47
'Delete objects created more than this many seconds ago, assuming '
.
48
'$wgParserCacheExpireTime has remained consistent.'
,
49
false
,
50
true
);
51
$this->addOption(
'dry-run'
,
'Perform a dry run, to verify age and date calculation.'
);
52
$this->addOption(
'msleep'
,
'Milliseconds to sleep between purge chunks of $wgUpdateRowsPerQuery.'
,
53
false
,
54
true
);
55
$this->addOption(
56
'tag'
,
57
'Purge a single server only. This feature is designed for use by large wiki farms where '
.
58
'one has to purge multiple servers concurrently in order to keep up with new writes. '
.
59
'This requires using the SqlBagOStuff "servers" option in $wgObjectCaches.'
,
60
false
,
61
true
);
62
$this->addOption(
63
'name'
,
64
'Name of the ParserCache instance to purge. Defaults to the'
.
65
'primary cache of ParserOutputAccess.'
,
66
false
,
67
true
);
68
}
69
70
public
function
execute
() {
71
$inputDate = $this->getOption(
'expiredate'
);
72
$inputAge = $this->getOption(
'age'
);
73
74
if
( $inputDate !==
null
) {
75
$timestamp = strtotime( $inputDate );
76
} elseif ( $inputAge !==
null
) {
77
$expireTime = (int)$this->getConfig()->get(
MainConfigNames::ParserCacheExpireTime
);
78
$timestamp = time() + $expireTime - intval( $inputAge );
79
}
else
{
80
$this->fatalError(
"Must specify either --expiredate or --age"
);
81
}
82
$this->usleep = 1e3 * $this->getOption(
'msleep'
, 0 );
83
$this->lastTimestamp = ConvertibleTimestamp::hrtime();
84
85
$humanDate = ConvertibleTimestamp::convert( TS::RFC2822, $timestamp );
86
if
( $this->hasOption(
'dry-run'
) ) {
87
$this->fatalError(
"\nDry run mode, would delete objects having an expiry before "
. $humanDate .
"\n"
);
88
}
89
90
$this->output(
"Deleting objects expiring before "
. $humanDate .
"\n"
);
91
92
if
( $this->hasOption(
'name'
) ) {
93
$pc = $this->getServiceContainer()->getParserCacheFactory()->getParserCache(
94
$this->getOption(
'name'
)
95
)->getCacheStorage();
96
}
else
{
97
$parserOutputAccess = $this->getServiceContainer()->getParserOutputAccess();
98
$parserOptions =
ParserOptions::newFromAnon
();
99
$pc = $parserOutputAccess->getPrimaryCache( $parserOptions )->getCacheStorage();
100
}
101
$success
= $pc->deleteObjectsExpiringBefore(
102
$timestamp,
103
$this->showProgressAndWait( ... ),
104
INF,
105
// Note that "0" can be a valid server tag, and must not be discarded or changed to null.
106
$this->getOption(
'tag'
,
null
)
107
);
108
if
( !
$success
) {
109
$this->fatalError(
"\nCannot purge this kind of parser cache."
);
110
}
111
$this->showProgressAndWait( 100 );
112
$this->output(
"\nDone\n"
);
113
}
114
115
private
function
showProgressAndWait(
int
$percent ) {
116
// Parser caches involve mostly-unthrottled writes of large blobs. This is sometimes prone
117
// to replication lag. As such, while our purge queries are simple primary key deletes,
118
// we want to avoid adding significant load to the replication stream, by being
119
// proactively graceful with these sleeps between each batch.
120
// The reason we don't explicitly wait for replication is that that would require the script
121
// to be aware of cross-dc replicas, which we prefer not to, and waiting for replication
122
// and confirmation latency might actually be *too* graceful and take so long that the
123
// purge script would not be able to finish within 24 hours for large wiki farms.
124
// (T150124).
125
usleep( $this->usleep );
126
$this->tmpCount++;
127
128
$percentString = sprintf(
"%.1f"
, $percent );
129
if
( $percentString === $this->lastProgress ) {
130
// Only print a line if we've progressed >= 0.1% since the last printed line.
131
// This does not mean every 0.1% step is printed since we only run this callback
132
// once after a deletion batch. How often and how many lines we print depends on the
133
// batch size (SqlBagOStuff::deleteObjectsExpiringBefore, $wgUpdateRowsPerQuery),
134
// and on how many table rows there are.
135
return
;
136
}
137
$now = ConvertibleTimestamp::hrtime();
138
$sec = sprintf(
"%.1f"
, ( $now - $this->lastTimestamp ) / 1e9 );
139
140
// Give a sense of how much time is spent in the delete operations vs the sleep time,
141
// by recording the number of iterations we've completed since the last progress update.
142
$this->output(
"... {$percentString}% done (+{$this->tmpCount} iterations in {$sec}s)\n"
);
143
144
$this->lastProgress = $percentString;
145
$this->tmpCount = 0;
146
$this->lastTimestamp = $now;
147
}
148
}
149
150
// @codeCoverageIgnoreStart
151
$maintClass
= PurgeParserCache::class;
152
require_once RUN_MAINTENANCE_IF_MAIN;
153
// @codeCoverageIgnoreEnd
$success
$success
Definition
NoLocalSettings.php:24
$maintClass
$maintClass
Definition
addChangeTag.php:56
MediaWiki\MainConfigNames
A class containing constants representing the names of configuration variables.
Definition
MainConfigNames.php:22
MediaWiki\MainConfigNames\ParserCacheExpireTime
const ParserCacheExpireTime
Name constant for the ParserCacheExpireTime setting, for use with Config\get().
Definition
MainConfigNames.php:1572
MediaWiki\Parser\ParserOptions
Set options of the Parser.
Definition
ParserOptions.php:70
MediaWiki\Parser\ParserOptions\newFromAnon
static newFromAnon()
Get a ParserOptions object for an anonymous user.
Definition
ParserOptions.php:1205
PurgeParserCache
Remove expired objects from the parser cache database.
Definition
purgeParserCache.php:27
PurgeParserCache\execute
execute()
Definition
purgeParserCache.php:70
PurgeParserCache\__construct
__construct()
Definition
purgeParserCache.php:40
MediaWiki\Maintenance
Definition
applyChangeTag.php:11
maintenance
purgeParserCache.php
Generated on
for MediaWiki by
1.18.0