-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
chore(aci): backfill detectorgroup for metric issues #103399
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore(aci): backfill detectorgroup for metric issues #103399
Conversation
| Detector = apps.get_model("workflow_engine", "Detector") | ||
|
|
||
| for group in RangeQuerySetWrapper( | ||
| Group.objects.filter(type=8001, detectorgroup__isnull=True) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this will end up being super slow and timing out using RangeQuerySetWrapper. Probably you just need to perform the detectorgroup query separately in the loop.
If you're concerned about performance, you could fetch groups in batches of 100 and fetch the detectorgroup rows in bulk.
Alternatively, there are only 26k rows with type 8001 in general. You could just bring them all into memory and skip the range wrapper completely
|
This PR has a migration; here is the generated SQL for for --
-- Alter field detector on detectorgroup
--
SET CONSTRAINTS "workflow_engine_dete_detector_id_0c22c088_fk_workflow_" IMMEDIATE; ALTER TABLE "workflow_engine_detectorgroup" DROP CONSTRAINT "workflow_engine_dete_detector_id_0c22c088_fk_workflow_";
ALTER TABLE "workflow_engine_detectorgroup" ALTER COLUMN "detector_id" DROP NOT NULL;
ALTER TABLE "workflow_engine_detectorgroup" ADD CONSTRAINT "workflow_engine_dete_detector_id_0c22c088_fk_workflow_" FOREIGN KEY ("detector_id") REFERENCES "workflow_engine_detector" ("id") DEFERRABLE INITIALLY DEFERRED NOT VALID;
ALTER TABLE "workflow_engine_detectorgroup" VALIDATE CONSTRAINT "workflow_engine_dete_detector_id_0c22c088_fk_workflow_";for --
-- Raw Python operation
--
-- THIS OPERATION CANNOT BE WRITTEN AS SQL |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #103399 +/- ##
===========================================
- Coverage 80.67% 80.59% -0.09%
===========================================
Files 9242 9243 +1
Lines 394806 394860 +54
Branches 25147 25147
===========================================
- Hits 318526 318238 -288
- Misses 75833 76175 +342
Partials 447 447 |
a47440d to
05b3d5b
Compare
|
This PR has a migration; here is the generated SQL for for --
-- Raw Python operation
--
-- THIS OPERATION CANNOT BE WRITTEN AS SQL |
src/sentry/workflow_engine/migrations/0099_backfill_metric_issue_detectorgroup.py
Show resolved
Hide resolved
f33ed70 to
e7f3c54
Compare
| ] | ||
|
|
||
|
|
||
| def get_oldest_or_latest_event( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe a "copied from group.py with minimal edits" comment, otherwise the flexibility feels a bit unnecessary.
| ) | ||
| continue | ||
|
|
||
| DetectorGroup.objects.create( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think technically Group is observable before DetectorGroup for a short period, so a get_or_create might be safer, but I doubt it'll come up.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should error in that case because DetectorGroup is now unique on Group, but we can rerun in that case
| DetectorGroup.objects.create( | ||
| group_id=group.id, | ||
| detector_id=None, | ||
| ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: Safe concurrent object creation prevents errors.
Using DetectorGroup.objects.create() without handling potential race conditions can cause IntegrityError since DetectorGroup has a unique constraint on group. If another process creates a DetectorGroup for the same group between the filter check and the create call, the migration will fail. Using get_or_create() would safely handle this scenario.
mifu67
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Backfill logic for metric issues looks good to me! With only 827 entries, this should be pretty fast.
Issues attributed to commits in this pull requestThis pull request was merged and Sentry observed the following issues: |
Backfill metric issue
DetectorGrouprows.If a metric issue's detector no longer exists, we create a row with a null
Detector.