Skip to content

Commit b39ecf0

Browse files
committed
@sanderegg use log_context
1 parent c5718ee commit b39ecf0

File tree

3 files changed

+115
-91
lines changed

3 files changed

+115
-91
lines changed

services/api-server/src/simcore_service_api_server/api/routes/files.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from models_library.projects_nodes_io import NodeID
1818
from pydantic import PositiveInt, ValidationError
1919
from servicelib.fastapi.requests_decorators import cancel_on_disconnect
20+
from servicelib.logging_utils import log_context
2021
from simcore_sdk.node_ports_common.constants import SIMCORE_LOCATION
2122
from simcore_sdk.node_ports_common.file_io_utils import UploadableFileObject
2223
from simcore_sdk.node_ports_common.filemanager import (
@@ -302,9 +303,12 @@ async def get_upload_links(
302303
webserver_api=webserver_api, file_id=None, client_file=client_file
303304
)
304305

305-
upload_links = await storage_client.get_file_upload_links(
306-
user_id=user_id, file=file_meta, client_file=client_file
307-
)
306+
with log_context(
307+
logger=_logger, level=logging.DEBUG, msg=f"Getting upload links for {file_meta}"
308+
):
309+
upload_links = await storage_client.get_file_upload_links(
310+
user_id=user_id, file=file_meta, client_file=client_file
311+
)
308312

309313
completion_url: URL = request.url_for(
310314
"complete_multipart_upload", file_id=file_meta.id

services/storage/src/simcore_service_storage/api/rest/_files.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from models_library.projects_nodes_io import LocationID, StorageFileID
2121
from pydantic import AnyUrl, ByteSize, TypeAdapter
2222
from servicelib.aiohttp import status
23+
from servicelib.logging_utils import log_context
2324
from yarl import URL
2425

2526
from ...dsm import get_dsm_provider
@@ -185,17 +186,17 @@ async def upload_file(
185186
"""
186187
# NOTE: Used by legacy dynamic services with single presigned link -> MUST BE BACKWARDS COMPATIBLE
187188
dsm = get_dsm_provider(request.app).get(location_id)
188-
_logger.debug(
189-
"creating upload links for file %s at location %s", file_id, location_id
190-
)
191-
links: UploadLinks = await dsm.create_file_upload_links(
192-
user_id=query_params.user_id,
193-
file_id=file_id,
194-
link_type=query_params.link_type,
195-
file_size_bytes=query_params.file_size or ByteSize(0),
196-
is_directory=query_params.is_directory,
197-
sha256_checksum=query_params.sha256_checksum,
198-
)
189+
with log_context(
190+
logger=_logger, level=logging.DEBUG, msg=f"Creating upload links for {file_id=}"
191+
):
192+
links: UploadLinks = await dsm.create_file_upload_links(
193+
user_id=query_params.user_id,
194+
file_id=file_id,
195+
link_type=query_params.link_type,
196+
file_size_bytes=query_params.file_size or ByteSize(0),
197+
is_directory=query_params.is_directory,
198+
sha256_checksum=query_params.sha256_checksum,
199+
)
199200
if query_params.is_v1_upload:
200201
# return v1 response
201202
assert len(links.urls) == 1 # nosec

services/storage/src/simcore_service_storage/simcore_s3_dsm.py

Lines changed: 96 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,6 @@ async def create_file_upload_links(
452452
# NOTE: if this gets called successively with the same file_id, and
453453
# there was a multipart upload in progress beforehand, it MUST be
454454
# cancelled to prevent unwanted costs in AWS
455-
_logger.debug("Cleaning pending uploads for file_id=%s", file_id)
456455
await self._clean_pending_upload(
457456
TypeAdapter(SimcoreS3FileID).validate_python(file_id)
458457
)
@@ -462,7 +461,6 @@ async def create_file_upload_links(
462461
): # NOTE: Delete is not needed for directories that are synced via an external tool (rclone/aws s3 cli).
463462
# ensure file is deleted first in case it already exists
464463
# https://github.com/ITISFoundation/osparc-simcore/pull/5108
465-
_logger.debug("Attempting to delete file_id=%s", file_id)
466464
await self.delete_file(
467465
user_id=user_id,
468466
file_id=file_id,
@@ -491,19 +489,23 @@ async def create_file_upload_links(
491489
file_size_bytes
492490
):
493491
# create multipart links
494-
_logger.debug("Creating multipart upload links for file_id=%s", file_id)
495-
assert file_size_bytes # nosec
496-
multipart_presigned_links = await get_s3_client(
497-
self.app
498-
).create_multipart_upload_links(
499-
bucket=fmd.bucket_name,
500-
object_key=fmd.file_id,
501-
file_size=file_size_bytes,
502-
expiration_secs=get_application_settings(
492+
with log_context(
493+
logger=_logger,
494+
level=logging.DEBUG,
495+
msg=f"Creating multipart upload links for {file_id=}",
496+
):
497+
assert file_size_bytes # nosec
498+
multipart_presigned_links = await get_s3_client(
503499
self.app
504-
).STORAGE_DEFAULT_PRESIGNED_LINK_EXPIRATION_SECONDS,
505-
sha256_checksum=fmd.sha256_checksum,
506-
)
500+
).create_multipart_upload_links(
501+
bucket=fmd.bucket_name,
502+
object_key=fmd.file_id,
503+
file_size=file_size_bytes,
504+
expiration_secs=get_application_settings(
505+
self.app
506+
).STORAGE_DEFAULT_PRESIGNED_LINK_EXPIRATION_SECONDS,
507+
sha256_checksum=fmd.sha256_checksum,
508+
)
507509
# update the database so we keep the upload id
508510
fmd.upload_id = multipart_presigned_links.upload_id
509511
await FileMetaDataRepository.instance(get_db_engine(self.app)).upsert(
@@ -515,29 +517,35 @@ async def create_file_upload_links(
515517
)
516518
if link_type == LinkType.PRESIGNED:
517519
# create single presigned link
518-
_logger.debug(
519-
"Creating single presigned upload link for file_id=%s", file_id
520-
)
521-
single_presigned_link = await get_s3_client(
522-
self.app
523-
).create_single_presigned_upload_link(
524-
bucket=self.simcore_bucket_name,
525-
object_key=fmd.file_id,
526-
expiration_secs=get_application_settings(
520+
with log_context(
521+
logger=_logger,
522+
level=logging.DEBUG,
523+
msg=f"Creating single presigned upload link for {file_id=}",
524+
):
525+
single_presigned_link = await get_s3_client(
527526
self.app
528-
).STORAGE_DEFAULT_PRESIGNED_LINK_EXPIRATION_SECONDS,
529-
)
527+
).create_single_presigned_upload_link(
528+
bucket=self.simcore_bucket_name,
529+
object_key=fmd.file_id,
530+
expiration_secs=get_application_settings(
531+
self.app
532+
).STORAGE_DEFAULT_PRESIGNED_LINK_EXPIRATION_SECONDS,
533+
)
530534
return UploadLinks(
531535
[single_presigned_link],
532536
file_size_bytes or MAX_LINK_CHUNK_BYTE_SIZE[link_type],
533537
)
534538

535539
# user wants just the s3 link
536-
_logger.debug("Creating S3 link for file_id=%s", file_id)
537-
s3_link = get_s3_client(self.app).compute_s3_url(
538-
bucket=self.simcore_bucket_name,
539-
object_key=TypeAdapter(SimcoreS3FileID).validate_python(file_id),
540-
)
540+
with log_context(
541+
logger=_logger,
542+
level=logging.DEBUG,
543+
msg=f"Compute S3 link for file_id={file_id}",
544+
):
545+
s3_link = get_s3_client(self.app).compute_s3_url(
546+
bucket=self.simcore_bucket_name,
547+
object_key=TypeAdapter(SimcoreS3FileID).validate_python(file_id),
548+
)
541549
return UploadLinks(
542550
[s3_link], file_size_bytes or MAX_LINK_CHUNK_BYTE_SIZE[link_type]
543551
)
@@ -694,46 +702,52 @@ async def delete_file(
694702
# Only use this in those circumstances where a collaborator requires to delete a file (the current
695703
# permissions model will not allow him to do so, even though this is a legitimate action)
696704
# SEE https://github.com/ITISFoundation/osparc-simcore/issues/5159
705+
with log_context(
706+
logger=_logger, level=logging.DEBUG, msg=f"Deleting file {file_id=}"
707+
):
708+
if enforce_access_rights:
709+
can = await AccessLayerRepository.instance(
710+
get_db_engine(self.app)
711+
).get_file_access_rights(user_id=user_id, file_id=file_id)
712+
if not can.delete:
713+
raise FileAccessRightError(access_right="delete", file_id=file_id)
697714

698-
if enforce_access_rights:
699-
can = await AccessLayerRepository.instance(
700-
get_db_engine(self.app)
701-
).get_file_access_rights(user_id=user_id, file_id=file_id)
702-
if not can.delete:
703-
raise FileAccessRightError(access_right="delete", file_id=file_id)
704-
705-
try:
706-
await get_s3_client(self.app).delete_objects_recursively(
707-
bucket=self.simcore_bucket_name,
708-
prefix=file_id,
709-
)
710-
except S3KeyNotFoundError:
711-
_logger.warning("File %s not found in S3", file_id)
712-
# we still need to clean up the database entry (it exists)
713-
# and to invalidate the size of the parent directory
715+
try:
716+
await get_s3_client(self.app).delete_objects_recursively(
717+
bucket=self.simcore_bucket_name,
718+
prefix=file_id,
719+
)
720+
except S3KeyNotFoundError:
721+
_logger.warning("File %s not found in S3", file_id)
722+
# we still need to clean up the database entry (it exists)
723+
# and to invalidate the size of the parent directory
714724

715-
async with transaction_context(get_db_engine(self.app)) as connection:
716-
file_meta_data_repo = FileMetaDataRepository.instance(
717-
get_db_engine(self.app)
718-
)
719-
await file_meta_data_repo.delete(connection=connection, file_ids=[file_id])
720-
721-
if parent_dir_fmds := await file_meta_data_repo.list_filter_with_partial_file_id(
722-
connection=connection,
723-
user_or_project_filter=UserOrProjectFilter(
724-
user_id=user_id, project_ids=[]
725-
),
726-
file_id_prefix=compute_file_id_prefix(file_id, 2),
727-
partial_file_id=None,
728-
is_directory=True,
729-
sha256_checksum=None,
730-
):
731-
parent_dir_fmd = max(parent_dir_fmds, key=lambda fmd: len(fmd.file_id))
732-
parent_dir_fmd.file_size = UNDEFINED_SIZE
733-
await file_meta_data_repo.upsert(
734-
connection=connection, fmd=parent_dir_fmd
725+
async with transaction_context(get_db_engine(self.app)) as connection:
726+
file_meta_data_repo = FileMetaDataRepository.instance(
727+
get_db_engine(self.app)
728+
)
729+
await file_meta_data_repo.delete(
730+
connection=connection, file_ids=[file_id]
735731
)
736732

733+
if parent_dir_fmds := await file_meta_data_repo.list_filter_with_partial_file_id(
734+
connection=connection,
735+
user_or_project_filter=UserOrProjectFilter(
736+
user_id=user_id, project_ids=[]
737+
),
738+
file_id_prefix=compute_file_id_prefix(file_id, 2),
739+
partial_file_id=None,
740+
is_directory=True,
741+
sha256_checksum=None,
742+
):
743+
parent_dir_fmd = max(
744+
parent_dir_fmds, key=lambda fmd: len(fmd.file_id)
745+
)
746+
parent_dir_fmd.file_size = UNDEFINED_SIZE
747+
await file_meta_data_repo.upsert(
748+
connection=connection, fmd=parent_dir_fmd
749+
)
750+
737751
async def delete_project_simcore_s3(
738752
self, user_id: UserID, project_id: ProjectID, node_id: NodeID | None = None
739753
) -> None:
@@ -963,17 +977,22 @@ async def create_soft_link(
963977
return convert_db_to_model(await file_meta_data_repo.insert(fmd=target))
964978

965979
async def _clean_pending_upload(self, file_id: SimcoreS3FileID) -> None:
966-
with suppress(FileMetaDataNotFoundError):
967-
fmd = await FileMetaDataRepository.instance(get_db_engine(self.app)).get(
968-
file_id=file_id
969-
)
970-
if is_valid_managed_multipart_upload(fmd.upload_id):
971-
assert fmd.upload_id # nosec
972-
await get_s3_client(self.app).abort_multipart_upload(
973-
bucket=self.simcore_bucket_name,
974-
object_key=file_id,
975-
upload_id=fmd.upload_id,
976-
)
980+
with log_context(
981+
logger=_logger,
982+
level=logging.DEBUG,
983+
msg=f"Cleaning pending uploads for {file_id=}",
984+
):
985+
with suppress(FileMetaDataNotFoundError):
986+
fmd = await FileMetaDataRepository.instance(
987+
get_db_engine(self.app)
988+
).get(file_id=file_id)
989+
if is_valid_managed_multipart_upload(fmd.upload_id):
990+
assert fmd.upload_id # nosec
991+
await get_s3_client(self.app).abort_multipart_upload(
992+
bucket=self.simcore_bucket_name,
993+
object_key=file_id,
994+
upload_id=fmd.upload_id,
995+
)
977996

978997
async def _clean_expired_uploads(self) -> None:
979998
"""this method will check for all incomplete updates by checking

0 commit comments

Comments
 (0)