Skip to content

fix(collections): Ensure safe collection creation - #1312

Merged
vprashrex merged 3 commits into
mainfrom
fix/collection-create-retry-safe
Oct 28, 2026
Merged

vprashrex merged 3 commits into
mainfrom
fix/collection-create-retry-safe

Conversation

@vprashrex

@vprashrex vprashrex commented Oct 28, 2026 •

Copy link
Copy Markdown
Collaborator

Issue

Closes #1311

Summary

  • Before: Glific's client times out on POST /api/v1/collections, causing each retry to create a new job and vector store due to the unique name index not being available until the worker finishes.
  • Now: The new implementation allows for retries without creating new jobs, returning the existing job on duplicates and enqueuing only when a new row is created.
  • Added a partial unique index on collection_jobs.name for jobs in PENDING/PROCESSING.
  • Updated logic to mark jobs FAILED if enqueue fails, preventing unrun jobs from holding names.

Checklist

Before submitting a pull request, please ensure that you mark these task.

  • Ran fastapi run --reload app/main.py or docker compose up in the repository root and test.
  • If you've fixed a bug or added code that is tested and has test cases.

Notes

A job stuck in PROCESSING (worker lost) keeps its name reserved; there is no recovery for that today, only the stale-PENDING alert. Existing duplicate vector stores from earlier retries are not cleaned up here.

Original PR description

Issue

Closes #1311

Summary

Glific's client times out on POST /api/v1/collections and retries. Each retry created a new job and vector store, because the collection row (and its unique name index) only exists once the worker finishes.

  • collection_jobs.name (nullable) plus a partial unique index uq_collection_jobs_project_id_name_in_flight on (project_id, name) for CREATE jobs in PENDING/PROCESSING. Migration 087.
  • CollectionJobCrud.create_or_get_in_flight returns the existing job on a duplicate, and the route only enqueues when a new row was created.
  • If enqueue fails the job is marked FAILED, so a job that never ran does not hold the name.
  • Wiki and create.md updated.

Contract: request/response schemas, status codes and the 409 for an already-created collection are unchanged. A retry now gets the same job_id (its status can be PROCESSING). callback_url/documents on a duplicate request are ignored. Requests without name are not covered.

Checklist

  • Ran an end-to-end check against a scratch Postgres DB (real concurrent requests, Celery enqueue mocked): 4 sequential and 8 concurrent same-name requests now give 1 job and 1 enqueue (was 4 and 8); FAILED job frees the name; unnamed requests and the 409 are unchanged; enqueue failure marks the job FAILED. Migration upgrade/downgrade verified.
  • Test cases not added in this PR.

Notes

A job stuck in PROCESSING (worker lost) keeps its name reserved; there is no recovery for that today, only the stale-PENDING alert. Existing duplicate vector stores from earlier retries are not cleaned up here.

… flight (#1311)

Retried POST /collections requests each created a new job and vector store because the collection row is only inserted when the worker finishes.

Store the requested name on collection_jobs with a partial unique index over in-flight CREATE jobs. A duplicate request now returns the existing job without enqueuing again, and a failed enqueue marks the job FAILED so the name is not held.
@coderabbitai

coderabbitai Bot commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are limited based on label configuration.

🏷️ Required labels (at least one): 1
  • ready-for-review

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions github-actions Bot changed the title fix(collections): Make collection create retry-safe while a job is in flight fix(collections): Ensure safe collection creation Oct 28, 2026
@vprashrex vprashrex self-assigned this Oct 28, 2026
@github-actions

github-actions Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

OpenAPI changes   ⚪ No API surface changes

Note

This PR does not modify the API contract.

main ↔ d4731121 · generated by oasdiff

@codecov

codecov Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@vprashrex
vprashrex requested a review from Ayush8923 October 28, 2026 12:18
@Ayush8923

Copy link
Copy Markdown
Collaborator

@vprashrex can you please codecov report.

Add crud tests for create_or_get_in_flight (new, duplicate, insert race, re-raise, name reuse after finish, unnamed) and route tests for duplicate in-flight name, existing-collection 409 and enqueue failure.
Comment on lines +138 to +141
logger.info(
"[CollectionJobCrud.create_or_get_in_flight] Duplicate in-flight create | "
f"{{'project_id': '{self.project_id}', 'job_id': '{existing.id}'}}"
)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this..

Comment on lines +145 to +148
logger.info(
"[CollectionJobCrud.create_or_get_in_flight] Collection job created | "
f"{{'collection_job_id': '{job.id}'}}"
)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this too not needed.

@vprashrex
vprashrex merged commit baf0ff2 into main Oct 28, 2026
6 checks passed
@vprashrex
vprashrex deleted the fix/collection-create-retry-safe branch October 28, 2026 13:44
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 1.9.0-main.2 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Collection: Create API Route Safeguard against duplicate jobs

2 participants