[SERVER-10594]  slaveOk bit ignored for unsharded aggregate Created: 21/Aug/13  Updated: 11/Jul/16  Resolved: 29/Aug/13

Status: Closed
Project: Core Server
Component/s: Aggregation Framework
Affects Version/s: None
Fix Version/s: 2.4.7, 2.5.3

Type: Bug Priority: Major - P3
Reporter: Bernie Hackett Assignee: Randolph Tan
Resolution: Done Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
is depended on by PYTHON-559 Aggregations are not performed on a s... Closed
Related
related to SERVER-9441 Secondaries execute distinct/aggregat... Closed
Backwards Compatibility: Fully Compatible
Operating System: ALL
Steps To Reproduce:

With this script the aggregate command is always sent to the primary:

var st = new ShardingTest({ shards: { rs0: { oplogSize: 10, verbose: 1 }}});                                                                                                                                                                 
                                                                                                                                                                                                                                             
var testDB = st.s.getDB('test');                                                                                                                                                                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                             
testDB.user.insert({ x: 10 });                                                                                                                                                                                                               
                                                                                                                                                                                                                                             
testDB.setSlaveOk(true);                                                                                                                                                                                                                     
var res = testDB.runCommand({ aggregate: 'user', pipeline: [{ $project: { x: 1 }}]});                                                                                                                                                        
jsTest.log(tojson(res));                                                                                                                                                                                                                     
                                                                                                                                                                                                                                             
st.stop();

Sharding the collection causes mongos to respect the slaveOk bit.

var st = new ShardingTest({ shards: { rs0: { oplogSize: 10, verbose: 1 }}});                                                                                                                                                                 
                                                                                                                                                                                                                                             
var testDB = st.s.getDB('test');                                                                                                                                                                                                             
testDB.adminCommand({ enableSharding: 'test' });                                                                                                                                                                                             
testDB.adminCommand({ shardCollection: 'test.user', key: { x: 1 }});                                                                                                                                                                         
                                                                                                                                                                                                                                             
testDB.user.insert({ x: 10 });                                                                                                                                                                                                               
                                                                                                                                                                                                                                             
testDB.setSlaveOk(true);                                                                                                                                                                                                                     
var res = testDB.runCommand({ aggregate: 'user', pipeline: [{ $project: { x: 1 }}]});                                                                                                                                                        
jsTest.log(tojson(res));                                                                                                                                                                                                                     
                                                                                                                                                                                                                                             
st.stop();      

Participants:

 Description   
Issue Status as of October 9th, 2013

ISSUE SUMMARY
The aggregate command only respects the slaveOk bit if the collection the aggregate command is run against is sharded. For unsharded collections aggregate is always sent to the primary.

USER IMPACT
This only impacts unsharded collections in a sharded cluster. A user can inadvertently send aggregate commands via mongos to unsharded collections to a primary replica set member when they were intended for a secondary member. This could cause unexpected load on the shard primary node and impact performance. It is present in versions of MongoDB prior to and including v2.4.6.

SOLUTION
Pass the query options to the aggregate command

WORKAROUNDS
No workaround.

PATCHES
Production release v2.4.7 contains the fix for this issue, and production release v2.6.0 will contain the fix as well.

Original Description

The summary says it all. The aggregate command only respects the slaveOk bit if the collection the aggregate command is run against is sharded. For unsharded collections aggregate is always sent to the primary.

The problem only seems to exist with the aggregate command. Commands like count and distinct seem to respect slaveOk properly.



 Comments   
Comment by auto [ 03/Oct/13 ]

Author:

{u'username': u'monkey101', u'name': u'Dan Pasette', u'email': u'dan@10gen.com'}

Message: SERVER-10594 slaveOk bit ignored for unsharded aggregate

Backport of original commit: 84ff1694c4bf92f3deef5c013ac9a5b912e6f96f
Branch: v2.4
https://github.com/mongodb/mongo/commit/22252fdaa6ec8744bb191c3d09d84e5e6e3f4058

Comment by Daniel Pasette (Inactive) [ 17/Sep/13 ]

backport test fix as well:
https://github.com/mongodb/mongo/commit/8b7ab2786e10cbe6bb84363ccb20c46ad5096f64

Comment by auto [ 29/Aug/13 ]

Author:

{u'username': u'renctan', u'name': u'Randolph Tan', u'email': u'randolph@10gen.com'}

Message: SERVER-10594 slaveOk bit ignored for unsharded aggregate
Branch: master
https://github.com/mongodb/mongo/commit/84ff1694c4bf92f3deef5c013ac9a5b912e6f96f

Comment by Randolph Tan [ 21/Aug/13 ]

Looks like this is caused by the aggregate command using the passthrough overload without the option argument so it is completely ignored:

https://github.com/mongodb/mongo/blob/v2.4/src/mongo/s/commands_public.cpp#L1818

Note: Code is slightly different in master

Generated at Thu Feb 08 03:23:34 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.