|
| 1 | +import logging |
| 2 | + |
| 3 | +from aiohttp import ClientError, ClientSession |
| 4 | +from models_library.api_schemas_storage import ( |
| 5 | + ETag, |
| 6 | + FileUploadCompleteFutureResponse, |
| 7 | + FileUploadCompleteResponse, |
| 8 | + FileUploadCompleteState, |
| 9 | + FileUploadCompletionBody, |
| 10 | + LocationID, |
| 11 | + LocationName, |
| 12 | + UploadedPart, |
| 13 | +) |
| 14 | +from models_library.generics import Envelope |
| 15 | +from models_library.users import UserID |
| 16 | +from models_library.utils.fastapi_encoders import jsonable_encoder |
| 17 | +from pydantic import AnyUrl, parse_obj_as |
| 18 | +from tenacity._asyncio import AsyncRetrying |
| 19 | +from tenacity.before_sleep import before_sleep_log |
| 20 | +from tenacity.retry import retry_if_exception_type |
| 21 | +from tenacity.stop import stop_after_delay |
| 22 | +from tenacity.wait import wait_fixed |
| 23 | + |
| 24 | +from . import exceptions, storage_client |
| 25 | +from .settings import NodePortsSettings |
| 26 | + |
| 27 | +_logger = logging.getLogger(__name__) |
| 28 | + |
| 29 | + |
| 30 | +async def _get_location_id_from_location_name( |
| 31 | + user_id: UserID, |
| 32 | + store: LocationName, |
| 33 | + session: ClientSession, |
| 34 | +) -> LocationID: |
| 35 | + resp = await storage_client.get_storage_locations(session=session, user_id=user_id) |
| 36 | + for location in resp: |
| 37 | + if location.name == store: |
| 38 | + return location.id |
| 39 | + # location id not found |
| 40 | + raise exceptions.S3InvalidStore(store) |
| 41 | + |
| 42 | + |
| 43 | +async def _complete_upload( |
| 44 | + session: ClientSession, |
| 45 | + upload_completion_link: AnyUrl, |
| 46 | + parts: list[UploadedPart], |
| 47 | + *, |
| 48 | + is_directory: bool, |
| 49 | +) -> ETag | None: |
| 50 | + """completes a potentially multipart upload in AWS |
| 51 | + NOTE: it can take several minutes to finish, see [AWS documentation](https://docs.aws.amazon.com/AmazonS3/latest/API/API_CompleteMultipartUpload.html) |
| 52 | + it can take several minutes |
| 53 | + :raises ValueError: _description_ |
| 54 | + :raises exceptions.S3TransferError: _description_ |
| 55 | + :rtype: ETag |
| 56 | + """ |
| 57 | + async with session.post( |
| 58 | + upload_completion_link, |
| 59 | + json=jsonable_encoder(FileUploadCompletionBody(parts=parts)), |
| 60 | + ) as resp: |
| 61 | + resp.raise_for_status() |
| 62 | + # now poll for state |
| 63 | + file_upload_complete_response = parse_obj_as( |
| 64 | + Envelope[FileUploadCompleteResponse], await resp.json() |
| 65 | + ) |
| 66 | + assert file_upload_complete_response.data # nosec |
| 67 | + state_url = file_upload_complete_response.data.links.state |
| 68 | + _logger.info( |
| 69 | + "completed upload of %s", |
| 70 | + f"{len(parts)} parts, received {file_upload_complete_response.json(indent=2)}", |
| 71 | + ) |
| 72 | + |
| 73 | + async for attempt in AsyncRetrying( |
| 74 | + reraise=True, |
| 75 | + wait=wait_fixed(1), |
| 76 | + stop=stop_after_delay( |
| 77 | + NodePortsSettings.create_from_envs().NODE_PORTS_MULTIPART_UPLOAD_COMPLETION_TIMEOUT_S |
| 78 | + ), |
| 79 | + retry=retry_if_exception_type(ValueError), |
| 80 | + before_sleep=before_sleep_log(_logger, logging.DEBUG), |
| 81 | + ): |
| 82 | + with attempt: |
| 83 | + async with session.post(state_url) as resp: |
| 84 | + resp.raise_for_status() |
| 85 | + future_enveloped = parse_obj_as( |
| 86 | + Envelope[FileUploadCompleteFutureResponse], await resp.json() |
| 87 | + ) |
| 88 | + assert future_enveloped.data # nosec |
| 89 | + if future_enveloped.data.state == FileUploadCompleteState.NOK: |
| 90 | + msg = "upload not ready yet" |
| 91 | + raise ValueError(msg) |
| 92 | + if is_directory: |
| 93 | + assert future_enveloped.data.e_tag is None # nosec |
| 94 | + return None |
| 95 | + |
| 96 | + assert future_enveloped.data.e_tag # nosec |
| 97 | + _logger.debug( |
| 98 | + "multipart upload completed in %s, received %s", |
| 99 | + attempt.retry_state.retry_object.statistics, |
| 100 | + f"{future_enveloped.data.e_tag=}", |
| 101 | + ) |
| 102 | + return future_enveloped.data.e_tag |
| 103 | + msg = f"Could not complete the upload using the upload_completion_link={upload_completion_link!r}" |
| 104 | + raise exceptions.S3TransferError(msg) |
| 105 | + |
| 106 | + |
| 107 | +async def _resolve_location_id( |
| 108 | + client_session: ClientSession, |
| 109 | + user_id: UserID, |
| 110 | + store_name: LocationName | None, |
| 111 | + store_id: LocationID | None, |
| 112 | +) -> LocationID: |
| 113 | + if store_name is None and store_id is None: |
| 114 | + msg = f"both {store_name=} and {store_id=} are None" |
| 115 | + raise exceptions.NodeportsException(msg) |
| 116 | + |
| 117 | + if store_name is not None: |
| 118 | + store_id = await _get_location_id_from_location_name( |
| 119 | + user_id, store_name, client_session |
| 120 | + ) |
| 121 | + assert store_id is not None # nosec |
| 122 | + return store_id |
| 123 | + |
| 124 | + |
| 125 | +async def _abort_upload( |
| 126 | + session: ClientSession, abort_upload_link: AnyUrl, *, reraise_exceptions: bool |
| 127 | +) -> None: |
| 128 | + # abort the upload correctly, so it can revert back to last version |
| 129 | + try: |
| 130 | + async with session.post(abort_upload_link) as resp: |
| 131 | + resp.raise_for_status() |
| 132 | + except ClientError: |
| 133 | + _logger.warning("Error while aborting upload", exc_info=True) |
| 134 | + if reraise_exceptions: |
| 135 | + raise |
| 136 | + _logger.warning("Upload aborted") |
0 commit comments