Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method to fetch the current lag from the Wikibase instance #470

Merged
merged 3 commits into from
Jun 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import okhttp3.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wikidata.wdtk.util.WebResourceFetcherImpl;
import org.wikidata.wdtk.wikibaseapi.apierrors.MaxlagErrorException;
import org.wikidata.wdtk.wikibaseapi.apierrors.AssertUserFailedException;
import org.wikidata.wdtk.wikibaseapi.apierrors.MediaWikiApiErrorException;
import org.wikidata.wdtk.wikibaseapi.apierrors.MediaWikiApiErrorHandler;
Expand Down Expand Up @@ -414,9 +416,16 @@ private void buildClient() {
protected void checkErrors(JsonNode root) throws MediaWikiApiErrorException {
if (root.has("error")) {
JsonNode errorNode = root.path("error");
MediaWikiApiErrorHandler.throwMediaWikiApiErrorException(errorNode
.path("code").asText("UNKNOWN"), errorNode.path("info")
.asText("No details provided"));
String code = errorNode.path("code").asText("UNKNOWN");
String info = errorNode.path("info").asText("No details provided");
// Special case for the maxlag error since we also want to return
// the lag value in the exception thrown
if (errorNode.has("lag") && MediaWikiApiErrorHandler.ERROR_MAXLAG.equals(code)) {
double lag = errorNode.path("lag").asDouble();
throw new MaxlagErrorException(info, lag);
} else {
MediaWikiApiErrorHandler.throwMediaWikiApiErrorException(code, info);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -894,4 +894,25 @@ public void setMaxLagBackOffFactor(double value) {
maxLagBackOffFactor = value;
}

/**
* Retrieves the current lag from the target site, by making an API call.
*
* @throws MediaWikiApiErrorException
* when an unexpected MediaWiki API error happened (not the spurious
* one normally returned by MediaWiki when retrieving lag).
* @throws IOException
* when communication with the server failed.
*
*/
public double getCurrentLag() throws IOException, MediaWikiApiErrorException {
Map<String,String> parameters = new HashMap<>();
parameters.put("action", "query");
parameters.put("maxlag", "-1");
try {
this.connection.sendJsonRequest("POST", parameters);
} catch (MaxlagErrorException e) {
return e.getLag();
}
throw new IllegalStateException("MediaWiki did not return any maxlag value");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
public class MaxlagErrorException extends MediaWikiApiErrorException {

private static final long serialVersionUID = -4013361654647685959L;

protected double lag = 0;

/**
* Creates a new exception.
Expand All @@ -43,5 +45,30 @@ public class MaxlagErrorException extends MediaWikiApiErrorException {
public MaxlagErrorException(String errorMessage) {
super(MediaWikiApiErrorHandler.ERROR_MAXLAG, errorMessage);
}


/**
* Creates an exception which also stores the lag announced by the server.
*
* @param errorMessage
* the error message reported by MediaWiki, or any other
* meaningful message for the user
* @param lag
* the value of the reported lag, in seconds
*/
public MaxlagErrorException(String errorMessage, double lag) {
super(MediaWikiApiErrorHandler.ERROR_MAXLAG, errorMessage);
this.lag = lag;
}

/**
* Retrieves the amount of lag announced by the server when this
* error was emitted. May return 0 if the lag was not extracted from
* the server response.
*
* @return
* the lag on the target server
*/
public double getLag() {
return lag;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.junit.*;
import org.wikidata.wdtk.testing.MockStringContentFactory;
import org.wikidata.wdtk.wikibaseapi.apierrors.AssertUserFailedException;
import org.wikidata.wdtk.wikibaseapi.apierrors.MaxlagErrorException;
import org.wikidata.wdtk.wikibaseapi.apierrors.MediaWikiApiErrorException;

import com.fasterxml.jackson.databind.JsonNode;
Expand Down Expand Up @@ -232,6 +233,18 @@ public void testErrors() throws IOException,
root = mapper.readTree(path.openStream());
connection.checkErrors(root);
}

@Test
public void testMaxlagError() throws IOException, MediaWikiApiErrorException {
JsonNode root;
URL path = this.getClass().getResource("/error-maxlag-full.json");
root = mapper.readTree(path.openStream());
try {
connection.checkErrors(root);
} catch(MaxlagErrorException e) {
assertEquals(3.45, e.getLag(), 0.001);
}
}

@Test
public void testClearCookies() throws LoginFailedException, IOException, MediaWikiApiErrorException {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.wikidata.wdtk.wikibaseapi;

import static org.junit.Assert.assertEquals;

/*
* #%L
* Wikidata Toolkit Wikibase API
Expand Down Expand Up @@ -191,5 +193,22 @@ public void testNoTask() throws IOException, MediaWikiApiErrorException {
new MockBasicApiConnection(), Datamodel.SITE_WIKIDATA);
weea.wbEditEntity(null, null, null, null, "{}", false, false, 0, null, null);
}

@Test
public void testGetLag() throws IOException, MediaWikiApiErrorException {
MockBasicApiConnection con = new MockBasicApiConnection();
Map<String, String> params = new HashMap<>();
params.put("action", "query");
params.put("maxlag", "-1");
params.put("format", "json");
con.setWebResourceFromPath(params, this.getClass(),
"/error-maxlag-full.json",
CompressionType.NONE);

WbEditingAction weea = new WbEditingAction(
con, Datamodel.SITE_WIKIDATA);
double lag = weea.getCurrentLag();
assertEquals(3.45, lag, 0.001);
}

}
12 changes: 12 additions & 0 deletions wdtk-wikibaseapi/src/test/resources/error-maxlag-full.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"error": {
"code": "maxlag",
"info": "Waiting for all: 3.45 seconds lagged.",
"host": "all",
"lag": 3.45,
"type": "wikibase-queryservice",
"queryserviceLag": 626,
"*": "See https://www.wikidata.org/w/api.php for API usage. Subscribe to the mediawiki-api-announce mailing list at &lt;https://lists.wikimedia.org/mailman/listinfo/mediawiki-api-announce&gt; for notice of API deprecations and breaking changes."
},
"servedby": "mw1280"
}