Understanding “CancellationException: Task was cancelled” error while doing a Google Datastore query

僤鯓⒐⒋嵵緔 提交于 2019-12-31 04:43:33

问题


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;
    }

回答1:


  1. You don't need to call .list().size(), you can simply call count().

  2. If you simply count, use a keys-only query - it's free and much faster.

  3. 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.




回答2:


Such error occurrs due to following common reasons:

  1. Any API call like datastore.list times out
    Solution: read data in paged manner via cursor

  2. 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



来源:https://stackoverflow.com/questions/44069779/understanding-cancellationexception-task-was-cancelled-error-while-doing-a-go

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!