Skip to content

RUBY-3898 Remove dead cursor-pinning block, fix cursor id for OP_MSG#3073

Merged
comandeo-mongo merged 2 commits into
mongodb:masterfrom
comandeo-mongo:3898-cursor-id-opmsg
Jul 8, 2026
Merged

RUBY-3898 Remove dead cursor-pinning block, fix cursor id for OP_MSG#3073
comandeo-mongo merged 2 commits into
mongodb:masterfrom
comandeo-mongo:3898-cursor-id-opmsg

Conversation

@comandeo-mongo

Copy link
Copy Markdown
Contributor

Operation::Result#has_cursor_id? checked replies.last.respond_to?(:cursor_id), which is only true for the legacy Protocol::Reply. Every modern reply is a Protocol::Msg, so the predicate was always false. Two call sites depended on it:

  • The load-balanced cursor pin/unpin block in executable.rb never executed. LB cursors already retain their connection via explicit ownership (Cursor holds @connection since RUBY-3616, released by Cursor#check_in_connection), so the block was a redundant guard.
  • The OpenTelemetry db.mongodb.cursor_id attribute on find/aggregate spans was gated on the dead predicate and therefore never recorded (the request-message path only covers getMore/killCursors).

Changes

  • lib/mongo/operation/shared/executable.rb — removed the dead LB cursor pin/unpin block. Transaction pinning, snapshot handling, and process_result are unchanged.
  • lib/mongo/operation/result.rb — removed has_cursor_id?; made base cursor_id read the id from the OP_MSG cursor subdocument (returning 0 when absent), so it is safe to call on any command result.
  • lib/mongo/tracing/open_telemetry/command_tracer.rb — gate process_cursor_context on cursor_id.positive?. Find/aggregate spans now record db.mongodb.cursor_id.
  • Specs — replaced the #has_cursor_id? coverage in result_spec.rb with OP_MSG #cursor_id cases; updated tracer spec doubles to stub cursor_id instead of the removed predicate.

Test plan

  • bundle exec rubocop on all changed files — no offenses
  • bundle exec rspec spec/mongo/operation/result_spec.rb spec/mongo/tracing/open_telemetry/command_tracer_spec.rb — 142 examples, 0 failures
  • bundle exec rspec spec/mongo/cursor_spec.rb spec/integration/cursor_pinning_spec.rb — 46 examples, 0 failures, 5 pending (LB-only cases, skipped on local replica set)
  • LB cursor_pinning_spec cases run in the Evergreen load-balanced variant (connection-retention regression guard)

RUBY-3898

Operation::Result#has_cursor_id? checked replies.last.respond_to?(:cursor_id),
which is only true for the legacy Protocol::Reply. Every modern reply is a
Protocol::Msg, so the predicate was always false. Two call sites depended on it:

- The load-balanced cursor pin/unpin block in executable.rb never ran. LB
  cursors already retain their connection via explicit ownership (Cursor holds
  @connection since RUBY-3616), so the block was a redundant guard. Removed.
- The OpenTelemetry db.mongodb.cursor_id attribute on find/aggregate spans was
  gated on the dead predicate and never recorded.

Remove has_cursor_id?, make base Result#cursor_id read the cursor id from the
OP_MSG `cursor` subdocument (returning 0 when absent, so it is safe to call on
any command result), and gate the tracer on cursor_id.positive?. Find and
aggregate spans now record the cursor id.
@comandeo-mongo comandeo-mongo marked this pull request as ready for review July 7, 2026 10:22
@comandeo-mongo comandeo-mongo requested a review from a team as a code owner July 7, 2026 10:22
@comandeo-mongo comandeo-mongo requested review from Copilot and jamis July 7, 2026 10:22

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes cursor ID handling for OP_MSG command replies by making Operation::Result#cursor_id work across modern responses, removing an always-false predicate, and ensuring OpenTelemetry spans can record db.mongodb.cursor_id for find/aggregate results.

Changes:

  • Remove the dead Operation::Result#has_cursor_id? predicate and update cursor_id to read from the OP_MSG cursor.id subdocument (defaulting to 0).
  • Remove the non-functional load-balanced cursor pin/unpin block guarded by the dead predicate.
  • Update OpenTelemetry command tracing and specs to gate cursor context on cursor_id.positive? and stub cursor_id in doubles.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
lib/mongo/operation/shared/executable.rb Removes unreachable LB cursor pin/unpin logic guarded by the removed predicate.
lib/mongo/operation/result.rb Removes has_cursor_id?; makes cursor_id work for OP_MSG cursor.id replies with a safe default.
lib/mongo/tracing/open_telemetry/command_tracer.rb Changes cursor context gating to rely on cursor_id.positive? to enable recording cursor IDs on command spans.
spec/mongo/operation/result_spec.rb Replaces has_cursor_id? tests with OP_MSG cursor-id extraction cases.
spec/mongo/tracing/open_telemetry/command_tracer_spec.rb Updates doubles to stub cursor_id (removes predicate stubbing) and keeps span attribute expectations.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread lib/mongo/tracing/open_telemetry/command_tracer.rb
result.cursor_id can be a BSON::Int64 when replies are deserialized in
:bson mode (deserialize_as_bson: true). BSON::Int64 does not implement
positive? and is not a valid OpenTelemetry attribute type, so
process_cursor_context would raise NoMethodError once the always-false
has_cursor_id? guard was removed. Unwrap to a plain Integer before the
positivity check and before setting the span attribute.
@comandeo-mongo comandeo-mongo merged commit 3eb49bf into mongodb:master Jul 8, 2026
213 checks passed
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