MediaWiki 1.44.0
purgeParserCache.php
Go to the documentation of this file.
1<?php
21// @codeCoverageIgnoreStart
22require_once __DIR__ . '/Maintenance.php';
23// @codeCoverageIgnoreEnd
24
27use Wikimedia\Timestamp\ConvertibleTimestamp;
28
40
42 private $lastProgress;
43
45 private $lastTimestamp;
46
48 private $tmpCount = 0;
50 private $usleep = 0;
51
52 public function __construct() {
53 parent::__construct();
54 $this->addDescription( "Remove old objects from the parser cache. " .
55 "This only works when the parser cache is in an SQL database." );
56 $this->addOption( 'expiredate', 'Delete objects expiring before this date.', false, true );
57 $this->addOption(
58 'age',
59 'Delete objects created more than this many seconds ago, assuming ' .
60 '$wgParserCacheExpireTime has remained consistent.',
61 false,
62 true );
63 $this->addOption( 'dry-run', 'Perform a dry run, to verify age and date calculation.' );
64 $this->addOption( 'msleep', 'Milliseconds to sleep between purge chunks of $wgUpdateRowsPerQuery.',
65 false,
66 true );
67 $this->addOption(
68 'tag',
69 'Purge a single server only. This feature is designed for use by large wiki farms where ' .
70 'one has to purge multiple servers concurrently in order to keep up with new writes. ' .
71 'This requires using the SqlBagOStuff "servers" option in $wgObjectCaches.',
72 false,
73 true );
74 }
75
76 public function execute() {
77 $inputDate = $this->getOption( 'expiredate' );
78 $inputAge = $this->getOption( 'age' );
79
80 if ( $inputDate !== null ) {
81 $timestamp = strtotime( $inputDate );
82 } elseif ( $inputAge !== null ) {
83 $expireTime = (int)$this->getConfig()->get( MainConfigNames::ParserCacheExpireTime );
84 $timestamp = time() + $expireTime - intval( $inputAge );
85 } else {
86 $this->fatalError( "Must specify either --expiredate or --age" );
87 }
88 $this->usleep = 1e3 * $this->getOption( 'msleep', 0 );
89 $this->lastTimestamp = microtime( true );
90
91 $humanDate = ConvertibleTimestamp::convert( TS_RFC2822, $timestamp );
92 if ( $this->hasOption( 'dry-run' ) ) {
93 $this->fatalError( "\nDry run mode, would delete objects having an expiry before " . $humanDate . "\n" );
94 }
95
96 $this->output( "Deleting objects expiring before " . $humanDate . "\n" );
97
98 $pc = $this->getServiceContainer()->getParserCache()->getCacheStorage();
99 $success = $pc->deleteObjectsExpiringBefore(
100 $timestamp,
101 [ $this, 'showProgressAndWait' ],
102 INF,
103 // Note that "0" can be a valid server tag, and must not be discarded or changed to null.
104 $this->getOption( 'tag', null )
105 );
106 if ( !$success ) {
107 $this->fatalError( "\nCannot purge this kind of parser cache." );
108 }
109 $this->showProgressAndWait( 100 );
110 $this->output( "\nDone\n" );
111 }
112
113 public function showProgressAndWait( int $percent ) {
114 // Parser caches involve mostly-unthrottled writes of large blobs. This is sometimes prone
115 // to replication lag. As such, while our purge queries are simple primary key deletes,
116 // we want to avoid adding significant load to the replication stream, by being
117 // proactively graceful with these sleeps between each batch.
118 // The reason we don't explicitly wait for replication is that that would require the script
119 // to be aware of cross-dc replicas, which we prefer not to, and waiting for replication
120 // and confirmation latency might actually be *too* graceful and take so long that the
121 // purge script would not be able to finish within 24 hours for large wiki farms.
122 // (T150124).
123 usleep( $this->usleep );
124 $this->tmpCount++;
125
126 $percentString = sprintf( "%.1f", $percent );
127 if ( $percentString === $this->lastProgress ) {
128 // Only print a line if we've progressed >= 0.1% since the last printed line.
129 // This does not mean every 0.1% step is printed since we only run this callback
130 // once after a deletion batch. How often and how many lines we print depends on the
131 // batch size (SqlBagOStuff::deleteObjectsExpiringBefore, $wgUpdateRowsPerQuery),
132 // and on how many table rows there are.
133 return;
134 }
135 $now = microtime( true );
136 $sec = sprintf( "%.1f", $now - $this->lastTimestamp );
137
138 // Give a sense of how much time is spent in the delete operations vs the sleep time,
139 // by recording the number of iterations we've completed since the last progress update.
140 $this->output( "... {$percentString}% done (+{$this->tmpCount} iterations in {$sec}s)\n" );
141
142 $this->lastProgress = $percentString;
143 $this->tmpCount = 0;
144 $this->lastTimestamp = $now;
145 }
146}
147
148// @codeCoverageIgnoreStart
149$maintClass = PurgeParserCache::class;
150require_once RUN_MAINTENANCE_IF_MAIN;
151// @codeCoverageIgnoreEnd
A class containing constants representing the names of configuration variables.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
output( $out, $channel=null)
Throw some output to the user.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
hasOption( $name)
Checks to see if a particular option was set.
getOption( $name, $default=null)
Get an option, or return the default.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
Remove expired objects from the parser cache database.
showProgressAndWait(int $percent)
execute()
Do the actual work.
__construct()
Default constructor.