Files
Alexey MilovidovandClaude Opus 4.8 295ced78d1 Enable more Ruff checks (F401/F403/F405/F541/F841) and clean up tests
Follow-up to #105171, which added `ruff` with a deliberately narrow,
bug-class-only rule set and left the unused-name and star-import rules
off pending a dedicated cleanup pass. This enables that second batch in
`pyproject.toml` and fixes every violation across the tree (the bulk in
integration tests):

- `F401` unused-import and `F841` unused-variable: removed, preserving
  any side-effecting right-hand side (e.g. `result = node.query(...)`
  becomes `node.query(...)`). Two `try: import ...` feature probes are
  kept with `# noqa: F401`.
- `F541` f-string-missing-placeholders: dropped the redundant `f` prefix.
- `F403`/`F405` star imports: every `from module import *` is expanded
  into explicit imports of the names actually used, taken from each
  name's real origin, so stdlib/third-party names import from their real
  source rather than through a test "convenience bundle". The now-empty
  `tests/integration/helpers/kafka/common_direct.py` bundle is removed and
  its three remaining consumers import `_VarintBytes`/`KafkaAdminClient`
  directly.

Eliminating the star imports surfaced a real bug the wildcard had masked
from `F821`: `generate_presets.py` in `test_prometheus_protocols` called
an undefined `execute_instant_query_with_http_api`; fixed to the existing
`execute_query_via_http_api`.

No `black`/`isort` reformatting was applied (neither is enforced in the
style check), so the diff is limited to the lint fixes themselves.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
2026-06-18 23:22:25 +00:00

125 lines
3.7 KiB
Python

import pytest
from helpers.cluster import ClickHouseCluster
from helpers.client import QueryRuntimeException
cluster = ClickHouseCluster(__file__)
node1 = cluster.add_instance(
"node1", main_configs=["configs/jemalloc_global_profiler.xml"], stay_alive=True
)
@pytest.fixture(scope="module")
def started_cluster():
try:
cluster.start()
yield cluster
finally:
cluster.shutdown()
def set_config(config, value):
node1.replace_in_config(
"/etc/clickhouse-server/config.d/jemalloc_global_profiler.xml",
f"{config}>.",
f"{config}>{value}",
)
def is_global_profiler_active():
return int(
node1.query(
"SELECT value FROM system.asynchronous_metrics WHERE metric = 'jemalloc.prof.thread_active_init'"
).strip()
)
def has_jemalloc_sample_in_trace_log(extra_query_condition=""):
node1.query("SYSTEM FLUSH LOGS")
return int(
node1.query(
f"SELECT count() > 0 FROM system.trace_log WHERE trace_type = 'JemallocSample' {extra_query_condition}"
).strip()
)
def test_jemalloc_global_profiler(started_cluster):
# default open
if node1.is_built_with_sanitizer() or node1.is_built_with_llvm_coverage():
pytest.skip("Disabled for sanitizers and llvm coverage builds")
set_config("jemalloc_enable_global_profiler", "0")
set_config("jemalloc_collect_global_profile_samples_in_trace_log", "0")
node1.restart_clickhouse()
try:
node1.query("TRUNCATE system.trace_log")
except QueryRuntimeException as e:
assert e.returncode == 60 # UNKNOWN_TABLE
assert is_global_profiler_active() == 0
assert has_jemalloc_sample_in_trace_log() == 0
set_config("jemalloc_enable_global_profiler", "1")
node1.restart_clickhouse()
assert is_global_profiler_active() == 1
assert has_jemalloc_sample_in_trace_log() == 0
# we enable collecting samples to trace_log for specific query only
profiled_query_id = "test_jemalloc_query_profiler"
node1.query(
"SELECT * FROM numbers(1000000) ORDER BY number",
settings={
"max_bytes_ratio_before_external_sort": 0,
"jemalloc_enable_profiler": 0,
"jemalloc_collect_profile_samples_in_trace_log": 1,
},
query_id=profiled_query_id,
)
def assert_only_traces_from_query(query_id):
assert (
has_jemalloc_sample_in_trace_log(
extra_query_condition=f"AND query_id = '{query_id}'"
)
== 1
)
# we can deallocate outside query some allocations we did in the query
assert (
has_jemalloc_sample_in_trace_log(
extra_query_condition=f"AND query_id != '{query_id}' AND size > 0"
)
== 0
)
assert_only_traces_from_query(profiled_query_id)
set_config("jemalloc_collect_global_profile_samples_in_trace_log", "1")
node1.restart_clickhouse()
assert is_global_profiler_active() == 1
assert has_jemalloc_sample_in_trace_log() == 1
set_config("jemalloc_enable_global_profiler", "0")
node1.restart_clickhouse()
assert is_global_profiler_active() == 0
node1.query("TRUNCATE system.trace_log")
# we enable jemalloc profiler for query but rely on global config to collect samples to trace_log
profiled_query_id = "test_jemalloc_query_profiler"
node1.query(
"SELECT * FROM numbers(1000000) ORDER BY number",
settings={
"max_bytes_ratio_before_external_sort": 0,
"jemalloc_enable_profiler": 1,
"jemalloc_collect_profile_samples_in_trace_log": 0,
},
query_id=profiled_query_id,
)
assert_only_traces_from_query(profiled_query_id)