-
Notifications
You must be signed in to change notification settings - Fork 5
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
Fix dataset timestamps #190
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
42240d2
Reproduce incorrect dataset timestamps
tillprochaska 1a86b26
Remove "end_time" timestamp
tillprochaska cbc905f
Only set `start_time` timestamp if it hasn’t been set yet
tillprochaska c110105
Delete dataset timestamps when all tasks have been processed
tillprochaska 053ac1d
Add tests covering dataset status when cancelling tasks
tillprochaska 3128c0a
Extract flushing status data into separate method
tillprochaska a68ff0e
Ensure time-machine is installed in GitHub Actions
tillprochaska 2b9f9ae
Delete dead code
tillprochaska 49feab8
Remove redundant code
tillprochaska 811ef5b
Use `is_done` helper method
tillprochaska 297872b
Merge branch 'release/1.23.0' into fix/dataset-timestamps
catileptic 292c3c0
Fix linter errors
catileptic 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 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 |
---|---|---|
|
@@ -5,3 +5,4 @@ pytest-env==1.1.3 | |
pytest-cov==5.0.0 | ||
pytest-mock==3.14.0 | ||
wheel==0.43.0 | ||
time-machine==2.14.1 |
This file contains 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 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 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 |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
from unittest.mock import patch | ||
import json | ||
from random import randrange | ||
import time_machine | ||
|
||
import pika | ||
from prometheus_client import REGISTRY | ||
|
@@ -73,7 +74,6 @@ def test_task_queue(self): | |
assert status["finished"] == 0, status | ||
assert status["pending"] == 1, status | ||
assert status["running"] == 0, status | ||
assert status["end_time"] is None | ||
started = unpack_datetime(status["start_time"]) | ||
last_updated = unpack_datetime(status["last_update"]) | ||
assert started < last_updated | ||
|
@@ -121,10 +121,8 @@ def test_task_queue(self): | |
assert status["finished"] == 0, status | ||
assert status["pending"] == 0, status | ||
assert status["running"] == 0, status | ||
# started = unpack_datetime(status["start_time"]) | ||
# last_updated = unpack_datetime(status["last_update"]) | ||
# end_time = unpack_datetime(status["end_time"]) | ||
# assert started < end_time < last_updated | ||
assert status["start_time"] is None | ||
assert status["last_update"] is None | ||
|
||
@patch("servicelayer.taskqueue.Dataset.should_execute") | ||
def test_task_that_shouldnt_execute(self, mock_should_execute): | ||
|
@@ -192,6 +190,199 @@ def did_nack(): | |
assert dataset.is_task_tracked(Task(**body)) | ||
|
||
|
||
def test_dataset_get_status(): | ||
conn = get_fakeredis() | ||
conn.flushdb() | ||
|
||
dataset = Dataset(conn=conn, name="123") | ||
status = dataset.get_status() | ||
|
||
assert status["pending"] == 0 | ||
assert status["running"] == 0 | ||
assert status["finished"] == 0 | ||
assert status["start_time"] is None | ||
assert status["last_update"] is None | ||
|
||
task_one = Task( | ||
task_id="1", | ||
job_id="abc", | ||
delivery_tag="", | ||
operation="ingest", | ||
context={}, | ||
payload={}, | ||
priority=5, | ||
collection_id="1", | ||
) | ||
|
||
task_two = Task( | ||
task_id="2", | ||
job_id="abc", | ||
delivery_tag="", | ||
operation="ingest", | ||
context={}, | ||
payload={}, | ||
priority=5, | ||
collection_id="1", | ||
) | ||
|
||
task_three = Task( | ||
task_id="3", | ||
job_id="abc", | ||
delivery_tag="", | ||
operation="index", | ||
context={}, | ||
payload={}, | ||
priority=5, | ||
collection_id="1", | ||
) | ||
|
||
# Adding a task updates `start_time` and `last_update` | ||
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. I appreciate your comments in this non-trivial test! |
||
with time_machine.travel("2024-01-01T00:00:00"): | ||
dataset.add_task(task_one.task_id, task_one.operation) | ||
|
||
status = dataset.get_status() | ||
assert status["pending"] == 1 | ||
assert status["running"] == 0 | ||
assert status["finished"] == 0 | ||
assert status["start_time"].startswith("2024-01-01T00:00:00") | ||
assert status["last_update"].startswith("2024-01-01T00:00:00") | ||
|
||
# Once a worker starts processing a task, only `last_update` is updated | ||
with time_machine.travel("2024-01-02T00:00:00"): | ||
dataset.checkout_task(task_one.task_id, task_one.operation) | ||
|
||
status = dataset.get_status() | ||
assert status["pending"] == 0 | ||
assert status["running"] == 1 | ||
assert status["finished"] == 0 | ||
assert status["start_time"].startswith("2024-01-01T00:00:00") | ||
assert status["last_update"].startswith("2024-01-02T00:00:00") | ||
|
||
# When another task is added, only `last_update` is updated | ||
with time_machine.travel("2024-01-03T00:00:00"): | ||
dataset.add_task(task_two.task_id, task_two.operation) | ||
|
||
status = dataset.get_status() | ||
assert status["pending"] == 1 | ||
assert status["running"] == 1 | ||
assert status["finished"] == 0 | ||
assert status["start_time"].startswith("2024-01-01T00:00:00") | ||
assert status["last_update"].startswith("2024-01-03T00:00:00") | ||
|
||
# When the first task has been processed, `last_update` is updated | ||
with time_machine.travel("2024-01-04T00:00:00"): | ||
dataset.mark_done(task_one) | ||
|
||
status = dataset.get_status() | ||
assert status["pending"] == 1 | ||
assert status["running"] == 0 | ||
assert status["finished"] == 1 | ||
assert status["start_time"].startswith("2024-01-01T00:00:00") | ||
assert status["last_update"].startswith("2024-01-04T00:00:00") | ||
|
||
# When the worker starts processing the second task, only `last_update` is updated | ||
with time_machine.travel("2024-01-05T00:00:00"): | ||
dataset.checkout_task(task_two.task_id, task_two.operation) | ||
|
||
status = dataset.get_status() | ||
assert status["pending"] == 0 | ||
assert status["running"] == 1 | ||
assert status["finished"] == 1 | ||
assert status["start_time"].startswith("2024-01-01T00:00:00") | ||
assert status["last_update"].startswith("2024-01-05T00:00:00") | ||
|
||
# Once all tasks have been processed, status data is flushed | ||
with time_machine.travel("2024-01-06T00:00:00"): | ||
dataset.mark_done(task_two) | ||
|
||
status = dataset.get_status() | ||
assert status["pending"] == 0 | ||
assert status["running"] == 0 | ||
assert status["finished"] == 0 | ||
assert status["start_time"] is None | ||
assert status["last_update"] is None | ||
|
||
# Adding a new task to an inactive dataset sets `start_time` | ||
with time_machine.travel("2024-01-07T00:00:00"): | ||
dataset.add_task(task_three.task_id, task_three.operation) | ||
|
||
status = dataset.get_status() | ||
assert status["pending"] == 1 | ||
assert status["running"] == 0 | ||
assert status["finished"] == 0 | ||
assert status["start_time"].startswith("2024-01-07T00:00:00") | ||
assert status["last_update"].startswith("2024-01-07T00:00:00") | ||
|
||
# Cancelling a dataset flushes status data | ||
with time_machine.travel("2024-01-08T00:00:00"): | ||
dataset.checkout_task(task_three.task_id, task_three.operation) | ||
dataset.cancel() | ||
|
||
status = dataset.get_status() | ||
assert status["pending"] == 0 | ||
assert status["running"] == 0 | ||
assert status["finished"] == 0 | ||
assert status["start_time"] is None | ||
assert status["last_update"] is None | ||
|
||
# Tasks that were already running when the dataset was cancelled | ||
# have no effect | ||
with time_machine.travel("2024-01-09T00:00:00"): | ||
dataset.mark_done(task_three) | ||
|
||
assert status["pending"] == 0 | ||
assert status["running"] == 0 | ||
assert status["finished"] == 0 | ||
assert status["start_time"] is None | ||
assert status["last_update"] is None | ||
|
||
|
||
def test_dataset_cancel(): | ||
conn = get_fakeredis() | ||
conn.flushdb() | ||
|
||
dataset = Dataset(conn=conn, name="abc") | ||
assert conn.keys() == [] | ||
|
||
# Enqueueing tasks stores status data in Redis | ||
dataset.add_task("1", "ingest") | ||
dataset.add_task("2", "index") | ||
dataset.checkout_task("1", "ingest") | ||
assert conn.keys() != [] | ||
|
||
# Cancelling a dataset removes associated data from Redis | ||
dataset.cancel() | ||
assert conn.keys() == [] | ||
|
||
|
||
def test_dataset_mark_done(): | ||
conn = get_fakeredis() | ||
conn.flushdb() | ||
|
||
dataset = Dataset(conn=conn, name="abc") | ||
assert conn.keys() == [] | ||
|
||
task = Task( | ||
task_id="1", | ||
job_id="abc", | ||
delivery_tag="", | ||
operation="ingest", | ||
context={}, | ||
payload={}, | ||
priority=5, | ||
collection_id="abc", | ||
) | ||
|
||
# Enqueueing a task stores status data in Redis | ||
dataset.add_task(task.task_id, task.operation) | ||
dataset.checkout_task(task.task_id, task.operation) | ||
assert conn.keys() != [] | ||
|
||
# Marking the last task as done cleans up status data in Redis | ||
dataset.mark_done(task) | ||
assert conn.keys() == [] | ||
|
||
|
||
@pytest.fixture | ||
def prom_registry(): | ||
# This relies on internal implementation details of the client to reset | ||
|
@@ -325,7 +516,6 @@ def test_get_priority_bucket(): | |
} | ||
], | ||
"start_time": "2024-06-25T10:58:49.779811", | ||
"end_time": None, | ||
"last_update": "2024-06-25T10:58:49.779819", | ||
} | ||
}, | ||
|
@@ -354,7 +544,6 @@ def test_get_priority_bucket(): | |
} | ||
], | ||
"start_time": "2024-06-25T10:58:49.779811", | ||
"end_time": None, | ||
"last_update": "2024-06-25T10:58:49.779819", | ||
} | ||
}, | ||
|
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.
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’m not entirely sure setting the "start_time" timestamp here is necessary in the first place as a worker shouldn’t be able to check out a task without
add_task
having been executed before. (The same probably also applies forpipe.sadd(self.key, self.name)
.)Didn’t change it because the primary goal of this PR is to fix the timestamps and not to refactor, but I’d still be interested to understand if there’s a legit case where this is relevant or if it’s merely as a precaution?
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 seems like some sort of defensive programming approach. Which I can't say I dislike :)