Add missing query context for system logs

Needed to allow attaching materialized views with joins or with
subqueries to system.logs.
This commit is contained in:
Mikhail Filimonov 2020-07-03 19:19:32 +02:00
parent 5a4d9fb9ae
commit 6e599533a7
3 changed files with 67 additions and 2 deletions

View File

@ -22,6 +22,7 @@
#include <Interpreters/InterpreterCreateQuery.h>
#include <Interpreters/InterpreterRenameQuery.h>
#include <Interpreters/InterpreterInsertQuery.h>
#include <Interpreters/Context.h>
#include <Common/setThreadName.h>
#include <Common/ThreadPool.h>
#include <IO/WriteHelpers.h>
@ -62,7 +63,6 @@ namespace ErrorCodes
#define DBMS_SYSTEM_LOG_QUEUE_SIZE 1048576
class Context;
class QueryLog;
class QueryThreadLog;
class PartLog;
@ -161,6 +161,7 @@ protected:
private:
/* Saving thread data */
Context & context;
Context insert_context;
const StorageID table_id;
const String storage_def;
StoragePtr table;
@ -207,11 +208,13 @@ SystemLog<LogElement>::SystemLog(Context & context_,
const String & storage_def_,
size_t flush_interval_milliseconds_)
: context(context_)
, insert_context(Context(context_))
, table_id(database_name_, table_name_)
, storage_def(storage_def_)
, flush_interval_milliseconds(flush_interval_milliseconds_)
{
assert(database_name_ == DatabaseCatalog::SYSTEM_DATABASE);
insert_context.makeQueryContext(); // we need query context to do inserts to target table with MV containing subqueries or joins
log = &Poco::Logger::get("SystemLog (" + database_name_ + "." + table_name_ + ")");
}
@ -425,7 +428,7 @@ void SystemLog<LogElement>::flushImpl(const std::vector<LogElement> & to_flush,
insert->table_id = table_id;
ASTPtr query_ptr(insert.release());
InterpreterInsertQuery interpreter(query_ptr, context);
InterpreterInsertQuery interpreter(query_ptr, insert_context);
BlockIO io = interpreter.execute();
io.out->writePrefix();

View File

@ -0,0 +1,10 @@
1
1
1
1
=== system.query_log ===
main_dashboard_bottom_query 2
main_dashboard_top_query 2
=== slowlog ===
main_dashboard_bottom_query 1
main_dashboard_top_query 1

View File

@ -0,0 +1,52 @@
DROP TABLE IF EXISTS slow_log;
DROP TABLE IF EXISTS expected_times;
CREATE TABLE expected_times (QUERY_GROUP_ID String, max_query_duration_ms UInt64) Engine=Memory;
INSERT INTO expected_times VALUES('main_dashboard_top_query', 100), ('main_dashboard_bottom_query', 100);
CREATE MATERIALIZED VIEW slow_log Engine=Memory AS
(
SELECT * FROM
(
SELECT
extract(query,'/\\*\\s*QUERY_GROUP_ID:(.*?)\\s*\\*/') as QUERY_GROUP_ID,
*
FROM system.query_log
WHERE type<>1 and event_date >= yesterday() and event_time > now() - 120
) as ql
INNER JOIN expected_times USING (QUERY_GROUP_ID)
WHERE query_duration_ms > max_query_duration_ms
);
SET log_queries=1;
SELECT 1 /* QUERY_GROUP_ID:main_dashboard_top_query */;
SELECT 1 /* QUERY_GROUP_ID:main_dashboard_bottom_query */;
SELECT 1 WHERE not ignore(sleep(0.105)) /* QUERY_GROUP_ID:main_dashboard_top_query */;
SELECT 1 WHERE not ignore(sleep(0.105)) /* QUERY_GROUP_ID:main_dashboard_bottom_query */;
SET log_queries=0;
SYSTEM FLUSH LOGS;
SELECT '=== system.query_log ===';
SELECT
extract(query,'/\\*\\s*QUERY_GROUP_ID:(.*?)\\s*\\*/') as QUERY_GROUP_ID,
count()
FROM system.query_log
WHERE type<>1 and event_date >= yesterday() and event_time > now() - 20 and QUERY_GROUP_ID<>''
GROUP BY QUERY_GROUP_ID
ORDER BY QUERY_GROUP_ID;
SELECT '=== slowlog ===';
SELECT
QUERY_GROUP_ID,
count()
FROM slow_log
GROUP BY QUERY_GROUP_ID
ORDER BY QUERY_GROUP_ID;
DROP TABLE slow_log;
DROP TABLE expected_times;