Re: How to kill/terminate map_reduce job alone?

506 views
Skip to first unread message

Jeremy Mikola

unread,
Aug 29, 2012, 11:09:54 AM8/29/12
to mongod...@googlegroups.com
You should be able to observe the map/reduce job using db.currentOp(). That will return a list of in-progress operations, along with their operation ID's and other details. You can then use db.killOp() to kill an operation by its ID.

Using the mongo shell, you can see what each command does internally, too. Omitting "()" from for "db.currentOp" on the shell shows:

function (arg) {
    var q = {};
    if (arg) {
        if (typeof arg == "object") {
            Object.extend(q, arg);
        } else if (arg) {
            q.$all = true;
        }
    }
    return this.$cmd.sys.inprog.findOne(q);
}

So, even if pymongo doesn't have a method for the currentOp or killOp shell helpers, you can implement it yourself. Additionally, you can query the current Ops. This would return any map/reduce jobs on the foo collection:

db.currentOp({"query.mapreduce":"foo"});

As for cleaning up temp collections, you can do that manually. It will vary depending on your output options for the map/reduce job. Thankfully, those options should be included (along with the map and reduce functions) in the record returned by the currentOp query.

That should do it, but you might also find this documentation article helpful: http://www.mongodb.org/display/DOCS/Viewing+and+Terminating+Current+Operation

Also, I'll cross-reference this with the Stack Overflow question: http://stackoverflow.com/q/12171586/162228

Ganesh

unread,
Aug 29, 2012, 9:45:35 PM8/29/12
to mongod...@googlegroups.com
Thanks for the quick reply.
I could find current_op() in pymongo but not killOp equivalent  .....I searched but no luck.....:-( can you please help me on the same?...

Jeremy Mikola

unread,
Aug 29, 2012, 10:29:29 PM8/29/12
to mongod...@googlegroups.com
On Wednesday, August 29, 2012 9:45:35 PM UTC-4, Ganesh wrote:
Thanks for the quick reply.
I could find current_op() in pymongo but not killOp equivalent  .....I searched but no luck.....:-( can you please help me on the same?...

Once again, you do not need a helper method in pymongo. Omitting "()" for "db.killOp" in the JS shell reveals:

function (op) {
    if (!op) {
        throw "no opNum to kill specified";
    }
    return this.$cmd.sys.killop.findOne({op:op});
}

You can run that query on the "$cmd.sys.killop" from any driver. This was mentioned in the documentation article I linked above: http://www.mongodb.org/display/DOCS/Viewing+and+Terminating+Current+Operation

Ganesh

unread,
Aug 29, 2012, 10:58:56 PM8/29/12
to mongod...@googlegroups.com
Hi Jeremy,
         I wrote code like this but i am getting JS error

               code = """
                    function (opid) {
                        if (!opid) {
                            throw "no opNum to kill specified";
                        }
                        return this.$cmd.sys.killop.findOne({opid:opid});
                    }
                """
               db['$cmd.sys.killop'].find_one({'opid':opid})
                print "first",opid
                return db.eval(code,opid)
                print "second"
The error on console is 
JS Error: TypeError: this.$cmd has no properties nofile_b:4
the first print gets printed but not the second one...
 
anything wrong?

Ganesh

unread,
Aug 29, 2012, 11:02:17 PM8/29/12
to mongod...@googlegroups.com
the db['$cmd.sys.killop'].find_one({'opid':opid}) line was commented ...i forgot to remove it before replying...

On Thursday, 30 August 2012 08:28:56 UTC+5:30, Ganesh wrote:
Hi Jeremy,
         I wrote code like this but i am getting JS error

               code = """
                    function (opid) {
                        if (!opid) {
                            throw "no opNum to kill specified";
                        }
                        return this.$cmd.sys.killop.findOne({opid:opid});
                    }
                """
              # db['$cmd.sys.killop'].find_one({'opid':opid})
                print "first",opid
                return db.eval(code,opid)
                print "second"

Jeremy Mikola

unread,
Aug 29, 2012, 11:45:03 PM8/29/12
to mongod...@googlegroups.com
If you're using the JS shell, you might as well use the db.killOp() function. I only pasted its definition to demonstrate:
  1. That you can view the source code of JS shell functions to see what they do internally
  2. That the killing of an operation is actually done with a findOne query on the special "$cmd.sys.killop" collection.
I don't understand why you would want to use db.eval().

Regarding point #2 above, that is only relevant if you decide you want to kill the operation from a Python script, since pymongo does not have a helper method for killOp. If you're comfortable killing the operation from the JS shell, by all means stick to the db.killOp() function.

Ganesh

unread,
Aug 30, 2012, 1:50:50 AM8/30/12
to mongod...@googlegroups.com
I am using pymongo to interact with mongoDB.  Based on the example that you gave i thought we need to use eval to execute javascript to kill the opid 
I have to kill map_reduce job from python so i couldn't find kill API in pymongo .. :-(
Which pymongo api should i use to invoke killop?

Jeremy Mikola

unread,
Aug 30, 2012, 9:06:55 AM8/30/12
to mongod...@googlegroups.com
On Thursday, August 30, 2012 1:50:50 AM UTC-4, Ganesh wrote:
I am using pymongo to interact with mongoDB.  Based on the example that you gave i thought we need to use eval to execute javascript to kill the opid 
I have to kill map_reduce job from python so i couldn't find kill API in pymongo .. :-(
Which pymongo api should i use to invoke killop?

The only API to speak of is running a query on the "$cmd.sys.killop" collection.

http://api.mongodb.org/python/current/api/pymongo/collection.html#pymongo.collection.Collection.find_one

Ganesh

unread,
Aug 30, 2012, 11:59:36 AM8/30/12
to mongod...@googlegroups.com
I tried it db['$cmd.sys.killop'].find_one({'op':int(opid)})
but i see error response {u'err': u'no op number field specified?'} ....googled and found that ....this error is set when the opid is not integer so i converted to int still i get this problem...:-(

Jeremy Mikola

unread,
Aug 30, 2012, 2:03:04 PM8/30/12
to mongod...@googlegroups.com

On Thursday, August 30, 2012 11:59:36 AM UTC-4, Ganesh wrote:
I tried it db['$cmd.sys.killop'].find_one({'op':int(opid)})
but i see error response {u'err': u'no op number field specified?'} ....googled and found that ....this error is set when the opid is not integer so i converted to int still i get this problem...:-(

Are you using pymongo 2.1 by any chance?

I was able to reproduce that problem on my end. I upgraded pymongo via pip to 2.3 and it worked. Additionally, my co-workers were able to get it working with 2.2.1, and noted the same error with 2.1.

Ganesh

unread,
Aug 30, 2012, 3:22:36 PM8/30/12
to mongod...@googlegroups.com
Yes i was. Thanks i don't see this error after upgrading to 2.3.:-)
Thanks for quick response and helping me out in resolving the issue.
Reply all
Reply to author
Forward
0 new messages