mirror of
https://github.com/hanzoai/datastore.git
synced 2026-09-26 13:55:07 +00:00
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]>
226 lines
5.5 KiB
Python
226 lines
5.5 KiB
Python
import logging
|
|
import time
|
|
|
|
import pytest
|
|
|
|
from helpers.cluster import ClickHouseCluster
|
|
from helpers.test_tools import assert_eq_with_retry
|
|
|
|
logger = logging.getLogger(__name__)
|
|
logger.setLevel(logging.DEBUG)
|
|
|
|
cluster = ClickHouseCluster(__file__)
|
|
node = cluster.add_instance(
|
|
"node",
|
|
stay_alive=True,
|
|
with_zookeeper=False,
|
|
main_configs=[
|
|
"configs/keeper.xml",
|
|
"configs/transactions.xml",
|
|
],
|
|
)
|
|
|
|
|
|
@pytest.fixture(scope="module", autouse=True)
|
|
def start_cluster():
|
|
try:
|
|
cluster.start()
|
|
yield cluster
|
|
finally:
|
|
cluster.shutdown()
|
|
|
|
|
|
@pytest.fixture(scope="function")
|
|
def test_name(request):
|
|
return request.node.name
|
|
|
|
|
|
@pytest.fixture(scope="function")
|
|
def exclusive_table(test_name):
|
|
normalized = (
|
|
test_name.replace("[", "_")
|
|
.replace("]", "_")
|
|
.replace(" ", "_")
|
|
.replace("-", "_")
|
|
)
|
|
return "table_" + normalized
|
|
|
|
|
|
def get_event_select_count():
|
|
return int(
|
|
node.query(
|
|
"""
|
|
SELECT value FROM system.events WHERE event = 'SelectQuery';
|
|
"""
|
|
)
|
|
)
|
|
|
|
|
|
def get_query_processes_count(query_id):
|
|
q = f"""
|
|
SELECT count() FROM system.processes WHERE query_id = '{query_id}';
|
|
"""
|
|
return q
|
|
|
|
|
|
def is_query_running(query_id):
|
|
return 1 == int(node.query(get_query_processes_count(query_id)))
|
|
|
|
|
|
def wait_select_start(query_id):
|
|
assert_eq_with_retry(
|
|
node,
|
|
get_query_processes_count(query_id),
|
|
"1\n",
|
|
)
|
|
|
|
|
|
LOCK_FREE_QUERIES = {
|
|
"detach table": "DETACH TABLE {table};",
|
|
"drop part": "ALTER TABLE {table} DROP PART 'all_1_1_0';",
|
|
"detach part": "ALTER TABLE {table} DETACH PART 'all_1_1_0';",
|
|
"truncate": "TRUNCATE TABLE {table};",
|
|
}
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"lock_free_query", LOCK_FREE_QUERIES.values(), ids=LOCK_FREE_QUERIES.keys()
|
|
)
|
|
def test_query_is_lock_free(lock_free_query, exclusive_table):
|
|
node.query(
|
|
f"""
|
|
CREATE TABLE {exclusive_table}
|
|
(a Int64)
|
|
Engine=MergeTree ORDER BY a;
|
|
"""
|
|
)
|
|
node.query(
|
|
f"""
|
|
INSERT INTO {exclusive_table} SELECT number FROM numbers(50);
|
|
"""
|
|
)
|
|
|
|
query_id = "select-" + exclusive_table
|
|
|
|
select_handler = node.get_query_request(
|
|
f"""
|
|
SELECT sleepEachRow(3) FROM {exclusive_table} SETTINGS function_sleep_max_microseconds_per_block = 0;
|
|
""",
|
|
query_id=query_id,
|
|
)
|
|
wait_select_start(query_id)
|
|
|
|
for _ in [1, 2, 3, 4, 5]:
|
|
assert is_query_running(query_id)
|
|
assert select_handler.process.poll() is None
|
|
time.sleep(1)
|
|
|
|
node.query(lock_free_query.format(table=exclusive_table))
|
|
|
|
assert is_query_running(query_id)
|
|
|
|
if "DETACH TABLE" in lock_free_query:
|
|
result = node.query_and_get_error(
|
|
f"""
|
|
SELECT count() FROM {exclusive_table};
|
|
"""
|
|
)
|
|
assert (
|
|
f"Table default.{exclusive_table} does not exist" in result
|
|
or f"Unknown table expression identifier '{exclusive_table}'" in result
|
|
)
|
|
else:
|
|
assert 0 == int(
|
|
node.query(
|
|
f"""
|
|
SELECT count() FROM {exclusive_table};
|
|
"""
|
|
)
|
|
)
|
|
|
|
|
|
PERMANENT_QUERIES = {
|
|
"truncate": ("TRUNCATE TABLE {table};", 0),
|
|
"detach-partition-all": ("ALTER TABLE {table} DETACH PARTITION ALL;", 0),
|
|
"detach-part": ("ALTER TABLE {table} DETACH PARTITION '20221001';", 49),
|
|
"drop-part": ("ALTER TABLE {table} DROP PART '20220901_1_1_0';", 49),
|
|
}
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"transaction", ["NoTx", "TxCommit", "TxRollback", "TxNotFinished"]
|
|
)
|
|
@pytest.mark.parametrize(
|
|
"permanent", PERMANENT_QUERIES.values(), ids=PERMANENT_QUERIES.keys()
|
|
)
|
|
def test_query_is_permanent(transaction, permanent, exclusive_table):
|
|
node.query(
|
|
f"""
|
|
CREATE TABLE {exclusive_table}
|
|
(
|
|
a Int64,
|
|
date Date
|
|
)
|
|
Engine=MergeTree
|
|
PARTITION BY date
|
|
ORDER BY a;
|
|
"""
|
|
)
|
|
node.query(
|
|
f"""
|
|
INSERT INTO {exclusive_table} SELECT number, toDate('2022-09-01') + INTERVAL number DAY FROM numbers(50);
|
|
"""
|
|
)
|
|
|
|
query_id = "select-" + exclusive_table
|
|
|
|
select_handler = node.get_query_request(
|
|
f"""
|
|
SELECT sleepEachRow(3) FROM {exclusive_table} SETTINGS function_sleep_max_microseconds_per_block = 0, max_threads=1;
|
|
""",
|
|
query_id=query_id,
|
|
)
|
|
wait_select_start(query_id)
|
|
|
|
for _ in [1, 2, 3, 4, 5]:
|
|
assert is_query_running(query_id)
|
|
assert select_handler.process.poll() is None
|
|
time.sleep(1)
|
|
|
|
permanent_query = permanent[0]
|
|
result = permanent[1]
|
|
statement = permanent_query.format(table=exclusive_table)
|
|
if transaction == "TxCommit":
|
|
query = f"""
|
|
BEGIN TRANSACTION;
|
|
{statement}
|
|
COMMIT;
|
|
"""
|
|
elif transaction == "TxRollback":
|
|
query = f"""
|
|
BEGIN TRANSACTION;
|
|
{statement}
|
|
ROLLBACK;
|
|
"""
|
|
result = 50
|
|
elif transaction == "TxNotFinished":
|
|
query = f"""
|
|
BEGIN TRANSACTION;
|
|
{statement}
|
|
"""
|
|
result = 50
|
|
else:
|
|
query = statement
|
|
|
|
node.query(query)
|
|
|
|
node.restart_clickhouse(kill=True)
|
|
|
|
assert result == int(
|
|
node.query(
|
|
f"""
|
|
SELECT count() FROM {exclusive_table};
|
|
"""
|
|
)
|
|
)
|