yangdx
commited on
Commit
·
036d85a
1
Parent(s):
9ce7e55
Add atomic data initialization lock to prevent race conditions
Browse files- lightrag/kg/json_doc_status_impl.py +12 -10
- lightrag/kg/json_kv_impl.py +20 -18
- lightrag/kg/shared_storage.py +17 -1
lightrag/kg/json_doc_status_impl.py
CHANGED
@@ -15,6 +15,7 @@ from lightrag.utils import (
|
|
15 |
from .shared_storage import (
|
16 |
get_namespace_data,
|
17 |
get_storage_lock,
|
|
|
18 |
try_initialize_namespace,
|
19 |
)
|
20 |
|
@@ -27,21 +28,22 @@ class JsonDocStatusStorage(DocStatusStorage):
|
|
27 |
def __post_init__(self):
|
28 |
working_dir = self.global_config["working_dir"]
|
29 |
self._file_name = os.path.join(working_dir, f"kv_store_{self.namespace}.json")
|
30 |
-
self._storage_lock = get_storage_lock()
|
31 |
self._data = None
|
32 |
|
33 |
async def initialize(self):
|
34 |
"""Initialize storage data"""
|
35 |
-
|
36 |
-
need_init = await try_initialize_namespace(self.namespace)
|
37 |
self._data = await get_namespace_data(self.namespace)
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
|
|
|
|
|
|
45 |
|
46 |
async def filter_keys(self, keys: set[str]) -> set[str]:
|
47 |
"""Return keys that should be processed (not in storage or not successfully processed)"""
|
|
|
15 |
from .shared_storage import (
|
16 |
get_namespace_data,
|
17 |
get_storage_lock,
|
18 |
+
get_data_init_lock,
|
19 |
try_initialize_namespace,
|
20 |
)
|
21 |
|
|
|
28 |
def __post_init__(self):
|
29 |
working_dir = self.global_config["working_dir"]
|
30 |
self._file_name = os.path.join(working_dir, f"kv_store_{self.namespace}.json")
|
|
|
31 |
self._data = None
|
32 |
|
33 |
async def initialize(self):
|
34 |
"""Initialize storage data"""
|
35 |
+
self._storage_lock = get_storage_lock()
|
|
|
36 |
self._data = await get_namespace_data(self.namespace)
|
37 |
+
async with get_data_init_lock():
|
38 |
+
# check need_init must before get_namespace_data
|
39 |
+
need_init = await try_initialize_namespace(self.namespace)
|
40 |
+
if need_init:
|
41 |
+
loaded_data = load_json(self._file_name) or {}
|
42 |
+
async with self._storage_lock:
|
43 |
+
self._data.update(loaded_data)
|
44 |
+
logger.info(
|
45 |
+
f"Process {os.getpid()} doc status load {self.namespace} with {len(loaded_data)} records"
|
46 |
+
)
|
47 |
|
48 |
async def filter_keys(self, keys: set[str]) -> set[str]:
|
49 |
"""Return keys that should be processed (not in storage or not successfully processed)"""
|
lightrag/kg/json_kv_impl.py
CHANGED
@@ -13,6 +13,7 @@ from lightrag.utils import (
|
|
13 |
from .shared_storage import (
|
14 |
get_namespace_data,
|
15 |
get_storage_lock,
|
|
|
16 |
try_initialize_namespace,
|
17 |
)
|
18 |
|
@@ -23,29 +24,30 @@ class JsonKVStorage(BaseKVStorage):
|
|
23 |
def __post_init__(self):
|
24 |
working_dir = self.global_config["working_dir"]
|
25 |
self._file_name = os.path.join(working_dir, f"kv_store_{self.namespace}.json")
|
26 |
-
self._storage_lock = get_storage_lock()
|
27 |
self._data = None
|
28 |
|
29 |
async def initialize(self):
|
30 |
"""Initialize storage data"""
|
31 |
-
|
32 |
-
need_init = await try_initialize_namespace(self.namespace)
|
33 |
self._data = await get_namespace_data(self.namespace)
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
|
|
|
|
|
|
49 |
|
50 |
async def index_done_callback(self) -> None:
|
51 |
async with self._storage_lock:
|
|
|
13 |
from .shared_storage import (
|
14 |
get_namespace_data,
|
15 |
get_storage_lock,
|
16 |
+
get_data_init_lock,
|
17 |
try_initialize_namespace,
|
18 |
)
|
19 |
|
|
|
24 |
def __post_init__(self):
|
25 |
working_dir = self.global_config["working_dir"]
|
26 |
self._file_name = os.path.join(working_dir, f"kv_store_{self.namespace}.json")
|
|
|
27 |
self._data = None
|
28 |
|
29 |
async def initialize(self):
|
30 |
"""Initialize storage data"""
|
31 |
+
self._storage_lock = get_storage_lock()
|
|
|
32 |
self._data = await get_namespace_data(self.namespace)
|
33 |
+
async with get_data_init_lock():
|
34 |
+
# check need_init must before get_namespace_data
|
35 |
+
need_init = await try_initialize_namespace(self.namespace)
|
36 |
+
if need_init:
|
37 |
+
loaded_data = load_json(self._file_name) or {}
|
38 |
+
async with self._storage_lock:
|
39 |
+
self._data.update(loaded_data)
|
40 |
+
|
41 |
+
# Calculate data count based on namespace
|
42 |
+
if self.namespace.endswith("cache"):
|
43 |
+
# For cache namespaces, sum the cache entries across all cache types
|
44 |
+
data_count = sum(len(first_level_dict) for first_level_dict in loaded_data.values()
|
45 |
+
if isinstance(first_level_dict, dict))
|
46 |
+
else:
|
47 |
+
# For non-cache namespaces, use the original count method
|
48 |
+
data_count = len(loaded_data)
|
49 |
+
|
50 |
+
logger.info(f"Process {os.getpid()} KV load {self.namespace} with {data_count} records")
|
51 |
|
52 |
async def index_done_callback(self) -> None:
|
53 |
async with self._storage_lock:
|
lightrag/kg/shared_storage.py
CHANGED
@@ -39,6 +39,7 @@ _storage_lock: Optional[LockType] = None
|
|
39 |
_internal_lock: Optional[LockType] = None
|
40 |
_pipeline_status_lock: Optional[LockType] = None
|
41 |
_graph_db_lock: Optional[LockType] = None
|
|
|
42 |
|
43 |
|
44 |
class UnifiedLock(Generic[T]):
|
@@ -188,6 +189,16 @@ def get_graph_db_lock(enable_logging: bool = False) -> UnifiedLock:
|
|
188 |
)
|
189 |
|
190 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
191 |
def initialize_share_data(workers: int = 1):
|
192 |
"""
|
193 |
Initialize shared storage data for single or multi-process mode.
|
@@ -214,6 +225,7 @@ def initialize_share_data(workers: int = 1):
|
|
214 |
_internal_lock, \
|
215 |
_pipeline_status_lock, \
|
216 |
_graph_db_lock, \
|
|
|
217 |
_shared_dicts, \
|
218 |
_init_flags, \
|
219 |
_initialized, \
|
@@ -226,15 +238,16 @@ def initialize_share_data(workers: int = 1):
|
|
226 |
)
|
227 |
return
|
228 |
|
229 |
-
_manager = Manager()
|
230 |
_workers = workers
|
231 |
|
232 |
if workers > 1:
|
233 |
is_multiprocess = True
|
|
|
234 |
_internal_lock = _manager.Lock()
|
235 |
_storage_lock = _manager.Lock()
|
236 |
_pipeline_status_lock = _manager.Lock()
|
237 |
_graph_db_lock = _manager.Lock()
|
|
|
238 |
_shared_dicts = _manager.dict()
|
239 |
_init_flags = _manager.dict()
|
240 |
_update_flags = _manager.dict()
|
@@ -247,6 +260,7 @@ def initialize_share_data(workers: int = 1):
|
|
247 |
_storage_lock = asyncio.Lock()
|
248 |
_pipeline_status_lock = asyncio.Lock()
|
249 |
_graph_db_lock = asyncio.Lock()
|
|
|
250 |
_shared_dicts = {}
|
251 |
_init_flags = {}
|
252 |
_update_flags = {}
|
@@ -415,6 +429,7 @@ def finalize_share_data():
|
|
415 |
_internal_lock, \
|
416 |
_pipeline_status_lock, \
|
417 |
_graph_db_lock, \
|
|
|
418 |
_shared_dicts, \
|
419 |
_init_flags, \
|
420 |
_initialized, \
|
@@ -481,6 +496,7 @@ def finalize_share_data():
|
|
481 |
_internal_lock = None
|
482 |
_pipeline_status_lock = None
|
483 |
_graph_db_lock = None
|
|
|
484 |
_update_flags = None
|
485 |
|
486 |
direct_log(f"Process {os.getpid()} storage data finalization complete")
|
|
|
39 |
_internal_lock: Optional[LockType] = None
|
40 |
_pipeline_status_lock: Optional[LockType] = None
|
41 |
_graph_db_lock: Optional[LockType] = None
|
42 |
+
_data_init_lock: Optional[LockType] = None
|
43 |
|
44 |
|
45 |
class UnifiedLock(Generic[T]):
|
|
|
189 |
)
|
190 |
|
191 |
|
192 |
+
def get_data_init_lock(enable_logging: bool = False) -> UnifiedLock:
|
193 |
+
"""return unified data initialization lock for ensuring atomic data initialization"""
|
194 |
+
return UnifiedLock(
|
195 |
+
lock=_data_init_lock,
|
196 |
+
is_async=not is_multiprocess,
|
197 |
+
name="data_init_lock",
|
198 |
+
enable_logging=enable_logging,
|
199 |
+
)
|
200 |
+
|
201 |
+
|
202 |
def initialize_share_data(workers: int = 1):
|
203 |
"""
|
204 |
Initialize shared storage data for single or multi-process mode.
|
|
|
225 |
_internal_lock, \
|
226 |
_pipeline_status_lock, \
|
227 |
_graph_db_lock, \
|
228 |
+
_data_init_lock, \
|
229 |
_shared_dicts, \
|
230 |
_init_flags, \
|
231 |
_initialized, \
|
|
|
238 |
)
|
239 |
return
|
240 |
|
|
|
241 |
_workers = workers
|
242 |
|
243 |
if workers > 1:
|
244 |
is_multiprocess = True
|
245 |
+
_manager = Manager()
|
246 |
_internal_lock = _manager.Lock()
|
247 |
_storage_lock = _manager.Lock()
|
248 |
_pipeline_status_lock = _manager.Lock()
|
249 |
_graph_db_lock = _manager.Lock()
|
250 |
+
_data_init_lock = _manager.Lock()
|
251 |
_shared_dicts = _manager.dict()
|
252 |
_init_flags = _manager.dict()
|
253 |
_update_flags = _manager.dict()
|
|
|
260 |
_storage_lock = asyncio.Lock()
|
261 |
_pipeline_status_lock = asyncio.Lock()
|
262 |
_graph_db_lock = asyncio.Lock()
|
263 |
+
_data_init_lock = asyncio.Lock()
|
264 |
_shared_dicts = {}
|
265 |
_init_flags = {}
|
266 |
_update_flags = {}
|
|
|
429 |
_internal_lock, \
|
430 |
_pipeline_status_lock, \
|
431 |
_graph_db_lock, \
|
432 |
+
_data_init_lock, \
|
433 |
_shared_dicts, \
|
434 |
_init_flags, \
|
435 |
_initialized, \
|
|
|
496 |
_internal_lock = None
|
497 |
_pipeline_status_lock = None
|
498 |
_graph_db_lock = None
|
499 |
+
_data_init_lock = None
|
500 |
_update_flags = None
|
501 |
|
502 |
direct_log(f"Process {os.getpid()} storage data finalization complete")
|