了解"CancellationException:任务已取消" Google数据存储区查询时发生错误 [英] Understanding "CancellationException: Task was cancelled" error while doing a Google Datastore query

查看:646
本文介绍了了解"CancellationException:任务已取消" Google数据存储区查询时发生错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Google App Engine 1.9.48版.在某些数据存储查询期间,我随机收到"CancellationException:任务已取消"错误.而且我不太确定到底是什么导致了此错误.来自其他 Stackoverflow

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.

下面是堆栈跟踪:

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

抛出该错误的我的App Engine代码在这里.我在引起错误的行注释中添加了注释(通常在list().size()之一):

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. 您无需致电.list().size(),您只需致电count().

如果您只是简单地数数,请使用仅键查询-它是免费的,而且速度更快.

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

当您希望处理大量实体时,请不要忘记在查询中设置chunckAll().它比默认设置快得多.

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.

这篇关于了解"CancellationException:任务已取消" Google数据存储区查询时发生错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆