I'm using Google App Engine v. 1.9.48. During some of my data store queries, I am randomly getting "CancellationException: Task was cancelled" error. And I'm not really sure what exactly is causing this error. From other Stackoverflow posts, I vaguely understand that this has to do with timeouts, but not entirely sure what is causing this. I'm not using any TaskQueues - if that helps.
Below is the stack trace:
java.util.concurrent.CancellationException: Task was cancelled.
at com.google.common.util.concurrent.AbstractFuture.cancellationExceptionWithCause(AbstractFuture.java:1126)
at com.google.common.util.concurrent.AbstractFuture.getDoneValue(AbstractFuture.java:504)
at com.google.common.util.concurrent.AbstractFuture.get(AbstractFuture.java:407)
at com.google.common.util.concurrent.AbstractFuture$TrustedFuture.get(AbstractFuture.java:86)
....
at com.sun.proxy.$Proxy14.size(Unknown Source)
at main.java.com.continentalist.app.model.Model.getEntitySentimentCounts(Model.java:285)
at main.java.com.continentalist.app.model.Model.access$100(Model.java:37)
at main.java.com.continentalist.app.model.Model$2.vrun(Model.java:251)
at com.googlecode.objectify.VoidWork.run(VoidWork.java:14)
at com.googlecode.objectify.VoidWork.run(VoidWork.java:11)
at com.googlecode.objectify.ObjectifyService.run(ObjectifyService.java:81)
...
My app engine code that is throwing that error is here. I added in line comments where the error is being thrown at (typically at one of the list().size()
):
private EntityAnalysis getEntitySentimentCounts(ComboCall comboCall) {
Query<ObjectifyArticle> queryArticles = ofy().load().type(ObjectifyArticle.class);
queryArticles = queryArticles.filter("domain", comboCall.getDomain());
Set<Entity> entitySet = comboCall.getEntitySet();
SentimentCount[] allSentimentCounts = new SentimentCount[entitySet.size()];
int index = 0;
for(Entity eachEntity : entitySet) {
SentimentCount sentimentCount = new SentimentCount();
String eachEntityName = eachEntity.getText();
Query<ObjectifyArticle> newQuery = queryArticles;
newQuery = newQuery.filter("entityName", eachEntityName);
sentimentCount.setEntityName(eachEntityName);
Query<ObjectifyArticle> positiveFilter = newQuery;
positiveFilter = positiveFilter.filter("entityType", POSITIVE);
int positive = positiveFilter.list().size(); // ERROR EITHER HERE
sentimentCount.setPositiveCount(positive+"");
Query<ObjectifyArticle> negativeFilter = newQuery;
negativeFilter = negativeFilter.filter("entityType", NEGATIVE);
int negative = negativeFilter.list().size(); // OR HERE
sentimentCount.setNegativeCount(""+negative);
Query<ObjectifyArticle> neutralFilter = newQuery;
neutralFilter = neutralFilter.filter("entityType", NEUTRAL);
int neutral = neutralFilter.list().size(); // OR HERE
sentimentCount.setNeutralCount(""+neutral);
allSentimentCounts[index] = sentimentCount;
index++;
}
EntityAnalysis entityAnalysis = new EntityAnalysis();
entityAnalysis.setDomain(comboCall.getDomain());
entityAnalysis.setSentimentCount(allSentimentCounts);
return entityAnalysis;
}
Such error occurrs due to following common reasons:
Any API call like datastore.list times out
Solution: read data in paged manner via cursor
Parent method hits the request time out limit e.g. cronjob hits 10 mins limit in standard app engine Solution: Increase time out or use multi-threading or use cache to expedite the execution
You don't need to call
.list().size()
, you can simply callcount()
.If you simply count, use a keys-only query - it's free and much faster.
Don't forget to set
chunckAll()
on your query when you expect to process a large number of entities. It's much faster than a default setting.If you still run into these exceptions, you need to use cursors in your query.