Skip to content

CDRIVER-3969 error when creating uncompleted cursor with no server ID #1321

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 26, 2023

Conversation

kevinAlbs
Copy link
Collaborator

@kevinAlbs kevinAlbs commented Jun 26, 2023

Patch build: https://spruce.mongodb.com/version/6495b91ed1fe075c5a548f5a

Summary

Return an error if mongoc_cursor_new_from_command_reply(_with_opts) is called with a non-zero cursor ID and no server ID.

Background & Motivation

mongoc_cursor_new_from_command_reply_with_opts is expected to be used by drivers wrapping the C driver to create a mongoc_cursor_t from a server reply.

The server reply indicates whether a cursor is completed by a non-zero cursor.id field. Example with mongosh:

> db.coll.drop()
true
> db.coll.insertMany([{_id:0},{_id:1}])
{ acknowledged: true, insertedIds: { '0': 0, '1': 1 } }
> db.runCommand({find: "coll", batchSize: 1})
{
  cursor: {
    firstBatch: [ { _id: 0 } ],
    id: Long("2413824932427886184"),
    ns: 'test.coll'
  },
  ok: 1,
  '$clusterTime': {
    clusterTime: Timestamp({ t: 1687535574, i: 3 }),
    signature: {
      hash: Binary(Buffer.from("0000000000000000000000000000000000000000", "hex"), 0),
      keyId: Long("0")
    }
  },
  operationTime: Timestamp({ t: 1687535574, i: 3 })
}

The cursor is iterated by sending a getMore with the cursor ID:

> db.runCommand({getMore: Long("2413824932427886184"), batchSize: 1, collection: "coll"})
{
  cursor: {
    nextBatch: [ { _id: 1 } ],
    id: Long("2413824932427886184"),
    ns: 'test.coll'
  },
  ok: 1,
  '$clusterTime': {
    clusterTime: Timestamp({ t: 1687535645, i: 1 }),
    signature: {
      hash: Binary(Buffer.from("0000000000000000000000000000000000000000", "hex"), 0),
      keyId: Long("0")
    }
  },
  operationTime: Timestamp({ t: 1687535645, i: 1 })
}

And the server indicates the cursor is closed with a 0 cursor.id:

> db.runCommand({getMore: Long("2413824932427886184"), batchSize: 1, collection: "coll"})
{
  cursor: { nextBatch: [], id: Long("0"), ns: 'test.coll' },
  ok: 1,
  '$clusterTime': {
    clusterTime: Timestamp({ t: 1687535655, i: 1 }),
    signature: {
      hash: Binary(Buffer.from("0000000000000000000000000000000000000000", "hex"), 0),
      keyId: Long("0")
    }
  },
  operationTime: Timestamp({ t: 1687535655, i: 1 })
}

An uncompleted cursor can be iterated by sending a "getMore" command or closed with a "killCursors" command. The command is expected to be sent to the same server the cursor was created.

This PR proposes returning an error if mongoc_cursor_new_from_command_reply(_with_opts) is called with a non-zero cursor ID and a zero server ID.

If creating an uncompleted cursor with a zero server ID, the cursor does not know the server that created the cursor. The cursor cannot guarantee the "getMore" or "killCursors" commands will be sent to the originating server. If the commands are sent to the wrong server, this results in a "cursor not found" error for "getMore" or the originating server resources not being destroyed by "killCursors".

@kevinAlbs kevinAlbs marked this pull request as ready for review June 26, 2023 12:54
Copy link
Contributor

@joshbsiegel joshbsiegel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM besides minor comment

@@ -216,5 +216,28 @@ _mongoc_cursor_cmd_new_from_reply (mongoc_client_t *client,
MONGOC_ERROR_CURSOR_INVALID_CURSOR,
"Couldn't parse cursor document");
}

if (0 != cursor->cursor_id) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logic could be simplified here being that there's no other work being done inside of this if statement. However, if you'd rather leave the if statements separate for readability and comments then that's fine as well.

Suggested change
if (0 != cursor->cursor_id) {
if (0 != cursor->cursor_id && 0 == cursor->server_id) {

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like a reasonable simplification. Removed the nested if.

@kevinAlbs kevinAlbs merged commit ffb3d34 into mongodb:master Jun 26, 2023
kevinAlbs added a commit that referenced this pull request Jun 26, 2023
…#1321)

* fix type of BCON_INT32 value

* require serverID for open cursor with `mongoc_cursor_new_from_command_reply_with_opts`

* document expectations of server ID option

* assert `found` after `mongoc_cursor_error`

If an error occurs, `found` is false. Assert on the error first to print the error.

* remove unnecessary nested `if`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants