J@ArangoDB

{ "subject" : "ArangoDB", "tags": [ "multi-model", "nosql", "database" ] }

Killing a Long-running Query

Suppose there is an AQL query that’s executing in the server for a long time already and you want to get rid of it. What can be done to abort that query?

If a connection to the server can still be established, the easiest is to use the ArangoShell to fetch the list of currently executing AQL queries and send a kill command to the server for the correct query.

To start, we can fetch the list of all running queries and print their ids, query strings and runtimes. This is only inspection and does not abort any query:

printing all currently running queries
1
2
var queries = require("org/arangodb/aql/queries");
queries.current();

Here’s an example result for the list of running queries:

example list of currently running queries
1
2
3
4
5
6
7
8
[
  {
    "id" : "190",
    "query" : "RETURN SLEEP(1000)",
    "started" : "2016-01-26T22:41:24Z",
    "runTime" : 218.49146389961243
  }
]

To now kill a query from the list, we can pass the query’s id to kill:

killing a specific query
1
2
var queries = require("org/arangodb/aql/queries");
queries.kill("190");  /* insert actual query id here */

If a query was actually killed on the server, that call should return without an error, and the server should have logged a warning in addition.

If we wanted to abort one or many queries from the list solely by looking at query string patterns or query runtime, we could iterate over the list of current queries and kill each one that matches a predicate.

For example, the following snippet will abort all currently running queries that contain the string SLEEP anywhere inside their query string:

aborting all queries containing the word SLEEP inside the query string
1
2
3
4
5
6
7
8
var queries = require("org/arangodb/aql/queries");

queries.current().filter(function(query) {
  return query.query.match(/SLEEP/);  /* predicate based on query string */
}).forEach(function(query) {
  print("killing query: ", query);    /* print what we're killing */
  queries.kill(query.id);             /* actually kill query */
});

Filtering based on current query runtime is also simple, by adjusting the predicate. To abort all queries that ran longer than 30 seconds use:

aborting all queries running at least 30 seconds
1
2
3
4
5
6
7
8
var queries = require("org/arangodb/aql/queries");

queries.current().filter(function(query) {
  return query.runTime > 30;          /* predicate based on query runtime */
}).forEach(function(query) {
  print("killing query: ", query);    /* print what we're killing */
  queries.kill(query.id);             /* actually kill query */
});

Please make sure the predicates are correct so only the actually intended queries get aborted!

To test a predicate without killing a query, use the above code without the forEach part that did the killing.