Files
dify/api/tasks/remove_document_from_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

181 lines
6.9 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.index_processor import IndexProcessorFactory
from extensions.ext_redis import redis_client
from libs.datetime_utils import naive_utc_now
from models.dataset import Document, DocumentSegment
from models.enums import IndexingStatus
from repositories.knowledge.document_query_repository import get_document_dataset
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 (
clear_document_indexing_cache,
is_terminal_failure,
resolve_documents_after_terminal_failure,
)
logger = logging.getLogger(__name__)
_RETRYABLE_ERRORS = (KnowledgeIndexingLockBusyError, KnowledgeIndexingLeaseLostError, RedisError)
@shared_task(
bind=True,
queue="dataset",
autoretry_for=_RETRYABLE_ERRORS,
retry_backoff=True,
max_retries=10,
)
def remove_document_from_index_task(self: Task, document_id: str):
"""
Async Remove document from index
:param document_id: document id
Postconditions: every terminal exit clears ``document_{id}_indexing``; an
attempt that will be retried keeps it. When the task ends without having
cleaned the index (retries exhausted, or a non-retryable error), a
disabled document is re-enabled exactly as after a failed removal, so the
database matches the index again and the user can repeat the action. An
archived document stays archived; its remaining index nodes are logged.
Usage: remove_document_from_index.delay(document_id)
"""
try:
locks = RedisKnowledgeIndexingLock(redis=redis_client)
with locks.hold(
document_index_lock_name(document_id),
timeout=600,
blocking_timeout=5,
) as lease:
_remove_document_from_index(document_id, lease=lease)
lease.assert_owned()
except Exception as error:
if is_terminal_failure(self, error, retryable=_RETRYABLE_ERRORS, max_retries=self.max_retries):
logger.exception(
"Remove document from index failed terminally; index nodes remain: document_id=%s", document_id
)
_restore_unremoved_document(document_id)
clear_document_indexing_cache(redis_client, document_id)
raise
clear_document_indexing_cache(redis_client, document_id)
def _restore_unremoved_document(document_id: str) -> None:
"""Re-enable a disabled document whose index nodes were never removed."""
def still_requires_removal(document: Document) -> bool:
return document.indexing_status == IndexingStatus.COMPLETED and not document.enabled and not document.archived
def enable(document: Document) -> None:
document.enabled = True
resolve_documents_after_terminal_failure(
[document_id],
still_owned=still_requires_removal,
resolve=enable,
)
def _remove_document_from_index(document_id: str, *, lease: KnowledgeIndexingLease) -> None:
logger.info(click.style(f"Start remove document segments from index: {document_id}", fg="green"))
start_at = time.perf_counter()
with session_factory.create_session() as session:
document = session.scalar(select(Document).where(Document.id == document_id).limit(1))
if not document:
logger.info(click.style(f"Document not found: {document_id}", fg="red"))
return
if document.indexing_status != IndexingStatus.COMPLETED:
logger.info(click.style(f"Document is not completed, remove is not allowed: {document_id}", fg="red"))
return
if document.enabled and not document.archived:
logger.info(click.style(f"Document no longer requires index removal: {document_id}", fg="yellow"))
return
try:
dataset = get_document_dataset(document, session=session)
if not dataset:
raise Exception("Document has no dataset")
index_processor = IndexProcessorFactory(document.doc_form).init_index_processor()
segments = session.scalars(select(DocumentSegment).where(DocumentSegment.document_id == document.id)).all()
# Disable summary indexes for all segments in this document
from services.knowledge.summaries.adapters import SummaryIndexAdapter
segment_ids_list = [segment.id for segment in segments]
if segment_ids_list:
try:
lease.assert_owned()
SummaryIndexAdapter.disable_summaries_for_segments(
dataset=dataset,
segment_ids=segment_ids_list,
disabled_by=document.disabled_by,
)
lease.assert_owned()
except KnowledgeIndexingLeaseLostError:
raise
except Exception:
logger.warning("Failed to disable summaries for document %s", document.id, exc_info=True)
index_node_ids = [segment.index_node_id for segment in segments if segment.index_node_id]
if index_node_ids:
try:
lease.assert_owned()
index_processor.clean(
dataset,
index_node_ids,
with_keywords=True,
delete_child_chunks=False,
session=session,
)
lease.assert_owned()
except KnowledgeIndexingLeaseLostError:
raise
except Exception:
logger.exception("clean dataset %s from index failed", dataset.id)
# update segment to disable
session.execute(
update(DocumentSegment)
.where(DocumentSegment.document_id == document.id)
.values(
enabled=False,
disabled_at=naive_utc_now(),
disabled_by=document.disabled_by,
updated_at=naive_utc_now(),
)
)
session.commit()
end_at = time.perf_counter()
logger.info(
click.style(
f"Document removed from index: {document.id} latency: {end_at - start_at}",
fg="green",
)
)
except KnowledgeIndexingLeaseLostError:
session.rollback()
raise
except Exception:
logger.exception("remove document from index failed")
if not document.archived:
document.enabled = True
session.commit()