Files
openobserve/src/super_cluster_queue
Loakesh Indiran 7337a29353 feat: add super cluster sync support for anomaly detection (#11587)
design at: designs/anomaly-detection

## Summary

Adds super cluster sync support for anomaly detection configs and fixes
a missing FK constraint on `anomaly_detection_config.folder_id`.

**Super cluster sync:**
- Add `infra::table::anomaly_detection` module with config CRUD ops
(`create`, `create_if_not_exists`, `update`, `put`, `delete`,
`get_by_id`, `list`)
- Add `super_cluster_queue::anomaly_detection` handler for
`ConfigCreate`, `ConfigUpdate`, and `ConfigDelete` events
- Refactor `service::anomaly_detection` and `scheduler::handlers` to use
the new table module instead of raw SeaORM entity queries
- Refactor `super_cluster_queue::scheduler` to use new table module
- Reschedule trigger instead of deleting when config not found locally —
handles SC race where trigger row arrives before `ConfigCreate` NATS
message is processed

**Migration / referential integrity fix:**
- Add migration `m20260504` to retrofit the missing `FOREIGN KEY
(folder_id) REFERENCES folders(id)` constraint on
`anomaly_detection_config` (alerts and dashboards have had this since
creation; anomaly detection was missing it)
- Pre-migration repair: any rows whose `folder_id` no longer exists in
`folders` (orphaned by folder deletion) are re-pointed to the org's
default Alerts folder before the constraint is added — prevents
migration failure on existing deployments
- Add folder existence check inside transaction in `create()` —
eliminates TOCTOU race between folder lookup and INSERT
- Add folder existence check in `update()` when folder is changing —
matches `alerts::update` behaviour
- Add `PutAnomalyConfigError::FolderDoesNotExist` error variant matching
the alert/dashboard error pattern

## Files changed (11)

- `src/infra/src/table/anomaly_detection/` — new module (config.rs,
mod.rs, models.rs)
- `src/super_cluster_queue/anomaly_detection.rs` — new SC queue handler
- `src/infra/src/table/mod.rs` — register new module
- `src/super_cluster_queue/mod.rs` — wire anomaly_detection handler
- `src/service/anomaly_detection.rs` — refactored to use new table
module
- `src/service/alerts/scheduler/handlers.rs` — refactored; reschedule on
missing config
- `src/super_cluster_queue/scheduler.rs` — refactored to use new table
module
- `src/infra/src/errors/mod.rs` — add
`PutAnomalyConfigError::FolderDoesNotExist`
-
`src/infra/src/table/migration/m20260504_000001_add_anomaly_detection_config_folder_fk.rs`
— new migration
- `src/infra/src/table/migration/mod.rs` — register migration

## Test plan

- [x] Anomaly config create/update/delete broadcasts appear in SC queue
logs on primary region
- [x] Follower region receives and applies `ConfigCreate`,
`ConfigUpdate`, `ConfigDelete`
- [x] Trigger reschedule (not delete) when config not yet synced to
follower
- [x] Create with non-existent folder returns clean `FolderDoesNotExist`
error
- [x] Update moving config to non-existent folder returns clean
`FolderDoesNotExist` error
- [x] Migration runs clean on existing DB with no orphaned rows
- [x] Migration repairs orphaned `folder_id` rows before adding FK
constraint
- [x] `cargo check --workspace` passes
2026-05-05 19:26:11 +00:00
..
2026-05-04 10:38:41 +00:00