-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
146 additions
and
64 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -0,0 +1,106 @@ | ||
# ByteDance Volcengine EMR, Copyright 2024. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""The module contains utility functions for the tosfs stability.""" | ||
|
||
import time | ||
from typing import Any, Optional | ||
|
||
import requests | ||
from requests.exceptions import ( | ||
ConnectTimeout, | ||
HTTPError, | ||
ProxyError, | ||
ReadTimeout, | ||
RetryError, | ||
SSLError, | ||
StreamConsumedError, | ||
Timeout, | ||
) | ||
from tos.exceptions import TosClientError, TosError, TosServerError | ||
|
||
from tosfs.exceptions import TosfsError | ||
|
||
TOS_SERVER_RETRYABLE_CODES = { | ||
"404", # NOT_FOUND | ||
"409", # CONFLICT | ||
"429", # TOO_MANY_REQUESTS | ||
"500", # INTERNAL_SERVER_ERROR | ||
} | ||
|
||
TOS_CLIENT_RETRYABLE_EXCEPTIONS = { | ||
HTTPError, | ||
requests.ConnectionError, | ||
ProxyError, | ||
SSLError, | ||
Timeout, | ||
ConnectTimeout, | ||
ReadTimeout, | ||
StreamConsumedError, | ||
RetryError, | ||
InterruptedError, | ||
} | ||
|
||
MAX_RETRY_NUM = 20 | ||
|
||
|
||
def retryable_func( | ||
func: Any, | ||
*, | ||
args: tuple[()] = (), | ||
kwargs: Optional[Any] = None, | ||
max_retry_num: int = MAX_RETRY_NUM, | ||
) -> Any: | ||
"""Retry a function in case of catch errors.""" | ||
if kwargs is None: | ||
kwargs = {} | ||
|
||
attempt = 0 | ||
|
||
while attempt < max_retry_num: | ||
attempt += 1 | ||
try: | ||
return func(*args, **kwargs) | ||
except TosError as e: | ||
from tosfs.core import logger | ||
|
||
if attempt >= max_retry_num: | ||
logger.error("Retry exhausted after %d times.", max_retry_num) | ||
raise e | ||
|
||
if is_retryable_exception(e): | ||
logger.warn("Retry TOS request in the %d times, error: %s", attempt, e) | ||
try: | ||
time.sleep(min(1.7**attempt * 0.1, 15)) | ||
except InterruptedError as ie: | ||
raise TosfsError(f"Request {func} interrupted.") from ie | ||
else: | ||
raise e | ||
except Exception as e: | ||
raise TosfsError(f"{e}") from e | ||
|
||
|
||
def is_retryable_exception(e: TosError) -> bool: | ||
"""Check if the exception is retryable.""" | ||
return _is_retryable_tos_server_exception(e) or _is_retryable_tos_client_exception( | ||
e | ||
) | ||
|
||
|
||
def _is_retryable_tos_server_exception(e: TosError) -> bool: | ||
return isinstance(e, TosServerError) and e.code in TOS_SERVER_RETRYABLE_CODES | ||
|
||
|
||
def _is_retryable_tos_client_exception(e: TosError) -> bool: | ||
return isinstance(e, TosClientError) and e.cause in TOS_CLIENT_RETRYABLE_EXCEPTIONS |
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