Files
dify/api/tasks/enable_segments_to_index_task.py
JyongandClaude Fable 5.2 f276afa378 fix(api): keep index tasks from stranding documents and cleanup
- Segment enable/disable/delete tasks wait for a busy document index
  lock instead of giving up after five retries.
- Tasks that exhaust retries resolve documents left in a transitional
  state so users can retry them, and clear indexing cache flags.
- Hand off the duplicate-indexing tenant queue slot only on a final
  attempt.
- Document cleanup bounds non-lock retries and falls back to relational
  cleanup when index cleanup fails terminally.
- App removal continues past failing records and reports them once.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-17 15:36:06 +08:00

244 lines
10 KiB
Python

import logging
import time
import click
from celery import Task, shared_task
from redis.exceptions import RedisError
from sqlalchemy import select, update
from core.db.session_factory import session_factory
from core.rag.index_processor.constant.doc_type import DocType
from core.rag.index_processor.constant.index_type import IndexStructureType
from core.rag.index_processor.index_processor import IndexProcessorFactory
from core.rag.models.document import AttachmentDocument, ChildDocument, Document
from extensions.ext_redis import redis_client
from libs.datetime_utils import naive_utc_now
from models.dataset import Dataset, DocumentSegment
from models.dataset import Document as DatasetDocument
from models.enums import SegmentStatus
from repositories.knowledge.segment_query_repository import get_segment_attachment_files, get_segment_child_chunks
from services.knowledge.indexing.adapters.redis_lock import RedisKnowledgeIndexingLock
from services.knowledge.indexing.locks import (
KnowledgeIndexingLease,
KnowledgeIndexingLeaseLostError,
KnowledgeIndexingLockBusyError,
document_index_lock_name,
)
from tasks.indexing_task_retry import (
TaskRetriesExhaustedError,
clear_segment_indexing_cache,
is_final_attempt,
lock_busy_countdown,
)
logger = logging.getLogger(__name__)
SEGMENT_INDEX_MAX_RETRIES = 5
@shared_task(
bind=True,
queue="dataset",
autoretry_for=(KnowledgeIndexingLeaseLostError, RedisError),
retry_backoff=True,
# Unbounded for Celery so lock-busy waits never run out; lease loss and
# Redis errors are bounded below by SEGMENT_INDEX_MAX_RETRIES.
max_retries=None,
acks_late=True,
reject_on_worker_lost=True,
)
def enable_segments_to_index_task(self: Task, segment_ids: list, dataset_id: str, document_id: str):
"""
Async enable segments to index
:param segment_ids: list of segment ids
:param dataset_id: dataset id
:param document_id: document id
Retry contract: the per-document index lock is also held for the whole of
document indexing, so a busy lock is waited for without limit (capped
backoff) instead of abandoning segments that are already enabled in the
database. Lease loss and Redis errors keep a bounded budget of
``SEGMENT_INDEX_MAX_RETRIES`` and end with ``TaskRetriesExhaustedError``.
Postcondition: every terminal exit, successful or not, clears the
``segment_{id}_indexing`` flag of each requested segment; an attempt that
will be retried keeps them.
Usage: enable_segments_to_index_task.delay(segment_ids, dataset_id, document_id)
"""
final_attempt = is_final_attempt(self, max_retries=SEGMENT_INDEX_MAX_RETRIES)
try:
locks = RedisKnowledgeIndexingLock(redis=redis_client)
with locks.hold(document_index_lock_name(document_id), timeout=600, blocking_timeout=5) as lease:
_enable_segments_to_index(segment_ids, dataset_id, document_id, lease=lease)
lease.assert_owned()
except KnowledgeIndexingLockBusyError as error:
# Another owner (often document indexing) holds the lock; contention is not a failure.
raise self.retry(exc=error, countdown=lock_busy_countdown(self)) from error
except (KnowledgeIndexingLeaseLostError, RedisError) as error:
if not final_attempt:
raise
logger.exception(
"Segment enable exhausted its retries: dataset_id=%s document_id=%s segment_ids=%s",
dataset_id,
document_id,
segment_ids,
)
clear_segment_indexing_cache(redis_client, segment_ids)
raise TaskRetriesExhaustedError(
f"Segment enable failed after {SEGMENT_INDEX_MAX_RETRIES} retries, document_id={document_id}: {error}"
) from error
except Exception:
clear_segment_indexing_cache(redis_client, segment_ids)
raise
clear_segment_indexing_cache(redis_client, segment_ids)
def _enable_segments_to_index(
segment_ids: list[str], dataset_id: str, document_id: str, *, lease: KnowledgeIndexingLease
) -> tuple[str, ...]:
start_at = time.perf_counter()
with session_factory.create_session() as session:
dataset = session.scalar(select(Dataset).where(Dataset.id == dataset_id).limit(1))
if not dataset:
logger.info(click.style(f"Dataset {dataset_id} not found, pass.", fg="cyan"))
return ()
dataset_document = session.scalar(
select(DatasetDocument)
.where(
DatasetDocument.id == document_id,
DatasetDocument.tenant_id == dataset.tenant_id,
DatasetDocument.dataset_id == dataset.id,
)
.limit(1)
)
if not dataset_document:
logger.info(click.style(f"Document {document_id} not found, pass.", fg="cyan"))
return ()
if not dataset_document.enabled or dataset_document.archived or dataset_document.indexing_status != "completed":
logger.info(click.style(f"Document {document_id} status is invalid, pass.", fg="cyan"))
return ()
# sync index processor
index_processor = IndexProcessorFactory(dataset_document.doc_form).init_index_processor()
segments = session.scalars(
select(DocumentSegment).where(
DocumentSegment.id.in_(segment_ids),
DocumentSegment.tenant_id == dataset.tenant_id,
DocumentSegment.dataset_id == dataset_id,
DocumentSegment.document_id == document_id,
DocumentSegment.enabled.is_(True),
# A segment that never finished indexing has no trustworthy index
# payload (its ``index_node_id`` may still be NULL), so it must not
# be pushed into the vector index. This mirrors the guard in
# ``enable_segment_to_index_task`` ("Segment is not completed,
# enable is not allowed") and in
# ``disable_segment_from_index_task``.
DocumentSegment.status == SegmentStatus.COMPLETED,
)
).all()
if not segments:
logger.info(click.style(f"Segments not found: {segment_ids}", fg="cyan"))
return ()
documents = []
multimodal_documents = []
for segment in segments:
document = Document(
page_content=segment.content,
metadata={
"doc_id": segment.index_node_id,
"doc_hash": segment.index_node_hash,
"document_id": document_id,
"dataset_id": dataset_id,
},
)
if dataset_document.doc_form == IndexStructureType.PARENT_CHILD_INDEX:
child_chunks = get_segment_child_chunks(segment, session=session)
if child_chunks:
child_documents = []
for child_chunk in child_chunks:
child_document = ChildDocument(
page_content=child_chunk.content,
metadata={
"doc_id": child_chunk.index_node_id,
"doc_hash": child_chunk.index_node_hash,
"document_id": document_id,
"dataset_id": dataset_id,
},
)
child_documents.append(child_document)
document.children = child_documents
if dataset.is_multimodal:
for attachment in get_segment_attachment_files(segment, session=session):
multimodal_documents.append(
AttachmentDocument(
page_content=attachment.name,
metadata={
"doc_id": attachment.id,
"doc_hash": "",
"document_id": segment.document_id,
"dataset_id": segment.dataset_id,
"doc_type": DocType.IMAGE,
},
)
)
documents.append(document)
# save vector index
try:
lease.assert_owned()
index_processor.load(dataset, documents, multimodal_documents=multimodal_documents, session=session)
lease.assert_owned()
session.commit()
except KnowledgeIndexingLeaseLostError:
session.rollback()
raise
except Exception as error:
session.rollback()
session.execute(
update(DocumentSegment)
.where(
DocumentSegment.id.in_([segment.id for segment in segments]),
DocumentSegment.tenant_id == dataset.tenant_id,
DocumentSegment.dataset_id == dataset_id,
DocumentSegment.document_id == document_id,
DocumentSegment.enabled.is_(True),
)
.values(
enabled=False,
disabled_at=naive_utc_now().replace(microsecond=0),
disabled_by=None,
error=str(error),
)
)
session.commit()
for segment in segments:
try:
redis_client.delete(f"segment_{segment.id}_indexing")
except Exception:
logger.warning("Failed to clear indexing cache for segment %s", segment.id, exc_info=True)
raise
# Enable summary indexes for these segments
from services.knowledge.summaries.adapters import SummaryIndexAdapter
segment_ids_list = [segment.id for segment in segments]
try:
lease.assert_owned()
SummaryIndexAdapter.enable_summaries_for_segments(
dataset=dataset,
segment_ids=segment_ids_list,
)
lease.assert_owned()
except KnowledgeIndexingLeaseLostError:
raise
except Exception:
logger.warning("Failed to enable summaries for segments", exc_info=True)
end_at = time.perf_counter()
logger.info(click.style(f"Segments enabled to index latency: {end_at - start_at}", fg="green"))
return tuple(segment.id for segment in segments)