Description
Improve performance of initiate and complete directory multipart upload.
The multipart upload for directories works fine for directories with small number of files.
However, there are directory assets with hundreds of files, that cause the initiate and complete endpoints to be slow.
Since most of the time is spent in calling S3, a possible improvement can be:
- to make requests in parallel using threads, limiting the number of threads in a pool
- to make requests in parallel using asyncio -> doesn't fit well with the current approach of sync endpoints, because it would require to:
- create an async loop inside the fastapi/starlette thread
- or rewrite the endpoint to be fully async, requiring the installation of async libraries
- to create a background job, and allow the client to check when it's finished, but it would add more complexity. It can be considered if the number of files is too high to be handled by parallel requests
Example in staging for a directory with 1527 files (maximum number currently registered):
{
"time": "2026-07-05T08:55:59.254862+00:00",
"level": "INFO",
"name": "app.middleware",
"message": "request_completed",
"extra": {
"method": "POST",
"url": "http://staging.cell-a.openbraininstitute.org/api/entitycore/em-cell-mesh/40f6e39b-c940-4bd4-942f-b564fc5b5890/assets/directory/multipart-upload/initiate",
"route_template": "/{entity_route}/{entity_id}/assets/directory/multipart-upload/initiate",
"status_code": 200,
"status_class": 2,
"process_time_ms": 58992,
"response_size": 3436907,
"client": "10.0.2.153",
"forwarded_for": "213.55.247.216",
"user_agent": "python-httpx/0.28.1",
"request_id": "5d30d1fd-f344-4a45-8efa-b36e1c9f814f",
"user_id": "3d4e4581-1a32-4356-beb6-74099d65de96"
},
"exception": null
}
{
"time": "2026-07-05T08:59:58.094867+00:00",
"level": "INFO",
"name": "app.middleware",
"message": "request_completed",
"extra": {
"method": "POST",
"url": "http://staging.cell-a.openbraininstitute.org/api/entitycore/em-cell-mesh/40f6e39b-c940-4bd4-942f-b564fc5b5890/assets/10ae3e97-68b7-4ebe-a93b-b74d3037d72e/directory/multipart-upload/complete",
"route_template": "/{entity_route}/{entity_id}/assets/{asset_id}/directory/multipart-upload/complete",
"status_code": 200,
"status_class": 2,
"process_time_ms": 215249,
"response_size": 426,
"client": "10.0.2.153",
"forwarded_for": "213.55.247.216",
"user_agent": "python-httpx/0.28.1",
"request_id": "2eeb4e1f-a15f-4a10-9907-4e22426b7d61",
"user_id": "3d4e4581-1a32-4356-beb6-74099d65de96"
},
"exception": null
}
Acceptance criteria
Description
Improve performance of initiate and complete directory multipart upload.
The multipart upload for directories works fine for directories with small number of files.
However, there are directory assets with hundreds of files, that cause the initiate and complete endpoints to be slow.
Since most of the time is spent in calling S3, a possible improvement can be:
Example in staging for a directory with 1527 files (maximum number currently registered):
{ "time": "2026-07-05T08:55:59.254862+00:00", "level": "INFO", "name": "app.middleware", "message": "request_completed", "extra": { "method": "POST", "url": "http://staging.cell-a.openbraininstitute.org/api/entitycore/em-cell-mesh/40f6e39b-c940-4bd4-942f-b564fc5b5890/assets/directory/multipart-upload/initiate", "route_template": "/{entity_route}/{entity_id}/assets/directory/multipart-upload/initiate", "status_code": 200, "status_class": 2, "process_time_ms": 58992, "response_size": 3436907, "client": "10.0.2.153", "forwarded_for": "213.55.247.216", "user_agent": "python-httpx/0.28.1", "request_id": "5d30d1fd-f344-4a45-8efa-b36e1c9f814f", "user_id": "3d4e4581-1a32-4356-beb6-74099d65de96" }, "exception": null } { "time": "2026-07-05T08:59:58.094867+00:00", "level": "INFO", "name": "app.middleware", "message": "request_completed", "extra": { "method": "POST", "url": "http://staging.cell-a.openbraininstitute.org/api/entitycore/em-cell-mesh/40f6e39b-c940-4bd4-942f-b564fc5b5890/assets/10ae3e97-68b7-4ebe-a93b-b74d3037d72e/directory/multipart-upload/complete", "route_template": "/{entity_route}/{entity_id}/assets/{asset_id}/directory/multipart-upload/complete", "status_code": 200, "status_class": 2, "process_time_ms": 215249, "response_size": 426, "client": "10.0.2.153", "forwarded_for": "213.55.247.216", "user_agent": "python-httpx/0.28.1", "request_id": "2eeb4e1f-a15f-4a10-9907-4e22426b7d61", "user_id": "3d4e4581-1a32-4356-beb6-74099d65de96" }, "exception": null }Acceptance criteria