`db:mongoid:shard_collections` returns incorrect result when using an optimized SRV connection string

XMLWordPrintableJSON

    • Type: Question
    • Resolution: Fixed
    • Priority: Major - P3
    • 9.0.7
    • Affects Version/s: None
    • Component/s: Integrations
    • None
    • None
    • Fully Compatible
    • Ruby Drivers
    • Not Needed
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?
    • None
    • None
    • None
    • None
    • None
    • None

      MongoID produces incorrect results when connecting to a sharded cluster via an optimized SRV connection string. Specifically, warning messages suggest that the shard configuration is not persisted in the sharded cluster when using a load-balanced topology.

      Warning Message:

      W, [2025-05-16T05:32:41.600441 #824091]  WARN -- : MONGOID: Product has shard config but is not persisted in a sharded cluster: #<Cluster topology=LoadBalanced[pl-0-ap-southeast-2.fp6lz.mongodb.net:1175] servers=[#<Server address=pl-0-ap-southeast-2.fp6lz.mongodb.net:1175 LB NO-MONITORING>]>
      

      The issue can be reproduced on both MongoID versions 9.0.6 and 8.0.10.

      Please confirm whether this behavior is caused by a defect in MongoID or a limitation in functionality that needs to be documented for users.

      Reproduction Steps:

      Testing Environment:

      $ uname -a
      Linux ip-172-31-2-67.ap-southeast-2.compute.internal 6.1.112-124.190.amzn2023.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Oct 23 06:32:04 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux

      $ ruby -v
      ruby 3.1.0p0 (2021-12-25 revision fb4df44d16) [x86_64-linux]

      Software versions:

      • MongoDB: 8.0.9
      • MongoID: 9.0.6 (confirmed reproduction on 8.0.10)
      • Ruby Driver: 2.21.1

      Gemfile:

      cat > Gemfile <<EOF
      source 'https://rubygems.org'
      
      gem 'mongoid', '~> 9.0'
      gem 'rake'
      EOF
      
      Install dependencies using:
      bundle install --path vendor/bundle
      

      Test Model:

      mkdir -p app/models/
      cat > app/models/product.rb <<EOF
      class Product
        include Mongoid::Document
      
        store_in collection: "products"
      
        field :sku, type: String
        field :name, type: String
        field :price, type: Float
      
        # Shard by SKU
        shard_key({ sku: 1 })  
      end
      EOF
      

      MongoID Configuration File:

      mkdir config
      cat > config/mongoid.yml <<EOF
      development:
        clients:
          default:
            uri: "mongodb+srv://*****:*****@nycluster2-sharded-pl-0-lb.fp6lz.mongodb.net/test?tls=true"
            options:
              server_selection_timeout: 5
      #      uri: "mongodb+srv://****:****@nycluster2-sharded.fp6lz.mongodb.net/test?tls=true"
      EOF
      

      Rakefile:

      cat > Rakefile <<EOF
      require 'mongoid'
      require 'mongoid/tasks/database'
      
      Mongoid.load!(File.join(__dir__, 'config', 'mongoid.yml'), :development)
      
      \$LOAD_PATH.unshift(File.expand_path("app/models", __dir__))  
      
      Dir.glob(File.join(__dir__, 'app', 'models', '**', '*.rb')).each do |file|
        require file
      end
      
      task :environment
      
      load 'mongoid/tasks/database.rake'
      EOF
      

      Run the MongoID task:

      bundle exec rake db:mongoid:shard_collections
      

      Observed Results:

      Case 1: Using Optimized SRV Connection String:

      Connection URI:

      uri: "mongodb+srv://*****:*****@cluster2-sharded-pl-0-lb.fp6lz.mongodb.net/test?tls=true"
      
      Run the task:

      $ bundle exec rake db:mongoid:shard_collections

      W, [2025-05-16T03:19:43.992593 #998276|#998276] WARN – : MONGOID: Product has shard config but is not persisted in a sharded cluster: 
      #<Cluster topology=LoadBalanced[pl-0-ap-southeast-2.fp6lz.mongodb.net:1175] servers=[#<Server address=pl-0-ap-southeast-2.fp6lz.mongodb.net:1175 LB NO-MONITORING>]>
      
      Observed Warning:

      MongoID does not persist the shard configuration in the cluster topology. Instead, it shows a load-balanced topology warning and no shard confirmation.

      Case 2: Using Standard SRV Connection String:

      Connection URI:

      uri: "mongodb+srv://*****:*****@nycluster2-sharded.fp6lz.mongodb.net/test?tls=true"
      

      Run the task:

      $ bundle exec rake db:mongoid:shard_collections

      I, [2025-05-16T03:17:45.701237 #998154|#998154] INFO – : MONGOID: test.products is already sharded for Product
      
      Observed Result:

      MongoID successfully detects that the test.products collection is already sharded for the Product model, and no warning is generated.

      Conclusion:

      The issue occurs when using the optimized SRV connection string, but there is no problem when using the legacy SRV or the standard connection string.

            Assignee:
            Jamis Buck
            Reporter:
            Nopavis Yamabhai
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: