From 85edec239e264068972ce3b880a2d5c55c013a58 Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Mon, 9 Nov 2015 22:57:00 -0800 Subject: [PATCH] API DB commit and sticky DC cookie fixes * Make sure the API commits DBs *before* sending the response, in case something goes south which would make a sent response wrong. * Make sticky DC cookies trigger with api.php too. * Make sure UseDC cookies do not end up with a prefix. VCL rules should not have to deal with having a DB name as a prefix or such. This was an oversight. Change-Id: I9e4090ab15c1c1493b0589a710184745dac9b0c1 --- includes/MediaWiki.php | 24 +++++++++++++++++------- includes/api/ApiMain.php | 3 +++ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/includes/MediaWiki.php b/includes/MediaWiki.php index d048b57cfa..22b896f6ef 100644 --- a/includes/MediaWiki.php +++ b/includes/MediaWiki.php @@ -486,13 +486,22 @@ class MediaWiki { $this->doPostOutputShutdown( 'normal' ); } + /** + * @see MediaWiki::preOutputCommit() + * @since 1.26 + */ + public function doPreOutputCommit() { + self::preOutputCommit( $this->context ); + } + /** * This function commits all DB changes as needed before * the user can receive a response (in case commit fails) * - * @since 1.26 + * @param IContextSource $context + * @since 1.27 */ - public function doPreOutputCommit() { + public static function preOutputCommit( IContextSource $context ) { // Either all DBs should commit or none ignore_user_abort( true ); @@ -505,16 +514,17 @@ class MediaWiki { // Set a cookie to tell all CDN edge nodes to "stick" the user to the // DC that handles this POST request (e.g. the "master" data center) - $request = $this->context->getRequest(); + $request = $context->getRequest(); + $config = $context->getConfig(); if ( $request->wasPosted() && $factory->hasOrMadeRecentMasterChanges() ) { - $expires = time() + $this->config->get( 'DataCenterUpdateStickTTL' ); - $request->response()->setCookie( 'UseDC', 'master', $expires ); + $expires = time() + $config->get( 'DataCenterUpdateStickTTL' ); + $request->response()->setCookie( 'UseDC', 'master', $expires, array( 'prefix' => '' ) ); } // Avoid letting a few seconds of slave lag cause a month of stale data if ( $factory->laggedSlaveUsed() ) { - $maxAge = $this->config->get( 'CdnMaxageLagged' ); - $this->context->getOutput()->lowerCdnMaxage( $maxAge ); + $maxAge = $config->get( 'CdnMaxageLagged' ); + $context->getOutput()->lowerCdnMaxage( $maxAge ); $request->response()->header( "X-Database-Lagged: true" ); wfDebugLog( 'replication', "Lagged DB used; CDN cache TTL limited to $maxAge seconds" ); } diff --git a/includes/api/ApiMain.php b/includes/api/ApiMain.php index 27684096e8..14b27ee8c7 100644 --- a/includes/api/ApiMain.php +++ b/includes/api/ApiMain.php @@ -440,6 +440,9 @@ class ApiMain extends ApiBase { // Log the request whether or not there was an error $this->logRequest( microtime( true ) - $t ); + // Commit DBs and send any related cookies and headers + MediaWiki::preOutputCommit( $this->getContext() ); + // Send cache headers after any code which might generate an error, to // avoid sending public cache headers for errors. $this->sendCacheHeaders( $isError ); -- 2.20.1