"MongoDB" MongoDB Management: Use Killop to kill long Running operation

Source: Internet
Author: User
Tags createindex mongodb mongo shell



http://www.mongoing.com/archives/2563



MongoDB provides a killOp request to kill long-running requests. killOp usually needs to be combined with currentOp; first query the requested opid according to currentOp, and then send killOp request according to opid.


Currentop


Currentop use, refer to the official documentation



Currentop will list the requests that are executing on the backend mongod, or it can be filtered based on the query criteria, such as the request type, whether the request is waiting for a lock, or the DB or collection that requested the operation.



Example 1: Query all write operations that are waiting for a lock


db.currentOp(
   {
     "waitingForLock" : true,
     $or: [
        { "op" : { "$in" : [ "insert", "update", "remove" ] } },
        { "query.findandmodify": { $exists: true } }
    ]
   }
)


Example 2: Query all requests that have db1 and have been executed for more than 3 seconds


db.currentOp(
   {
     "active" : true,
     "secs_running" : { "$gt" : 3 },
     "ns" : /^db1\./
   }
)


The Currentop filter conditions include


    1. Request operation type, INSERT, UPDATE, delete ...
    2. Request the corresponding Connectionid,threadid
    3. Whether the request is waiting for a lock
    4. Request Execution Time
    5. DB or collection of request operation
    6. Request content for Query
    7. ...
Killop


Currentop output, each request contains a opid field, with Opid, you can send killop to kill the corresponding request.


db.killOp(opid)


To understand the meaning of killop, we need to clarify a few questions first.


Will the request executed on the connection end immediately after the client-to-MONOGD server connection is broken?


For example, by MONGO Shell, you send a CreateIndex request to index a collection of 1000w documents, the request takes a long time, you want to abort the request prematurely, Ctrl-c stops the MONGO shell, and MONGO The shell-to-server connection is shut down.



But back-end CreateIndex requests (MongoDB each connection request by a corresponding thread to handle) will not end immediately, but will continue to execute until the end of CreateIndex, to send a reply to the client, found that the connection has been closed, and then the line friend exit.



In order for CreateIndex to end early, you need killop to help, currentop find the opid of Craeteindex request, then send Killop,createindex will end execution at the next "checkpoint" and the entire thread exits.


Will the request end immediately after sending the Killop?


The implementation principle of Killop is as follows



The service thread that corresponds to each connection stores a killpending field, and when sending Killop, the field is set to 1; During execution, the request can be called operationcontext::checkforinterrupt () To check if the killpending is set and if it is set, the thread exits.



A request to support Killop, must add the Checkforinterrupt () checkpoint in the processing logic of the request, or even if the killop is sent, it can only wait until the request is fully processed before the thread exits.



For example, CreateIndex's processing logic contains code similar to the following, in the CreateIndex cycle, once the killpending is set to 1, CreateIndex execution can exit at the end of the current loop.


while (!createIndexFinished) {
    createIndexForOneElement();
    checkForInterupt();
}


So after sending Killop, the request is executed to the next "checkpoint" thread to exit, and MongoDB joins the Checkforinterrupt () checkpoint in many potentially lengthy requests, such as creating an index, repair database,mapreduce , aggregation and so on.



"MongoDB" MongoDB Management: Use Killop to kill long Running operation


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.