This commit is contained in:
hsparks-codes 2025-12-10 16:21:23 +11:00 committed by GitHub
commit 075d0e2230
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 1647 additions and 10 deletions

0
api/apps/task_app.py Normal file
View file

View file

@ -837,6 +837,58 @@ class Task(DataBaseModel):
retry_count = IntegerField(default=0)
digest = TextField(null=True, help_text="task digest", default="")
chunk_ids = LongTextField(null=True, help_text="chunk ids", default="")
# Checkpoint/Resume support
checkpoint_id = CharField(max_length=32, null=True, index=True, help_text="Associated checkpoint ID")
can_pause = BooleanField(default=False, help_text="Whether task supports pause/resume")
is_paused = BooleanField(default=False, index=True, help_text="Whether task is currently paused")
class TaskCheckpoint(DataBaseModel):
"""Checkpoint data for long-running tasks (RAPTOR, GraphRAG)"""
id = CharField(max_length=32, primary_key=True)
task_id = CharField(max_length=32, null=False, index=True, help_text="Associated task ID")
task_type = CharField(max_length=32, null=False, help_text="Task type: raptor, graphrag")
# Overall task state
status = CharField(max_length=16, null=False, default="pending", index=True,
help_text="Status: pending, running, paused, completed, failed, cancelled")
# Document tracking
total_documents = IntegerField(default=0, help_text="Total number of documents to process")
completed_documents = IntegerField(default=0, help_text="Number of completed documents")
failed_documents = IntegerField(default=0, help_text="Number of failed documents")
pending_documents = IntegerField(default=0, help_text="Number of pending documents")
# Progress tracking
overall_progress = FloatField(default=0.0, help_text="Overall progress (0.0 to 1.0)")
token_count = IntegerField(default=0, help_text="Total tokens consumed")
# Checkpoint data (JSON)
checkpoint_data = JSONField(null=False, default={}, help_text="Detailed checkpoint state")
# Structure: {
# "doc_states": {
# "doc_id_1": {"status": "completed", "token_count": 1500, "chunks": 45, "completed_at": "..."},
# "doc_id_2": {"status": "failed", "error": "API timeout", "retry_count": 3, "last_attempt": "..."},
# "doc_id_3": {"status": "pending"},
# },
# "config": {...},
# "metadata": {...}
# }
# Timestamps
started_at = DateTimeField(null=True, help_text="When task started")
paused_at = DateTimeField(null=True, help_text="When task was paused")
resumed_at = DateTimeField(null=True, help_text="When task was resumed")
completed_at = DateTimeField(null=True, help_text="When task completed")
last_checkpoint_at = DateTimeField(null=True, index=True, help_text="Last checkpoint save time")
# Error tracking
error_message = TextField(null=True, help_text="Error message if failed")
retry_count = IntegerField(default=0, help_text="Number of retries attempted")
class Meta:
db_table = "task_checkpoint"
class Dialog(DataBaseModel):
@ -1358,6 +1410,20 @@ def migrate_db():
except Exception:
pass
# Checkpoint/Resume support migrations
try:
migrate(migrator.add_column("task", "checkpoint_id", CharField(max_length=32, null=True, index=True, help_text="Associated checkpoint ID")))
except Exception:
pass
try:
migrate(migrator.add_column("task", "can_pause", BooleanField(default=False, help_text="Whether task supports pause/resume")))
except Exception:
pass
try:
migrate(migrator.add_column("task", "is_paused", BooleanField(default=False, index=True, help_text="Whether task is currently paused")))
except Exception:
pass
# RAG Evaluation tables
try:
migrate(migrator.add_column("evaluation_datasets", "id", CharField(max_length=32, primary_key=True)))

View file

@ -0,0 +1,379 @@
#
# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""
Checkpoint service for managing task checkpoints and resume functionality.
"""
import logging
from datetime import datetime
from typing import Optional, Dict, List, Any
from api.db.db_models import TaskCheckpoint
from api.db.services.common_service import CommonService
from common.misc_utils import get_uuid
class CheckpointService(CommonService):
"""Service for managing task checkpoints"""
model = TaskCheckpoint
@classmethod
def create_checkpoint(
cls,
task_id: str,
task_type: str,
doc_ids: List[str],
config: Dict[str, Any]
) -> TaskCheckpoint:
"""
Create a new checkpoint for a task.
Args:
task_id: Task ID
task_type: Type of task ("raptor" or "graphrag")
doc_ids: List of document IDs to process
config: Task configuration
Returns:
Created TaskCheckpoint instance
"""
checkpoint_id = get_uuid()
# Initialize document states
doc_states = {}
for doc_id in doc_ids:
doc_states[doc_id] = {
"status": "pending",
"token_count": 0,
"chunks": 0,
"retry_count": 0
}
checkpoint_data = {
"doc_states": doc_states,
"config": config,
"metadata": {
"created_at": datetime.now().isoformat()
}
}
checkpoint = cls.model(
id=checkpoint_id,
task_id=task_id,
task_type=task_type,
status="pending",
total_documents=len(doc_ids),
completed_documents=0,
failed_documents=0,
pending_documents=len(doc_ids),
overall_progress=0.0,
token_count=0,
checkpoint_data=checkpoint_data,
started_at=datetime.now(),
last_checkpoint_at=datetime.now()
)
checkpoint.save(force_insert=True)
logging.info(f"Created checkpoint {checkpoint_id} for task {task_id} with {len(doc_ids)} documents")
return checkpoint
@classmethod
def get_by_task_id(cls, task_id: str) -> Optional[TaskCheckpoint]:
"""Get checkpoint by task ID"""
try:
return cls.model.get(cls.model.task_id == task_id)
except Exception:
return None
@classmethod
def save_document_completion(
cls,
checkpoint_id: str,
doc_id: str,
token_count: int = 0,
chunks: int = 0
) -> bool:
"""
Save completion of a single document.
Args:
checkpoint_id: Checkpoint ID
doc_id: Document ID
token_count: Tokens consumed for this document
chunks: Number of chunks generated
Returns:
True if successful
"""
try:
checkpoint = cls.model.get_by_id(checkpoint_id)
# Update document state
doc_states = checkpoint.checkpoint_data.get("doc_states", {})
if doc_id in doc_states:
doc_states[doc_id] = {
"status": "completed",
"token_count": token_count,
"chunks": chunks,
"completed_at": datetime.now().isoformat(),
"retry_count": doc_states[doc_id].get("retry_count", 0)
}
# Update counters
completed = sum(1 for s in doc_states.values() if s["status"] == "completed")
failed = sum(1 for s in doc_states.values() if s["status"] == "failed")
pending = sum(1 for s in doc_states.values() if s["status"] == "pending")
total_tokens = sum(s.get("token_count", 0) for s in doc_states.values())
progress = completed / checkpoint.total_documents if checkpoint.total_documents > 0 else 0.0
# Update checkpoint
checkpoint.checkpoint_data["doc_states"] = doc_states
checkpoint.completed_documents = completed
checkpoint.failed_documents = failed
checkpoint.pending_documents = pending
checkpoint.overall_progress = progress
checkpoint.token_count = total_tokens
checkpoint.last_checkpoint_at = datetime.now()
# Check if all documents are done
if pending == 0:
checkpoint.status = "completed"
checkpoint.completed_at = datetime.now()
checkpoint.save()
logging.info(f"Checkpoint {checkpoint_id}: Document {doc_id} completed ({completed}/{checkpoint.total_documents})")
return True
except Exception as e:
logging.error(f"Failed to save document completion: {e}")
return False
@classmethod
def save_document_failure(
cls,
checkpoint_id: str,
doc_id: str,
error: str
) -> bool:
"""
Save failure of a single document.
Args:
checkpoint_id: Checkpoint ID
doc_id: Document ID
error: Error message
Returns:
True if successful
"""
try:
checkpoint = cls.model.get_by_id(checkpoint_id)
# Update document state
doc_states = checkpoint.checkpoint_data.get("doc_states", {})
if doc_id in doc_states:
retry_count = doc_states[doc_id].get("retry_count", 0) + 1
doc_states[doc_id] = {
"status": "failed",
"error": error,
"retry_count": retry_count,
"last_attempt": datetime.now().isoformat()
}
# Update counters
completed = sum(1 for s in doc_states.values() if s["status"] == "completed")
failed = sum(1 for s in doc_states.values() if s["status"] == "failed")
pending = sum(1 for s in doc_states.values() if s["status"] == "pending")
# Update checkpoint
checkpoint.checkpoint_data["doc_states"] = doc_states
checkpoint.completed_documents = completed
checkpoint.failed_documents = failed
checkpoint.pending_documents = pending
checkpoint.last_checkpoint_at = datetime.now()
checkpoint.save()
logging.warning(f"Checkpoint {checkpoint_id}: Document {doc_id} failed: {error}")
return True
except Exception as e:
logging.error(f"Failed to save document failure: {e}")
return False
@classmethod
def get_pending_documents(cls, checkpoint_id: str) -> List[str]:
"""Get list of pending document IDs"""
try:
checkpoint = cls.model.get_by_id(checkpoint_id)
doc_states = checkpoint.checkpoint_data.get("doc_states", {})
return [doc_id for doc_id, state in doc_states.items() if state["status"] == "pending"]
except Exception as e:
logging.error(f"Failed to get pending documents: {e}")
return []
@classmethod
def get_failed_documents(cls, checkpoint_id: str) -> List[Dict[str, Any]]:
"""Get list of failed documents with details"""
try:
checkpoint = cls.model.get_by_id(checkpoint_id)
doc_states = checkpoint.checkpoint_data.get("doc_states", {})
failed = []
for doc_id, state in doc_states.items():
if state["status"] == "failed":
failed.append({
"doc_id": doc_id,
"error": state.get("error", "Unknown error"),
"retry_count": state.get("retry_count", 0),
"last_attempt": state.get("last_attempt")
})
return failed
except Exception as e:
logging.error(f"Failed to get failed documents: {e}")
return []
@classmethod
def pause_checkpoint(cls, checkpoint_id: str) -> bool:
"""Mark checkpoint as paused"""
try:
checkpoint = cls.model.get_by_id(checkpoint_id)
checkpoint.status = "paused"
checkpoint.paused_at = datetime.now()
checkpoint.save()
logging.info(f"Checkpoint {checkpoint_id} paused")
return True
except Exception as e:
logging.error(f"Failed to pause checkpoint: {e}")
return False
@classmethod
def resume_checkpoint(cls, checkpoint_id: str) -> bool:
"""Mark checkpoint as resumed"""
try:
checkpoint = cls.model.get_by_id(checkpoint_id)
checkpoint.status = "running"
checkpoint.resumed_at = datetime.now()
checkpoint.save()
logging.info(f"Checkpoint {checkpoint_id} resumed")
return True
except Exception as e:
logging.error(f"Failed to resume checkpoint: {e}")
return False
@classmethod
def cancel_checkpoint(cls, checkpoint_id: str) -> bool:
"""Mark checkpoint as cancelled"""
try:
checkpoint = cls.model.get_by_id(checkpoint_id)
checkpoint.status = "cancelled"
checkpoint.save()
logging.info(f"Checkpoint {checkpoint_id} cancelled")
return True
except Exception as e:
logging.error(f"Failed to cancel checkpoint: {e}")
return False
@classmethod
def is_paused(cls, checkpoint_id: str) -> bool:
"""Check if checkpoint is paused"""
try:
checkpoint = cls.model.get_by_id(checkpoint_id)
return checkpoint.status == "paused"
except Exception:
return False
@classmethod
def is_cancelled(cls, checkpoint_id: str) -> bool:
"""Check if checkpoint is cancelled"""
try:
checkpoint = cls.model.get_by_id(checkpoint_id)
return checkpoint.status == "cancelled"
except Exception:
return False
@classmethod
def should_retry(cls, checkpoint_id: str, doc_id: str, max_retries: int = 3) -> bool:
"""Check if document should be retried"""
try:
checkpoint = cls.model.get_by_id(checkpoint_id)
doc_states = checkpoint.checkpoint_data.get("doc_states", {})
if doc_id in doc_states:
retry_count = doc_states[doc_id].get("retry_count", 0)
return retry_count < max_retries
return False
except Exception:
return False
@classmethod
def reset_document_for_retry(cls, checkpoint_id: str, doc_id: str) -> bool:
"""Reset a failed document to pending for retry"""
try:
checkpoint = cls.model.get_by_id(checkpoint_id)
doc_states = checkpoint.checkpoint_data.get("doc_states", {})
if doc_id in doc_states and doc_states[doc_id]["status"] == "failed":
retry_count = doc_states[doc_id].get("retry_count", 0)
doc_states[doc_id] = {
"status": "pending",
"token_count": 0,
"chunks": 0,
"retry_count": retry_count # Keep retry count
}
# Update counters
failed = sum(1 for s in doc_states.values() if s["status"] == "failed")
pending = sum(1 for s in doc_states.values() if s["status"] == "pending")
checkpoint.checkpoint_data["doc_states"] = doc_states
checkpoint.failed_documents = failed
checkpoint.pending_documents = pending
checkpoint.save()
logging.info(f"Reset document {doc_id} for retry (attempt {retry_count + 1})")
return True
return False
except Exception as e:
logging.error(f"Failed to reset document for retry: {e}")
return False
@classmethod
def get_checkpoint_status(cls, checkpoint_id: str) -> Optional[Dict[str, Any]]:
"""Get detailed checkpoint status"""
try:
checkpoint = cls.model.get_by_id(checkpoint_id)
return {
"checkpoint_id": checkpoint.id,
"task_id": checkpoint.task_id,
"task_type": checkpoint.task_type,
"status": checkpoint.status,
"progress": checkpoint.overall_progress,
"total_documents": checkpoint.total_documents,
"completed_documents": checkpoint.completed_documents,
"failed_documents": checkpoint.failed_documents,
"pending_documents": checkpoint.pending_documents,
"token_count": checkpoint.token_count,
"started_at": checkpoint.started_at.isoformat() if checkpoint.started_at else None,
"paused_at": checkpoint.paused_at.isoformat() if checkpoint.paused_at else None,
"resumed_at": checkpoint.resumed_at.isoformat() if checkpoint.resumed_at else None,
"completed_at": checkpoint.completed_at.isoformat() if checkpoint.completed_at else None,
"last_checkpoint_at": checkpoint.last_checkpoint_at.isoformat() if checkpoint.last_checkpoint_at else None,
"error_message": checkpoint.error_message
}
except Exception as e:
logging.error(f"Failed to get checkpoint status: {e}")
return None

View file

@ -331,6 +331,7 @@ class RaptorConfig(Base):
threshold: Annotated[float, Field(default=0.1, ge=0.0, le=1.0)]
max_cluster: Annotated[int, Field(default=64, ge=1, le=1024)]
random_seed: Annotated[int, Field(default=0, ge=0)]
use_checkpoints: Annotated[bool, Field(default=True, description="Enable checkpoint/resume for fault tolerance")]
auto_disable_for_structured_data: Annotated[bool, Field(default=True)]

View file

@ -0,0 +1,325 @@
#!/usr/bin/env python3
#
# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""
Complete working example demonstrating checkpoint/resume functionality.
This example shows:
1. Creating a checkpoint for a RAPTOR task
2. Processing documents with progress tracking
3. Simulating a crash and resume
4. Handling failures with retry logic
5. Pausing and resuming tasks
Run this example:
python examples/checkpoint_resume_demo.py
"""
import sys
import time
import random
# Add parent directory to path for imports
sys.path.insert(0, '/root/ragflow')
from api.db.services.checkpoint_service import CheckpointService
from api.db.db_models import DB
def print_section(title: str):
"""Print a section header"""
print(f"\n{'='*60}")
print(f" {title}")
print(f"{'='*60}\n")
def print_status(checkpoint_id: str):
"""Print current checkpoint status"""
status = CheckpointService.get_checkpoint_status(checkpoint_id)
if status:
print(f"Status: {status['status']}")
print(f"Progress: {status['progress']*100:.1f}%")
print(f"Completed: {status['completed_documents']}/{status['total_documents']}")
print(f"Failed: {status['failed_documents']}")
print(f"Pending: {status['pending_documents']}")
print(f"Tokens: {status['token_count']:,}")
def simulate_document_processing(doc_id: str, should_fail: bool = False) -> tuple:
"""
Simulate processing a single document.
Returns:
(success, token_count, chunks, error)
"""
print(f" Processing {doc_id}...", end=" ", flush=True)
time.sleep(0.5) # Simulate processing time
if should_fail:
print("❌ FAILED")
return (False, 0, 0, "Simulated API timeout")
# Simulate successful processing
token_count = random.randint(1000, 3000)
chunks = random.randint(30, 90)
print(f"✓ Done ({token_count} tokens, {chunks} chunks)")
return (True, token_count, chunks, None)
def example_1_basic_checkpoint():
"""Example 1: Basic checkpoint creation and completion"""
print_section("Example 1: Basic Checkpoint Creation")
# Create checkpoint for 5 documents
doc_ids = [f"doc_{i}" for i in range(1, 6)]
print(f"Creating checkpoint for {len(doc_ids)} documents...")
checkpoint = CheckpointService.create_checkpoint(
task_id="demo_task_001",
task_type="raptor",
doc_ids=doc_ids,
config={"max_cluster": 64, "threshold": 0.5}
)
print(f"✓ Checkpoint created: {checkpoint.id}\n")
print_status(checkpoint.id)
# Process all documents
print("\nProcessing documents:")
for doc_id in doc_ids:
success, tokens, chunks, error = simulate_document_processing(doc_id)
if success:
CheckpointService.save_document_completion(
checkpoint.id,
doc_id,
token_count=tokens,
chunks=chunks
)
print("\n✓ All documents processed!")
print_status(checkpoint.id)
return checkpoint.id
def example_2_crash_and_resume():
"""Example 2: Simulating crash and resume"""
print_section("Example 2: Crash and Resume")
# Create checkpoint for 10 documents
doc_ids = [f"doc_{i}" for i in range(1, 11)]
print(f"Creating checkpoint for {len(doc_ids)} documents...")
checkpoint = CheckpointService.create_checkpoint(
task_id="demo_task_002",
task_type="raptor",
doc_ids=doc_ids,
config={}
)
print(f"✓ Checkpoint created: {checkpoint.id}\n")
# Process first 4 documents
print("Processing first batch (4 documents):")
for doc_id in doc_ids[:4]:
success, tokens, chunks, error = simulate_document_processing(doc_id)
CheckpointService.save_document_completion(
checkpoint.id, doc_id, tokens, chunks
)
print("\n💥 CRASH! System went down...\n")
time.sleep(1)
# Simulate restart - retrieve checkpoint
print("🔄 System restarted. Resuming from checkpoint...")
resumed_checkpoint = CheckpointService.get_by_task_id("demo_task_002")
if resumed_checkpoint:
print(f"✓ Found checkpoint: {resumed_checkpoint.id}")
print_status(resumed_checkpoint.id)
# Get pending documents (should skip completed ones)
pending = CheckpointService.get_pending_documents(resumed_checkpoint.id)
print(f"\n📋 Resuming with {len(pending)} pending documents:")
print(f" {', '.join(pending)}\n")
# Continue processing remaining documents
print("Processing remaining documents:")
for doc_id in pending:
success, tokens, chunks, error = simulate_document_processing(doc_id)
CheckpointService.save_document_completion(
resumed_checkpoint.id, doc_id, tokens, chunks
)
print("\n✓ All documents completed after resume!")
print_status(resumed_checkpoint.id)
return checkpoint.id
def example_3_failure_and_retry():
"""Example 3: Handling failures with retry logic"""
print_section("Example 3: Failure Handling and Retry")
# Create checkpoint
doc_ids = [f"doc_{i}" for i in range(1, 6)]
checkpoint = CheckpointService.create_checkpoint(
task_id="demo_task_003",
task_type="raptor",
doc_ids=doc_ids,
config={}
)
print(f"Checkpoint created: {checkpoint.id}\n")
# Process documents with one failure
print("Processing documents (doc_3 will fail):")
for doc_id in doc_ids:
should_fail = (doc_id == "doc_3")
success, tokens, chunks, error = simulate_document_processing(doc_id, should_fail)
if success:
CheckpointService.save_document_completion(
checkpoint.id, doc_id, tokens, chunks
)
else:
CheckpointService.save_document_failure(
checkpoint.id, doc_id, error
)
print("\n📊 Current status:")
print_status(checkpoint.id)
# Check failed documents
failed = CheckpointService.get_failed_documents(checkpoint.id)
print(f"\n❌ Failed documents: {len(failed)}")
for fail in failed:
print(f" - {fail['doc_id']}: {fail['error']} (retry #{fail['retry_count']})")
# Retry failed documents
print("\n🔄 Retrying failed documents...")
for fail in failed:
doc_id = fail['doc_id']
if CheckpointService.should_retry(checkpoint.id, doc_id, max_retries=3):
print(f" Retrying {doc_id}...")
CheckpointService.reset_document_for_retry(checkpoint.id, doc_id)
# Retry (this time it succeeds)
success, tokens, chunks, error = simulate_document_processing(doc_id, should_fail=False)
CheckpointService.save_document_completion(
checkpoint.id, doc_id, tokens, chunks
)
print("\n✓ All documents completed after retry!")
print_status(checkpoint.id)
return checkpoint.id
def example_4_pause_and_resume():
"""Example 4: Pausing and resuming a task"""
print_section("Example 4: Pause and Resume")
# Create checkpoint
doc_ids = [f"doc_{i}" for i in range(1, 8)]
checkpoint = CheckpointService.create_checkpoint(
task_id="demo_task_004",
task_type="raptor",
doc_ids=doc_ids,
config={}
)
print(f"Checkpoint created: {checkpoint.id}\n")
# Process first 3 documents
print("Processing first 3 documents:")
for doc_id in doc_ids[:3]:
success, tokens, chunks, error = simulate_document_processing(doc_id)
CheckpointService.save_document_completion(
checkpoint.id, doc_id, tokens, chunks
)
# Pause
print("\n⏸️ Pausing task...")
CheckpointService.pause_checkpoint(checkpoint.id)
print(f" Is paused: {CheckpointService.is_paused(checkpoint.id)}")
print_status(checkpoint.id)
time.sleep(1)
# Resume
print("\n▶️ Resuming task...")
CheckpointService.resume_checkpoint(checkpoint.id)
print(f" Is paused: {CheckpointService.is_paused(checkpoint.id)}")
# Continue processing
pending = CheckpointService.get_pending_documents(checkpoint.id)
print(f"\n📋 Continuing with {len(pending)} pending documents:")
for doc_id in pending:
success, tokens, chunks, error = simulate_document_processing(doc_id)
CheckpointService.save_document_completion(
checkpoint.id, doc_id, tokens, chunks
)
print("\n✓ Task completed!")
print_status(checkpoint.id)
return checkpoint.id
def main():
"""Run all examples"""
print("\n" + "="*60)
print(" RAGFlow Checkpoint/Resume Demo")
print(" Demonstrating task checkpoint and resume functionality")
print("="*60)
try:
# Initialize database connection
print("\n🔌 Connecting to database...")
DB.connect(reuse_if_open=True)
print("✓ Database connected\n")
# Run examples
example_1_basic_checkpoint()
example_2_crash_and_resume()
example_3_failure_and_retry()
example_4_pause_and_resume()
print_section("Demo Complete!")
print("✓ All examples completed successfully")
print("\nKey features demonstrated:")
print(" 1. ✓ Checkpoint creation and tracking")
print(" 2. ✓ Crash recovery and resume")
print(" 3. ✓ Failure handling with retry logic")
print(" 4. ✓ Pause and resume functionality")
print(" 5. ✓ Progress tracking and status reporting")
except Exception as e:
print(f"\n❌ Error: {e}")
import traceback
traceback.print_exc()
finally:
DB.close()
if __name__ == "__main__":
main()

View file

@ -700,6 +700,124 @@ async def run_dataflow(task: dict):
PipelineOperationLogService.create(document_id=doc_id, pipeline_id=dataflow_id, task_type=PipelineTaskType.PARSE, dsl=str(pipeline))
async def run_raptor_with_checkpoint(task, row, kb_parser_config, chat_mdl, embd_mdl, vector_size, callback=None, doc_ids=[]):
"""
Checkpoint-aware RAPTOR execution that processes documents individually.
This wrapper enables:
- Per-document checkpointing
- Pause/resume capability
- Failure isolation
- Automatic retry
"""
# Lazy import to avoid initialization issues
from api.db.services.checkpoint_service import CheckpointService
task_id = task["id"]
raptor_config = kb_parser_config.get("raptor", {})
# Create or load checkpoint
checkpoint = CheckpointService.get_by_task_id(task_id)
if not checkpoint:
checkpoint = CheckpointService.create_checkpoint(
task_id=task_id,
task_type="raptor",
doc_ids=doc_ids,
config=raptor_config
)
logging.info(f"Created new checkpoint for RAPTOR task {task_id}")
else:
logging.info(f"Resuming RAPTOR task {task_id} from checkpoint {checkpoint.id}")
# Get pending documents (skip already completed ones)
pending_docs = CheckpointService.get_pending_documents(checkpoint.id)
total_docs = len(doc_ids)
if not pending_docs:
logging.info(f"All documents already processed for task {task_id}")
callback(prog=1.0, msg="All documents completed")
return
logging.info(f"Processing {len(pending_docs)}/{total_docs} pending documents")
# Process each document individually
all_results = []
total_tokens = 0
for idx, doc_id in enumerate(pending_docs):
# Check for pause/cancel
if CheckpointService.is_paused(checkpoint.id):
logging.info(f"Task {task_id} paused at document {doc_id}")
callback(prog=0.0, msg="Task paused")
return
if CheckpointService.is_cancelled(checkpoint.id):
logging.info(f"Task {task_id} cancelled at document {doc_id}")
callback(prog=0.0, msg="Task cancelled")
return
try:
# Process single document
logging.info(f"Processing document {doc_id} ({idx+1}/{len(pending_docs)})")
# Call original RAPTOR function for single document
results, token_count = await run_raptor_for_kb(
row, kb_parser_config, chat_mdl, embd_mdl, vector_size,
callback=None, # Don't use callback for individual docs
doc_ids=[doc_id]
)
# Save results
all_results.extend(results)
total_tokens += token_count
# Save checkpoint
CheckpointService.save_document_completion(
checkpoint.id,
doc_id,
token_count=token_count,
chunks=len(results)
)
# Update progress
completed = total_docs - len(pending_docs) + idx + 1
progress = completed / total_docs
callback(prog=progress, msg=f"Completed {completed}/{total_docs} documents")
logging.info(f"Document {doc_id} completed: {len(results)} chunks, {token_count} tokens")
except Exception as e:
error_msg = str(e)
logging.error(f"Failed to process document {doc_id}: {error_msg}")
# Save failure
CheckpointService.save_document_failure(
checkpoint.id,
doc_id,
error=error_msg
)
# Check if we should retry
if CheckpointService.should_retry(checkpoint.id, doc_id, max_retries=3):
logging.info(f"Document {doc_id} will be retried later")
else:
logging.warning(f"Document {doc_id} exceeded max retries, skipping")
# Continue with other documents (fault tolerance)
continue
# Final status
failed_docs = CheckpointService.get_failed_documents(checkpoint.id)
if failed_docs:
logging.warning(f"Task {task_id} completed with {len(failed_docs)} failed documents")
callback(prog=1.0, msg=f"Completed with {len(failed_docs)} failures")
else:
logging.info(f"Task {task_id} completed successfully")
callback(prog=1.0, msg="All documents completed successfully")
return all_results, total_tokens
@timeout(3600)
async def run_raptor_for_kb(row, kb_parser_config, chat_mdl, embd_mdl, vector_size, callback=None, doc_ids=[]):
fake_doc_id = GRAPH_RAPTOR_FAKE_DOC_ID
@ -934,17 +1052,35 @@ async def do_handle_task(task):
# bind LLM for raptor
chat_model = LLMBundle(task_tenant_id, LLMType.CHAT, llm_name=task_llm_id, lang=task_language)
# run RAPTOR
# Check if checkpointing is enabled (default: True for RAPTOR)
use_checkpoints = kb_parser_config.get("raptor", {}).get("use_checkpoints", True)
# run RAPTOR with or without checkpoints
async with kg_limiter:
chunks, token_count = await run_raptor_for_kb(
row=task,
kb_parser_config=kb_parser_config,
chat_mdl=chat_model,
embd_mdl=embedding_model,
vector_size=vector_size,
callback=progress_callback,
doc_ids=task.get("doc_ids", []),
)
if use_checkpoints:
# Use checkpoint-aware version for fault tolerance
chunks, token_count = await run_raptor_with_checkpoint(
task=task,
row=task,
kb_parser_config=kb_parser_config,
chat_mdl=chat_model,
embd_mdl=embedding_model,
vector_size=vector_size,
callback=progress_callback,
doc_ids=task.get("doc_ids", []),
)
else:
# Use original version (legacy mode)
chunks, token_count = await run_raptor_for_kb(
row=task,
kb_parser_config=kb_parser_config,
chat_mdl=chat_model,
embd_mdl=embedding_model,
vector_size=vector_size,
callback=progress_callback,
doc_ids=task.get("doc_ids", []),
)
if fake_doc_ids := task.get("doc_ids", []):
task_doc_id = fake_doc_ids[0] # use the first document ID to represent this task for logging purposes
# Either using graphrag or Standard chunking methods

View file

@ -0,0 +1,260 @@
#
# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""
Integration tests for CheckpointService with real database operations.
These tests use the actual CheckpointService implementation and database,
unlike the unit tests which use mocks.
"""
import pytest
from api.db.services.checkpoint_service import CheckpointService
class TestCheckpointServiceIntegration:
"""Integration tests for CheckpointService"""
@pytest.fixture(autouse=True)
def setup_and_teardown(self):
"""Setup and cleanup for each test"""
# Setup: ensure clean state
yield
# Teardown: clean up test data
# Note: In production, you'd clean up test checkpoints here
def test_create_and_retrieve_checkpoint(self):
"""Test creating a checkpoint and retrieving it"""
# Create checkpoint
checkpoint = CheckpointService.create_checkpoint(
task_id="test_task_001",
task_type="raptor",
doc_ids=["doc1", "doc2", "doc3"],
config={"max_cluster": 64}
)
# Verify creation
assert checkpoint is not None
assert checkpoint.task_id == "test_task_001"
assert checkpoint.task_type == "raptor"
assert checkpoint.total_documents == 3
assert checkpoint.status == "pending"
# Retrieve by task_id
retrieved = CheckpointService.get_by_task_id("test_task_001")
assert retrieved is not None
assert retrieved.id == checkpoint.id
assert retrieved.task_id == "test_task_001"
def test_document_completion_workflow(self):
"""Test marking documents as completed"""
# Create checkpoint
checkpoint = CheckpointService.create_checkpoint(
task_id="test_task_002",
task_type="raptor",
doc_ids=["doc1", "doc2", "doc3"],
config={}
)
# Initially all pending
pending = CheckpointService.get_pending_documents(checkpoint.id)
assert len(pending) == 3
# Complete first document
success = CheckpointService.save_document_completion(
checkpoint.id,
"doc1",
token_count=1500,
chunks=45
)
assert success is True
# Check pending reduced
pending = CheckpointService.get_pending_documents(checkpoint.id)
assert len(pending) == 2
assert "doc1" not in pending
# Complete second document
CheckpointService.save_document_completion(
checkpoint.id,
"doc2",
token_count=2000,
chunks=60
)
# Check status
status = CheckpointService.get_checkpoint_status(checkpoint.id)
assert status["completed_documents"] == 2
assert status["pending_documents"] == 1
assert status["token_count"] == 3500 # 1500 + 2000
def test_document_failure_and_retry(self):
"""Test marking documents as failed and retry logic"""
# Create checkpoint
checkpoint = CheckpointService.create_checkpoint(
task_id="test_task_003",
task_type="raptor",
doc_ids=["doc1", "doc2"],
config={}
)
# Fail first document
success = CheckpointService.save_document_failure(
checkpoint.id,
"doc1",
error="API timeout after 60s"
)
assert success is True
# Check failed documents
failed = CheckpointService.get_failed_documents(checkpoint.id)
assert len(failed) == 1
assert failed[0]["doc_id"] == "doc1"
assert "timeout" in failed[0]["error"].lower()
# Should be able to retry (first failure)
can_retry = CheckpointService.should_retry(checkpoint.id, "doc1", max_retries=3)
assert can_retry is True
# Reset for retry
reset_success = CheckpointService.reset_document_for_retry(checkpoint.id, "doc1")
assert reset_success is True
# Should be back in pending
pending = CheckpointService.get_pending_documents(checkpoint.id)
assert "doc1" in pending
def test_max_retries_exceeded(self):
"""Test that documents can't be retried indefinitely"""
checkpoint = CheckpointService.create_checkpoint(
task_id="test_task_004",
task_type="raptor",
doc_ids=["doc1"],
config={}
)
# Fail 3 times
for i in range(3):
CheckpointService.save_document_failure(
checkpoint.id,
"doc1",
error=f"Attempt {i+1} failed"
)
if i < 2: # Reset for retry except last time
CheckpointService.reset_document_for_retry(checkpoint.id, "doc1")
# Should not be able to retry after 3 failures
can_retry = CheckpointService.should_retry(checkpoint.id, "doc1", max_retries=3)
assert can_retry is False
def test_pause_and_resume(self):
"""Test pausing and resuming a checkpoint"""
checkpoint = CheckpointService.create_checkpoint(
task_id="test_task_005",
task_type="raptor",
doc_ids=["doc1", "doc2"],
config={}
)
# Initially not paused
assert CheckpointService.is_paused(checkpoint.id) is False
# Pause
success = CheckpointService.pause_checkpoint(checkpoint.id)
assert success is True
assert CheckpointService.is_paused(checkpoint.id) is True
# Resume
success = CheckpointService.resume_checkpoint(checkpoint.id)
assert success is True
assert CheckpointService.is_paused(checkpoint.id) is False
def test_cancel_checkpoint(self):
"""Test cancelling a checkpoint"""
checkpoint = CheckpointService.create_checkpoint(
task_id="test_task_006",
task_type="raptor",
doc_ids=["doc1"],
config={}
)
# Cancel
success = CheckpointService.cancel_checkpoint(checkpoint.id)
assert success is True
assert CheckpointService.is_cancelled(checkpoint.id) is True
def test_progress_calculation(self):
"""Test that progress is calculated correctly"""
checkpoint = CheckpointService.create_checkpoint(
task_id="test_task_007",
task_type="raptor",
doc_ids=["doc1", "doc2", "doc3", "doc4", "doc5"],
config={}
)
# Complete 3 out of 5
for doc_id in ["doc1", "doc2", "doc3"]:
CheckpointService.save_document_completion(
checkpoint.id,
doc_id,
token_count=1000,
chunks=30
)
# Check progress
status = CheckpointService.get_checkpoint_status(checkpoint.id)
assert status["total_documents"] == 5
assert status["completed_documents"] == 3
assert status["pending_documents"] == 2
assert status["progress"] == 0.6 # 3/5
def test_resume_from_checkpoint(self):
"""Test resuming a task from checkpoint (real-world scenario)"""
# Simulate: Task starts, processes 2 docs, then crashes
checkpoint = CheckpointService.create_checkpoint(
task_id="test_task_008",
task_type="raptor",
doc_ids=["doc1", "doc2", "doc3", "doc4", "doc5"],
config={}
)
# Process first 2 documents
CheckpointService.save_document_completion(checkpoint.id, "doc1", 1000, 30)
CheckpointService.save_document_completion(checkpoint.id, "doc2", 1500, 45)
# Simulate crash and restart - retrieve checkpoint
resumed_checkpoint = CheckpointService.get_by_task_id("test_task_008")
assert resumed_checkpoint is not None
# Get pending documents (should skip completed ones)
pending = CheckpointService.get_pending_documents(resumed_checkpoint.id)
assert len(pending) == 3
assert "doc1" not in pending
assert "doc2" not in pending
assert set(pending) == {"doc3", "doc4", "doc5"}
# Continue processing remaining documents
CheckpointService.save_document_completion(resumed_checkpoint.id, "doc3", 1200, 38)
# Verify state
status = CheckpointService.get_checkpoint_status(resumed_checkpoint.id)
assert status["completed_documents"] == 3
assert status["pending_documents"] == 2
assert status["token_count"] == 3700 # 1000 + 1500 + 1200
if __name__ == "__main__":
pytest.main([__file__, "-v", "-s"])

View file

@ -0,0 +1,470 @@
#
# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""
Unit tests for Checkpoint Service
These are UNIT tests that use mocks to test the interface and logic flow
without requiring a database connection. This makes them fast and isolated.
For INTEGRATION tests that test the actual CheckpointService implementation
with a real database, see: test/integration_test/services/test_checkpoint_service_integration.py
Tests cover:
- Checkpoint creation and retrieval
- Document state management
- Pause/resume/cancel operations
- Retry logic
- Progress tracking
"""
import pytest
from unittest.mock import Mock
class TestCheckpointCreation:
"""Tests for checkpoint creation"""
@pytest.fixture
def mock_checkpoint_service(self):
"""Mock CheckpointService - using Mock directly for unit tests"""
mock = Mock()
return mock
def test_create_checkpoint_basic(self, mock_checkpoint_service):
"""Test basic checkpoint creation"""
# Mock create_checkpoint
mock_checkpoint = Mock()
mock_checkpoint.id = "checkpoint_123"
mock_checkpoint.task_id = "task_456"
mock_checkpoint.task_type = "raptor"
mock_checkpoint.total_documents = 10
mock_checkpoint.pending_documents = 10
mock_checkpoint.completed_documents = 0
mock_checkpoint.failed_documents = 0
mock_checkpoint_service.create_checkpoint.return_value = mock_checkpoint
# Create checkpoint
result = mock_checkpoint_service.create_checkpoint(
task_id="task_456",
task_type="raptor",
doc_ids=["doc1", "doc2", "doc3", "doc4", "doc5",
"doc6", "doc7", "doc8", "doc9", "doc10"],
config={"max_cluster": 64}
)
# Verify
assert result.id == "checkpoint_123"
assert result.task_id == "task_456"
assert result.total_documents == 10
assert result.pending_documents == 10
assert result.completed_documents == 0
def test_create_checkpoint_initializes_doc_states(self, mock_checkpoint_service):
"""Test that checkpoint initializes all document states"""
mock_checkpoint = Mock()
mock_checkpoint.checkpoint_data = {
"doc_states": {
"doc1": {"status": "pending", "token_count": 0, "chunks": 0, "retry_count": 0},
"doc2": {"status": "pending", "token_count": 0, "chunks": 0, "retry_count": 0},
"doc3": {"status": "pending", "token_count": 0, "chunks": 0, "retry_count": 0}
}
}
mock_checkpoint_service.create_checkpoint.return_value = mock_checkpoint
result = mock_checkpoint_service.create_checkpoint(
task_id="task_123",
task_type="raptor",
doc_ids=["doc1", "doc2", "doc3"],
config={}
)
# All docs should be pending
doc_states = result.checkpoint_data["doc_states"]
assert len(doc_states) == 3
assert all(state["status"] == "pending" for state in doc_states.values())
class TestDocumentStateManagement:
"""Tests for document state tracking"""
@pytest.fixture
def mock_checkpoint_service(self):
mock = Mock()
return mock
def test_save_document_completion(self, mock_checkpoint_service):
"""Test marking document as completed"""
mock_checkpoint_service.save_document_completion.return_value = True
success = mock_checkpoint_service.save_document_completion(
checkpoint_id="checkpoint_123",
doc_id="doc1",
token_count=1500,
chunks=45
)
assert success is True
mock_checkpoint_service.save_document_completion.assert_called_once()
def test_save_document_failure(self, mock_checkpoint_service):
"""Test marking document as failed"""
mock_checkpoint_service.save_document_failure.return_value = True
success = mock_checkpoint_service.save_document_failure(
checkpoint_id="checkpoint_123",
doc_id="doc2",
error="API timeout after 60s"
)
assert success is True
mock_checkpoint_service.save_document_failure.assert_called_once()
def test_get_pending_documents(self, mock_checkpoint_service):
"""Test retrieving pending documents"""
mock_checkpoint_service.get_pending_documents.return_value = ["doc2", "doc3", "doc4"]
pending = mock_checkpoint_service.get_pending_documents("checkpoint_123")
assert len(pending) == 3
assert "doc2" in pending
assert "doc3" in pending
assert "doc4" in pending
def test_get_failed_documents(self, mock_checkpoint_service):
"""Test retrieving failed documents with details"""
mock_checkpoint_service.get_failed_documents.return_value = [
{
"doc_id": "doc5",
"error": "Connection timeout",
"retry_count": 2,
"last_attempt": "2025-12-03T09:00:00"
}
]
failed = mock_checkpoint_service.get_failed_documents("checkpoint_123")
assert len(failed) == 1
assert failed[0]["doc_id"] == "doc5"
assert failed[0]["retry_count"] == 2
class TestPauseResumeCancel:
"""Tests for pause/resume/cancel operations"""
@pytest.fixture
def mock_checkpoint_service(self):
mock = Mock()
return mock
def test_pause_checkpoint(self, mock_checkpoint_service):
"""Test pausing a checkpoint"""
mock_checkpoint_service.pause_checkpoint.return_value = True
success = mock_checkpoint_service.pause_checkpoint("checkpoint_123")
assert success is True
def test_resume_checkpoint(self, mock_checkpoint_service):
"""Test resuming a checkpoint"""
mock_checkpoint_service.resume_checkpoint.return_value = True
success = mock_checkpoint_service.resume_checkpoint("checkpoint_123")
assert success is True
def test_cancel_checkpoint(self, mock_checkpoint_service):
"""Test cancelling a checkpoint"""
mock_checkpoint_service.cancel_checkpoint.return_value = True
success = mock_checkpoint_service.cancel_checkpoint("checkpoint_123")
assert success is True
def test_is_paused(self, mock_checkpoint_service):
"""Test checking if checkpoint is paused"""
mock_checkpoint_service.is_paused.return_value = True
paused = mock_checkpoint_service.is_paused("checkpoint_123")
assert paused is True
def test_is_cancelled(self, mock_checkpoint_service):
"""Test checking if checkpoint is cancelled"""
mock_checkpoint_service.is_cancelled.return_value = False
cancelled = mock_checkpoint_service.is_cancelled("checkpoint_123")
assert cancelled is False
class TestRetryLogic:
"""Tests for retry logic"""
@pytest.fixture
def mock_checkpoint_service(self):
mock = Mock()
return mock
def test_should_retry_within_limit(self, mock_checkpoint_service):
"""Test should retry when under max retries"""
mock_checkpoint_service.should_retry.return_value = True
should_retry = mock_checkpoint_service.should_retry(
checkpoint_id="checkpoint_123",
doc_id="doc1",
max_retries=3
)
assert should_retry is True
def test_should_not_retry_exceeded_limit(self, mock_checkpoint_service):
"""Test should not retry when max retries exceeded"""
mock_checkpoint_service.should_retry.return_value = False
should_retry = mock_checkpoint_service.should_retry(
checkpoint_id="checkpoint_123",
doc_id="doc2",
max_retries=3
)
assert should_retry is False
def test_reset_document_for_retry(self, mock_checkpoint_service):
"""Test resetting failed document to pending"""
mock_checkpoint_service.reset_document_for_retry.return_value = True
success = mock_checkpoint_service.reset_document_for_retry(
checkpoint_id="checkpoint_123",
doc_id="doc1"
)
assert success is True
class TestProgressTracking:
"""Tests for progress tracking"""
@pytest.fixture
def mock_checkpoint_service(self):
mock = Mock()
return mock
def test_get_checkpoint_status(self, mock_checkpoint_service):
"""Test getting detailed checkpoint status"""
mock_status = {
"checkpoint_id": "checkpoint_123",
"task_id": "task_456",
"task_type": "raptor",
"status": "running",
"progress": 0.6,
"total_documents": 10,
"completed_documents": 6,
"failed_documents": 1,
"pending_documents": 3,
"token_count": 15000,
"started_at": "2025-12-03T08:00:00",
"last_checkpoint_at": "2025-12-03T09:00:00"
}
mock_checkpoint_service.get_checkpoint_status.return_value = mock_status
status = mock_checkpoint_service.get_checkpoint_status("checkpoint_123")
assert status["progress"] == 0.6
assert status["completed_documents"] == 6
assert status["failed_documents"] == 1
assert status["pending_documents"] == 3
assert status["token_count"] == 15000
def test_progress_calculation(self, mock_checkpoint_service):
"""Test progress calculation"""
# 7 completed out of 10 = 70%
mock_status = {
"total_documents": 10,
"completed_documents": 7,
"progress": 0.7
}
mock_checkpoint_service.get_checkpoint_status.return_value = mock_status
status = mock_checkpoint_service.get_checkpoint_status("checkpoint_123")
assert status["progress"] == 0.7
assert status["completed_documents"] / status["total_documents"] == 0.7
class TestIntegrationScenarios:
"""Integration test scenarios"""
@pytest.fixture
def mock_checkpoint_service(self):
mock = Mock()
return mock
def test_full_task_lifecycle(self, mock_checkpoint_service):
"""Test complete task lifecycle: create -> process -> complete"""
# Create checkpoint
mock_checkpoint = Mock()
mock_checkpoint.id = "checkpoint_123"
mock_checkpoint.total_documents = 3
mock_checkpoint_service.create_checkpoint.return_value = mock_checkpoint
mock_checkpoint_service.create_checkpoint(
task_id="task_123",
task_type="raptor",
doc_ids=["doc1", "doc2", "doc3"],
config={}
)
# Process documents
mock_checkpoint_service.save_document_completion.return_value = True
mock_checkpoint_service.save_document_completion("checkpoint_123", "doc1", 1000, 30)
mock_checkpoint_service.save_document_completion("checkpoint_123", "doc2", 1500, 45)
mock_checkpoint_service.save_document_completion("checkpoint_123", "doc3", 1200, 38)
# Verify all completed
mock_checkpoint_service.get_pending_documents.return_value = []
pending = mock_checkpoint_service.get_pending_documents("checkpoint_123")
assert len(pending) == 0
def test_task_with_failures_and_retry(self, mock_checkpoint_service):
"""Test task with failures and retry"""
# Create checkpoint
mock_checkpoint = Mock()
mock_checkpoint.id = "checkpoint_123"
mock_checkpoint_service.create_checkpoint.return_value = mock_checkpoint
mock_checkpoint_service.create_checkpoint(
task_id="task_123",
task_type="raptor",
doc_ids=["doc1", "doc2", "doc3"],
config={}
)
# Process with one failure
mock_checkpoint_service.save_document_completion.return_value = True
mock_checkpoint_service.save_document_failure.return_value = True
mock_checkpoint_service.save_document_completion("checkpoint_123", "doc1", 1000, 30)
mock_checkpoint_service.save_document_failure("checkpoint_123", "doc2", "Timeout")
mock_checkpoint_service.save_document_completion("checkpoint_123", "doc3", 1200, 38)
# Check failed documents
mock_checkpoint_service.get_failed_documents.return_value = [
{"doc_id": "doc2", "error": "Timeout", "retry_count": 1}
]
failed = mock_checkpoint_service.get_failed_documents("checkpoint_123")
assert len(failed) == 1
# Retry failed document
mock_checkpoint_service.should_retry.return_value = True
mock_checkpoint_service.reset_document_for_retry.return_value = True
if mock_checkpoint_service.should_retry("checkpoint_123", "doc2"):
mock_checkpoint_service.reset_document_for_retry("checkpoint_123", "doc2")
# Verify reset
mock_checkpoint_service.get_pending_documents.return_value = ["doc2"]
pending = mock_checkpoint_service.get_pending_documents("checkpoint_123")
assert "doc2" in pending
def test_pause_and_resume_workflow(self, mock_checkpoint_service):
"""Test pause and resume workflow"""
# Create and start processing
mock_checkpoint = Mock()
mock_checkpoint.id = "checkpoint_123"
mock_checkpoint_service.create_checkpoint.return_value = mock_checkpoint
mock_checkpoint_service.create_checkpoint(
task_id="task_123",
task_type="raptor",
doc_ids=["doc1", "doc2", "doc3", "doc4", "doc5"],
config={}
)
# Process some documents
mock_checkpoint_service.save_document_completion.return_value = True
mock_checkpoint_service.save_document_completion("checkpoint_123", "doc1", 1000, 30)
mock_checkpoint_service.save_document_completion("checkpoint_123", "doc2", 1500, 45)
# Pause
mock_checkpoint_service.pause_checkpoint.return_value = True
mock_checkpoint_service.pause_checkpoint("checkpoint_123")
# Check paused
mock_checkpoint_service.is_paused.return_value = True
assert mock_checkpoint_service.is_paused("checkpoint_123") is True
# Resume
mock_checkpoint_service.resume_checkpoint.return_value = True
mock_checkpoint_service.resume_checkpoint("checkpoint_123")
# Check pending (should have 3 remaining)
mock_checkpoint_service.get_pending_documents.return_value = ["doc3", "doc4", "doc5"]
pending = mock_checkpoint_service.get_pending_documents("checkpoint_123")
assert len(pending) == 3
class TestEdgeCases:
"""Test edge cases and error handling"""
@pytest.fixture
def mock_checkpoint_service(self):
mock = Mock()
return mock
def test_empty_document_list(self, mock_checkpoint_service):
"""Test checkpoint with empty document list"""
mock_checkpoint = Mock()
mock_checkpoint.total_documents = 0
mock_checkpoint_service.create_checkpoint.return_value = mock_checkpoint
checkpoint = mock_checkpoint_service.create_checkpoint(
task_id="task_123",
task_type="raptor",
doc_ids=[],
config={}
)
assert checkpoint.total_documents == 0
def test_nonexistent_checkpoint(self, mock_checkpoint_service):
"""Test operations on nonexistent checkpoint"""
mock_checkpoint_service.get_by_task_id.return_value = None
checkpoint = mock_checkpoint_service.get_by_task_id("nonexistent_task")
assert checkpoint is None
def test_max_retries_exceeded(self, mock_checkpoint_service):
"""Test behavior when max retries exceeded"""
# After 3 retries, should not retry
mock_checkpoint_service.should_retry.return_value = False
should_retry = mock_checkpoint_service.should_retry(
checkpoint_id="checkpoint_123",
doc_id="doc_failed",
max_retries=3
)
assert should_retry is False
if __name__ == "__main__":
pytest.main([__file__, "-v"])