Mongo 通过 Grails 域标准在活动游标中出现 CursorNotFound 异常

Mongo CursorNotFound exception in active cursor via Grails domain criteria

我正在使用 Grails 2.4.4,mongo 插件 3.0.2,MongoDB 2.4.10 使用远程数据库连接。

grails {
    mongo {
        host = "11.12.13.14"    // A remote server IP
        port = 27017
        databaseName = "blogger"
        username = "blog"
        password = "xyz"
        options {
            autoConnectRetry = true
            connectTimeout = 3000
            connectionsPerHost = 40
            socketTimeout = 120000
            threadsAllowedToBlockForConnectionMultiplier = 5
            maxAutoConnectRetryTime=5
            maxWaitTime=120000
        }
    }
}

在我们应用程序的一部分中,服务方法遍历 20,000 个用户并向他们发送电子邮件:

Person.withCriteria {       // Line 323
    eq("active", true)
    order("dateJoined", "asc")
}.each { personInstance ->
   // Code to send an email which takes an average of 1 second
}

在为大约 6000 名用户执行此操作后,我得到一个 MongoDB 游标异常:

2015-04-11 07:31:14,218 [quartzScheduler_Worker-1] ERROR listeners.ExceptionPrinterJobListener  - Exception occurred in job: Grails Job
org.quartz.JobExecutionException: com.mongodb.MongoException$CursorNotFound: Cursor 1337814790631604331 not found on server 11.12.13.14:27017 [See nested exception: com.mongodb.MongoException$CursorNotFound: Cursor 1337814790631604331 not found on server 11.12.13.14:27017]
        at grails.plugins.quartz.GrailsJobFactory$GrailsJob.execute(GrailsJobFactory.java:111)
        at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
        at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)
Caused by: com.mongodb.MongoException$CursorNotFound: Cursor 1337814790631604331 not found on server 11.12.13.14:27017
        at com.mongodb.QueryResultIterator.throwOnQueryFailure(QueryResultIterator.java:218)
        at com.mongodb.QueryResultIterator.init(QueryResultIterator.java:198)
        at com.mongodb.QueryResultIterator.initFromQueryResponse(QueryResultIterator.java:176)
        at com.mongodb.QueryResultIterator.getMore(QueryResultIterator.java:141)
        at com.mongodb.QueryResultIterator.hasNext(QueryResultIterator.java:127)
        at com.mongodb.DBCursor._hasNext(DBCursor.java:551)
        at com.mongodb.DBCursor.hasNext(DBCursor.java:571)
        at org.grails.datastore.mapping.mongo.query.MongoQuery$MongoResultList.hasNext(MongoQuery.java:1893)
        at com.test.person.PersonService.sendWeeklyEmail(PersonService.groovy:323)
        at com.test.WeeklyJob.execute(WeeklyJob.groovy:41)
        at grails.plugins.quartz.GrailsJobFactory$GrailsJob.execute(GrailsJobFactory.java:104)
        ... 2 more

我查找了文档,发现游标会在 20 分钟后自动关闭,当我通过日志确认时,这个异常恰好在 20 分钟后出现。

但是这种20分钟后自动关闭的行为适用于不活动的游标,但这里的游标是活动的。

更新:

看了一些文章发现,这可能是TCP keepalive超时的问题。所以我们把TCP keepalive超时时间从默认的2小时改成了2分钟,但是还是没有解决问题。

这台服务器上的 MongoDB 似乎存在兼容性问题。阅读更多有关 Jira 的详细信息 https://jira.mongodb.org/browse/SERVER-18439

希望这对其他人有帮助!