Skip to content

fix: bound ClickHouse system log retention - #13735

Merged
HarshMN2345 merged 18 commits into
mainfrom
codex/fix-clickhouse-log-retention
Oct 8, 2026
Merged

HarshMN2345 merged 18 commits into
mainfrom
codex/fix-clickhouse-log-retention

Conversation

@HarshMN2345

@HarshMN2345 HarshMN2345 commented Sep 17, 2026 •

Copy link
Copy Markdown
Member

What does this PR do?

Bounds the bundled ClickHouse server's own diagnostic logs on self-hosted installations. Most of them have no TTL by default, so trace_log, text_log, metric_log and the rest keep growing: the reporter saw a 10 GB volume fill in two weeks with about 1.5 GB of Appwrite data.

  • clickhouse-config.xml is mounted into config.d and sets a TTL of 3 days for text_log, trace_log and asynchronous_insert_log, 14 days for error_log, and 7 days for the other listed logs. asynchronous_insert_log already defaults to 3 days; setting it explicitly means retention no longer depends on that default surviving an image upgrade.
  • The TTL has to live in server config. An ALTER on its own, for example from usage-setup, is undone by the next restart: ClickHouse moves the altered table to query_log_0 and creates a new query_log with no TTL. usage-setup also runs against whatever _APP_CONNECTIONS_DB_USAGE points to, including external ClickHouse servers.
  • That replacement keeps the old table as <log>_<N> with all of its rows, so existing volumes would never get the space back. clickhouse-init.sh runs on every start (CLICKHOUSE_ALWAYS_RUN_INITDB_SCRIPTS): it flushes the logs, then gives each old table the TTL of the log that replaced it. Old tables that already have a TTL and Appwrite's own tables are left alone. Nothing is truncated; ClickHouse deletes expired rows in the background.

clickhouse-init.sh is committed as executable. On Docker Desktop bind mounts bash's -x check reports every file as executable, so the entrypoint executed the non-executable script and ClickHouse exited with bad interpreter: Permission denied. The installer's copyFiles() now keeps the source file's mode, because copy() drops it.

Both files ship like mongo-init.js and mongo-entrypoint.sh: copied into the image, copied into the install directory by the installer on every install and upgrade, bind-mounted from docker-compose.yml, and rewritten to the host path by the compose generator. External ClickHouse deployments are not affected.

Test Plan

Ran the clickhouse service from this docker-compose.yml, with its bind mounts, on clickhouse/clickhouse-server:26.4.3-alpine:

  • Fresh volume: every listed log has its TTL, there are no <log>_<N> tables, and a restart replaces no tables.
  • Upgrade: started on a volume created without this change, with 30-day-old rows in query_log and 5-day-old rows in trace_log. The 12 listed logs without a matching TTL were replaced and each <log>_0 got its log's TTL (processors_profile_log_0 kept its existing 30 days). asynchronous_insert_log already matched and was not replaced. The old rows were deleted and today's rows remained. A <log>_<N> table with its own 21-day TTL and a table in the appwrite database were unchanged.
  • Repeat: running clickhouse-init.sh again and restarting changed nothing.
  • ALTER only: on a plain volume, ALTER TABLE system.query_log MODIFY TTL ... followed by a restart left a new query_log without a TTL.
  • Installer: the compose generator keeps both bind mounts and rewrites them to hostPath for released and local versions. GeneratorTest passes, and PHPStan and Pint pass on the changed PHP files. A full installer upgrade was not run.

Related PRs and Issues

  • Community report on Discord of ClickHouse volumes filling up on self-hosted 2.x.

Checklist

  • Have you read the Contributing Guidelines on issues?
  • If the PR includes a change to an API's metadata (desc, label, params, etc.), does it also include updated API specs and example docs?

@greptile-apps

greptile-apps Bot commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

The PR appears safe to merge; no actionable correctness, security, or repository-rule violation remains.

Summary

This PR bounds storage consumed by the bundled ClickHouse diagnostic logs and migrates retired system-log tables onto the active tables’ retention policies.

  • Adds explicit TTL configuration for bundled ClickHouse system logs.
  • Runs an initialization migration that flushes logs and applies matching TTLs to retired, previously unbounded log tables.
  • Mounts and distributes the new ClickHouse configuration and initialization files through Compose, the application image, and the installer.
  • Preserves source permissions when installer assets are copied to the host.

Reviews (9) · Last reviewed commit: "fix: run the ClickHouse init script as a..."

@github-actions

github-actions Bot commented Sep 17, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → codex/fix-clickhouse-log-retention (after).

Metric Before After Change
🚀 Requests/sec 199.32 209.6 🟢 +5.2%
⏱️ Latency P50 87.11 ms 82.61 ms 🟢 -5.2%
⏱️ Latency P95 207.22 ms 190.44 ms 🟢 -8.1%
Per-scenario breakdown & investigation details

Metrics below reflect the current branch (after). Δ P95 compares against the base.

Scenario P50 (ms) P95 (ms) Requests RPS Δ P95 (ms)
API total 82.61 190.44 13,167 209.6 -16.77
Account 151.21 287.28 693 11.53 -40.71
TablesDB 80.23 147.53 7,161 116.67 -5.85
Storage 74.84 171.1 3,465 58.23 -2.43
Functions 117.8 223.34 1,848 31.75 -31.26

Top API waits (after)

API request Max wait (ms)
account.name.update 467
storage.files.update 372.79
account.prefs.update 360.89
tablesdb.rows.increment 348.26
storage.files.create 325.71

The script ran outside CI and restated the retention config it checked; docs/tutorials holds maintainer guides, not self-hosting notes.
Give each <log>_<N> table the TTL of the log that replaced it instead of repeating the retention rules in SQL, so logs.xml is the only place they are defined. SYSTEM FLUSH LOGS already replaces every configured log, so current tables need no ALTER. Leave asynchronous_insert_log at its 3-day default.
Comment thread docker-compose.yml Resolved
HarshMN2345 and others added 3 commits September 17, 2026 12:28
Keep it at the 3-day server default, but set it in logs.xml so retention does not depend on that default surviving an image upgrade. Matching the default leaves existing tables in place.
Move the retention config and init script out of inline Compose configs into clickhouse-config.xml and clickhouse-init.sh. They are bind-mounted, copied into the image and by the installer, and rewritten to the host path, the same way as mongo-init.js and mongo-entrypoint.sh. This drops the Compose 2.23.1 requirement.
Comment thread clickhouse-init.sh Resolved
HarshMN2345 and others added 8 commits September 24, 2026 13:09
Docker Desktop bind mounts report every file as executable to bash, so
the entrypoint executed the non-executable script and ClickHouse exited
with 'bad interpreter: Permission denied'. The installer's copy() also
dropped the mode, so installs now keep it.
…on' into fix/clickhouse-idle-cpu

# Conflicts:
#	clickhouse-config.xml
@ghost

ghost commented Oct 5, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

No actionable defects were found in the incremental changes.

The PR configures retention for the bundled ClickHouse system logs, disables several high-volume logs that Appwrite does not consume, and applies retention to stale log tables left behind across restarts. It ships the configuration and initialization script through Docker and the installer, including preserving the script's executable mode when copied.

Latest changes: The newest commits disable the unused per-second and verbose logs, and extend initialization to apply a seven-day fallback TTL to stale log tables whose active counterpart is no longer configured.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 6
File Change
Dockerfile Copies the ClickHouse configuration and initialization script into the runtime and installer images.
clickhouse-config.xml Disables unused high-volume logs and configures TTLs for the retained ClickHouse logs.
clickhouse-init.sh Flushes logs and applies configured or fallback TTLs to stale log tables without retention.
docker-compose.yml Mounts the ClickHouse configuration and init script and runs init scripts on every start.
src/Appwrite/Docker/Compose/Generator.php Includes both ClickHouse bind mounts in generated compose configurations.
src/Appwrite/Platform/Tasks/Install.php Copies ClickHouse support files during installation and preserves their source modes.

Reviewed the commits since 9aaf634 · Details · Comment @hansi-codes review to re-run, or mention @hansi-codes with a question.

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Tier S · Looks good to merge. Summary

@HarshMN2345
HarshMN2345 merged commit fd6aa61 into main Oct 8, 2026
9 checks passed
@github-actions

github-actions Bot commented Oct 8, 2026

Copy link
Copy Markdown

Security rules

No new WARNING or ERROR findings from security rules.

32 existing findings tracked in .semgrep/baseline.json
  • php.appwrite.guest-write-without-abuse-limit (17)
  • php.appwrite.permissive-write-permission (8)
  • php.appwrite.secret-compare-timing (5)
  • php.appwrite.weak-secret-env-default (2)

Posted by Checks / Rules. Re-runs update this comment in place. Rule details and baseline: .semgrep/README.md.

@HarshMN2345 HarshMN2345 mentioned this pull request Oct 9, 2026
2 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants