Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB How do we get all CURRENT open cursors and the queries they are running?

Tags:

mongodb

Some users open cursors to mongo with noTimeout set. I want to list all the open cursors, their IDs, their options, the query they are running under, the start time for that cursor, the lasttime it was used(getmore). Is there any command to do that? Can someone point me to any hints so that I can write a small app if needed.

like image 365
sam Avatar asked Jan 08 '14 18:01

sam


1 Answers

MongoDB has a command 'currentOp' which contains current operations (in general, not only open cursors). The result is an array of "inprog" values. You have the thread id of the operation, not a cursor id but it's very useful to investigate heavy operations or those ones that've been running for a long time. It's not perfectly what you want but I think you could write a small program that parse time running of operations to identify which one have been running for a certain period of time.

Look at an example of my database of an aggregation I run just for testing purposes, I will hide some data because it's very sensible in our case :)

"inprog" : [
    {
        "opid" : 74074645,
        "active" : true,
        "secs_running" : 2,
        "op" : "query",
        "ns" : "mydb.Terms.ByHour",
        "query" : {
            "aggregate" : "Terms.ByHour",
            "pipeline" : [
                {
                    "$match" : {
                        "cluster" : "my_key",
                        "start" : {
                            "$gte" : ISODate("2013-11-10T00:00:00Z"),
                            "$lte" : ISODate("2013-11-11T00:00:00Z")
                        }
                    }
                },
                {
                    "$group" : {
                        "_id" : "$start",
                        "count" : {
                            "$sum" : "$count"
                        }
                    }
                },
                {
                    "$sort" : {
                        "_id" : 1
                    }
                }
            ]

Just put the result in a variable:

currentOps = db.currentOp()

and use it as a regular json, or write a small program that iterate currentOps.inprog array and check for secs_running > something

I hope it helps.

like image 152
Maximiliano Rios Avatar answered Oct 19 '22 17:10

Maximiliano Rios