2020-09-16 04:26:10 +00:00
|
|
|
import os
|
2017-05-19 18:54:05 +00:00
|
|
|
import subprocess as sp
|
2017-05-30 11:49:17 +00:00
|
|
|
import tempfile
|
2021-06-21 15:42:40 +00:00
|
|
|
import logging
|
2020-09-16 04:26:10 +00:00
|
|
|
from threading import Timer
|
2017-05-19 18:54:05 +00:00
|
|
|
|
2022-08-15 20:19:41 +00:00
|
|
|
DEFAULT_QUERY_TIMEOUT = 600
|
|
|
|
|
2017-05-19 18:54:05 +00:00
|
|
|
|
|
|
|
class Client:
|
2022-09-25 16:13:17 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
host,
|
|
|
|
port=9000,
|
|
|
|
command="/usr/bin/clickhouse-client",
|
|
|
|
secure=False,
|
|
|
|
config=None,
|
|
|
|
):
|
2017-05-19 18:54:05 +00:00
|
|
|
self.host = host
|
|
|
|
self.port = port
|
2018-08-23 15:31:20 +00:00
|
|
|
self.command = [command]
|
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
if os.path.basename(command) == "clickhouse":
|
|
|
|
self.command.append("client")
|
2022-09-25 16:13:17 +00:00
|
|
|
|
2022-09-25 15:46:12 +00:00
|
|
|
if secure:
|
|
|
|
self.command.append("--secure")
|
|
|
|
if config is not None:
|
|
|
|
self.command += ["--config-file", config]
|
2022-03-22 16:39:58 +00:00
|
|
|
|
|
|
|
self.command += ["--host", self.host, "--port", str(self.port), "--stacktrace"]
|
|
|
|
|
2022-08-15 20:12:07 +00:00
|
|
|
def stacktraces_on_timeout_decorator(func):
|
|
|
|
def wrap(self, *args, **kwargs):
|
|
|
|
try:
|
|
|
|
return func(self, *args, **kwargs)
|
|
|
|
except sp.TimeoutExpired:
|
|
|
|
# I failed to make pytest print stacktraces using print(...) or logging.debug(...), so...
|
|
|
|
self.get_query_request(
|
|
|
|
"INSERT INTO TABLE FUNCTION file('stacktraces_on_timeout.txt', 'TSVRaw', 'tn String, tid UInt64, qid String, st String') "
|
|
|
|
"SELECT thread_name, thread_id, query_id, arrayStringConcat(arrayMap(x -> demangle(addressToSymbol(x)), trace), '\n') AS res FROM system.stack_trace "
|
|
|
|
"SETTINGS allow_introspection_functions=1",
|
|
|
|
timeout=60,
|
|
|
|
).get_answer_and_error()
|
|
|
|
raise
|
2022-08-15 20:19:41 +00:00
|
|
|
|
2022-08-15 20:12:07 +00:00
|
|
|
return wrap
|
|
|
|
|
|
|
|
@stacktraces_on_timeout_decorator
|
2022-03-22 16:39:58 +00:00
|
|
|
def query(
|
|
|
|
self,
|
|
|
|
sql,
|
|
|
|
stdin=None,
|
|
|
|
timeout=None,
|
|
|
|
settings=None,
|
|
|
|
user=None,
|
|
|
|
password=None,
|
|
|
|
database=None,
|
2022-06-07 06:28:33 +00:00
|
|
|
host=None,
|
2022-03-22 16:39:58 +00:00
|
|
|
ignore_error=False,
|
|
|
|
query_id=None,
|
|
|
|
):
|
|
|
|
return self.get_query_request(
|
|
|
|
sql,
|
|
|
|
stdin=stdin,
|
|
|
|
timeout=timeout,
|
|
|
|
settings=settings,
|
|
|
|
user=user,
|
|
|
|
password=password,
|
|
|
|
database=database,
|
2022-06-07 06:28:33 +00:00
|
|
|
host=host,
|
2022-03-22 16:39:58 +00:00
|
|
|
ignore_error=ignore_error,
|
|
|
|
query_id=query_id,
|
|
|
|
).get_answer()
|
|
|
|
|
|
|
|
def get_query_request(
|
|
|
|
self,
|
|
|
|
sql,
|
|
|
|
stdin=None,
|
|
|
|
timeout=None,
|
|
|
|
settings=None,
|
|
|
|
user=None,
|
|
|
|
password=None,
|
|
|
|
database=None,
|
2022-06-07 06:28:33 +00:00
|
|
|
host=None,
|
2022-03-22 16:39:58 +00:00
|
|
|
ignore_error=False,
|
|
|
|
query_id=None,
|
|
|
|
):
|
2017-07-24 20:12:59 +00:00
|
|
|
command = self.command[:]
|
2017-05-30 11:49:17 +00:00
|
|
|
|
2017-05-19 18:54:05 +00:00
|
|
|
if stdin is None:
|
2022-04-05 10:17:34 +00:00
|
|
|
command += ["--multiquery"]
|
2017-05-19 18:54:05 +00:00
|
|
|
stdin = sql
|
|
|
|
else:
|
2022-03-22 16:39:58 +00:00
|
|
|
command += ["--query", sql]
|
2017-05-19 18:54:05 +00:00
|
|
|
|
2017-11-02 14:18:46 +00:00
|
|
|
if settings is not None:
|
2020-10-02 16:54:07 +00:00
|
|
|
for setting, value in settings.items():
|
2022-03-22 16:39:58 +00:00
|
|
|
command += ["--" + setting, str(value)]
|
2017-11-02 14:18:46 +00:00
|
|
|
|
2018-08-14 02:18:57 +00:00
|
|
|
if user is not None:
|
2022-03-22 16:39:58 +00:00
|
|
|
command += ["--user", user]
|
2020-02-29 12:57:52 +00:00
|
|
|
if password is not None:
|
2022-03-22 16:39:58 +00:00
|
|
|
command += ["--password", password]
|
2020-06-24 14:31:05 +00:00
|
|
|
if database is not None:
|
2022-03-22 16:39:58 +00:00
|
|
|
command += ["--database", database]
|
2022-06-07 06:28:33 +00:00
|
|
|
if host is not None:
|
|
|
|
command += ["--host", host]
|
2021-09-28 18:27:32 +00:00
|
|
|
if query_id is not None:
|
2022-03-22 16:39:58 +00:00
|
|
|
command += ["--query_id", query_id]
|
2021-09-28 18:27:32 +00:00
|
|
|
|
2018-08-22 15:42:27 +00:00
|
|
|
return CommandRequest(command, stdin, timeout, ignore_error)
|
2017-05-30 11:49:17 +00:00
|
|
|
|
2022-08-15 20:12:07 +00:00
|
|
|
@stacktraces_on_timeout_decorator
|
2022-03-22 16:39:58 +00:00
|
|
|
def query_and_get_error(
|
|
|
|
self,
|
|
|
|
sql,
|
|
|
|
stdin=None,
|
|
|
|
timeout=None,
|
|
|
|
settings=None,
|
|
|
|
user=None,
|
|
|
|
password=None,
|
|
|
|
database=None,
|
2023-06-03 18:59:04 +00:00
|
|
|
query_id=None,
|
2022-03-22 16:39:58 +00:00
|
|
|
):
|
|
|
|
return self.get_query_request(
|
|
|
|
sql,
|
|
|
|
stdin=stdin,
|
|
|
|
timeout=timeout,
|
|
|
|
settings=settings,
|
|
|
|
user=user,
|
|
|
|
password=password,
|
|
|
|
database=database,
|
2023-06-03 18:59:04 +00:00
|
|
|
query_id=query_id,
|
2022-03-22 16:39:58 +00:00
|
|
|
).get_error()
|
|
|
|
|
2022-08-15 20:12:07 +00:00
|
|
|
@stacktraces_on_timeout_decorator
|
2022-03-22 16:39:58 +00:00
|
|
|
def query_and_get_answer_with_error(
|
|
|
|
self,
|
|
|
|
sql,
|
|
|
|
stdin=None,
|
|
|
|
timeout=None,
|
|
|
|
settings=None,
|
|
|
|
user=None,
|
|
|
|
password=None,
|
|
|
|
database=None,
|
|
|
|
):
|
|
|
|
return self.get_query_request(
|
|
|
|
sql,
|
|
|
|
stdin=stdin,
|
|
|
|
timeout=timeout,
|
|
|
|
settings=settings,
|
|
|
|
user=user,
|
|
|
|
password=password,
|
|
|
|
database=database,
|
|
|
|
).get_answer_and_error()
|
2019-04-07 00:31:20 +00:00
|
|
|
|
2019-07-17 11:55:18 +00:00
|
|
|
|
2017-07-26 14:41:21 +00:00
|
|
|
class QueryTimeoutExceedException(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class QueryRuntimeException(Exception):
|
2021-01-07 03:42:39 +00:00
|
|
|
def __init__(self, message, returncode, stderr):
|
|
|
|
super(QueryRuntimeException, self).__init__(message)
|
|
|
|
self.returncode = returncode
|
|
|
|
self.stderr = stderr
|
2017-07-26 14:41:21 +00:00
|
|
|
|
|
|
|
|
2017-07-24 20:12:59 +00:00
|
|
|
class CommandRequest:
|
2018-08-22 15:42:27 +00:00
|
|
|
def __init__(self, command, stdin=None, timeout=None, ignore_error=False):
|
2017-05-30 11:49:17 +00:00
|
|
|
# Write data to tmp file to avoid PIPEs and execution blocking
|
2022-03-22 16:39:58 +00:00
|
|
|
stdin_file = tempfile.TemporaryFile(mode="w+")
|
2017-05-30 11:49:17 +00:00
|
|
|
stdin_file.write(stdin)
|
|
|
|
stdin_file.seek(0)
|
|
|
|
self.stdout_file = tempfile.TemporaryFile()
|
|
|
|
self.stderr_file = tempfile.TemporaryFile()
|
2018-08-22 15:42:27 +00:00
|
|
|
self.ignore_error = ignore_error
|
2017-05-19 18:54:05 +00:00
|
|
|
|
2020-09-07 08:25:14 +00:00
|
|
|
# print " ".join(command)
|
2017-05-30 11:49:17 +00:00
|
|
|
|
2020-01-28 10:58:37 +00:00
|
|
|
# we suppress stderror on client becase sometimes thread sanitizer
|
|
|
|
# can print some debug information there
|
|
|
|
env = {}
|
|
|
|
env["TSAN_OPTIONS"] = "verbosity=0"
|
2022-03-22 16:39:58 +00:00
|
|
|
self.process = sp.Popen(
|
|
|
|
command,
|
|
|
|
stdin=stdin_file,
|
|
|
|
stdout=self.stdout_file,
|
|
|
|
stderr=self.stderr_file,
|
|
|
|
env=env,
|
|
|
|
universal_newlines=True,
|
|
|
|
)
|
2017-05-30 11:49:17 +00:00
|
|
|
|
|
|
|
self.timer = None
|
|
|
|
self.process_finished_before_timeout = True
|
2017-05-19 18:54:05 +00:00
|
|
|
if timeout is not None:
|
2022-03-22 16:39:58 +00:00
|
|
|
|
2017-05-19 18:54:05 +00:00
|
|
|
def kill_process():
|
2017-05-30 11:49:17 +00:00
|
|
|
if self.process.poll() is None:
|
|
|
|
self.process_finished_before_timeout = False
|
2017-07-24 20:12:59 +00:00
|
|
|
self.process.kill()
|
2017-05-30 11:49:17 +00:00
|
|
|
|
|
|
|
self.timer = Timer(timeout, kill_process)
|
|
|
|
self.timer.start()
|
2017-05-19 18:54:05 +00:00
|
|
|
|
2023-03-30 22:42:16 +00:00
|
|
|
def remove_trash_from_stderr(self, stderr):
|
2023-03-31 10:57:29 +00:00
|
|
|
# FIXME https://github.com/ClickHouse/ClickHouse/issues/48181
|
2023-03-30 22:42:16 +00:00
|
|
|
if not stderr:
|
|
|
|
return stderr
|
|
|
|
lines = stderr.split("\n")
|
|
|
|
lines = [
|
|
|
|
x for x in lines if ("completion_queue" not in x and "Kick failed" not in x)
|
|
|
|
]
|
|
|
|
return "\n".join(lines)
|
|
|
|
|
2017-05-30 11:49:17 +00:00
|
|
|
def get_answer(self):
|
2022-08-15 20:12:07 +00:00
|
|
|
self.process.wait(timeout=DEFAULT_QUERY_TIMEOUT)
|
2017-05-30 11:49:17 +00:00
|
|
|
self.stdout_file.seek(0)
|
|
|
|
self.stderr_file.seek(0)
|
2017-05-19 18:54:05 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
stdout = self.stdout_file.read().decode("utf-8", errors="replace")
|
|
|
|
stderr = self.stderr_file.read().decode("utf-8", errors="replace")
|
2017-05-19 18:54:05 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
if (
|
|
|
|
self.timer is not None
|
|
|
|
and not self.process_finished_before_timeout
|
|
|
|
and not self.ignore_error
|
|
|
|
):
|
2021-06-21 15:42:40 +00:00
|
|
|
logging.debug(f"Timed out. Last stdout:{stdout}, stderr:{stderr}")
|
2022-03-22 16:39:58 +00:00
|
|
|
raise QueryTimeoutExceedException("Client timed out!")
|
2017-07-26 14:41:21 +00:00
|
|
|
|
2023-03-30 22:42:16 +00:00
|
|
|
if (
|
|
|
|
self.process.returncode != 0 or self.remove_trash_from_stderr(stderr)
|
|
|
|
) and not self.ignore_error:
|
2020-09-16 04:26:10 +00:00
|
|
|
raise QueryRuntimeException(
|
2022-03-22 16:39:58 +00:00
|
|
|
"Client failed! Return code: {}, stderr: {}".format(
|
|
|
|
self.process.returncode, stderr
|
|
|
|
),
|
|
|
|
self.process.returncode,
|
|
|
|
stderr,
|
|
|
|
)
|
2017-05-19 18:54:05 +00:00
|
|
|
|
|
|
|
return stdout
|
2019-04-07 00:31:20 +00:00
|
|
|
|
|
|
|
def get_error(self):
|
2022-08-15 20:12:07 +00:00
|
|
|
self.process.wait(timeout=DEFAULT_QUERY_TIMEOUT)
|
2019-04-07 00:31:20 +00:00
|
|
|
self.stdout_file.seek(0)
|
|
|
|
self.stderr_file.seek(0)
|
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
stdout = self.stdout_file.read().decode("utf-8", errors="replace")
|
|
|
|
stderr = self.stderr_file.read().decode("utf-8", errors="replace")
|
2019-04-07 00:31:20 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
if (
|
|
|
|
self.timer is not None
|
|
|
|
and not self.process_finished_before_timeout
|
|
|
|
and not self.ignore_error
|
|
|
|
):
|
|
|
|
raise QueryTimeoutExceedException("Client timed out!")
|
2019-04-07 00:31:20 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
if self.process.returncode == 0:
|
|
|
|
raise QueryRuntimeException(
|
|
|
|
"Client expected to be failed but succeeded! stdout: {}".format(stdout),
|
|
|
|
self.process.returncode,
|
|
|
|
stderr,
|
|
|
|
)
|
2019-04-07 00:31:20 +00:00
|
|
|
|
|
|
|
return stderr
|
2019-07-17 11:55:18 +00:00
|
|
|
|
|
|
|
def get_answer_and_error(self):
|
2022-08-15 20:12:07 +00:00
|
|
|
self.process.wait(timeout=DEFAULT_QUERY_TIMEOUT)
|
2019-07-17 11:55:18 +00:00
|
|
|
self.stdout_file.seek(0)
|
|
|
|
self.stderr_file.seek(0)
|
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
stdout = self.stdout_file.read().decode("utf-8", errors="replace")
|
|
|
|
stderr = self.stderr_file.read().decode("utf-8", errors="replace")
|
2019-07-17 11:55:18 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
if (
|
|
|
|
self.timer is not None
|
|
|
|
and not self.process_finished_before_timeout
|
|
|
|
and not self.ignore_error
|
|
|
|
):
|
|
|
|
raise QueryTimeoutExceedException("Client timed out!")
|
2019-07-17 11:55:18 +00:00
|
|
|
|
|
|
|
return (stdout, stderr)
|