| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Deferred; |
| 22 |
|
| 23 |
use CdnPurgeJob; |
| 24 |
use Exception; |
| 25 |
use InvalidArgumentException; |
| 26 |
use MediaWiki\MainConfigNames; |
| 27 |
use MediaWiki\MediaWikiServices; |
| 28 |
use MediaWiki\Page\PageReference; |
| 29 |
use RuntimeException; |
| 30 |
use Wikimedia\Assert\Assert; |
| 31 |
use Wikimedia\IPUtils; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
class CdnCacheUpdate implements DeferrableUpdate, MergeableUpdate { |
| 38 |
|
| 39 |
private $urlTuples = []; |
| 40 |
|
| 41 |
private $pageTuples = []; |
| 42 |
|
| 43 |
|
| 44 |
private const MAX_REBOUND_DELAY = 300; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public function __construct( array $targets, array $options = [] ) { |
| 53 |
$delay = min( |
| 54 |
(int)max( $options['reboundDelay'] ?? 0, 0 ), |
| 55 |
self::MAX_REBOUND_DELAY |
| 56 |
); |
| 57 |
|
| 58 |
foreach ( $targets as $target ) { |
| 59 |
if ( $target instanceof PageReference ) { |
| 60 |
$this->pageTuples[] = [ $target, $delay ]; |
| 61 |
} else { |
| 62 |
$this->urlTuples[] = [ $target, $delay ]; |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
public function merge( MergeableUpdate $update ) { |
| 68 |
|
| 69 |
Assert::parameterType( __CLASS__, $update, '$update' ); |
| 70 |
'@phan-var self $update'; |
| 71 |
|
| 72 |
$this->urlTuples = array_merge( $this->urlTuples, $update->urlTuples ); |
| 73 |
$this->pageTuples = array_merge( $this->pageTuples, $update->pageTuples ); |
| 74 |
} |
| 75 |
|
| 76 |
public function doUpdate() { |
| 77 |
|
| 78 |
$reboundDelayByUrl = $this->resolveReboundDelayByUrl(); |
| 79 |
|
| 80 |
|
| 81 |
self::purge( array_keys( $reboundDelayByUrl ) ); |
| 82 |
$immediatePurgeTimestamp = time(); |
| 83 |
|
| 84 |
|
| 85 |
$urlsWithReboundByDelay = []; |
| 86 |
foreach ( $reboundDelayByUrl as $url => $delay ) { |
| 87 |
if ( $delay > 0 ) { |
| 88 |
$urlsWithReboundByDelay[$delay][] = $url; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
$jobs = []; |
| 93 |
foreach ( $urlsWithReboundByDelay as $delay => $urls ) { |
| 94 |
$jobs[] = new CdnPurgeJob( [ |
| 95 |
'urls' => $urls, |
| 96 |
'jobReleaseTimestamp' => $immediatePurgeTimestamp + $delay |
| 97 |
] ); |
| 98 |
} |
| 99 |
MediaWikiServices::getInstance()->getJobQueueGroup()->lazyPush( $jobs ); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
public static function purge( array $urls ) { |
| 110 |
$cdnServers = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::CdnServers ); |
| 111 |
$htcpRouting = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::HTCPRouting ); |
| 112 |
if ( !$urls ) { |
| 113 |
return; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
$urls = array_unique( $urls ); |
| 118 |
|
| 119 |
wfDebugLog( 'squid', __METHOD__ . ': ' . implode( ' ', $urls ) ); |
| 120 |
|
| 121 |
|
| 122 |
$ts = microtime( true ); |
| 123 |
$relayerGroup = MediaWikiServices::getInstance()->getEventRelayerGroup(); |
| 124 |
$relayerGroup->getRelayer( 'cdn-url-purges' )->notifyMulti( |
| 125 |
'cdn-url-purges', |
| 126 |
array_map( |
| 127 |
static function ( $url ) use ( $ts ) { |
| 128 |
return [ |
| 129 |
'url' => $url, |
| 130 |
'timestamp' => $ts, |
| 131 |
]; |
| 132 |
}, |
| 133 |
$urls |
| 134 |
) |
| 135 |
); |
| 136 |
|
| 137 |
|
| 138 |
if ( $htcpRouting ) { |
| 139 |
self::HTCPPurge( $urls ); |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
if ( $cdnServers ) { |
| 144 |
self::naivePurge( $urls ); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
public function getUrls() { |
| 152 |
return array_keys( $this->resolveReboundDelayByUrl() ); |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
private function resolveReboundDelayByUrl() { |
| 159 |
$services = MediaWikiServices::getInstance(); |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
$lb = $services->getLinkBatchFactory()->newLinkBatch(); |
| 164 |
foreach ( $this->pageTuples as [ $page, ] ) { |
| 165 |
$lb->addObj( $page ); |
| 166 |
} |
| 167 |
$lb->execute(); |
| 168 |
|
| 169 |
$reboundDelayByUrl = []; |
| 170 |
|
| 171 |
|
| 172 |
$htmlCacheUpdater = $services->getHtmlCacheUpdater(); |
| 173 |
foreach ( $this->pageTuples as [ $page, $delay ] ) { |
| 174 |
foreach ( $htmlCacheUpdater->getUrls( $page ) as $url ) { |
| 175 |
|
| 176 |
$reboundDelayByUrl[$url] = max( $reboundDelayByUrl[$url] ?? 0, $delay ); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
foreach ( $this->urlTuples as [ $url, $delay ] ) { |
| 181 |
|
| 182 |
$reboundDelayByUrl[$url] = max( $reboundDelayByUrl[$url] ?? 0, $delay ); |
| 183 |
} |
| 184 |
|
| 185 |
return $reboundDelayByUrl; |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
private static function HTCPPurge( array $urls ) { |
| 194 |
$htcpRouting = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::HTCPRouting ); |
| 195 |
$htcpMulticastTTL = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::HTCPMulticastTTL ); |
| 196 |
|
| 197 |
$htcpOpCLR = 4; |
| 198 |
|
| 199 |
|
| 200 |
if ( !defined( "IPPROTO_IP" ) ) { |
| 201 |
define( "IPPROTO_IP", 0 ); |
| 202 |
define( "IP_MULTICAST_LOOP", 34 ); |
| 203 |
define( "IP_MULTICAST_TTL", 33 ); |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
$conn = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP ); |
| 208 |
if ( !$conn ) { |
| 209 |
$errstr = socket_strerror( socket_last_error() ); |
| 210 |
wfDebugLog( 'squid', __METHOD__ . |
| 211 |
": Error opening UDP socket: $errstr" ); |
| 212 |
|
| 213 |
return; |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
socket_set_option( $conn, IPPROTO_IP, IP_MULTICAST_LOOP, 0 ); |
| 218 |
if ( $htcpMulticastTTL != 1 ) { |
| 219 |
|
| 220 |
socket_set_option( $conn, IPPROTO_IP, IP_MULTICAST_TTL, |
| 221 |
$htcpMulticastTTL ); |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
$idGenerator = MediaWikiServices::getInstance()->getGlobalIdGenerator(); |
| 226 |
$ids = $idGenerator->newSequentialPerNodeIDs( |
| 227 |
'squidhtcppurge', |
| 228 |
32, |
| 229 |
count( $urls ) |
| 230 |
); |
| 231 |
|
| 232 |
foreach ( $urls as $url ) { |
| 233 |
if ( !is_string( $url ) ) { |
| 234 |
throw new InvalidArgumentException( 'Bad purge URL' ); |
| 235 |
} |
| 236 |
$url = self::expand( $url ); |
| 237 |
$conf = self::getRuleForURL( $url, $htcpRouting ); |
| 238 |
if ( !$conf ) { |
| 239 |
wfDebugLog( 'squid', __METHOD__ . |
| 240 |
"No HTCP rule configured for URL {$url} , skipping" ); |
| 241 |
continue; |
| 242 |
} |
| 243 |
|
| 244 |
if ( isset( $conf['host'] ) && isset( $conf['port'] ) ) { |
| 245 |
|
| 246 |
$conf = [ $conf ]; |
| 247 |
} |
| 248 |
foreach ( $conf as $subconf ) { |
| 249 |
if ( !isset( $subconf['host'] ) || !isset( $subconf['port'] ) ) { |
| 250 |
throw new RuntimeException( "Invalid HTCP rule for URL $url\n" ); |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
$htcpTransID = current( $ids ); |
| 258 |
next( $ids ); |
| 259 |
|
| 260 |
$htcpSpecifier = pack( 'na4na*na8n', |
| 261 |
4, 'HEAD', strlen( $url ), $url, |
| 262 |
8, 'HTTP/1.0', 0 ); |
| 263 |
|
| 264 |
$htcpDataLen = 8 + 2 + strlen( $htcpSpecifier ); |
| 265 |
$htcpLen = 4 + $htcpDataLen + 2; |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
$htcpPacket = pack( 'nxxnCxNxxa*n', |
| 271 |
$htcpLen, $htcpDataLen, $htcpOpCLR, |
| 272 |
$htcpTransID, $htcpSpecifier, 2 ); |
| 273 |
|
| 274 |
wfDebugLog( 'squid', __METHOD__ . |
| 275 |
"Purging URL $url via HTCP" ); |
| 276 |
foreach ( $conf as $subconf ) { |
| 277 |
socket_sendto( $conn, $htcpPacket, $htcpLen, 0, |
| 278 |
$subconf['host'], $subconf['port'] ); |
| 279 |
} |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
private static function naivePurge( array $urls ) { |
| 290 |
$cdnServers = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::CdnServers ); |
| 291 |
|
| 292 |
$reqs = []; |
| 293 |
foreach ( $urls as $url ) { |
| 294 |
$url = self::expand( $url ); |
| 295 |
$urlInfo = wfGetUrlUtils()->parse( $url ) ?? false; |
| 296 |
$urlHost = strlen( $urlInfo['port'] ?? '' ) |
| 297 |
? IPUtils::combineHostAndPort( $urlInfo['host'], (int)$urlInfo['port'] ) |
| 298 |
: $urlInfo['host']; |
| 299 |
$baseReq = [ |
| 300 |
'method' => 'PURGE', |
| 301 |
'url' => $url, |
| 302 |
'headers' => [ |
| 303 |
'Host' => $urlHost, |
| 304 |
'Connection' => 'Keep-Alive', |
| 305 |
'Proxy-Connection' => 'Keep-Alive', |
| 306 |
'User-Agent' => 'MediaWiki/' . MW_VERSION . ' ' . __CLASS__ |
| 307 |
] |
| 308 |
]; |
| 309 |
foreach ( $cdnServers as $server ) { |
| 310 |
$reqs[] = ( $baseReq + [ 'proxy' => $server ] ); |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
$http = MediaWikiServices::getInstance()->getHttpRequestFactory() |
| 315 |
->createMultiClient( [ 'maxConnsPerHost' => 8, 'usePipelining' => true ] ); |
| 316 |
$http->runMulti( $reqs ); |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
private static function expand( $url ) { |
| 334 |
return (string)MediaWikiServices::getInstance()->getUrlUtils()->expand( $url, PROTO_INTERNAL ); |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
private static function getRuleForURL( $url, $rules ) { |
| 344 |
foreach ( $rules as $regex => $routing ) { |
| 345 |
if ( $regex === '' || preg_match( $regex, $url ) ) { |
| 346 |
return $routing; |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
return false; |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
class_alias( CdnCacheUpdate::class, 'CdnCacheUpdate' ); |