Skip to content

Commit 556ae12

Browse files
Add max_execution_time for freeze queries (#284)
* Add max_execution_time for freeze queries * add kill
1 parent 99ca1a6 commit 556ae12

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

ch_backup/clickhouse/control.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,12 @@
207207
"""
208208
)
209209

210+
KILL_RUNNING_FREEZE_SQL = strip_query(
211+
"""
212+
KILL QUERY WHERE query_kind='Alter' AND user='{user}' AND query ilike '%ALTER TABLE%FREEZE%'
213+
"""
214+
)
215+
210216
DROP_TABLE_IF_EXISTS_SQL = strip_query(
211217
"""
212218
DROP TABLE IF EXISTS `{db_name}`.`{table_name}` NO DELAY
@@ -586,6 +592,14 @@ def attach_table(self, table: Union[TableMetadata, Table]) -> None:
586592

587593
self._ch_client.query(query_sql)
588594

595+
def kill_old_freeze_queries(self):
596+
"""
597+
Collect all freeze queries from prev backup and kill them all.
598+
"""
599+
self._ch_client.query(
600+
KILL_RUNNING_FREEZE_SQL.format(user=self._ch_ctl_config["user"])
601+
)
602+
589603
def freeze_table(
590604
self,
591605
backup_name: str,
@@ -598,6 +612,12 @@ def freeze_table(
598612
# Table has no partitions or created with deprecated syntax.
599613
# FREEZE PARTITION ID with deprecated syntax throws segmentation fault in CH.
600614
freeze_by_partitions = threads > 0 and "PARTITION BY" in table.create_statement
615+
616+
query_settings = None
617+
# Since https://github.com/ClickHouse/ClickHouse/pull/75016
618+
if self.ch_version_ge("25.2"):
619+
query_settings = {"max_execution_time": self._freeze_timeout}
620+
601621
if freeze_by_partitions:
602622
with ThreadExecPool(max(1, threads)) as pool:
603623
if freeze_by_partitions:
@@ -613,6 +633,7 @@ def freeze_table(
613633
f'Freeze partition "{partition}"',
614634
self._ch_client.query,
615635
query_sql,
636+
settings=query_settings,
616637
timeout=self._freeze_timeout,
617638
should_retry=False,
618639
new_session=True,
@@ -629,6 +650,7 @@ def freeze_table(
629650
)
630651
self._ch_client.query(
631652
query_sql,
653+
settings=query_settings,
632654
timeout=self._freeze_timeout,
633655
should_retry=False,
634656
new_session=True,

ch_backup/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ def _as_seconds(t: str) -> int:
7373
"validate_part_after_upload": False,
7474
"restore_fail_on_attach_error": False,
7575
"update_metadata_interval": _as_seconds("30 min"),
76+
"kill_old_freeze_queries": True,
7677
},
7778
"restore": {
7879
"use_inplace_cloud_restore": False,

ch_backup/logic/table.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ def backup(
7777
logging.debug('Cloud Storage "shadow" backup will be compressed')
7878
context.backup_meta.cloud_storage.compress()
7979

80+
# Since https://github.com/ClickHouse/ClickHouse/pull/75016
81+
if (
82+
context.ch_ctl.ch_version_ge("25.2")
83+
and context.config["kill_old_freeze_queries"]
84+
):
85+
context.ch_ctl.kill_old_freeze_queries()
86+
8087
for db in databases:
8188
self._backup(
8289
context,

0 commit comments

Comments
 (0)