Skip to content

fix(deletes): batch expired transaction log cleanup - #13563

Merged
HarshMN2345 merged 13 commits into
mainfrom
codex/batch-expired-transactions
Sep 21, 2026
Merged

HarshMN2345 merged 13 commits into
mainfrom
codex/batch-expired-transactions

Conversation

@HarshMN2345

@HarshMN2345 HarshMN2345 commented Sep 9, 2026 •

Copy link
Copy Markdown
Member

What does this PR do?

Maintenance deletes expired transactions and then removes their logs by transaction sequence. With more than 5,000 expired transactions, the single log-deletion query exceeds the database's value limit and fails after the transactions have been deleted.

Split the collected sequences into batches sized by the database's configured maximum query values, so every deleted transaction's logs can be cleaned up.

Test Plan

  • Added a General E2E regression that creates its own project through HTTP, seeds real transaction/log documents, and publishes maintenance to a unique queue consumed by the real app/worker.php deletes process. No worker or database stubs.
  • Seeds one more expired transaction than the actual configured query limit (5,001 locally), without changing that limit. Checks that all expired transactions/logs disappear and active data survives. A second queued job removes newly expired data, proving repeated maintenance still works.
  • Against the baseline worker, the test reproduces the transactionInternalId query-value-limit exception and retained logs. With batching restored, the real PostgreSQL integration passes.
  • docker exec appwrite-transactions-test /usr/local/bin/test tests/e2e/General/DeletesTest.php — 1 test passed using an isolated worktree runner against local services.
  • Targeted Pint, PHPStan, Rector dry-run, and git diff --check passed.
  • Worker teardown polls with a deadline and escalates to SIGKILL for its own process group. A temporary SIGSTOP fault injection verified escalation completes within a 25-second external timeout; the injection is not in the committed test.

This prevents new over-limit failures. Logs orphaned by earlier failed runs require separate cleanup because their transaction documents have already been deleted.

Related PRs and Issues

Checklist

@greptile-apps

greptile-apps Bot commented Sep 9, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

The current implementation appears safe to merge, with no actionable new correctness or security issues identified.

Summary

This PR prevents expired-transaction log cleanup from exceeding the database query-value limit.

  • Splits collected transaction sequence IDs into database-configured query-sized batches.
  • Deletes transaction logs once per batch while preserving the existing best-effort error handling.
  • Guards against a non-positive configured limit before calling array_chunk.

Reviews (11) · Last reviewed commit: "test: drop the maintenance worker e2e fo..."

Comment thread tests/e2e/General/DeletesTest.php Resolved
@github-actions

github-actions Bot commented Sep 9, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → codex/batch-expired-transactions (after).

Metric Before After Change
🚀 Requests/sec 198.79 200.92 ⚪ +1.1%
⏱️ Latency P50 86.6 ms 87.08 ms ⚪ +0.6%
⏱️ Latency P95 206.26 ms 198.94 ms ⚪ -3.5%
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 87.08 198.94 12,540 200.92 -7.32
Account 170.7 318.19 660 11.31 +8.26
TablesDB 84.55 158.58 6,820 112.22 +5.3
Storage 78.71 165.92 3,300 55.69 -12.32
Functions 127.1 237.17 1,760 30.33 -32.48

Top API waits (after)

API request Max wait (ms)
account.name.update 523.35
account.prefs.update 392.58
tablesdb.rows.get 366.12
functions.variables.update 358.83
tablesdb.rows.increment 344.38

Comment thread tests/e2e/General/DeletesTest.php Resolved
HarshMN2345 and others added 9 commits September 9, 2026 12:02
…tive limit

array_chunk() throws a ValueError when the length is below 1, so a database
configured with a non-positive maxQueryValues would take the cleanup worker
down instead of batching. utopia-php/database wraps every one of its own
array_chunk() calls on that value in max(1, ...); match it here.

Not reachable today - Factory::configureOptions only applies the setter when
the value is above zero, and the library default is 5000 - so this is a guard
against future configuration rather than a fix for a live failure.
Comment thread tests/e2e/General/DeletesTest.php Resolved
HarshMN2345 and others added 2 commits September 21, 2026 18:23
The test fails in CI. It was the only failure in Tests / E2E / General on
run 35599864714:

    1) Tests\E2E\General\DeletesTest::testDeleteExpiredTransactions
    tests/e2e/General/DeletesTest.php:134
    Tests: 32, Assertions: 182, Failures: 1, Skipped: 10

Line 134 is the catch-all fail(), so the coroutine threw inside the CI
container rather than tripping an assertion about batching. The test passed
locally only, against a hand-built worktree runner.

It was also the only test in the suite to fork an OS process: proc_open of
app/worker.php under setsid, terminated by process-group SIGKILL, seeding
about 10,000 documents to clear the 5,000 query-value limit. Nothing else in
tests/e2e does that, and the cost is not worth carrying for a one-line chunk.

Removed rather than repaired so the fix is not blocked on it. Coverage should
come back as a worker e2e that drives the queue in-process, the way
tests/e2e/General/WorkerConcurrencyTest.php already does.
@HarshMN2345
HarshMN2345 merged commit b83cbd4 into main Sep 21, 2026
50 checks passed
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