Lucas Werkmeister (WMDE) has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/372044 )

Change subject: Make SparqlHelper timeout detection configurable
......................................................................

Make SparqlHelper timeout detection configurable

SparqlHelper’s timeout detection (used to count timeouts in statsd) is
extracted into a separate function, and a configuration variable is
added for the exception class(es) that it looks for in the error
message.

In addition to the old timeout exception class, a second value is added
to the default value of that configuration variable, which reflects the
recently changed behavior of the Wikidata Query Service.

Tests for the new function are also added.

Bug: T173368
Change-Id: I8d4fde6fa03b9036380aaf505713cdaf7433d571
---
M extension.json
M includes/ConstraintCheck/Helper/SparqlHelper.php
M tests/phpunit/Helper/SparqlHelperTest.php
3 files changed, 97 insertions(+), 2 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikibaseQualityConstraints
 refs/changes/44/372044/1

diff --git a/extension.json b/extension.json
index 40229ec..5a0acef 100644
--- a/extension.json
+++ b/extension.json
@@ -124,6 +124,14 @@
                        "description": "The maximum runtime for queries on the 
configured SPARQL endpoint, in milliseconds.",
                        "public": true
                },
+               "WBQualityConstraintsSparqlTimeoutExceptionClasses": {
+                       "value": [
+                               "com.bigdata.bop.engine.QueryTimeoutException",
+                               "java.util.concurrent.TimeoutException"
+                       ],
+                       "description": "Strings that, when they occur inside an 
error response of the SPARQL endpoint, indicate that the error is a query 
timeout. On the Wikidata Query Service, these are fully qualified names of 
exception classes.",
+                       "public": true
+               },
                "WBQualityConstraintsCheckFormatConstraint": {
                        "value": true,
                        "description": "Whether or not to check the 'format' 
constraint. If this flag is set to false, any check of the 'format' constraint 
will return a 'todo' status with the 'wbqc-violation-message-security-reason' 
message.",
diff --git a/includes/ConstraintCheck/Helper/SparqlHelper.php 
b/includes/ConstraintCheck/Helper/SparqlHelper.php
index 85ee746..13400b4 100644
--- a/includes/ConstraintCheck/Helper/SparqlHelper.php
+++ b/includes/ConstraintCheck/Helper/SparqlHelper.php
@@ -204,6 +204,22 @@
        }
 
        /**
+        * Check whether the text content of an error response indicates a 
query timeout.
+        *
+        * @param string $responseContent
+        * @return boolean
+        */
+       public function isTimeout( $responseContent ) {
+               $timeoutRegex = implode( '|', array_map(
+                       function ( $fqn ) {
+                               return preg_quote( $fqn, '/' );
+                       },
+                       $this->config->get( 
'WBQualityConstraintsSparqlTimeoutExceptionClasses' )
+               ) );
+               return (bool) preg_match( '/' . $timeoutRegex . '/', 
$responseContent );
+       }
+
+       /**
         * Runs a query against the configured endpoint and returns the results.
         *
         * @param string $query The query, unencoded (plain string).
@@ -256,8 +272,7 @@
                                
"wikibase.quality.constraints.sparql.error.http.{$request->getStatus()}"
                        );
 
-                       $timeoutExceptionClass = 
'com.bigdata.bop.engine.QueryTimeoutException';
-                       if ( strpos( $request->getContent(), " 
$timeoutExceptionClass:" ) !== false ) {
+                       if ( $this->isTimeout( $request->getContent() ) ) {
                                $this->dataFactory->increment(
                                        
'wikibase.quality.constraints.sparql.error.timeout'
                                );
diff --git a/tests/phpunit/Helper/SparqlHelperTest.php 
b/tests/phpunit/Helper/SparqlHelperTest.php
index c87cf3d..fc57202 100644
--- a/tests/phpunit/Helper/SparqlHelperTest.php
+++ b/tests/phpunit/Helper/SparqlHelperTest.php
@@ -2,6 +2,7 @@
 
 namespace WikibaseQuality\ConstraintReport\Test\Helper;
 
+use HashConfig;
 use Wikibase\DataModel\Entity\EntityIdValue;
 use Wikibase\DataModel\Entity\ItemId;
 use Wikibase\DataModel\Entity\ItemIdParser;
@@ -159,4 +160,75 @@
                }
        }
 
+       /**
+        * @dataProvider isTimeoutProvider
+        */
+       public function testIsTimeout( $content, $expected ) {
+               $sparqlHelper = new SparqlHelper(
+                       $this->getDefaultConfig(),
+                       new RdfVocabulary(
+                               'http://www.wikidata.org/entity/',
+                               
'http://www.wikidata.org/wiki/Special:EntityData/'
+                       ),
+                       new ItemIdParser()
+               );
+
+               $actual = $sparqlHelper->isTimeout( $content );
+
+               $this->assertSame( $expected, $actual );
+       }
+
+       public function testIsTimeoutRegex() {
+               $sparqlHelper = new SparqlHelper(
+                       new HashConfig( [
+                               
'WBQualityConstraintsSparqlTimeoutExceptionClasses' => [
+                                       '(?!this may look like a regular 
expression)',
+                                       '/but should not be interpreted as 
one/',
+                                       '(x+x+)+y',
+                               ]
+                       ] ),
+                       new RdfVocabulary(
+                               'http://www.wikidata.org/entity/',
+                               
'http://www.wikidata.org/wiki/Special:EntityData/'
+                       ),
+                       new ItemIdParser()
+               );
+               $content = '(x+x+)+y';
+
+               $actual = $sparqlHelper->isTimeout( $content );
+
+               $this->assertTrue( $actual );
+       }
+
+       public function isTimeoutProvider() {
+               return [
+                       'empty' => [
+                               '',
+                               false
+                       ],
+                       'syntax error' => [
+                               'org.openrdf.query.MalformedQueryException: ' .
+                                       'Encountered "<EOF>" at line 1, column 
6.',
+                               false
+                       ],
+                       'QueryTimeoutException' => [
+                               'java.util.concurrent.ExecutionException: ' .
+                                       
'java.util.concurrent.ExecutionException: ' .
+                                       
'org.openrdf.query.QueryInterruptedException: ' .
+                                       'java.lang.RuntimeException: ' .
+                                       
'java.util.concurrent.ExecutionException: ' .
+                                       
'com.bigdata.bop.engine.QueryTimeoutException: ' .
+                                       'Query deadline is expired.',
+                               true
+                       ],
+                       'TimeoutException' => [
+                               "java.util.concurrent.TimeoutException\n" .
+                                       "\tat 
java.util.concurrent.FutureTask.get(FutureTask.java:205)\n" .
+                                       "\tat 
com.bigdata.rdf.sail.webapp.BigdataServlet.submitApiTask(BigdataServlet.java:289)\n"
 .
+                                       "\tat 
com.bigdata.rdf.sail.webapp.QueryServlet.doSparqlQuery(QueryServlet.java:653)\n",
+                               true
+                       ],
+               ];
+       }
+
 }

-- 
To view, visit https://gerrit.wikimedia.org/r/372044
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I8d4fde6fa03b9036380aaf505713cdaf7433d571
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/WikibaseQualityConstraints
Gerrit-Branch: master
Gerrit-Owner: Lucas Werkmeister (WMDE) <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to