-
Notifications
You must be signed in to change notification settings - Fork 710
UN-3973 [DEV] Cut dashboard cron DB time by deriving monthly metrics from the daily tier #2255
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
base: UN-3883-Optimize-DB-cron-queries-causing-high-DB-load
Are you sure you want to change the base?
Changes from all commits
fe467d8
3ea08b7
3953934
86ed04c
72cfed7
01e01f0
ade1e68
5804a12
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| """Data migration to schedule the daily-tier reconciliation pass. | ||
|
|
||
| The 15-minute aggregation reads a narrow source window, which cannot repair | ||
| gaps left by cron downtime. This runs the same task once a day at a wider | ||
| window to backfill them. | ||
| """ | ||
|
|
||
| from django.db import migrations | ||
|
|
||
| RECONCILE_TASK_NAME = "dashboard_metrics_reconcile_source_window" | ||
|
|
||
|
|
||
| def create_reconciliation_task(apps, schema_editor): | ||
| """Create the once-daily reconciliation periodic task.""" | ||
| crontab_model = apps.get_model("django_celery_beat", "CrontabSchedule") | ||
| periodic_task_model = apps.get_model("django_celery_beat", "PeriodicTask") | ||
|
|
||
| # 4:00 AM UTC — clear of the 2:00 and 3:00 cleanup tasks | ||
| schedule_4am, _ = crontab_model.objects.get_or_create( | ||
| minute="0", | ||
| hour="4", | ||
| day_of_week="*", | ||
| day_of_month="*", | ||
| month_of_year="*", | ||
| defaults={"timezone": "UTC"}, | ||
| ) | ||
|
|
||
| periodic_task_model.objects.update_or_create( | ||
| name=RECONCILE_TASK_NAME, | ||
| defaults={ | ||
| "task": "dashboard_metrics.aggregate_from_sources", | ||
| "crontab": schedule_4am, | ||
| "queue": "dashboard_metric_events", | ||
| "kwargs": '{"source_window_days": 7}', | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [High] [Lens 5, 11] — this kwarg cannot reach the task on the PG scheduler path
Widening the worker signature alone is not enough: Net: once Also: |
||
| "enabled": True, | ||
| "description": ( | ||
| "Re-aggregate metrics over a 7 day source window to repair " | ||
| "daily-tier gaps left by cron downtime" | ||
| ), | ||
| }, | ||
| ) | ||
|
|
||
|
|
||
| def remove_reconciliation_task(apps, schema_editor): | ||
| """Remove the reconciliation periodic task on rollback.""" | ||
| periodic_task_model = apps.get_model("django_celery_beat", "PeriodicTask") | ||
| periodic_task_model.objects.filter(name=RECONCILE_TASK_NAME).delete() | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
| dependencies = [ | ||
| ("dashboard_metrics", "0004_pg_periodic_tasks"), | ||
| ("django_celery_beat", "0018_improve_crontab_helptext"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.RunPython( | ||
| create_reconciliation_task, | ||
| remove_reconciliation_task, | ||
| ), | ||
| ] | ||
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.
[High] [Lens 6, 8] — the reconciliation pass silently no-ops for the day whenever it loses the lock
This row reuses the same task name, so it contends for the single
AGGREGATION_LOCK_KEY. The 15-minute row is anIntervalSchedule(every=15, period="minutes")(0002_setup_periodic_tasks.py:20-21), which drifts against this fixed 04:00 crontab.On collision the run returns
{"success": True, "skipped": True, "reason": "lock_held"}and is never retried. At roughly 138 s of work per 15-minute slot that is a materially recurring loss of the only repair mechanism for the narrowed window — and it is reported as success.Suggested fix: give the reconciliation its own lock key, or retry on
lock_held.