Fix: ensure data migration is handled by single-process

- Wrap migration logic with get_data_init_lock() to ensure single-process execution
- Prevent race conditions when multiple processes start simultaneously
This commit is contained in:
yangdx 2025-08-04 01:47:20 +08:00
parent e04d8ed8a7
commit 63496698a1
2 changed files with 54 additions and 48 deletions

View file

@ -151,9 +151,11 @@ def create_app(args):
try: try:
# Initialize database connections # Initialize database connections
await rag.initialize_storages() await rag.initialize_storages()
await initialize_pipeline_status()
# Data migration regardless of storage implementation
await rag.check_and_migrate_data() await rag.check_and_migrate_data()
await initialize_pipeline_status()
pipeline_status = await get_namespace_data("pipeline_status") pipeline_status = await get_namespace_data("pipeline_status")
should_start_autoscan = False should_start_autoscan = False

View file

@ -49,6 +49,7 @@ from lightrag.kg.shared_storage import (
get_namespace_data, get_namespace_data,
get_pipeline_status_lock, get_pipeline_status_lock,
get_graph_db_lock, get_graph_db_lock,
get_data_init_lock,
) )
from .base import ( from .base import (
@ -602,13 +603,16 @@ class LightRAG:
async def check_and_migrate_data(self): async def check_and_migrate_data(self):
"""Check if data migration is needed and perform migration if necessary""" """Check if data migration is needed and perform migration if necessary"""
async with get_data_init_lock(enable_logging=True):
try: try:
# Check if migration is needed: # Check if migration is needed:
# 1. chunk_entity_relation_graph has entities and relations (count > 0) # 1. chunk_entity_relation_graph has entities and relations (count > 0)
# 2. full_entities and full_relations are empty # 2. full_entities and full_relations are empty
# Get all entity labels from graph # Get all entity labels from graph
all_entity_labels = await self.chunk_entity_relation_graph.get_all_labels() all_entity_labels = (
await self.chunk_entity_relation_graph.get_all_labels()
)
if not all_entity_labels: if not all_entity_labels:
logger.debug("No entities found in graph, skipping migration check") logger.debug("No entities found in graph, skipping migration check")