6

Killing a Long-running Query

 2 years ago
source link: http://jsteemann.github.io/blog/2016/01/26/killing-a-long-running-query/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Killing a Long-running Query

Jan 26th, 2016

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

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

[
  {
    "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

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

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

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.

Posted by jsteemann

Jan 26th, 2016AQL, ArangoDB, ArangoShell

« Small things in 2.8: explain improvements Compiling V8 with g++6 »


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK