MaxSem has uploaded a new change for review.
https://gerrit.wikimedia.org/r/265554
Change subject: WIP: POC of automatic request wrapping that makes manual
exception handling not needed
......................................................................
WIP: POC of automatic request wrapping that makes manual exception handling not
needed
Change-Id: I3b58ee672a7e76df3aa6befb7b0edd563f1cc33a
---
M includes/ElasticsearchIntermediary.php
M includes/Version.php
2 files changed, 60 insertions(+), 20 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/CirrusSearch
refs/changes/54/265554/1
diff --git a/includes/ElasticsearchIntermediary.php
b/includes/ElasticsearchIntermediary.php
index be2008f..6afb628 100644
--- a/includes/ElasticsearchIntermediary.php
+++ b/includes/ElasticsearchIntermediary.php
@@ -5,6 +5,7 @@
use DeferredUpdates;
use Elastica\Exception\PartialShardFailureException;
use Elastica\Exception\ResponseException;
+use Exception;
use FormatJson;
use MediaWiki\Logger\LoggerFactory;
use RequestContext;
@@ -280,13 +281,37 @@
}
}
+ public function performRequest( $worker, $description, $logContext =
array() ) {
+ $this->startInternal( $description, $logContext );
+
+ try {
+ $result = call_user_func( $worker, $this );
+ $this->finishRequest();
+ return Status::newGood( $result );
+ } catch ( Exception $ex ) {
+ return $this->failureInternal( $ex );
+ }
+ }
+
/**
* Mark the start of a request to Elasticsearch. Public so it can be
called from pool counter methods.
+ *
+ * @deprecated Use performRequest()
*
* @param string $description name of the action being started
* @param array $logContext Contextual variables for generating log
messages
*/
public function start( $description, array $logContext = array() ) {
+ $this->startInternal( $description, $logContext );
+ }
+
+ /**
+ * Mark the start of a request to Elasticsearch.
+ *
+ * @param string $description name of the action being started
+ * @param array $logContext Contextual variables for generating log
messages
+ */
+ public function startInternal( $description, array $logContext =
array() ) {
$this->description = $description;
$this->logContext = $logContext;
$this->requestStart = microtime( true );
@@ -295,6 +320,8 @@
/**
* Log a successful request and return the provided result in a good
Status. If you don't need the status
* just ignore the return. Public so it can be called from pool
counter methods.
+ *
+ * @deprecated Use performRequest()
*
* @param mixed $result result of the request. defaults to null in
case the request doesn't have a result
* @return Status wrapping $result
@@ -307,10 +334,22 @@
/**
* Log a failure and return an appropriate status. Public so it can be
called from pool counter methods.
*
+ * @deprecated Use performRequest()
+ *
* @param \Elastica\Exception\ExceptionInterface|null $exception if the
request failed
* @return Status representing a backend failure
*/
public function failure( $exception = null ) {
+ return $this->failureInternal( $exception );
+ }
+
+ /**
+ * Log a failure and return an appropriate status.
+ *
+ * @param Exception|null $exception if the request failed
+ * @return Status representing a backend failure
+ */
+ protected function failureInternal( $exception = null ) {
$context = $this->logContext;
$context['took'] = $this->finishRequest();
list( $status, $message ) = $this->extractMessageAndStatus(
$exception );
@@ -380,7 +419,7 @@
* @param Exception $exception exception from which to extract a message
* @return string message from the exception
*/
- public static function extractMessage( \Exception $exception ) {
+ public static function extractMessage( Exception $exception ) {
if ( !( $exception instanceof ResponseException ) ) {
return $exception->getMessage();
}
@@ -563,10 +602,10 @@
}
/**
- * @param \Exception $exception
+ * @param Exception $exception
* @return array Two elements, first is Status object, second is string.
*/
- private function extractMessageAndStatus( \Exception $exception ) {
+ private function extractMessageAndStatus( Exception $exception ) {
if ( !$exception ) {
return array( Status::newFatal(
'cirrussearch-backend-error' ), '' );
}
diff --git a/includes/Version.php b/includes/Version.php
index 4de9a44..ea745b5 100644
--- a/includes/Version.php
+++ b/includes/Version.php
@@ -36,27 +36,28 @@
* @return Status<string> version number as a string
*/
public function get() {
- global $wgMemc, $wgCirrusSearchClientSideSearchTimeout;
+ global $wgMemc;
$mcKey = wfMemcKey( 'CirrusSearch', 'Elasticsearch', 'version'
);
$result = $wgMemc->get( $mcKey );
- if ( !$result ) {
- try {
- $this->start( 'fetching elasticsearch version',
- array( 'queryType' => 'version' ) );
+ if ( $result ) {
+ return Status::newGood( $result );
+ }
+ $that = $this;
+ return $this->performRequest(
+ function () use ( $that, $wgMemc, $mcKey ) {
+ global $wgCirrusSearchClientSideSearchTimeout;
+
+ $that->connection->setTimeout(
$wgCirrusSearchClientSideSearchTimeout['default'] );
+ $result =
$this->connection->getClient()->request( '' );
+ $result = $result->getData();
+ $result = $result[ 'version' ][ 'number' ];
+ $wgMemc->set( $mcKey, $result, 3600 * 12 );
// If this times out the cluster is in really
bad shape but we should still
// check it.
- $this->connection->setTimeout(
$wgCirrusSearchClientSideSearchTimeout[ 'default' ] );
- $result =
$this->connection->getClient()->request( '' );
- $this->success();
- } catch ( \Elastica\Exception\ExceptionInterface $e ) {
- return $this->failure( $e );
- }
- $result = $result->getData();
- $result = $result[ 'version' ][ 'number' ];
- $wgMemc->set( $mcKey, $result, 3600 * 12 );
- }
-
- return Status::newGood( $result );
+ },
+ 'fetching elasticsearch version',
+ array( 'queryType' => 'version' )
+ );
}
}
--
To view, visit https://gerrit.wikimedia.org/r/265554
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I3b58ee672a7e76df3aa6befb7b0edd563f1cc33a
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/CirrusSearch
Gerrit-Branch: master
Gerrit-Owner: MaxSem <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits