500 errors

Moped::Errors::QueryFailure: The operation: #<Moped::Protocol::Query @length=210 @request_id=88981 @response_to=0 @op_code=2004 @flags=[] @full_collection_name="comments-loadtest.contents" @skip=24800 @limit=100 @selector={"$query"=>{"_type"=>"CommentThread", "course_id"=>"BerkeleyX/ColWri2.2x/1T2014", "context"=>:course}, "$orderby"=>{"pinned"=>-1, "last_activity_at"=>-1}} @fields=nil>failed with error 17144: "Runner error: Overflow sort stage buffered data usage of 33556783 bytes exceeds internal limit of 33554432 bytes"See https://github.com/mongodb/mongo/blob/master/docs/errors.mdfor details about this error.

Hitting the 32MB limit for in-memory sort. Commonly suggested causes were missing indexes. 

Endpoint: 

"https://courses-loadtest.edx.org/api/discussion/v1/threads/?course_id={}&page_size=100" against a course with ~80,000 posts. 

Consistently fails when querying for page 249. 

"https://courses-loadtest.edx.org/api/discussion/v1/threads/?course_id=BerkeleyX/ColWri2.2x/1T2014&page_size=100&page=249"

Query:

{"$query"=>{"_type"=>"CommentThread", "course_id"=>"BerkeleyX/ColWri2.2x/1T2014", "context"=>:course}, "$orderby"=>{"pinned"=>-1, "last_activity_at"=>-1}}


There exists a query for  "_type", "course_id", "pinned" but none for "_type", "course_id", "pinned", "last_activity_at"

 Equivalent query in mongo shell:

db.contents.find({_type:"CommentThread", course_id:"BerkeleyX/ColWri2.2x/1T2014", context:"course"}).sort({"pinned":-1, "last_activity_at":-1}).explain()

Will also return the memory error. 

 

Note: Sorting by "asc" can lead to similar errors as that is not indexed specifically.

Suggested solution:

Add the index for because the default query for GET Thread_list uses last_activity_at as a filter.

db.contents.ensureIndex({"_type" : 1, "course_id" : 1, "context" : 1, "pinned" : -1, "last_activity_at" : -1 }, {"background":true});

 

Production and browser discussion api:

There is a possibility that this may be an issue on production.