Skip to content
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

Retry storage operations in case of ClientError. #1107

Merged
merged 5 commits into from
Oct 8, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.D/1107.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Retry storage operations in case of some errors.
50 changes: 34 additions & 16 deletions neuromation/api/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
normalize_storage_path_uri,
)
from .users import Action
from .utils import NoPublicConstructor
from .utils import NoPublicConstructor, retries


MAX_OPEN_FILES = 100
Expand Down Expand Up @@ -384,9 +384,12 @@ async def _upload_file(
progress: AbstractFileProgress,
queue: "asyncio.Queue[ProgressQueueItem]",
) -> None:
await self.create(
dst, self._iterate_file(src_path, dst, progress=progress, queue=queue)
)
for retry in retries(f"Fail to upload {dst}"):
async with retry:
await self.create(
dst,
self._iterate_file(src_path, dst, progress=progress, queue=queue),
)

async def upload_dir(
self,
Expand Down Expand Up @@ -428,14 +431,20 @@ async def _upload_dir(
exists = False
if update:
try:
dst_files = {
item.name: item for item in await self.ls(dst) if item.is_file()
}
for retry in retries(f"Fail to list {dst}"):
async with retry:
dst_files = {
item.name: item
for item in await self.ls(dst)
if item.is_file()
}
exists = True
except ResourceNotFound:
update = False
if not exists:
await self.mkdir(dst, exist_ok=True)
for retry in retries(f"Fail to create {dst}"):
async with retry:
await self.mkdir(dst, exist_ok=True)
except (FileExistsError, neuromation.api.IllegalArgumentError):
raise NotADirectoryError(errno.ENOTDIR, "Not a directory", str(dst))
await queue.put((progress.enter, StorageProgressEnterDir(src, dst)))
Expand Down Expand Up @@ -532,13 +541,18 @@ async def _download_file(
async with self._file_sem:
with dst_path.open("wb") as stream:
await queue.put((progress.start, StorageProgressStart(src, dst, size)))
pos = 0
async for chunk in self.open(src):
pos += len(chunk)
await queue.put(
(progress.step, StorageProgressStep(src, dst, pos, size))
)
await loop.run_in_executor(None, stream.write, chunk)
for retry in retries(f"Fail to download {src}"):
async with retry:
pos = 0
async for chunk in self.open(src):
pos += len(chunk)
await queue.put(
(
progress.step,
StorageProgressStep(src, dst, pos, size),
)
)
await loop.run_in_executor(None, stream.write, chunk)
await queue.put(
(progress.complete, StorageProgressComplete(src, dst, size))
)
Expand Down Expand Up @@ -586,7 +600,11 @@ async def _download_dir(
item.name: item for item in dst_path.iterdir() if item.is_file()
},
)
folder = await self.ls(src)

for retry in retries(f"Fail to list {src}"):
async with retry:
folder = await self.ls(src)

for child in folder:
name = child.name
if child.is_file():
Expand Down
35 changes: 35 additions & 0 deletions neuromation/api/utils.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import asyncio
import logging
import sys
from types import TracebackType
from typing import (
Any,
AsyncIterator,
Awaitable,
Callable,
Coroutine,
Generator,
Generic,
Iterator,
Optional,
Type,
TypeVar,
)

import aiohttp


if sys.version_info >= (3, 7): # pragma: no cover
from contextlib import asynccontextmanager # noqa
Expand Down Expand Up @@ -73,3 +80,31 @@ async def __aexit__(
# Need to teach mypy about this facility
await self._ret.close() # type: ignore
return None


log = logging.getLogger(__name__)


def retries(
msg: str, attempts: int = 10, logger: Callable[[str], None] = log.info
) -> Iterator[AsyncContextManager[None]]:
sleeptime = 0.0
while attempts:

@asynccontextmanager
async def retry() -> AsyncIterator[None]:
nonlocal attempts
if attempts:
try:
yield
except aiohttp.ClientError as err:
logger(f"{msg}: {err}. Retry...")
await asyncio.sleep(sleeptime)
else:
attempts = 0
else:
yield

sleeptime += 0.1
attempts -= 1
yield retry()