Skip to content

Commit

Permalink
Merge pull request #1944 from fractal-analytics-platform/1917-bis
Browse files Browse the repository at this point in the history
Remove rate limit for submit-job v2 endpoint
  • Loading branch information
tcompa authored Oct 21, 2024
2 parents 31f7821 + 7835c2a commit 30be479
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 89 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
* Remove `TaskDumpV2.owner` attribute (\#1909).
* Jobs
* Prevent job submission if includes non-active or non-accessible tasks (\#1817).
* Remove rate limit for `POST /project/{project_id}/job/submit/` (\#1944).
* Admin:
* Remove `owner` from `GET admin/v2/task/` (\#1909).
* Deprecate `kind` query parameter for `/admin/v2/task/` (\#1893).
Expand Down
32 changes: 0 additions & 32 deletions fractal_server/app/routes/api/v2/submit.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os
from datetime import datetime
from datetime import timedelta
from datetime import timezone
from pathlib import Path
from typing import Optional
Expand Down Expand Up @@ -181,37 +180,6 @@ async def apply_workflow(
**job_create.dict(),
)

# Rate Limiting:
# raise `429 TOO MANY REQUESTS` if this endpoint has been called with the
# same database keys (Project, Workflow and Datasets) during the last
# `settings.FRACTAL_API_SUBMIT_RATE_LIMIT` seconds.
stm = (
select(JobV2)
.where(JobV2.project_id == project_id)
.where(JobV2.workflow_id == workflow_id)
.where(JobV2.dataset_id == dataset_id)
)
res = await db.execute(stm)
db_jobs = res.scalars().all()
if db_jobs and any(
abs(
job.start_timestamp
- db_job.start_timestamp.replace(tzinfo=timezone.utc)
)
< timedelta(seconds=settings.FRACTAL_API_SUBMIT_RATE_LIMIT)
for db_job in db_jobs
):
raise HTTPException(
status_code=status.HTTP_429_TOO_MANY_REQUESTS,
detail=(
f"The endpoint 'POST /api/v2/project/{project_id}/job/submit/'"
" was called several times within an interval of less "
f"than {settings.FRACTAL_API_SUBMIT_RATE_LIMIT} seconds, using"
" the same foreign keys. If it was intentional, please wait "
"and try again."
),
)

db.add(job)
await db.commit()
await db.refresh(job)
Expand Down
57 changes: 0 additions & 57 deletions tests/v2/03_api/test_api_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,63 +457,6 @@ async def test_project_apply_slurm_account(
assert res.status_code == 422


async def test_rate_limit(
MockCurrentUser,
project_factory_v2,
dataset_factory_v2,
workflow_factory_v2,
task_factory_v2,
client,
db,
override_settings_factory,
tmp_path,
):
override_settings_factory(
FRACTAL_API_SUBMIT_RATE_LIMIT=1,
FRACTAL_RUNNER_WORKING_BASE_DIR=tmp_path / "artifacts",
)
async with MockCurrentUser(user_kwargs=dict(is_verified=True)) as user:

project = await project_factory_v2(user)
dataset = await dataset_factory_v2(project_id=project.id, name="ds")
workflow = await workflow_factory_v2(project_id=project.id)
task = await task_factory_v2(user_id=user.id, source="source")
await _workflow_insert_task(
workflow_id=workflow.id, task_id=task.id, db=db
)
# Call 1: OK
res = await client.post(
f"{PREFIX}/project/{project.id}/job/submit/"
f"?workflow_id={workflow.id}&dataset_id={dataset.id}",
json={},
)
assert res.status_code == 202
time.sleep(1)
# Call 2: OK
res = await client.post(
f"{PREFIX}/project/{project.id}/job/submit/"
f"?workflow_id={workflow.id}&dataset_id={dataset.id}",
json={},
)
assert res.status_code == 202
# Call 2: too early!
res = await client.post(
f"{PREFIX}/project/{project.id}/job/submit/"
f"?workflow_id={workflow.id}&dataset_id={dataset.id}",
json={},
)
assert res.status_code == 429
assert "less than 1 second" in res.json()["detail"]
time.sleep(1)
# Call 3: OK
res = await client.post(
f"{PREFIX}/project/{project.id}/job/submit/"
f"?workflow_id={workflow.id}&dataset_id={dataset.id}",
json={},
)
assert res.status_code == 202


async def test_get_jobs(
db,
client,
Expand Down

0 comments on commit 30be479

Please sign in to comment.