feat: Ensure thread safety for graph write operations
Add a lock to delete, adelete_by_entity, and adelete_by_relation methods to prevent race conditions and ensure data consistency during concurrent modifications to the knowledge graph.
This commit is contained in:
parent
c947b20bb1
commit
9fae0eadff
1 changed files with 120 additions and 109 deletions
|
|
@ -35,6 +35,7 @@ from lightrag.kg import (
|
|||
from lightrag.kg.shared_storage import (
|
||||
get_namespace_data,
|
||||
get_pipeline_status_lock,
|
||||
get_graph_db_lock,
|
||||
)
|
||||
|
||||
from .base import (
|
||||
|
|
@ -1728,6 +1729,10 @@ class LightRAG:
|
|||
relationships_to_delete = set()
|
||||
relationships_to_rebuild = {} # (src, tgt) -> remaining_chunk_ids
|
||||
|
||||
# Use graph database lock to ensure atomic merges and updates
|
||||
graph_db_lock = get_graph_db_lock(enable_logging=False)
|
||||
async with graph_db_lock:
|
||||
|
||||
# Process entities
|
||||
all_labels = await self.chunk_entity_relation_graph.get_all_labels()
|
||||
for node_label in all_labels:
|
||||
|
|
@ -1857,6 +1862,9 @@ class LightRAG:
|
|||
"""
|
||||
from .utils_graph import adelete_by_entity
|
||||
|
||||
# Use graph database lock to ensure atomic merges and updates
|
||||
graph_db_lock = get_graph_db_lock(enable_logging=False)
|
||||
async with graph_db_lock:
|
||||
return await adelete_by_entity(
|
||||
self.chunk_entity_relation_graph,
|
||||
self.entities_vdb,
|
||||
|
|
@ -1877,6 +1885,9 @@ class LightRAG:
|
|||
"""
|
||||
from .utils_graph import adelete_by_relation
|
||||
|
||||
# Use graph database lock to ensure atomic merges and updates
|
||||
graph_db_lock = get_graph_db_lock(enable_logging=False)
|
||||
async with graph_db_lock:
|
||||
return await adelete_by_relation(
|
||||
self.chunk_entity_relation_graph,
|
||||
self.relationships_vdb,
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue