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
- Request operation type, INSERT, UPDATE, delete ...
- Request the corresponding Connectionid,threadid
- Whether the request is waiting for a lock
- Request Execution Time
- DB or collection of request operation
- Request content for Query
- ...
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