-
Notifications
You must be signed in to change notification settings - Fork 13
fix(jobs): throttle and defer pipeline heartbeat update #1258
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
fd1277f
fix(jobs): throttle + defer pipeline heartbeat update
mihow 660360e
fix(jobs): harden throttled pipeline heartbeat updates (#1260)
Copilot fd8e379
fix(jobs): gate heartbeat dispatch with Redis cache to cut broker churn
mihow e2f982e
refactor(jobs): simplify heartbeat task to rely on view-level gate
mihow File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ | |
| from ami.jobs.models import Job, JobDispatchMode, JobProgress, JobState, MLJob, SourceImageCollectionPopulateJob | ||
| from ami.main.models import Project, SourceImage, SourceImageCollection | ||
| from ami.ml.models import Pipeline | ||
| from ami.ml.models.processing_service import ProcessingService | ||
| from ami.ml.orchestration.jobs import queue_images_to_nats | ||
| from ami.users.models import User | ||
|
|
||
|
|
@@ -1016,3 +1017,143 @@ def test_tasks_endpoint_rejects_non_async_jobs(self): | |
| resp = self.client.post(tasks_url, {"batch_size": 1}, format="json") | ||
| self.assertEqual(resp.status_code, 400) | ||
| self.assertIn("async_api", resp.json()[0].lower()) | ||
|
|
||
|
|
||
| class TestPipelineHeartbeatTask(APITestCase): | ||
|
Collaborator
Author
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. This is a of tests for a "nice to have" feature that is slowing down our required core features. What is necessary? |
||
| """ | ||
| Unit tests for update_pipeline_pull_services_seen and the view-level | ||
| _mark_pipeline_pull_services_seen fire-and-forget dispatch. | ||
| """ | ||
|
|
||
| def setUp(self): | ||
| from django.core.cache import cache | ||
|
|
||
| # Cache-based gate in _mark_pipeline_pull_services_seen would otherwise | ||
| # carry over between tests and suppress the .delay() we want to assert. | ||
| cache.clear() | ||
|
|
||
| self.project = Project.objects.create(name="Heartbeat Test Project") | ||
| self.pipeline = Pipeline.objects.create(name="Heartbeat Pipeline", slug="heartbeat-pipeline") | ||
| self.pipeline.projects.add(self.project) | ||
| self.collection = SourceImageCollection.objects.create(name="HB Collection", project=self.project) | ||
| self.job = Job.objects.create( | ||
| job_type_key=MLJob.key, | ||
| project=self.project, | ||
| name="Heartbeat Test Job", | ||
| pipeline=self.pipeline, | ||
| source_image_collection=self.collection, | ||
| dispatch_mode=JobDispatchMode.ASYNC_API, | ||
| ) | ||
| self.service = ProcessingService.objects.create( | ||
| name="Heartbeat Worker", | ||
| endpoint_url=None, # None = pull-mode / async service | ||
| ) | ||
| self.service.pipelines.add(self.pipeline) | ||
| self.service.projects.add(self.project) | ||
|
|
||
| def test_tasks_endpoint_dispatches_heartbeat_task(self): | ||
| """The /tasks endpoint calls update_pipeline_pull_services_seen.delay(), not the DB directly.""" | ||
| from unittest.mock import patch | ||
|
|
||
| job = self.job | ||
| job.status = JobState.STARTED | ||
| job.save(update_fields=["status"]) | ||
|
|
||
| images = [ | ||
| SourceImage.objects.create( | ||
| path=f"hb_tasks_{i}.jpg", | ||
| public_base_url="http://example.com", | ||
| project=self.project, | ||
| ) | ||
| for i in range(2) | ||
| ] | ||
| queue_images_to_nats(job, images) | ||
|
|
||
| user = User.objects.create_user(email="hbtest@example.com", is_superuser=True, is_active=True) | ||
| self.client.force_authenticate(user=user) | ||
|
|
||
| with patch("ami.jobs.views.update_pipeline_pull_services_seen.delay") as mock_delay: | ||
| tasks_url = reverse_with_params("api:job-tasks", args=[job.pk], params={"project_id": self.project.pk}) | ||
| resp = self.client.post(tasks_url, {"batch_size": 1}, format="json") | ||
|
|
||
| self.assertEqual(resp.status_code, 200) | ||
| mock_delay.assert_called_once_with(job.pk) | ||
|
|
||
| def test_result_endpoint_dispatches_heartbeat_task(self): | ||
| """The /result endpoint calls update_pipeline_pull_services_seen.delay(), not the DB directly.""" | ||
| from unittest.mock import MagicMock, patch | ||
|
|
||
| user = User.objects.create_user(email="hbresult@example.com", is_superuser=True, is_active=True) | ||
| self.client.force_authenticate(user=user) | ||
|
|
||
| result_data = { | ||
| "results": [ | ||
| { | ||
| "reply_subject": "test.reply.hb", | ||
| "result": { | ||
| "pipeline": "heartbeat-pipeline", | ||
| "algorithms": {}, | ||
| "total_time": 0.1, | ||
| "source_images": [], | ||
| "detections": [], | ||
| "errors": None, | ||
| }, | ||
| } | ||
| ] | ||
| } | ||
|
|
||
| mock_async_result = MagicMock() | ||
| mock_async_result.id = "hb-task-id" | ||
| with ( | ||
| patch("ami.jobs.views.process_nats_pipeline_result.delay", return_value=mock_async_result), | ||
| patch("ami.jobs.views.update_pipeline_pull_services_seen.delay") as mock_delay, | ||
| ): | ||
| result_url = reverse_with_params( | ||
| "api:job-result", args=[self.job.pk], params={"project_id": self.project.pk} | ||
| ) | ||
| resp = self.client.post(result_url, result_data, format="json") | ||
|
|
||
| self.assertEqual(resp.status_code, 200) | ||
| mock_delay.assert_called_once_with(self.job.pk) | ||
|
|
||
| def test_tasks_endpoint_tolerates_heartbeat_dispatch_failure(self): | ||
| """Heartbeat enqueue errors should not fail the /tasks response.""" | ||
| from unittest.mock import patch | ||
|
|
||
| from kombu.exceptions import OperationalError | ||
|
|
||
| job = self.job | ||
| job.status = JobState.STARTED | ||
| job.save(update_fields=["status"]) | ||
|
|
||
| image = SourceImage.objects.create( | ||
| path="hb_tasks_broker.jpg", | ||
| public_base_url="http://example.com", | ||
| project=self.project, | ||
| ) | ||
| queue_images_to_nats(job, [image]) | ||
|
|
||
| user = User.objects.create_user(email="hbbroker@example.com", is_superuser=True, is_active=True) | ||
| self.client.force_authenticate(user=user) | ||
|
|
||
| with patch( | ||
| "ami.jobs.views.update_pipeline_pull_services_seen.delay", | ||
| side_effect=OperationalError("broker unavailable"), | ||
| ): | ||
| tasks_url = reverse_with_params("api:job-tasks", args=[job.pk], params={"project_id": self.project.pk}) | ||
| resp = self.client.post(tasks_url, {"batch_size": 1}, format="json") | ||
|
|
||
| self.assertEqual(resp.status_code, 200) | ||
| self.assertEqual(len(resp.json()["tasks"]), 1) | ||
|
|
||
| def test_view_gate_suppresses_redundant_dispatches(self): | ||
| """Rapid repeated calls to _mark_pipeline_pull_services_seen should only enqueue once per window.""" | ||
| from unittest.mock import patch | ||
|
|
||
| from ami.jobs.views import _mark_pipeline_pull_services_seen | ||
|
|
||
| with patch("ami.jobs.views.update_pipeline_pull_services_seen.delay") as mock_delay: | ||
| for _ in range(5): | ||
| _mark_pipeline_pull_services_seen(self.job) | ||
|
|
||
| self.assertEqual(mock_delay.call_count, 1) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.