Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix memory accounting via HTTP interface #11840

Merged
merged 5 commits into from Jun 23, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/Server/HTTPHandler.cpp
Expand Up @@ -232,15 +232,12 @@ HTTPHandler::HTTPHandler(IServer & server_, const std::string & name)


void HTTPHandler::processQuery(
Context & context,
Poco::Net::HTTPServerRequest & request,
HTMLForm & params,
Poco::Net::HTTPServerResponse & response,
Output & used_output)
{
Context context = server.context();

CurrentThread::QueryScope query_scope(context);

LOG_TRACE(log, "Request URI: {}", request.getURI());

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

/// Should be initialized before anything,
/// For correct memory accounting.
Context context = server.context();
CurrentThread::QueryScope query_scope(context);

Output used_output;

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

processQuery(request, params, response, used_output);
processQuery(context, request, params, response, used_output);
LOG_INFO(log, "Done processing query");
}
catch (...)
Expand Down
1 change: 1 addition & 0 deletions src/Server/HTTPHandler.h
Expand Up @@ -72,6 +72,7 @@ class HTTPHandler : public Poco::Net::HTTPRequestHandler

/// Also initializes 'used_output'.
void processQuery(
Context & context,
Poco::Net::HTTPServerRequest & request,
HTMLForm & params,
Poco::Net::HTTPServerResponse & response,
Expand Down
@@ -0,0 +1 @@
10000
26 changes: 26 additions & 0 deletions tests/queries/0_stateless/01238_http_memory_tracking.sh
@@ -0,0 +1,26 @@
#!/usr/bin/env bash

CURDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
. $CURDIR/../shell_config.sh

# TODO: the test can be way more optimal

set -o pipefail

function execute_null()
{
${CLICKHOUSE_CLIENT} --format Null -n "$@"
}

# This is needed to keep at least one running query for user for the time of test.
# (10k http queries takes 10seconds, let's run for 3x more to avoid flaps)
execute_null <<<'SELECT sleepEachRow(1) FROM numbers(30)' &

# ignore "yes: standard output: Broken pipe"
yes 'SELECT 1' 2>/dev/null | {
head -n10000
} | {
xargs -P10000 -i ${CLICKHOUSE_CURL} -sS "${CLICKHOUSE_URL}&wait_end_of_query=1&max_memory_usage_for_user=$((10<<30))" -d '{}'
} | grep -x -c 1

wait