Skip to content

Commit b447508

Browse files
Merge pull request #11840 from azat/fix-http-memory-accounting
Fix memory accounting via HTTP interface
2 parents 442b78d + 267a6c8 commit b447508

File tree

5 files changed

+34
-11
lines changed

5 files changed

+34
-11
lines changed

src/Server/HTTPHandler.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -232,15 +232,12 @@ HTTPHandler::HTTPHandler(IServer & server_, const std::string & name)
232232

233233

234234
void HTTPHandler::processQuery(
235+
Context & context,
235236
Poco::Net::HTTPServerRequest & request,
236237
HTMLForm & params,
237238
Poco::Net::HTTPServerResponse & response,
238239
Output & used_output)
239240
{
240-
Context context = server.context();
241-
242-
CurrentThread::QueryScope query_scope(context);
243-
244241
LOG_TRACE(log, "Request URI: {}", request.getURI());
245242

246243
std::istream & istr = request.stream();
@@ -683,6 +680,11 @@ void HTTPHandler::handleRequest(Poco::Net::HTTPServerRequest & request, Poco::Ne
683680
setThreadName("HTTPHandler");
684681
ThreadStatus thread_status;
685682

683+
/// Should be initialized before anything,
684+
/// For correct memory accounting.
685+
Context context = server.context();
686+
CurrentThread::QueryScope query_scope(context);
687+
686688
Output used_output;
687689

688690
/// In case of exception, send stack trace to client.
@@ -706,7 +708,7 @@ void HTTPHandler::handleRequest(Poco::Net::HTTPServerRequest & request, Poco::Ne
706708
throw Exception("The Transfer-Encoding is not chunked and there is no Content-Length header for POST request", ErrorCodes::HTTP_LENGTH_REQUIRED);
707709
}
708710

709-
processQuery(request, params, response, used_output);
711+
processQuery(context, request, params, response, used_output);
710712
LOG_INFO(log, "Done processing query");
711713
}
712714
catch (...)

src/Server/HTTPHandler.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ class HTTPHandler : public Poco::Net::HTTPRequestHandler
7272

7373
/// Also initializes 'used_output'.
7474
void processQuery(
75+
Context & context,
7576
Poco::Net::HTTPServerRequest & request,
7677
HTMLForm & params,
7778
Poco::Net::HTTPServerResponse & response,

tests/queries/0_stateless/01091_num_threads.sql

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,44 @@
11
set log_queries=1;
22
set log_query_threads=1;
33

4-
SELECT 1;
4+
WITH 01091 AS id SELECT 1;
55
SYSTEM FLUSH LOGS;
66

77
WITH
88
(
99
SELECT query_id
1010
FROM system.query_log
11-
WHERE (query = 'SELECT 1') AND (event_date >= (today() - 1))
11+
WHERE (query = 'WITH 01091 AS id SELECT 1;\n') AND (event_date >= (today() - 1))
1212
ORDER BY event_time DESC
1313
LIMIT 1
1414
) AS id
1515
SELECT uniqExact(thread_id)
1616
FROM system.query_thread_log
1717
WHERE (event_date >= (today() - 1)) AND (query_id = id) AND (thread_id != master_thread_id);
1818

19-
select sum(number) from numbers(1000000);
19+
with 01091 as id select sum(number) from numbers(1000000);
2020
SYSTEM FLUSH LOGS;
2121

2222
WITH
2323
(
2424
SELECT query_id
2525
FROM system.query_log
26-
WHERE (query LIKE 'select sum(number) from numbers(1000000);%') AND (event_date >= (today() - 1))
26+
WHERE (query LIKE 'with 01091 as id select sum(number) from numbers(1000000);%') AND (event_date >= (today() - 1))
2727
ORDER BY event_time DESC
2828
LIMIT 1
2929
) AS id
3030
SELECT uniqExact(thread_id)
3131
FROM system.query_thread_log
3232
WHERE (event_date >= (today() - 1)) AND (query_id = id) AND (thread_id != master_thread_id);
3333

34-
select sum(number) from numbers_mt(1000000);
34+
with 01091 as id select sum(number) from numbers_mt(1000000);
3535
SYSTEM FLUSH LOGS;
3636

3737
WITH
3838
(
3939
SELECT query_id
4040
FROM system.query_log
41-
WHERE (query LIKE 'select sum(number) from numbers_mt(1000000);%') AND (event_date >= (today() - 1))
41+
WHERE (query LIKE 'with 01091 as id select sum(number) from numbers_mt(1000000);%') AND (event_date >= (today() - 1))
4242
ORDER BY event_time DESC
4343
LIMIT 1
4444
) AS id
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1000
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env bash
2+
3+
CURDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
4+
. $CURDIR/../shell_config.sh
5+
6+
set -o pipefail
7+
8+
# This is needed to keep at least one running query for user for the time of test.
9+
# (1k http queries takes ~1 second, let's run for 5x more to avoid flaps)
10+
${CLICKHOUSE_CLIENT} --format Null -n <<<'SELECT sleepEachRow(1) FROM numbers(5)' &
11+
12+
# ignore "yes: standard output: Broken pipe"
13+
yes 'SELECT 1' 2>/dev/null | {
14+
head -n1000
15+
} | {
16+
xargs -i ${CLICKHOUSE_CURL} -sS "${CLICKHOUSE_URL}&wait_end_of_query=1&max_memory_usage_for_user=$((1<<30))" -d '{}'
17+
} | grep -x -c 1
18+
19+
wait

0 commit comments

Comments
 (0)