Skip to content

Commit

Permalink
[ServiceBus&EventHubs] remove RetryMode enum (#22369)
Browse files Browse the repository at this point in the history
* remove RetryMode from SB

* remove RetryMode from EH

* annas comments
  • Loading branch information
swathipil authored Jan 7, 2022
1 parent 9a544e9 commit e358eb4
Show file tree
Hide file tree
Showing 19 changed files with 97 additions and 67 deletions.
1 change: 0 additions & 1 deletion sdk/eventhub/azure-eventhub/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ This version and all future versions will require Python 3.7+. Python 2.7 and 3.

- Added support for fixed (linear) retry backoff:
- Sync/async `EventHubProducerClient` and `EventHubConsumerClient` constructors and `from_connection_string` take `retry_mode` as a keyword argument.
- `RetryMode` enum has been added to `azure.eventhub`, with values `FIXED` and `EXPONENTIAL`.

### Breaking Changes

Expand Down
2 changes: 0 additions & 2 deletions sdk/eventhub/azure-eventhub/azure/eventhub/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
parse_connection_string,
EventHubConnectionStringProperties
)
from ._retry import RetryMode

TransportType = constants.TransportType

Expand All @@ -35,5 +34,4 @@
"PartitionContext",
"parse_connection_string",
"EventHubConnectionStringProperties",
"RetryMode"
]
4 changes: 2 additions & 2 deletions sdk/eventhub/azure-eventhub/azure/eventhub/_client_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@
AzureNamedKeyCredential,
)
from azure.core.utils import parse_connection_string as core_parse_connection_string
from azure.core.pipeline.policies import RetryMode


from .exceptions import _handle_exception, ClientClosedError, ConnectError
from ._configuration import Configuration
from ._retry import RetryMode
from ._utils import utc_from_timestamp, parse_sas_credential
from ._connection_manager import get_connection_manager
from ._constants import (
Expand Down Expand Up @@ -164,7 +164,7 @@ def _build_uri(address, entity):


def _get_backoff_time(retry_mode, backoff_factor, backoff_max, retried_times):
if retry_mode == RetryMode.FIXED:
if retry_mode == RetryMode.Fixed:
backoff_value = backoff_factor
else:
backoff_value = backoff_factor * (2 ** retried_times)
Expand Down
4 changes: 2 additions & 2 deletions sdk/eventhub/azure-eventhub/azure/eventhub/_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
from urllib.parse import urlparse

from uamqp.constants import TransportType, DEFAULT_AMQPS_PORT, DEFAULT_AMQP_WSS_PORT
from ._retry import RetryMode
from azure.core.pipeline.policies import RetryMode


class Configuration(object): # pylint:disable=too-many-instance-attributes
def __init__(self, **kwargs):
self.user_agent = kwargs.get("user_agent") # type: Optional[str]
self.retry_total = kwargs.get("retry_total", 3) # type: int
self.max_retries = self.retry_total # type: int
self.retry_mode = kwargs.get("retry_mode", RetryMode.EXPONENTIAL)
self.retry_mode = RetryMode(kwargs.get("retry_mode", "exponential"))
self.backoff_factor = kwargs.get("retry_backoff_factor", 0.8) # type: float
self.backoff_max = kwargs.get("retry_backoff_max", 120) # type: int
self.network_tracing = kwargs.get("network_tracing", False) # type: bool
Expand Down
25 changes: 18 additions & 7 deletions sdk/eventhub/azure-eventhub/azure/eventhub/_consumer_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@

if TYPE_CHECKING:
import datetime
from azure.core.credentials import TokenCredential, AzureSasCredential, AzureNamedKeyCredential
from azure.core.credentials import (
TokenCredential,
AzureSasCredential,
AzureNamedKeyCredential,
)
from typing import ( # pylint: disable=ungrouped-imports
Any,
Union,
Expand Down Expand Up @@ -79,8 +83,9 @@ class EventHubConsumerClient(ClientBase):
seconds. If the backoff_factor is 0.1, then the retry will sleep
for [0.0s, 0.2s, 0.4s, ...] between retries. The default value is 0.8.
:keyword float retry_backoff_max: The maximum back off time. Default value is 120 seconds (2 minutes).
:keyword retry_mode: Fixed or exponential delay between attempts, default is exponential.
:paramtype retry_mode: ~azure.eventhub.RetryMode
:keyword retry_mode: The delay behavior between retry attempts. Supported values are 'fixed' or 'exponential',
where default is 'exponential'.
:paramtype retry_mode: str
:keyword float idle_timeout: Timeout, in seconds, after which this client will close the underlying connection
if there is no further activity. By default the value is None, meaning that the client will not shutdown due to
inactivity unless initiated by the service.
Expand Down Expand Up @@ -151,10 +156,15 @@ def __init__(
"partition_ownership_expiration_interval", None
)
if self._partition_ownership_expiration_interval is None:
self._partition_ownership_expiration_interval = 6 * self._load_balancing_interval
self._partition_ownership_expiration_interval = (
6 * self._load_balancing_interval
)
load_balancing_strategy = kwargs.pop("load_balancing_strategy", None)
self._load_balancing_strategy = LoadBalancingStrategy(load_balancing_strategy) if load_balancing_strategy \
self._load_balancing_strategy = (
LoadBalancingStrategy(load_balancing_strategy)
if load_balancing_strategy
else LoadBalancingStrategy.GREEDY
)
self._consumer_group = consumer_group
network_tracing = kwargs.pop("logging_enable", False)
super(EventHubConsumerClient, self).__init__(
Expand Down Expand Up @@ -235,8 +245,9 @@ def from_connection_string(cls, conn_str, consumer_group, **kwargs):
seconds. If the backoff_factor is 0.1, then the retry will sleep
for [0.0s, 0.2s, 0.4s, ...] between retries. The default value is 0.8.
:keyword float retry_backoff_max: The maximum back off time. Default value is 120 seconds (2 minutes).
:keyword retry_mode: Fixed or exponential delay between attempts, default is exponential.
:paramtype retry_mode: ~azure.eventhub.RetryMode
:keyword retry_mode: The delay behavior between retry attempts. Supported values are 'fixed' or 'exponential',
where default is 'exponential'.
:paramtype retry_mode: str
:keyword float idle_timeout: Timeout, in seconds, after which this client will close the underlying connection
if there is no furthur activity. By default the value is None, meaning that the client will not shutdown due
to inactivity unless initiated by the service.
Expand Down
10 changes: 6 additions & 4 deletions sdk/eventhub/azure-eventhub/azure/eventhub/_producer_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ class EventHubProducerClient(ClientBase):
seconds. If the backoff_factor is 0.1, then the retry will sleep
for [0.0s, 0.2s, 0.4s, ...] between retries. The default value is 0.8.
:keyword float retry_backoff_max: The maximum back off time. Default value is 120 seconds (2 minutes).
:keyword retry_mode: Fixed or exponential delay between attempts, default is exponential.
:paramtype retry_mode: ~azure.eventhub.RetryMode
:keyword retry_mode: The delay behavior between retry attempts. Supported values are 'fixed' or 'exponential',
where default is 'exponential'.
:paramtype retry_mode: str
:keyword float idle_timeout: Timeout, in seconds, after which this client will close the underlying connection
if there is no activity. By default the value is None, meaning that the client will not shutdown due to inactivity
unless initiated by the service.
Expand Down Expand Up @@ -201,8 +202,9 @@ def from_connection_string(cls, conn_str, **kwargs):
seconds. If the backoff_factor is 0.1, then the retry will sleep
for [0.0s, 0.2s, 0.4s, ...] between retries. The default value is 0.8.
:keyword float retry_backoff_max: The maximum back off time. Default value is 120 seconds (2 minutes).
:keyword retry_mode: Fixed or exponential delay between attempts, default is exponential.
:paramtype retry_mode: ~azure.eventhub.RetryMode
:keyword retry_mode: The delay behavior between retry attempts. Supported values are 'fixed' or 'exponential',
where default is 'exponential'.
:paramtype retry_mode: str
:keyword float idle_timeout: Timeout, in seconds, after which this client will close the underlying connection
if there is no activity. By default the value is None, meaning that the client will not shutdown due to
inactivity unless initiated by the service.
Expand Down
9 changes: 0 additions & 9 deletions sdk/eventhub/azure-eventhub/azure/eventhub/_retry.py

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,9 @@ class EventHubConsumerClient(ClientBaseAsync):
seconds. If the backoff_factor is 0.1, then the retry will sleep
for [0.0s, 0.2s, 0.4s, ...] between retries. The default value is 0.8.
:keyword float retry_backoff_max: The maximum back off time. Default value is 120 seconds (2 minutes).
:keyword retry_mode: Fixed or exponential delay between attempts, default is exponential.
:paramtype retry_mode: ~azure.eventhub.RetryMode
:keyword retry_mode: The delay behavior between retry attempts. Supported values are 'fixed' or 'exponential',
where default is 'exponential'.
:paramtype retry_mode: str
:keyword float idle_timeout: Timeout, in seconds, after which this client will close the underlying connection
if there is no further activity. By default the value is None, meaning that the client will not shutdown due to
inactivity unless initiated by the service.
Expand Down Expand Up @@ -255,8 +256,9 @@ def from_connection_string(
seconds. If the backoff_factor is 0.1, then the retry will sleep
for [0.0s, 0.2s, 0.4s, ...] between retries. The default value is 0.8.
:keyword float retry_backoff_max: The maximum back off time. Default value is 120 seconds (2 minutes).
:keyword retry_mode: Fixed or exponential delay between attempts, default is exponential.
:paramtype retry_mode: ~azure.eventhub.RetryMode
:keyword retry_mode: The delay behavior between retry attempts. Supported values are 'fixed' or 'exponential',
where default is 'exponential'.
:paramtype retry_mode: str
:keyword float idle_timeout: Timeout, in seconds, after which this client will close the underlying connection
if there is no further activity. By default the value is None, meaning that the client will not shutdown due
to inactivity unless initiated by the service.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ class EventHubProducerClient(ClientBaseAsync):
seconds. If the backoff_factor is 0.1, then the retry will sleep
for [0.0s, 0.2s, 0.4s, ...] between retries. The default value is 0.8.
:keyword float retry_backoff_max: The maximum back off time. Default value is 120 seconds (2 minutes).
:keyword retry_mode: Fixed or exponential delay between attempts, default is exponential.
:paramtype retry_mode: ~azure.eventhub.RetryMode
:keyword retry_mode: The delay behavior between retry attempts. Supported values are 'fixed' or 'exponential',
where default is 'exponential'.
:paramtype retry_mode: str
:keyword float idle_timeout: Timeout, in seconds, after which this client will close the underlying connection
if there is no activity. By default the value is None, meaning that the client will not shutdown due to inactivity
unless initiated by the service.
Expand Down Expand Up @@ -217,8 +218,9 @@ def from_connection_string(
seconds. If the backoff_factor is 0.1, then the retry will sleep
for [0.0s, 0.2s, 0.4s, ...] between retries. The default value is 0.8.
:keyword float retry_backoff_max: The maximum back off time. Default value is 120 seconds (2 minutes).
:keyword retry_mode: Fixed or exponential delay between attempts, default is exponential.
:paramtype retry_mode: ~azure.eventhub.RetryMode
:keyword retry_mode: The delay behavior between retry attempts. Supported values are 'fixed' or 'exponential',
where default is 'exponential'.
:paramtype retry_mode: str
:keyword float idle_timeout: Timeout, in seconds, after which this client will close the underlying connection
if there is no activity. By default the value is None, meaning that the client will not shutdown due to
inactivity unless initiated by the service.
Expand Down
22 changes: 19 additions & 3 deletions sdk/eventhub/azure-eventhub/tests/unittest/test_client_creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
#--------------------------------------------------------------------------

import time
from azure.eventhub import EventHubProducerClient, EventHubConsumerClient, TransportType, RetryMode
from azure.core.pipeline.policies import RetryMode
from azure.eventhub import EventHubProducerClient, EventHubConsumerClient, TransportType


def test_custom_endpoint():
Expand Down Expand Up @@ -136,13 +137,28 @@ def test_backoff_fixed_retry():
'fake.host.com',
'fake_eh',
None,
retry_mode=RetryMode.FIXED
retry_mode='fixed'
)
backoff = client._config.backoff_factor
start_time = time.time()
client._backoff(retried_times=1, last_exception=Exception('fake'), timeout_time=None)
sleep_time = time.time() - start_time
# exp = 0.8 * (2 ** 1) = 1.6
# time.sleep() in _backoff will take AT LEAST time 'exp' for RetryMode.EXPONENTIAL
# time.sleep() in _backoff will take AT LEAST time 'exp' for 'exponential'
# check that fixed is less than 'exp'
assert sleep_time < backoff * (2 ** 1)

client = EventHubProducerClient(
'fake.host.com',
'fake_eh',
None,
retry_mode=RetryMode.Fixed
)
backoff = client._config.backoff_factor
start_time = time.time()
client._backoff(retried_times=1, last_exception=Exception('fake'), timeout_time=None)
sleep_time = time.time() - start_time
# exp = 0.8 * (2 ** 1) = 1.6
# time.sleep() in _backoff will take AT LEAST time 'exp' for 'exponential'
# check that fixed is less than 'exp'
assert sleep_time < backoff * (2 ** 1)
1 change: 0 additions & 1 deletion sdk/servicebus/azure-servicebus/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ This version and all future versions will require Python 3.7+. Python 2.7 and 3.

- Added support for fixed (linear) retry backoff:
- Sync/async `ServiceBusClient` constructors and `from_connection_string` take `retry_mode` as a keyword argument.
- `RetryMode` enum has been added to `azure.servicebus`, with values `FIXED` and `EXPONENTIAL`.

### Breaking Changes

Expand Down
2 changes: 0 additions & 2 deletions sdk/servicebus/azure-servicebus/azure/servicebus/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
parse_connection_string,
ServiceBusConnectionStringProperties,
)
from ._retry import RetryMode

TransportType = constants.TransportType

Expand All @@ -47,5 +46,4 @@
"AutoLockRenewer",
"parse_connection_string",
"ServiceBusConnectionStringProperties",
"RetryMode",
]
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
from uamqp.message import MessageProperties

from azure.core.credentials import AccessToken, AzureSasCredential, AzureNamedKeyCredential
from azure.core.pipeline.policies import RetryMode

from ._common._configuration import Configuration
from ._retry import RetryMode
from .exceptions import (
ServiceBusError,
ServiceBusConnectionError,
Expand Down Expand Up @@ -154,7 +154,7 @@ def _generate_sas_token(uri, policy, key, expiry=None):
return AccessToken(token=token, expires_on=abs_expiry)

def _get_backoff_time(retry_mode, backoff_factor, backoff_max, retried_times):
if retry_mode == RetryMode.FIXED:
if retry_mode == RetryMode.Fixed:
backoff_value = backoff_factor
else:
backoff_value = backoff_factor * (2 ** retried_times)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
from typing import Optional, Dict, Any

from uamqp.constants import TransportType
from .._retry import RetryMode
from azure.core.pipeline.policies import RetryMode


class Configuration(object): # pylint:disable=too-many-instance-attributes
def __init__(self, **kwargs):
self.user_agent = kwargs.get("user_agent") # type: Optional[str]
self.retry_total = kwargs.get("retry_total", 3) # type: int
self.retry_mode = kwargs.get("retry_mode", RetryMode.EXPONENTIAL)
self.retry_mode = RetryMode(kwargs.get("retry_mode", 'exponential'))
self.retry_backoff_factor = kwargs.get(
"retry_backoff_factor", 0.8
) # type: float
Expand Down
9 changes: 0 additions & 9 deletions sdk/servicebus/azure-servicebus/azure/servicebus/_retry.py

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ class ServiceBusClient(object):
:keyword float retry_backoff_factor: Delta back-off internal in the unit of second between retries.
Default value is 0.8.
:keyword float retry_backoff_max: Maximum back-off interval in the unit of second. Default value is 120.
:keyword retry_mode: Fixed or exponential delay between attempts, default is exponential.
:paramtype retry_mode: ~azure.servicebus.RetryMode
:keyword retry_mode: The delay behavior between retry attempts. Supported values are 'fixed' or 'exponential',
where default is 'exponential'.
:paramtype retry_mode: str
.. admonition:: Example:
Expand Down Expand Up @@ -154,8 +155,9 @@ def from_connection_string(cls, conn_str, **kwargs):
:keyword float retry_backoff_factor: Delta back-off internal in the unit of second between retries.
Default value is 0.8.
:keyword float retry_backoff_max: Maximum back-off interval in the unit of second. Default value is 120.
:keyword retry_mode: Fixed or exponential delay between attempts, default is exponential.
:paramtype retry_mode: ~azure.servicebus.RetryMode
:keyword retry_mode: The delay behavior between retry attempts. Supported values are 'fixed' or 'exponential',
where default is 'exponential'.
:paramtype retry_mode: str
:rtype: ~azure.servicebus.ServiceBusClient
.. admonition:: Example:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ class ServiceBusClient(object):
:keyword float retry_backoff_factor: Delta back-off internal in the unit of second between retries.
Default value is 0.8.
:keyword float retry_backoff_max: Maximum back-off interval in the unit of second. Default value is 120.
:keyword retry_mode: Fixed or exponential delay between attempts, default is exponential.
:paramtype retry_mode: ~azure.eventhub.RetryMode
:keyword retry_mode: The delay behavior between retry attempts. Supported values are 'fixed' or 'exponential',
where default is 'exponential'.
:paramtype retry_mode: str
.. admonition:: Example:
Expand Down Expand Up @@ -131,8 +132,9 @@ def from_connection_string(cls, conn_str: str, **kwargs: Any) -> "ServiceBusClie
:keyword float retry_backoff_factor: Delta back-off internal in the unit of second between retries.
Default value is 0.8.
:keyword float retry_backoff_max: Maximum back-off interval in the unit of second. Default value is 120.
:keyword retry_mode: Fixed or exponential delay between attempts, default is exponential.
:paramtype retry_mode: ~azure.eventhub.RetryMode
:keyword retry_mode: The delay behavior between retry attempts. Supported values are 'fixed' or 'exponential',
where default is 'exponential'.
:paramtype retry_mode: str
:rtype: ~azure.servicebus.aio.ServiceBusClient
.. admonition:: Example:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ async def test_backoff_fixed_retry(self):
client = ServiceBusClient(
'fake.host.com',
'fake_eh',
retry_mode=RetryMode.FIXED
retry_mode='fixed'
)
# queue sender
sender = await client.get_queue_sender('fake_name')
Expand All @@ -416,7 +416,7 @@ async def test_backoff_fixed_retry(self):
sender._backoff(retried_times=1, last_exception=Exception('fake'), abs_timeout_time=None)
sleep_time_fixed = time.time() - start_time
# exp = 0.8 * (2 ** 1) = 1.6
# time.sleep() in _backoff will take AT LEAST time 'exp' for RetryMode.EXPONENTIAL
# time.sleep() in _backoff will take AT LEAST time 'exp' for retry_mode='exponential'
# check that fixed is less than 'exp'
assert sleep_time_fixed < backoff * (2 ** 1)

Expand Down
Loading

0 comments on commit e358eb4

Please sign in to comment.