Skip to content

fix(knowledge): share segment creation lock with batches - #44896

Open
MindOfqc wants to merge 1 commit into
langgenius:mainfrom
MindOfqc:fix/shared-segment-creation-lock
Open

MindOfqc wants to merge 1 commit into
langgenius:mainfrom
MindOfqc:fix/shared-segment-creation-lock

Conversation

@MindOfqc

Copy link
Copy Markdown
Contributor

Refs #39022 (the single-request versus Service API batch creation case).

Single segment creation and SegmentService.multi_create_segment currently use different per-document Redis keys. A single creation can therefore reach its persistence port while the batch creation lease for that document is held. Both paths allocate the next position and update the document's word count.

Make single creation use the existing batch lease name. This keeps the Service API batch path's key unchanged and serializes these two creation paths for the same document. Creation for another document stays independent.

Validation:

  • New regression on the original implementation: 1 failed / 2 passed (DID NOT RAISE DocumentBusyError while the batch lease was held).
  • Updated test_application.py: 25 passed. Tests execute the actual application service with in-memory persistence/index ports and standard-library mutual exclusion; they cover contention, different documents, and release after a failed write.
  • Scoped Ruff 0.16.9 and Pyrefly 1.3.2 checks passed for the changed source and test module.
  • The repository's normal staged commit hook passed (Ruff format/check/format); the spec-mock and new-Base-model guards passed. The hook used the prepared isolated environment without syncing the full backend dependency set.

Test command (Python 3.13.7, isolated dependency environment):

uv run --project api --no-sync python -m pytest -c api/pyproject.toml \
  --confcutdir=api/tests/unit_tests/services/knowledge/segments \
  api/tests/unit_tests/services/knowledge/segments/test_application.py -q

This does not cover CSV imports or old single-request workers during a rolling deployment. The existing 600-second lease and batch indexing scope are unchanged. Real Redis/SQL concurrent integration tests and the complete backend suite were not run locally.

AI assistance was used for investigation, implementation, and review.

Checklist

  • This change requires a documentation update, included in Dify Document.
  • I understand that this PR may be closed in case there was no previous discussion or issues.
  • I've verified the change and added tests for meaningful regression risks.
  • I've updated the documentation accordingly.
  • I ran the full make lint && make type-check commands (scoped checks were run instead).

@MindOfqc
MindOfqc requested a review from JohnJyong as a code owner October 27, 2026 12:09
@github-actions github-actions Bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Oct 27, 2026

MindOfqc commented Nov 5, 2026

Copy link
Copy Markdown
Contributor Author

The Main CI Pipeline for the current head 4ed3518 is waiting for workflow approval (action_required). The two successful checks on this head are label and labeler; the backend test workflow has not executed.

Could a maintainer approve the pending fork workflows so the implementation and regression tests can receive CI validation?

Prepared with AI coding assistance.

@asukaminato0721 asukaminato0721 left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

1 inline suggestion at 4ed3518c1c, from a rule learned from earlier reviews in this repository. Reply on the comment if it is wrong.

dify-codereview

class CreationStore:
"""In-memory implementation of the creation path's persistence capability."""

rows: dict[SegmentRef, Mapping[str, object]] = field(default_factory=dict)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Parse, don't validate (https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate/): model this data once at the boundary as a pydantic model / dataclass / TypedDict (Model.model_validate(raw)), and pass the typed value on, instead of passing dicts around and re-checking keys and types where they are used. In a test, don't build this as an ad hoc dict[str, object]: define the shape (TypedDict / model) in the production code and import it here.

dify-codereview · parse-dont-validate Learned from an earlier review. Reply here if this is wrong.

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

Labels

size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants