-
Notifications
You must be signed in to change notification settings - Fork 107
Retry policy support for v2 programming model #1268
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
15 commits
Select commit
Hold shift + click to select a range
b389091
Retry policies
ab9b715
Merge remote-tracking branch 'origin/dev' into gaaguiar/retry_context
50e391c
Additional changes for retry policy
3c891d0
Added retry policy support for v2 function
1431b59
Minor updates and added e2e tests
92efc8c
Merge remote-tracking branch 'origin/dev' into gaaguiar/retry_context
fe648ee
Reverted change to worker config
dadbc77
Added e2e tests
8c04842
Merge branch 'dev' into gaaguiar/retry_context
01af6a7
Updated according to new libary
659e76c
Added tests
9866fca
Fixed existing tests
4f167d6
Added unit tests
30bd2e7
Updated time format
e3821f6
Updated time format
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,28 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
|
||
from dataclasses import dataclass | ||
from enum import Enum | ||
|
||
from . import rpcexception | ||
|
||
|
||
class RetryPolicy(Enum): | ||
"""Retry policy for the function invocation""" | ||
|
||
MAX_RETRY_COUNT = "max_retry_count" | ||
STRATEGY = "strategy" | ||
DELAY_INTERVAL = "delay_interval" | ||
MINIMUM_INTERVAL = "minimum_interval" | ||
MAXIMUM_INTERVAL = "maximum_interval" | ||
|
||
|
||
@dataclass | ||
class RetryContext: | ||
"""Check https://docs.microsoft.com/en-us/azure/azure-functions/ | ||
functions-bindings-error-pages?tabs=python#retry-policies-preview""" | ||
|
||
def __init__(self, | ||
retry_count: int, | ||
max_retry_count: int, | ||
rpc_exception: rpcexception.RpcException) -> None: | ||
self.__retry_count = retry_count | ||
self.__max_retry_count = max_retry_count | ||
self.__rpc_exception = rpc_exception | ||
|
||
@property | ||
def retry_count(self) -> int: | ||
"""Gets the current retry count from retry-context""" | ||
return self.__retry_count | ||
|
||
@property | ||
def max_retry_count(self) -> int: | ||
"""Gets the max retry count from retry-context""" | ||
return self.__max_retry_count | ||
|
||
@property | ||
def exception(self) -> rpcexception.RpcException: | ||
return self.__rpc_exception | ||
"""Gets the current retry count from retry-context""" | ||
retry_count: int | ||
|
||
"""Gets the max retry count from retry-context""" | ||
max_retry_count: int | ||
|
||
rpc_exception: rpcexception.RpcException |
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 |
---|---|---|
|
@@ -8,12 +8,17 @@ | |
import os.path | ||
import pathlib | ||
import sys | ||
import time | ||
from datetime import timedelta | ||
from os import PathLike, fspath | ||
from typing import Optional, Dict | ||
|
||
from google.protobuf.duration_pb2 import Duration | ||
|
||
from . import protos, functions | ||
from .bindings.retrycontext import RetryPolicy | ||
from .constants import MODULE_NOT_FOUND_TS_URL, SCRIPT_FILE_NAME, \ | ||
PYTHON_LANGUAGE_RUNTIME | ||
PYTHON_LANGUAGE_RUNTIME, RETRY_POLICY | ||
from .utils.wrappers import attach_message_to_exception | ||
|
||
_AZURE_NAMESPACE = '__app__' | ||
|
@@ -45,6 +50,12 @@ def install() -> None: | |
sys.modules[_AZURE_NAMESPACE] = ns_pkg | ||
|
||
|
||
def convert_to_seconds(timestr: str): | ||
x = time.strptime(timestr, '%H:%M:%S') | ||
return int(timedelta(hours=x.tm_hour, minutes=x.tm_min, | ||
seconds=x.tm_sec).total_seconds()) | ||
|
||
|
||
def uninstall() -> None: | ||
pass | ||
|
||
|
@@ -60,6 +71,39 @@ def build_binding_protos(indexed_function) -> Dict: | |
return binding_protos | ||
|
||
|
||
def build_retry_protos(indexed_function) -> Dict: | ||
vrdmr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
retry = indexed_function.get_settings_dict(RETRY_POLICY) | ||
if not retry: | ||
return None | ||
|
||
strategy = retry.get(RetryPolicy.STRATEGY.value) | ||
if strategy == "fixed_delay": | ||
delay_interval = Duration( | ||
seconds=convert_to_seconds( | ||
retry.get(RetryPolicy.DELAY_INTERVAL.value))) | ||
retry_protos = protos.RpcRetryOptions( | ||
max_retry_count=int(retry.get(RetryPolicy.MAX_RETRY_COUNT.value)), | ||
retry_strategy=retry.get(RetryPolicy.STRATEGY.value), | ||
delay_interval=delay_interval, | ||
) | ||
else: | ||
minimum_interval = Duration( | ||
seconds=convert_to_seconds( | ||
retry.get(RetryPolicy.MINIMUM_INTERVAL.value))) | ||
maximum_interval = Duration( | ||
seconds=convert_to_seconds( | ||
retry.get(RetryPolicy.MAXIMUM_INTERVAL.value))) | ||
|
||
retry_protos = protos.RpcRetryOptions( | ||
max_retry_count=int(retry.get(RetryPolicy.MAX_RETRY_COUNT.value)), | ||
retry_strategy=retry.get(RetryPolicy.STRATEGY.value), | ||
minimum_interval=minimum_interval, | ||
maximum_interval=maximum_interval | ||
) | ||
|
||
return retry_protos | ||
|
||
|
||
def process_indexed_function(functions_registry: functions.Registry, | ||
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. Do we have a test which validates the json being generated? |
||
indexed_functions): | ||
fx_metadata_results = [] | ||
|
@@ -68,6 +112,7 @@ def process_indexed_function(functions_registry: functions.Registry, | |
function=indexed_function) | ||
|
||
binding_protos = build_binding_protos(indexed_function) | ||
retry_protos = build_retry_protos(indexed_function) | ||
|
||
function_metadata = protos.RpcFunctionMetadata( | ||
name=function_info.name, | ||
|
@@ -80,6 +125,7 @@ def process_indexed_function(functions_registry: functions.Registry, | |
language=PYTHON_LANGUAGE_RUNTIME, | ||
bindings=binding_protos, | ||
raw_bindings=indexed_function.get_raw_bindings(), | ||
retry_options=retry_protos, | ||
properties={"worker_indexed": "True"}) | ||
|
||
fx_metadata_results.append(function_metadata) | ||
|
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
22 changes: 22 additions & 0 deletions
22
tests/endtoend/retry_policy_functions/exponential_strategy/function_app.py
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,22 @@ | ||
from azure.functions import FunctionApp, TimerRequest, Context, AuthLevel | ||
import logging | ||
|
||
app = FunctionApp(http_auth_level=AuthLevel.ANONYMOUS) | ||
|
||
|
||
@app.timer_trigger(schedule="*/1 * * * * *", arg_name="mytimer", | ||
run_on_startup=False, | ||
use_monitor=False) | ||
@app.retry(strategy="exponential_backoff", max_retry_count="3", | ||
minimum_interval="00:00:01", | ||
maximum_interval="00:00:02") | ||
def mytimer(mytimer: TimerRequest, context: Context) -> None: | ||
logging.info(f'Current retry count: {context.retry_context.retry_count}') | ||
|
||
if context.retry_context.retry_count == \ | ||
context.retry_context.max_retry_count: | ||
logging.info( | ||
f"Max retries of {context.retry_context.max_retry_count} for " | ||
f"function {context.function_name} has been reached") | ||
else: | ||
raise Exception("This is a retryable exception") |
21 changes: 21 additions & 0 deletions
21
tests/endtoend/retry_policy_functions/fixed_strategy/function_app.py
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,21 @@ | ||
from azure.functions import FunctionApp, TimerRequest, Context, AuthLevel | ||
import logging | ||
|
||
app = FunctionApp(http_auth_level=AuthLevel.ANONYMOUS) | ||
|
||
|
||
@app.timer_trigger(schedule="*/1 * * * * *", arg_name="mytimer", | ||
run_on_startup=False, | ||
use_monitor=False) | ||
@app.retry(strategy="fixed_delay", max_retry_count="3", | ||
delay_interval="00:00:01") | ||
def mytimer(mytimer: TimerRequest, context: Context) -> None: | ||
logging.info(f'Current retry count: {context.retry_context.retry_count}') | ||
|
||
if context.retry_context.retry_count == \ | ||
context.retry_context.max_retry_count: | ||
logging.info( | ||
f"Max retries of {context.retry_context.max_retry_count} for " | ||
f"function {context.function_name} has been reached") | ||
else: | ||
raise Exception("This is a retryable exception") |
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,48 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
import time | ||
import typing | ||
|
||
from tests.utils import testutils | ||
|
||
|
||
class TestFixedRetryPolicyFunctions(testutils.WebHostTestCase): | ||
|
||
@classmethod | ||
def get_script_dir(cls): | ||
return testutils.E2E_TESTS_FOLDER / 'retry_policy_functions' / \ | ||
'fixed_strategy' | ||
|
||
def test_fixed_retry_policy(self): | ||
# Checking webhost status. | ||
time.sleep(5) | ||
r = self.webhost.request('GET', '', no_prefix=True) | ||
self.assertTrue(r.ok) | ||
|
||
def check_log_fixed_retry_policy(self, host_out: typing.List[str]): | ||
self.assertIn('Current retry count: 0', host_out) | ||
self.assertIn('Current retry count: 1', host_out) | ||
self.assertIn("Max retries of 3 for function mytimer" | ||
" has been reached", host_out) | ||
|
||
|
||
class TestExponentialRetryPolicyFunctions(testutils.WebHostTestCase): | ||
|
||
@classmethod | ||
def get_script_dir(cls): | ||
return testutils.E2E_TESTS_FOLDER / 'retry_policy_functions' / \ | ||
'exponential_strategy' | ||
|
||
def test_retry_policy(self): | ||
# Checking webhost status. | ||
r = self.webhost.request('GET', '', no_prefix=True, | ||
timeout=5) | ||
time.sleep(5) | ||
self.assertTrue(r.ok) | ||
|
||
def check_log_retry_policy(self, host_out: typing.List[str]): | ||
self.assertIn('Current retry count: 1', host_out) | ||
self.assertIn('Current retry count: 2', host_out) | ||
self.assertIn('Current retry count: 3', host_out) | ||
self.assertIn("Max retries of 3 for function mytimer" | ||
" has been reached", host_out) |
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.