在AutoReconnect异常后,我可以继续使用pymongo游标对象吗?

时间:2022-01-31 08:10:55

When you receive an AutoReconnect exception from a database query, a common practice is to wait a bit then try the query again (perhaps ad-infinitum in some contexts).

当您从数据库查询中收到AutoReconnect异常时,通常的做法是稍等一下,然后再次尝试查询(在某些情况下可能是无限的)。

If this happens while traversing a cursor, does it make sense to continue attempting to get data from the same cursor object, or do I have to create a new one and start from scratch? Are pymongo cursor objects able to handle this situation gracefully without missing data?

如果在遍历游标时发生这种情况,继续尝试从同一个游标对象获取数据是否有意义,还是我必须创建一个新的并从头开始? pymongo游标对象是否能够优雅地处理这种情况而不会丢失数据?

Let's assume the problem was some sort of temporary network interruption, and that the server is fine (and hence should still know about the cursor).

让我们假设问题是某种临时网络中断,并且服务器很好(因此应该仍然知道光标)。

1 个解决方案

#1


The cursor is stored on the MongoDB server, and all operations are also performed on the server (e.g. sorting, limiting, etc). Besides, the server transfers the data to the client in chunks (see documentation for more details).

光标存储在MongoDB服务器上,所有操作也在服务器上执行(例如排序,限制等)。此外,服务器以块的形式将数据传输到客户端(有关详细信息,请参阅文档)。

So imagine the following scenario:

想象一下以下场景:

         Client                        Server
         ------                        ------
           |                              |
           |                              |
       make query  =======================>
           |                              |
           |                        process query
           |                              |
           |                       construct cursor
           |                              |                   +--------+
           |                         store cursor ----------> | Cursor |
           |                              |                   +--------+
           <===================== return cursor/chunk
           |                              |
   iterate half chunk                     |
           |                         SERVER GOES DOWN
           |             -----------------------------------------------------------
           |                              :                      X
  iterate till the end                    :                      ^
           |                        SERVER COMES LIVE            |
           |                              |                      |
 request the next chunk ==================> ---------------------+ The cursor no longer exists
           |                              |
           <============================= |
           |    server responds with a    |
           |    pretty much intelligent   |
           |            error             |

If you run this scenario on the Mongo Shell, you will get a nice Error: getMore: cursor didn't exist on server, possible restart or timeout?.

如果你在Mongo Shell上运行这个场景,你会得到一个很好的错误:getMore:游标在服务器上不存在,可能重启或超时?

Thus, it would be reasonable to start the request from the beginning.

因此,从一开始就开始请求是合理的。

#1


The cursor is stored on the MongoDB server, and all operations are also performed on the server (e.g. sorting, limiting, etc). Besides, the server transfers the data to the client in chunks (see documentation for more details).

光标存储在MongoDB服务器上,所有操作也在服务器上执行(例如排序,限制等)。此外,服务器以块的形式将数据传输到客户端(有关详细信息,请参阅文档)。

So imagine the following scenario:

想象一下以下场景:

         Client                        Server
         ------                        ------
           |                              |
           |                              |
       make query  =======================>
           |                              |
           |                        process query
           |                              |
           |                       construct cursor
           |                              |                   +--------+
           |                         store cursor ----------> | Cursor |
           |                              |                   +--------+
           <===================== return cursor/chunk
           |                              |
   iterate half chunk                     |
           |                         SERVER GOES DOWN
           |             -----------------------------------------------------------
           |                              :                      X
  iterate till the end                    :                      ^
           |                        SERVER COMES LIVE            |
           |                              |                      |
 request the next chunk ==================> ---------------------+ The cursor no longer exists
           |                              |
           <============================= |
           |    server responds with a    |
           |    pretty much intelligent   |
           |            error             |

If you run this scenario on the Mongo Shell, you will get a nice Error: getMore: cursor didn't exist on server, possible restart or timeout?.

如果你在Mongo Shell上运行这个场景,你会得到一个很好的错误:getMore:游标在服务器上不存在,可能重启或超时?

Thus, it would be reasonable to start the request from the beginning.

因此,从一开始就开始请求是合理的。