r70680 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r70679‎ | r70680 | r70681 >
Date:09:23, 8 August 2010
Author:nikerabbit
Status:ok
Tags:
Comment:
Fixed typo in reportTranslationServiceFailure name. Added a grace period where one failure is enough to keep the service suspended further.
Modified paths:
  • /trunk/extensions/Translate/TranslateTasks.php (modified) (history)
  • /trunk/extensions/Translate/utils/TranslationHelpers.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Translate/TranslateTasks.php
@@ -210,7 +210,7 @@
211211 }
212212 }
213213 } else {
214 - TranslationHelpers::reportTranslationSerficeFailure( 'tmserver' );
 214+ TranslationHelpers::reportTranslationServiceFailure( 'tmserver' );
215215 }
216216 unset( $this->collection[$key] );
217217 }
Index: trunk/extensions/Translate/utils/TranslationHelpers.php
@@ -280,7 +280,7 @@
281281 }
282282 } else {
283283 // Assume timeout
284 - self::reportTranslationSerficeFailure( $serviceName );
 284+ self::reportTranslationServiceFailure( $serviceName );
285285 }
286286
287287 $boxes = array_slice( $boxes, 0, 3 );
@@ -356,7 +356,7 @@
357357 if ( $json === false ) {
358358 wfWarn( __METHOD__ . ': Http::get failed' );
359359 // Most likely a timeout or other general error
360 - self::reportTranslationSerficeFailure( $serviceName );
 360+ self::reportTranslationServiceFailure( $serviceName );
361361
362362 return null;
363363 } elseif ( !is_object( $response ) ) {
@@ -376,7 +376,7 @@
377377 $wgMemc->set( $memckey, $unsupported, 60 * 60 * 8 );
378378 } else {
379379 // Unknown error, assume the worst
380 - self::reportTranslationSerficeFailure( $serviceName );
 380+ self::reportTranslationServiceFailure( $serviceName );
381381 wfWarn( __METHOD__ . "($serviceName): " . $response->responseDetails );
382382 error_log( __METHOD__ . "($serviceName): " . $response->responseDetails );
383383 return null;
@@ -425,7 +425,7 @@
426426 $response = FormatJson::decode( $json );
427427
428428 if ( $json === false ) {
429 - self::reportTranslationSerficeFailure( $serviceName );
 429+ self::reportTranslationServiceFailure( $serviceName );
430430 return null;
431431 } elseif ( !is_object( $response ) ) {
432432 error_log( __METHOD__ . ': Unable to parse reply: ' . strval( $json ) );
@@ -479,7 +479,7 @@
480480 $json = Http::post( $config['url'], $options );
481481 $response = FormatJson::decode( $json );
482482 if ( $json === false || !is_object( $response ) ) {
483 - self::reportTranslationSerficeFailure( $serviceName );
 483+ self::reportTranslationServiceFailure( $serviceName );
484484 break; // Too slow, back off
485485 } elseif ( $response->responseStatus !== 200 ) {
486486 error_log( __METHOD__ . " with ($serviceName ($candidate)): " . $response->responseDetails );
@@ -937,32 +937,49 @@
938938 /**
939939 * Checks whether the given service has exceeded failure count */
940940 public static function checkTranslationServiceFailure( $service ) {
941 - global $wgMemc;
 941+ global $wgMemc, $wgContLang;
942942
943943 $key = wfMemckey( "translate-service-$service" );
 944+ $value = $wgMemc->get( $key );
 945+ if ( !is_string( $value ) ) return false;
 946+ list( $count, $failed ) = explode( '|', $value, 2 );
944947
945 - // Both false and null are converted to zero, which is desirable
946 - return intval( $wgMemc->get( $key ) ) >= self::$serviceFailureCount;
 948+ if ( $failed + ( 2 * self::$serviceFailurePeriod ) < wfTimestamp() ) {
 949+ error_log( "Translation service $service (was) restored" );
 950+ $wgMemc->delete( $key );
 951+ return false;
 952+ } elseif ( $failed + self::$serviceFailurePeriod < wfTimestamp() ) {
 953+ /* We are in suspicious mode and one failure is enough to update
 954+ * failed timestamp. If the service works however, let's use it.
 955+ * Previous failures are forgotten after another failure period
 956+ * has passed */
 957+ return false;
 958+ }
 959+
 960+ return $count >= self::$serviceFailureCount;
947961 }
948962
949963 /**
950964 * Increases the failure count for a given service */
951 - public static function reportTranslationSerficeFailure( $service ) {
 965+ public static function reportTranslationServiceFailure( $service ) {
952966 global $wgMemc;
953967
954968 $key = wfMemckey( "translate-service-$service" );
955 - // Both false and null are converted to zero, which is desirable.
956 - /* FIXME: not atomic, but the default incr() implemention seems to
957 - * ignore expiry time */
958 - $count = intval( $wgMemc->get( $key ) );
959 - $wgMemc->set( $key, $count + 1, self::$serviceFailurePeriod );
 969+ $value = $wgMemc->get( $key );
 970+ if ( !is_string( $value ) ) {
 971+ $count = 0;
 972+ } else {
 973+ list( $count, ) = explode( '|', $value, 2 );
 974+ }
960975
961 - /* By using >= we expose if something is still increasing failure
962 - * count if we are over the limit */
963 - if ( $count + 1 >= self::$serviceFailureCount ) {
964 - $language = Language::factory( 'en' );
965 - $period = $language->formatTimePeriod( self::$serviceFailurePeriod );
966 - error_log( "Translation service $service suspended for $period" );
 976+ $count += 1;
 977+ $failed = wfTimestamp();
 978+ $wgMemc->set( $key, "$count|$failed", self::$serviceFailurePeriod*5 );
 979+
 980+ if ( $count == self::$serviceFailureCount ) {
 981+ error_log( "Translation service $service suspended" );
 982+ } elseif( $count > self::$serviceFailureCount ) {
 983+ error_log( "Translation service $service still suspended" );
967984 }
968985 }
969986 }

Status & tagging log