Skip to content

fix(usage): realign skip indexes that drifted from their definition - #14591

Merged
abnegate merged 1 commit into
mainfrom
qfix-usage-skip-index-realign
Oct 16, 2026
Merged

abnegate merged 1 commit into
mainfrom
qfix-usage-skip-index-realign

Conversation

@abnegate

@abnegate abnegate commented Oct 16, 2026 •

Copy link
Copy Markdown
Member

What does this PR do?

Usage::migrate() (and so setup()) adds skip indexes with ADD INDEX IF NOT EXISTS, which matches by name only. Two shapes therefore survive every migration:

  1. A defined name with another expression or type. For example index-resourceType on service, or index-resourceId as set(0) where the definition says bloom_filter. The name exists, so nothing happens.
  2. A stale name that duplicates a defined index. This is the production shape. On fra1 and nyc3, projects_usage_gauges (as set) and projects_usage_events (as bloom_filter) still have index-resource on resourceType. The column used to be called resource. It was renamed to resourceType, but its index kept its old name. The library now defines index-resourceType on resourceType and no index-resource, so migrate() added index-resourceType next to the old index. Both now index the same column, and every merge maintains both.

Note: on main the gauges definition has no index-resource on resource. Metric::getGaugeIndexes() covers service, resourceType, resourceId, resourceInternalId, teamId, teamInternalId, ordinal. The only index-resource on resource I found is on staging, which still has a legacy resource column. This PR leaves that index alone, because it duplicates nothing.

The fix is in ensureIndexes(). It reads the live indexes from system.data_skipping_indices, using the query modifyColumns() already ran (now getSkipIndexes()). It compares each one with the definition: the expression by its identifiers, using the getIdentifiers() tokenizer from #14561 (no regex), and the type with spaces ignored. Then:

  • a defined name whose expression or type differs is dropped;
  • an index under any other name whose expression and type equal a defined index is dropped;
  • the defined indexes are added (ADD INDEX IF NOT EXISTS, as before);
  • every index that replaces a dropped one is materialized (MATERIALIZE INDEX, as modifyColumns() does), so the parts the dropped index covered are indexed again.

Indexes that match their definition are not touched. Indexes that duplicate nothing, such as an operator's own index, are left as they are. On a converged table, migrate() issues no DROP INDEX and no MATERIALIZE INDEX, so a rerun adds nothing to system.mutations. Granularity is not compared.

If a run is interrupted after the drop, the next run adds the index again, and merges build it for older parts. That is the same recovery modifyColumns() documents.

Production

Running this against production is an operator decision. I have not run it anywhere except the package's local ClickHouse. On fra1 and nyc3, the next task-usage-setup would drop index-resource on both tables and run MATERIALIZE INDEX index-resourceType. That is an asynchronous mutation that builds the index for every existing part, which on the largest tables takes a while. Until it finishes, older parts are read without that skip index, which is slower but returns the same results. Regions without the stale index see no change.

Test Plan

Both new tests run against a real ClickHouse 25.11 (bin/monorepo test usage). Each starts from the mismatched shape.

Test Starting shape Before (main, ensureIndexes() by name) After
ClickHouseSchemaTest::testMigrateRealignsSkipIndexesWhoseExpressionOrTypeDrifted gauges with index-resourceType on service and index-resourceId as set(0) FAIL: index-resourceType still on service PASS: both recreated as defined, MATERIALIZE INDEX issued for each, index-service untouched, rerun adds no mutation
ClickHouseSchemaTest::testMigrateDropsAnIndexThatDuplicatesADefinedIndexUnderAnotherName the fra1/nyc3 shape on gauges (set(0)) and events (bloom_filter): index-resource on resourceType, plus an unrelated index-custom on metric FAIL: index-resource still present PASS: index-resource dropped, index-resourceType present and materialized, index-custom kept, rerun adds no mutation
  • bin/monorepo test usage: 112 unit tests pass, and 277 E2E tests pass, up from 275 with the two new ones. The suite's 5 deprecations (Query::contains() in UsageBase) and 1 notice (file_get_contents() in ClickHouseTest) are the same on main.
  • bin/monorepo check usage (Pint, Rector, PHPStan) passes.

Related PRs and Issues

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? (No API changes.)

🤖 Generated with Claude Code

ensureIndexes() added indexes with ADD INDEX IF NOT EXISTS, which matches by
name only, so an index whose name is defined but whose expression or type is
not was never corrected. It also left a second shape in place: production
fra1 and nyc3 still carry `index-resource` on `resourceType`, because the
`resource` column was renamed to `resourceType` but its index kept the old
name. The definition no longer has `index-resource`, so migrate() added
`index-resourceType` next to it and both now index the same column.

migrate() now reads the live indexes from system.data_skipping_indices and,
comparing expressions by their identifiers with the existing tokenizer,
drops a defined name whose expression or type differs, and drops an index
under another name that duplicates a defined one. The defined index is then
added and materialized, as modifyColumns() already does, so the parts the
dropped index covered are indexed again. A converged table issues no drops
or materializations, so a rerun is a no-op.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@luxor-code-review

luxor-code-review Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

Review complete. No blocking issues — approved ✅; 1 nitpick below.

🧹 Nitpicks (1) — 🟢 1 low
  • 🟢 Drifted skip index rebuilt twice in one migrate (ClickHouse.php:1554) — When a drifted dimension column is covered by a skip index whose shape also drifted, modifyColumns drops it, re-adds it with its live (drifted) expression and type, and issues a full MATERIALIZE INDEX rebuild (packages/usage/src/Adapter/ClickHouse.php:1554-1562).

The change aligns live ClickHouse skip indexes with the adapter's declared schema: it now detects drifted or missing indexes and issues drop/re-add/MATERIALIZE statements to converge the table, with a new e2e test covering the schema reconciliation and a changelog entry documenting it.

Files Change
packages/usage/src/Adapter/ClickHouse.php Adds skip-index drift detection and realignment logic (drop/re-add/MATERIALIZE) to the schema-ensure path
packages/usage/tests/E2E/Adapter/ClickHouseSchemaTest.php Adds e2e coverage asserting schema convergence for skip indexes
packages/usage/CHANGELOG.md Documents the skip-index realignment change

Reviewed commit: 63d08a5

@ghost

ghost commented Oct 16, 2026

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

The ClickHouse usage migration now compares live skip-index expressions and types with the definitions, removes drifted or duplicate indexes, and materializes replacements. The schema tests cover mismatched definitions, renamed duplicate indexes, preservation of unrelated indexes, and idempotent reruns.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 3
File Change
packages/usage/CHANGELOG.md Documents skip-index realignment during usage migrations.
packages/usage/src/Adapter/ClickHouse.php Compares live skip-index shapes with definitions and replaces drifted or duplicate indexes.
packages/usage/tests/E2E/Adapter/ClickHouseSchemaTest.php Adds ClickHouse integration coverage for index realignment and duplicate cleanup.

Reviewed 63d08a5 · 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

@github-actions

Copy link
Copy Markdown

Security rules

No new WARNING or ERROR findings from security rules.

19 existing findings tracked in .semgrep/baseline.json
  • php.appwrite.guest-write-without-abuse-limit (12)
  • 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.

@github-actions

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → qfix-usage-skip-index-realign (after).

Metric Before After Change
🚀 Requests/sec 211.81 218.34 ⚪ +3.1%
⏱️ Latency P50 81.03 ms 79.94 ms ⚪ -1.3%
⏱️ Latency P95 192.61 ms 182.96 ms ⚪ -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 79.94 182.96 13,680 218.34 -9.65
Account 145.55 294.53 720 12.1 +23.74
TablesDB 79.5 154.61 7,440 121.07 -2.94
Storage 68.88 160.64 3,600 60.58 -21.2
Functions 109.42 209.36 1,920 33.07 -4.22

Top API waits (after)

API request Max wait (ms)
account.name.update 465.29
account.prefs.update 394.11
storage.files.create 347.77
tablesdb.rows.create 337.49
storage.buckets.create 335.18

@abnegate
abnegate merged commit a776dd3 into main Oct 16, 2026
66 checks passed
@ChiragAgg5k ChiragAgg5k mentioned this pull request Oct 21, 2026
6 tasks
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.

1 participant