Repository navigation
Default RetryPolicy max_retry_interval and prevent capped backoff overflow #370
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
Open
andystaples
wants to merge
5
commits into
main
Choose a base branch
from
andystaples-retry-policy-default-max-interval
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+197
−5
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
31c8f81
Default RetryPolicy max_retry_interval and prevent capped backoff ove…
andystaples 360adbf
Merge branch 'main' into andystaples-retry-policy-default-max-interval
andystaples 03f9ea8
Narrow retry default replay warnings
andystaples 0a6efc6
Merge remote-tracking branch 'origin/andystaples-retry-policy-default…
andystaples f1e4322
Test uncapped retry replay with explicit max_retry_interval=None
andystaples 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
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
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 |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
|
|
||
| import logging | ||
| from collections.abc import Generator | ||
| from datetime import datetime, timedelta | ||
| from typing import Any | ||
|
|
||
| import pytest | ||
|
|
||
| from durabletask import task, worker | ||
| from durabletask.internal import helpers | ||
| from durabletask.internal import orchestrator_service_pb2 as pb | ||
| from durabletask.serialization import JsonDataConverter | ||
|
|
||
| START = datetime(2020, 1, 1) | ||
|
|
||
|
|
||
| def _delay_at_attempt(policy: task.RetryPolicy, attempt: int) -> timedelta | None: | ||
| retry = task.RetryableTask[Any](policy, pb.OrchestratorAction(), START, is_sub_orch=False) | ||
| for _ in range(attempt - 1): | ||
| retry.increment_attempt_count() | ||
| return retry.compute_next_delay(START) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize(("first", "expected_cap"), [ | ||
| (timedelta(seconds=1), timedelta(hours=1)), | ||
| (timedelta(hours=6), timedelta(hours=6)), | ||
| ]) | ||
| def test_omitted_max_retry_interval_defaults_to_one_hour_or_first_interval( | ||
| first: timedelta, expected_cap: timedelta, | ||
| ) -> None: | ||
| policy = task.RetryPolicy(first_retry_interval=first, max_number_of_attempts=30, backoff_coefficient=2) | ||
|
|
||
| assert policy.max_retry_interval == expected_cap | ||
| assert _delay_at_attempt(policy, 1) == first | ||
| assert _delay_at_attempt(policy, 20) == expected_cap | ||
|
|
||
|
|
||
| def test_explicit_none_max_retry_interval_is_uncapped() -> None: | ||
| policy = task.RetryPolicy( | ||
| first_retry_interval=timedelta(seconds=1), | ||
| max_number_of_attempts=30, | ||
| backoff_coefficient=2, | ||
| max_retry_interval=None, | ||
| ) | ||
|
|
||
| assert policy.max_retry_interval is None | ||
| assert _delay_at_attempt(policy, 20) == timedelta(seconds=2 ** 19) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize(("coefficient", "attempt"), [(2.0, 1100), (1e200, 3)]) | ||
| @pytest.mark.parametrize("cap", [timedelta(milliseconds=1), timedelta(0)]) | ||
| def test_capped_backoff_does_not_overflow(coefficient: float, attempt: int, cap: timedelta) -> None: | ||
| policy = task.RetryPolicy( | ||
| first_retry_interval=timedelta(milliseconds=1), | ||
| max_number_of_attempts=attempt + 1, | ||
| backoff_coefficient=coefficient, | ||
| max_retry_interval=cap, | ||
| ) | ||
|
|
||
| assert _delay_at_attempt(policy, attempt) == cap | ||
|
|
||
|
|
||
| def test_explicit_none_replays_recorded_uncapped_split_retry_timers() -> None: | ||
| """Histories recorded with the previous uncapped default replay unchanged with ``None``.""" | ||
| hour = timedelta(hours=1) | ||
|
|
||
| def orchestrator(ctx: task.OrchestrationContext, _: Any) -> Generator[task.Task[Any], Any, Any]: | ||
| return (yield ctx.call_activity("flaky", retry_policy=task.RetryPolicy( | ||
| first_retry_interval=hour, | ||
| max_number_of_attempts=3, | ||
| backoff_coefficient=100, | ||
| max_retry_interval=None, | ||
| ))) | ||
|
|
||
| registry = worker._Registry() # pyright: ignore[reportPrivateUsage] | ||
| name = registry.add_orchestrator(orchestrator) | ||
| retried_at = START + hour | ||
| # The second retry waited 100 hours uncapped, split into 72-hour and 28-hour timers. | ||
| history = [ | ||
| helpers.new_orchestrator_started_event(START), | ||
| helpers.new_execution_started_event(name, "instance", encoded_input=None), | ||
| helpers.new_task_scheduled_event(1, "flaky"), | ||
| helpers.new_task_failed_event(1, ValueError("transient")), | ||
| helpers.new_timer_created_event(2, retried_at), | ||
| helpers.new_orchestrator_started_event(retried_at), | ||
| helpers.new_timer_fired_event(2, retried_at), | ||
| helpers.new_task_scheduled_event(1, "flaky"), | ||
| helpers.new_task_failed_event(1, ValueError("transient")), | ||
| helpers.new_timer_created_event(3, retried_at + 72 * hour), | ||
| ] | ||
|
|
||
| def execute(new_events: list[pb.HistoryEvent]) -> list[pb.OrchestratorAction]: | ||
| executor = worker._OrchestrationExecutor( # pyright: ignore[reportPrivateUsage] | ||
| registry, logging.getLogger(__name__), JsonDataConverter()) | ||
| return list(executor.execute("instance", history, new_events).actions) | ||
|
|
||
| first_part_fired = [ | ||
| helpers.new_orchestrator_started_event(retried_at + 72 * hour), | ||
| helpers.new_timer_fired_event(3, retried_at + 72 * hour), | ||
| ] | ||
| actions = execute(first_part_fired) | ||
| assert [a.WhichOneof("orchestratorActionType") for a in actions] == ["createTimer"] | ||
| assert actions[0].id == 4 | ||
| assert actions[0].createTimer.fireAt.ToDatetime() == retried_at + 100 * hour | ||
|
|
||
| history += first_part_fired + [helpers.new_timer_created_event(4, retried_at + 100 * hour)] | ||
| actions = execute([ | ||
| helpers.new_orchestrator_started_event(retried_at + 100 * hour), | ||
| helpers.new_timer_fired_event(4, retried_at + 100 * hour), | ||
| ]) | ||
| assert [(a.id, a.WhichOneof("orchestratorActionType")) for a in actions] == [(1, "scheduleTask")] | ||
|
|
||
|
|
||
| def test_zero_first_interval_with_overflowing_backoff_is_zero() -> None: | ||
| policy = task.RetryPolicy( | ||
| first_retry_interval=timedelta(0), | ||
| max_number_of_attempts=5, | ||
| backoff_coefficient=1e200, | ||
| max_retry_interval=None, | ||
| ) | ||
|
|
||
| assert _delay_at_attempt(policy, 3) == timedelta(0) |
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.