2017-04-01 09:19:00 +00:00
|
|
|
#include <Interpreters/InterpreterKillQueryQuery.h>
|
|
|
|
#include <Parsers/ASTKillQueryQuery.h>
|
|
|
|
#include <Parsers/queryToString.h>
|
2017-05-23 18:24:43 +00:00
|
|
|
#include <Interpreters/Context.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Interpreters/ProcessList.h>
|
|
|
|
#include <Interpreters/executeQuery.h>
|
|
|
|
#include <Columns/ColumnString.h>
|
2017-07-13 20:58:19 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <DataTypes/DataTypeString.h>
|
|
|
|
#include <Columns/ColumnsNumber.h>
|
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
|
|
|
#include <DataStreams/OneBlockInputStream.h>
|
2016-11-30 17:31:05 +00:00
|
|
|
#include <thread>
|
|
|
|
#include <iostream>
|
2017-03-14 00:14:19 +00:00
|
|
|
#include <cstddef>
|
2016-11-30 17:31:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const int READONLY;
|
|
|
|
extern const int LOGICAL_ERROR;
|
2016-11-30 17:31:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
using CancellationCode = ProcessList::CancellationCode;
|
|
|
|
|
|
|
|
static const char * cancellationCodeToStatus(CancellationCode code)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
switch (code)
|
|
|
|
{
|
|
|
|
case CancellationCode::NotFound:
|
|
|
|
return "finished";
|
|
|
|
case CancellationCode::QueryIsNotInitializedYet:
|
|
|
|
return "pending";
|
|
|
|
case CancellationCode::CancelCannotBeSent:
|
|
|
|
return "error";
|
|
|
|
case CancellationCode::CancelSent:
|
|
|
|
return "waiting";
|
|
|
|
default:
|
|
|
|
return "unknown_status";
|
|
|
|
};
|
2016-11-30 17:31:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct QueryDescriptor
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
String query_id;
|
|
|
|
String user;
|
|
|
|
size_t source_num;
|
|
|
|
bool processed = false;
|
2016-11-30 17:31:05 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
QueryDescriptor(String && query_id_, String && user_, size_t source_num_, bool processed_ = false)
|
|
|
|
: query_id(std::move(query_id_)), user(std::move(user_)), source_num(source_num_), processed(processed_) {}
|
2016-11-30 17:31:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
using QueryDescriptors = std::vector<QueryDescriptor>;
|
|
|
|
|
|
|
|
|
2017-12-15 18:23:05 +00:00
|
|
|
static void insertResultRow(size_t n, CancellationCode code, const Block & source_processes, const Block & sample_block, MutableColumns & columns)
|
2016-11-30 17:31:05 +00:00
|
|
|
{
|
2017-12-15 18:23:05 +00:00
|
|
|
columns[0]->insert(String(cancellationCodeToStatus(code)));
|
2016-11-30 17:31:05 +00:00
|
|
|
|
2017-12-15 18:23:05 +00:00
|
|
|
for (size_t col_num = 1, size = columns.size(); col_num < size; ++col_num)
|
|
|
|
columns[col_num]->insertFrom(*source_processes.getByName(sample_block.getByPosition(col_num).name).column, n);
|
2016-11-30 17:31:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static QueryDescriptors extractQueriesExceptMeAndCheckAccess(const Block & processes_block, Context & context)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
QueryDescriptors res;
|
|
|
|
size_t num_processes = processes_block.rows();
|
|
|
|
res.reserve(num_processes);
|
2016-11-30 17:31:05 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
const ColumnString & query_id_col = typeid_cast<const ColumnString &>(*processes_block.getByName("query_id").column);
|
|
|
|
const ColumnString & user_col = typeid_cast<const ColumnString &>(*processes_block.getByName("user").column);
|
|
|
|
const ClientInfo & my_client = context.getProcessListElement()->client_info;
|
2016-11-30 17:31:05 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
for (size_t i = 0; i < num_processes; ++i)
|
|
|
|
{
|
|
|
|
auto query_id = query_id_col.getDataAt(i).toString();
|
|
|
|
auto user = user_col.getDataAt(i).toString();
|
2016-11-30 17:31:05 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (my_client.current_query_id == query_id && my_client.current_user == user)
|
|
|
|
continue;
|
2016-11-30 17:31:05 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (context.getSettingsRef().limits.readonly && my_client.current_user != user)
|
|
|
|
{
|
|
|
|
throw Exception("Readonly user " + my_client.current_user + " attempts to kill query created by " + user,
|
|
|
|
ErrorCodes::READONLY);
|
|
|
|
}
|
2016-11-30 17:31:05 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
res.emplace_back(std::move(query_id), std::move(user), i, false);
|
|
|
|
}
|
2016-11-30 17:31:05 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return res;
|
2016-11-30 17:31:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SyncKillQueryInputStream : public IProfilingBlockInputStream
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
SyncKillQueryInputStream(ProcessList & process_list_, QueryDescriptors && processes_to_stop_, Block && processes_block_,
|
|
|
|
const Block & res_sample_block_)
|
2018-01-06 18:10:44 +00:00
|
|
|
: process_list(process_list_),
|
2017-04-01 07:20:54 +00:00
|
|
|
processes_to_stop(std::move(processes_to_stop_)),
|
|
|
|
processes_block(std::move(processes_block_)),
|
|
|
|
res_sample_block(res_sample_block_)
|
|
|
|
{
|
|
|
|
total_rows_approx = processes_to_stop.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
String getName() const override
|
|
|
|
{
|
|
|
|
return "SynchronousQueryKiller";
|
|
|
|
}
|
|
|
|
|
|
|
|
String getID() const override
|
|
|
|
{
|
|
|
|
return "SynchronousQueryKiller_" + toString(intptr_t(this));
|
|
|
|
}
|
|
|
|
|
2018-01-06 18:10:44 +00:00
|
|
|
Block getHeader() override { return res_sample_block; };
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
Block readImpl() override
|
|
|
|
{
|
|
|
|
size_t num_result_queries = processes_to_stop.size();
|
|
|
|
|
|
|
|
if (num_processed_queries >= num_result_queries)
|
|
|
|
return Block();
|
|
|
|
|
2017-12-15 18:23:05 +00:00
|
|
|
MutableColumns columns = res_sample_block.cloneEmptyColumns();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
for (auto & curr_process : processes_to_stop)
|
|
|
|
{
|
|
|
|
if (curr_process.processed)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
auto code = process_list.sendCancelToQuery(curr_process.query_id, curr_process.user);
|
|
|
|
|
|
|
|
if (code != CancellationCode::QueryIsNotInitializedYet && code != CancellationCode::CancelSent)
|
|
|
|
{
|
|
|
|
curr_process.processed = true;
|
2017-12-15 18:23:05 +00:00
|
|
|
insertResultRow(curr_process.source_num, code, processes_block, res_sample_block, columns);
|
2017-04-01 07:20:54 +00:00
|
|
|
++num_processed_queries;
|
|
|
|
}
|
|
|
|
/// Wait if QueryIsNotInitializedYet or CancelSent
|
|
|
|
}
|
|
|
|
|
|
|
|
/// KILL QUERY could be killed also
|
|
|
|
/// Probably interpreting KILL QUERIES as complete (not internal) queries is extra functionality
|
|
|
|
if (is_cancelled)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/// Sleep if there are unprocessed queries
|
|
|
|
if (num_processed_queries < num_result_queries)
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
|
|
|
|
|
|
|
/// Don't produce empty block
|
2017-12-15 18:23:05 +00:00
|
|
|
} while (columns.empty() || columns[0]->empty());
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-12-15 18:23:05 +00:00
|
|
|
return res_sample_block.cloneWithColumns(std::move(columns));
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ProcessList & process_list;
|
|
|
|
QueryDescriptors processes_to_stop;
|
|
|
|
Block processes_block;
|
|
|
|
Block res_sample_block;
|
|
|
|
size_t num_processed_queries = 0;
|
2016-11-30 17:31:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
BlockIO InterpreterKillQueryQuery::execute()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
ASTKillQueryQuery & query = typeid_cast<ASTKillQueryQuery &>(*query_ptr);
|
|
|
|
|
|
|
|
BlockIO res_io;
|
|
|
|
Block processes_block = getSelectFromSystemProcessesResult();
|
|
|
|
if (!processes_block)
|
|
|
|
return res_io;
|
|
|
|
|
|
|
|
ProcessList & process_list = context.getProcessList();
|
|
|
|
QueryDescriptors queries_to_stop = extractQueriesExceptMeAndCheckAccess(processes_block, context);
|
|
|
|
|
2018-01-07 00:35:44 +00:00
|
|
|
auto header = processes_block.cloneEmpty();
|
|
|
|
header.insert(0, {ColumnString::create(), std::make_shared<DataTypeString>(), "kill_status"});
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
if (!query.sync || query.test)
|
|
|
|
{
|
2018-01-07 00:35:44 +00:00
|
|
|
MutableColumns res_columns = header.cloneEmptyColumns();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
for (const auto & query_desc : queries_to_stop)
|
|
|
|
{
|
|
|
|
auto code = (query.test) ? CancellationCode::Unknown : process_list.sendCancelToQuery(query_desc.query_id, query_desc.user);
|
2018-01-07 00:35:44 +00:00
|
|
|
insertResultRow(query_desc.source_num, code, processes_block, header, res_columns);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2018-01-07 00:35:44 +00:00
|
|
|
res_io.in = std::make_shared<OneBlockInputStream>(header.cloneWithColumns(std::move(res_columns)));
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
res_io.in = std::make_shared<SyncKillQueryInputStream>(
|
2018-01-07 00:35:44 +00:00
|
|
|
process_list, std::move(queries_to_stop), std::move(processes_block), header);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res_io;
|
2016-11-30 17:31:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Block InterpreterKillQueryQuery::getSelectFromSystemProcessesResult()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
String system_processes_query = "SELECT query_id, user, query FROM system.processes WHERE "
|
|
|
|
+ queryToString(static_cast<ASTKillQueryQuery &>(*query_ptr).where_expression);
|
2016-11-30 17:31:05 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
BlockIO system_processes_io = executeQuery(system_processes_query, context, true);
|
|
|
|
Block res = system_processes_io.in->read();
|
2016-11-30 17:31:05 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (res && system_processes_io.in->read())
|
|
|
|
throw Exception("Expected one block from input stream", ErrorCodes::LOGICAL_ERROR);
|
2016-11-30 17:31:05 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return res;
|
2016-11-30 17:31:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|