Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-28739

make the js scripting engine correctly verify that accessed collections are unsharded



    • Bug
    • Status: Closed
    • Major - P3
    • Resolution: Fixed
    • 3.5.5
    • 3.5.6
    • Sharding
    • None
    • Fully Compatible
    • ALL
    • Sharding 2017-04-17, Sharding 2017-05-08


      The eval command on mongod calls into the js scripting engine.

      Since eval() is called on a database, not a namespace, it does not check the shardVersion sent in the request (or the shardVersion for the connection) directly.

      Instead, any time a namespace is accessed by the script, the scripting engine checks that the namespace is not sharded (in DBInfo::getProperty() and DBCollectionInfo::construct).

      It does this check by calling ShardingState::haveLocalShardingInfo():

      bool haveLocalShardingInfo(OperationContext* opCtx, const string& ns) {
          if (!ShardingState::get(opCtx)->enabled()) {
              return false;
          const auto& oss = OperationShardingState::get(opCtx);
          if (oss.hasShardVersion()) {
              return true;
          const auto& sci = ShardedConnectionInfo::get(opCtx->getClient(), false);
          if (sci && !sci->getVersion(ns).isStrictlyEqualTo(ChunkVersion::UNSHARDED())) {
              return true;
          return false;

      This function assumes the collection is sharded if:

      1) the OperationShardingState has a shardVersion (aka, a shardVersion was attached to the command in the request)
      2) the connection used for sending the eval() command previously set a shardVersion for the namespace.

      The mongos EvalCmd, which forwards the command to the primary shard for the database eval() was run on, does not attach a shardVersion to the cmdObj.

      On the other hand, the passthrough() function called by mongos's EvalCmd uses ShardConnection on an empty namespace.

      Therefore, only if the connection mongos gets from the pool had previously set a shardVersion for a namespace accessed by the script (e.g., due to a different, prior command), will the shard host have a ShardedConnectionInfo with a non-UNSHARDED version for the namespace, and only in this case will the haveLocalShardingInfo() check on mongod work if the collection is sharded.




            esha.maharishi@mongodb.com Esha Maharishi
            esha.maharishi@mongodb.com Esha Maharishi
            0 Vote for this issue
            2 Start watching this issue