File size: 48,867 Bytes
167596f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 | import os
import sys
import asyncio
import multiprocessing as mp
from multiprocessing.synchronize import Lock as ProcessLock
from multiprocessing import Manager
import time
import logging
from typing import Any, Dict, List, Optional, Union, TypeVar, Generic
from lightrag.exceptions import PipelineNotInitializedError
# Define a direct print function for critical logs that must be visible in all processes
def direct_log(message, enable_output: bool = False, level: str = "DEBUG"):
"""
Log a message directly to stderr to ensure visibility in all processes,
including the Gunicorn master process.
Args:
message: The message to log
level: Log level (default: "DEBUG")
enable_output: Whether to actually output the log (default: True)
"""
if not enable_output:
return
# Get the current logger level from the lightrag logger
try:
from lightrag.utils import logger
current_level = logger.getEffectiveLevel()
except ImportError:
# Fallback if lightrag.utils is not available
current_level = 20 # INFO
# Convert string level to numeric level for comparison
level_mapping = {
"DEBUG": 10, # DEBUG
"INFO": 20, # INFO
"WARNING": 30, # WARNING
"ERROR": 40, # ERROR
"CRITICAL": 50, # CRITICAL
}
message_level = level_mapping.get(level.upper(), logging.DEBUG)
# print(f"Diret_log: {level.upper()} {message_level} ? {current_level}", file=sys.stderr, flush=True)
if message_level >= current_level:
print(f"{level}: {message}", file=sys.stderr, flush=True)
T = TypeVar("T")
LockType = Union[ProcessLock, asyncio.Lock]
_is_multiprocess = None
_workers = None
_manager = None
# Global singleton data for multi-process keyed locks
_lock_registry: Optional[Dict[str, mp.synchronize.Lock]] = None
_lock_registry_count: Optional[Dict[str, int]] = None
_lock_cleanup_data: Optional[Dict[str, time.time]] = None
_registry_guard = None
# Timeout for keyed locks in seconds (Default 300)
CLEANUP_KEYED_LOCKS_AFTER_SECONDS = 300
# Cleanup pending list threshold for triggering cleanup (Default 500)
CLEANUP_THRESHOLD = 500
# Minimum interval between cleanup operations in seconds (Default 30)
MIN_CLEANUP_INTERVAL_SECONDS = 30
# Track the earliest cleanup time for efficient cleanup triggering (multiprocess locks only)
_earliest_mp_cleanup_time: Optional[float] = None
# Track the last cleanup time to enforce minimum interval (multiprocess locks only)
_last_mp_cleanup_time: Optional[float] = None
_initialized = None
# shared data for storage across processes
_shared_dicts: Optional[Dict[str, Any]] = None
_init_flags: Optional[Dict[str, bool]] = None # namespace -> initialized
_update_flags: Optional[Dict[str, bool]] = None # namespace -> updated
# locks for mutex access
_storage_lock: Optional[LockType] = None
_internal_lock: Optional[LockType] = None
_pipeline_status_lock: Optional[LockType] = None
_graph_db_lock: Optional[LockType] = None
_data_init_lock: Optional[LockType] = None
# Manager for all keyed locks
_storage_keyed_lock: Optional["KeyedUnifiedLock"] = None
# async locks for coroutine synchronization in multiprocess mode
_async_locks: Optional[Dict[str, asyncio.Lock]] = None
DEBUG_LOCKS = False
_debug_n_locks_acquired: int = 0
def inc_debug_n_locks_acquired():
global _debug_n_locks_acquired
if DEBUG_LOCKS:
_debug_n_locks_acquired += 1
print(f"DEBUG: Keyed Lock acquired, total: {_debug_n_locks_acquired:>5}")
def dec_debug_n_locks_acquired():
global _debug_n_locks_acquired
if DEBUG_LOCKS:
if _debug_n_locks_acquired > 0:
_debug_n_locks_acquired -= 1
print(f"DEBUG: Keyed Lock released, total: {_debug_n_locks_acquired:>5}")
else:
raise RuntimeError("Attempting to release lock when no locks are acquired")
def get_debug_n_locks_acquired():
global _debug_n_locks_acquired
return _debug_n_locks_acquired
class UnifiedLock(Generic[T]):
"""Provide a unified lock interface type for asyncio.Lock and multiprocessing.Lock"""
def __init__(
self,
lock: Union[ProcessLock, asyncio.Lock],
is_async: bool,
name: str = "unnamed",
enable_logging: bool = True,
async_lock: Optional[asyncio.Lock] = None,
):
self._lock = lock
self._is_async = is_async
self._pid = os.getpid() # for debug only
self._name = name # for debug only
self._enable_logging = enable_logging # for debug only
self._async_lock = async_lock # auxiliary lock for coroutine synchronization
async def __aenter__(self) -> "UnifiedLock[T]":
try:
# If in multiprocess mode and async lock exists, acquire it first
if not self._is_async and self._async_lock is not None:
await self._async_lock.acquire()
direct_log(
f"== Lock == Process {self._pid}: Async lock for '{self._name}' acquired",
enable_output=self._enable_logging,
)
# Then acquire the main lock
if self._is_async:
await self._lock.acquire()
else:
self._lock.acquire()
direct_log(
f"== Lock == Process {self._pid}: Lock '{self._name}' acquired (async={self._is_async})",
enable_output=self._enable_logging,
)
return self
except Exception as e:
# If main lock acquisition fails, release the async lock if it was acquired
if (
not self._is_async
and self._async_lock is not None
and self._async_lock.locked()
):
self._async_lock.release()
direct_log(
f"== Lock == Process {self._pid}: Failed to acquire lock '{self._name}': {e}",
level="ERROR",
enable_output=self._enable_logging,
)
raise
async def __aexit__(self, exc_type, exc_val, exc_tb):
main_lock_released = False
try:
# Release main lock first
if self._is_async:
self._lock.release()
else:
self._lock.release()
main_lock_released = True
direct_log(
f"== Lock == Process {self._pid}: Lock '{self._name}' released (async={self._is_async})",
enable_output=self._enable_logging,
)
# Then release async lock if in multiprocess mode
if not self._is_async and self._async_lock is not None:
self._async_lock.release()
direct_log(
f"== Lock == Process {self._pid}: Async lock '{self._name}' released",
enable_output=self._enable_logging,
)
except Exception as e:
direct_log(
f"== Lock == Process {self._pid}: Failed to release lock '{self._name}': {e}",
level="ERROR",
enable_output=self._enable_logging,
)
# If main lock release failed but async lock hasn't been released, try to release it
if (
not main_lock_released
and not self._is_async
and self._async_lock is not None
):
try:
direct_log(
f"== Lock == Process {self._pid}: Attempting to release async lock after main lock failure",
level="WARNING",
enable_output=self._enable_logging,
)
self._async_lock.release()
direct_log(
f"== Lock == Process {self._pid}: Successfully released async lock after main lock failure",
enable_output=self._enable_logging,
)
except Exception as inner_e:
direct_log(
f"== Lock == Process {self._pid}: Failed to release async lock after main lock failure: {inner_e}",
level="ERROR",
enable_output=self._enable_logging,
)
raise
def __enter__(self) -> "UnifiedLock[T]":
"""For backward compatibility"""
try:
if self._is_async:
raise RuntimeError("Use 'async with' for shared_storage lock")
direct_log(
f"== Lock == Process {self._pid}: Acquiring lock '{self._name}' (sync)",
enable_output=self._enable_logging,
)
self._lock.acquire()
direct_log(
f"== Lock == Process {self._pid}: Lock '{self._name}' acquired (sync)",
enable_output=self._enable_logging,
)
return self
except Exception as e:
direct_log(
f"== Lock == Process {self._pid}: Failed to acquire lock '{self._name}' (sync): {e}",
level="ERROR",
enable_output=self._enable_logging,
)
raise
def __exit__(self, exc_type, exc_val, exc_tb):
"""For backward compatibility"""
try:
if self._is_async:
raise RuntimeError("Use 'async with' for shared_storage lock")
direct_log(
f"== Lock == Process {self._pid}: Releasing lock '{self._name}' (sync)",
enable_output=self._enable_logging,
)
self._lock.release()
direct_log(
f"== Lock == Process {self._pid}: Lock '{self._name}' released (sync)",
enable_output=self._enable_logging,
)
except Exception as e:
direct_log(
f"== Lock == Process {self._pid}: Failed to release lock '{self._name}' (sync): {e}",
level="ERROR",
enable_output=self._enable_logging,
)
raise
def locked(self) -> bool:
if self._is_async:
return self._lock.locked()
else:
return self._lock.locked()
def _get_combined_key(factory_name: str, key: str) -> str:
"""Return the combined key for the factory and key."""
return f"{factory_name}:{key}"
def _perform_lock_cleanup(
lock_type: str,
cleanup_data: Dict[str, float],
lock_registry: Optional[Dict[str, Any]],
lock_count: Optional[Dict[str, int]],
earliest_cleanup_time: Optional[float],
last_cleanup_time: Optional[float],
current_time: float,
threshold_check: bool = True,
) -> tuple[int, Optional[float], Optional[float]]:
"""
Generic lock cleanup function to unify cleanup logic for both multiprocess and async locks.
Args:
lock_type: Lock type identifier ("mp" or "async")
cleanup_data: Cleanup data dictionary
lock_registry: Lock registry dictionary (can be None for async locks)
lock_count: Lock count dictionary (can be None for async locks)
earliest_cleanup_time: Earliest cleanup time
last_cleanup_time: Last cleanup time
current_time: Current time
threshold_check: Whether to check threshold condition (default True, set to False in cleanup_expired_locks)
Returns:
tuple: (cleaned_count, new_earliest_time, new_last_cleanup_time)
"""
if len(cleanup_data) == 0:
return 0, earliest_cleanup_time, last_cleanup_time
# If threshold check is needed and threshold not reached, return directly
if threshold_check and len(cleanup_data) < CLEANUP_THRESHOLD:
return 0, earliest_cleanup_time, last_cleanup_time
# Time rollback detection
if last_cleanup_time is not None and current_time < last_cleanup_time:
direct_log(
f"== {lock_type} Lock == Time rollback detected, resetting cleanup time",
level="WARNING",
enable_output=False,
)
last_cleanup_time = None
# Check cleanup conditions
has_expired_locks = (
earliest_cleanup_time is not None
and current_time - earliest_cleanup_time > CLEANUP_KEYED_LOCKS_AFTER_SECONDS
)
interval_satisfied = (
last_cleanup_time is None
or current_time - last_cleanup_time > MIN_CLEANUP_INTERVAL_SECONDS
)
if not (has_expired_locks and interval_satisfied):
return 0, earliest_cleanup_time, last_cleanup_time
try:
cleaned_count = 0
new_earliest_time = None
# Calculate total count before cleanup
total_cleanup_len = len(cleanup_data)
# Perform cleanup operation
for cleanup_key, cleanup_time in list(cleanup_data.items()):
if current_time - cleanup_time > CLEANUP_KEYED_LOCKS_AFTER_SECONDS:
# Remove from cleanup data
cleanup_data.pop(cleanup_key, None)
# Remove from lock registry if exists
if lock_registry is not None:
lock_registry.pop(cleanup_key, None)
if lock_count is not None:
lock_count.pop(cleanup_key, None)
cleaned_count += 1
else:
# Track the earliest time among remaining locks
if new_earliest_time is None or cleanup_time < new_earliest_time:
new_earliest_time = cleanup_time
# Update state only after successful cleanup
if cleaned_count > 0:
new_last_cleanup_time = current_time
# Log cleanup results
next_cleanup_in = max(
(new_earliest_time + CLEANUP_KEYED_LOCKS_AFTER_SECONDS - current_time)
if new_earliest_time
else float("inf"),
MIN_CLEANUP_INTERVAL_SECONDS,
)
if lock_type == "async":
direct_log(
f"== {lock_type} Lock == Cleaned up {cleaned_count}/{total_cleanup_len} expired {lock_type} locks, "
f"next cleanup in {next_cleanup_in:.1f}s",
enable_output=False,
level="INFO",
)
else:
direct_log(
f"== {lock_type} Lock == Cleaned up {cleaned_count}/{total_cleanup_len} expired locks, "
f"next cleanup in {next_cleanup_in:.1f}s",
enable_output=False,
level="INFO",
)
return cleaned_count, new_earliest_time, new_last_cleanup_time
else:
return 0, earliest_cleanup_time, last_cleanup_time
except Exception as e:
direct_log(
f"== {lock_type} Lock == Cleanup failed: {e}",
level="ERROR",
enable_output=False,
)
return 0, earliest_cleanup_time, last_cleanup_time
def _get_or_create_shared_raw_mp_lock(
factory_name: str, key: str
) -> Optional[mp.synchronize.Lock]:
"""Return the *singleton* manager.Lock() proxy for keyed lock, creating if needed."""
if not _is_multiprocess:
return None
with _registry_guard:
combined_key = _get_combined_key(factory_name, key)
raw = _lock_registry.get(combined_key)
count = _lock_registry_count.get(combined_key)
if raw is None:
raw = _manager.Lock()
_lock_registry[combined_key] = raw
count = 0
else:
if count is None:
raise RuntimeError(
f"Shared-Data lock registry for {factory_name} is corrupted for key {key}"
)
if (
count == 0 and combined_key in _lock_cleanup_data
): # Reusing an key waiting for cleanup, remove it from cleanup list
_lock_cleanup_data.pop(combined_key)
count += 1
_lock_registry_count[combined_key] = count
return raw
def _release_shared_raw_mp_lock(factory_name: str, key: str):
"""Release the *singleton* manager.Lock() proxy for *key*."""
if not _is_multiprocess:
return
global _earliest_mp_cleanup_time, _last_mp_cleanup_time
with _registry_guard:
combined_key = _get_combined_key(factory_name, key)
raw = _lock_registry.get(combined_key)
count = _lock_registry_count.get(combined_key)
if raw is None and count is None:
return
elif raw is None or count is None:
raise RuntimeError(
f"Shared-Data lock registry for {factory_name} is corrupted for key {key}"
)
count -= 1
if count < 0:
raise RuntimeError(
f"Attempting to release lock for {key} more times than it was acquired"
)
_lock_registry_count[combined_key] = count
current_time = time.time()
if count == 0:
_lock_cleanup_data[combined_key] = current_time
# Update earliest multiprocess cleanup time (only when earlier)
if (
_earliest_mp_cleanup_time is None
or current_time < _earliest_mp_cleanup_time
):
_earliest_mp_cleanup_time = current_time
# Use generic cleanup function
cleaned_count, new_earliest_time, new_last_cleanup_time = _perform_lock_cleanup(
lock_type="mp",
cleanup_data=_lock_cleanup_data,
lock_registry=_lock_registry,
lock_count=_lock_registry_count,
earliest_cleanup_time=_earliest_mp_cleanup_time,
last_cleanup_time=_last_mp_cleanup_time,
current_time=current_time,
threshold_check=True,
)
# Update global state if cleanup was performed
if cleaned_count > 0:
_earliest_mp_cleanup_time = new_earliest_time
_last_mp_cleanup_time = new_last_cleanup_time
class KeyedUnifiedLock:
"""
Manager for unified keyed locks, supporting both single and multi-process
• Keeps only a table of async keyed locks locally
• Fetches the multi-process keyed lock on every acquire
• Builds a fresh `UnifiedLock` each time, so `enable_logging`
(or future options) can vary per call.
• Supports dynamic namespaces specified at lock usage time
"""
def __init__(self, *, default_enable_logging: bool = True) -> None:
self._default_enable_logging = default_enable_logging
self._async_lock: Dict[str, asyncio.Lock] = {} # local keyed locks
self._async_lock_count: Dict[
str, int
] = {} # local keyed locks referenced count
self._async_lock_cleanup_data: Dict[
str, time.time
] = {} # local keyed locks timeout
self._mp_locks: Dict[
str, mp.synchronize.Lock
] = {} # multi-process lock proxies
self._earliest_async_cleanup_time: Optional[float] = (
None # track earliest async cleanup time
)
self._last_async_cleanup_time: Optional[float] = (
None # track last async cleanup time for minimum interval
)
def __call__(
self, namespace: str, keys: list[str], *, enable_logging: Optional[bool] = None
):
"""
Ergonomic helper so you can write:
async with storage_keyed_lock("namespace", ["key1", "key2"]):
...
"""
if enable_logging is None:
enable_logging = self._default_enable_logging
return _KeyedLockContext(
self,
namespace=namespace,
keys=keys,
enable_logging=enable_logging,
)
def _get_or_create_async_lock(self, combined_key: str) -> asyncio.Lock:
async_lock = self._async_lock.get(combined_key)
count = self._async_lock_count.get(combined_key, 0)
if async_lock is None:
async_lock = asyncio.Lock()
self._async_lock[combined_key] = async_lock
elif count == 0 and combined_key in self._async_lock_cleanup_data:
self._async_lock_cleanup_data.pop(combined_key)
count += 1
self._async_lock_count[combined_key] = count
return async_lock
def _release_async_lock(self, combined_key: str):
count = self._async_lock_count.get(combined_key, 0)
count -= 1
current_time = time.time()
if count == 0:
self._async_lock_cleanup_data[combined_key] = current_time
# Update earliest async cleanup time (only when earlier)
if (
self._earliest_async_cleanup_time is None
or current_time < self._earliest_async_cleanup_time
):
self._earliest_async_cleanup_time = current_time
self._async_lock_count[combined_key] = count
# Use generic cleanup function
cleaned_count, new_earliest_time, new_last_cleanup_time = _perform_lock_cleanup(
lock_type="async",
cleanup_data=self._async_lock_cleanup_data,
lock_registry=self._async_lock,
lock_count=self._async_lock_count,
earliest_cleanup_time=self._earliest_async_cleanup_time,
last_cleanup_time=self._last_async_cleanup_time,
current_time=current_time,
threshold_check=True,
)
# Update instance state if cleanup was performed
if cleaned_count > 0:
self._earliest_async_cleanup_time = new_earliest_time
self._last_async_cleanup_time = new_last_cleanup_time
def _get_lock_for_key(
self, namespace: str, key: str, enable_logging: bool = False
) -> UnifiedLock:
# 1. Create combined key for this namespace:key combination
combined_key = _get_combined_key(namespace, key)
# 2. get (or create) the per‑process async gate for this combined key
# Is synchronous, so no need to acquire a lock
async_lock = self._get_or_create_async_lock(combined_key)
# 3. fetch the shared raw lock
raw_lock = _get_or_create_shared_raw_mp_lock(namespace, key)
is_multiprocess = raw_lock is not None
if not is_multiprocess:
raw_lock = async_lock
# 4. build a *fresh* UnifiedLock with the chosen logging flag
if is_multiprocess:
return UnifiedLock(
lock=raw_lock,
is_async=False, # manager.Lock is synchronous
name=combined_key,
enable_logging=enable_logging,
async_lock=async_lock, # prevents event‑loop blocking
)
else:
return UnifiedLock(
lock=raw_lock,
is_async=True,
name=combined_key,
enable_logging=enable_logging,
async_lock=None, # No need for async lock in single process mode
)
def _release_lock_for_key(self, namespace: str, key: str):
combined_key = _get_combined_key(namespace, key)
self._release_async_lock(combined_key)
_release_shared_raw_mp_lock(namespace, key)
def cleanup_expired_locks(self) -> Dict[str, Any]:
"""
Cleanup expired locks for both async and multiprocess locks following the same
conditions as _release_shared_raw_mp_lock and _release_async_lock functions.
Only performs cleanup when both has_expired_locks and interval_satisfied conditions are met
to avoid too frequent cleanup operations.
Since async and multiprocess locks work together, this method cleans up
both types of expired locks and returns comprehensive statistics.
Returns:
Dict containing cleanup statistics and current status:
{
"process_id": 12345,
"cleanup_performed": {
"mp_cleaned": 5,
"async_cleaned": 3
},
"current_status": {
"total_mp_locks": 10,
"pending_mp_cleanup": 2,
"total_async_locks": 8,
"pending_async_cleanup": 1
}
}
"""
global _lock_registry, _lock_registry_count, _lock_cleanup_data
global _registry_guard, _earliest_mp_cleanup_time, _last_mp_cleanup_time
cleanup_stats = {"mp_cleaned": 0, "async_cleaned": 0}
current_time = time.time()
# 1. Cleanup multiprocess locks using generic function
if (
_is_multiprocess
and _lock_registry is not None
and _registry_guard is not None
):
try:
with _registry_guard:
if _lock_cleanup_data is not None:
# Use generic cleanup function without threshold check
cleaned_count, new_earliest_time, new_last_cleanup_time = (
_perform_lock_cleanup(
lock_type="mp",
cleanup_data=_lock_cleanup_data,
lock_registry=_lock_registry,
lock_count=_lock_registry_count,
earliest_cleanup_time=_earliest_mp_cleanup_time,
last_cleanup_time=_last_mp_cleanup_time,
current_time=current_time,
threshold_check=False, # Force cleanup in cleanup_expired_locks
)
)
# Update global state if cleanup was performed
if cleaned_count > 0:
_earliest_mp_cleanup_time = new_earliest_time
_last_mp_cleanup_time = new_last_cleanup_time
cleanup_stats["mp_cleaned"] = cleaned_count
except Exception as e:
direct_log(
f"Error during multiprocess lock cleanup: {e}",
level="ERROR",
enable_output=False,
)
# 2. Cleanup async locks using generic function
try:
# Use generic cleanup function without threshold check
cleaned_count, new_earliest_time, new_last_cleanup_time = (
_perform_lock_cleanup(
lock_type="async",
cleanup_data=self._async_lock_cleanup_data,
lock_registry=self._async_lock,
lock_count=self._async_lock_count,
earliest_cleanup_time=self._earliest_async_cleanup_time,
last_cleanup_time=self._last_async_cleanup_time,
current_time=current_time,
threshold_check=False, # Force cleanup in cleanup_expired_locks
)
)
# Update instance state if cleanup was performed
if cleaned_count > 0:
self._earliest_async_cleanup_time = new_earliest_time
self._last_async_cleanup_time = new_last_cleanup_time
cleanup_stats["async_cleaned"] = cleaned_count
except Exception as e:
direct_log(
f"Error during async lock cleanup: {e}",
level="ERROR",
enable_output=False,
)
# 3. Get current status after cleanup
current_status = self.get_lock_status()
return {
"process_id": os.getpid(),
"cleanup_performed": cleanup_stats,
"current_status": current_status,
}
def get_lock_status(self) -> Dict[str, int]:
"""
Get current status of both async and multiprocess locks.
Returns comprehensive lock counts for both types of locks since
they work together in the keyed lock system.
Returns:
Dict containing lock counts:
{
"total_mp_locks": 10,
"pending_mp_cleanup": 2,
"total_async_locks": 8,
"pending_async_cleanup": 1
}
"""
global _lock_registry_count, _lock_cleanup_data, _registry_guard
status = {
"total_mp_locks": 0,
"pending_mp_cleanup": 0,
"total_async_locks": 0,
"pending_async_cleanup": 0,
}
try:
# Count multiprocess locks
if _is_multiprocess and _lock_registry_count is not None:
if _registry_guard is not None:
with _registry_guard:
status["total_mp_locks"] = len(_lock_registry_count)
if _lock_cleanup_data is not None:
status["pending_mp_cleanup"] = len(_lock_cleanup_data)
# Count async locks
status["total_async_locks"] = len(self._async_lock_count)
status["pending_async_cleanup"] = len(self._async_lock_cleanup_data)
except Exception as e:
direct_log(
f"Error getting keyed lock status: {e}",
level="ERROR",
enable_output=False,
)
return status
class _KeyedLockContext:
def __init__(
self,
parent: KeyedUnifiedLock,
namespace: str,
keys: list[str],
enable_logging: bool,
) -> None:
self._parent = parent
self._namespace = namespace
# The sorting is critical to ensure proper lock and release order
# to avoid deadlocks
self._keys = sorted(keys)
self._enable_logging = (
enable_logging
if enable_logging is not None
else parent._default_enable_logging
)
self._ul: Optional[List["UnifiedLock"]] = None # set in __aenter__
# ----- enter -----
async def __aenter__(self):
if self._ul is not None:
raise RuntimeError("KeyedUnifiedLock already acquired in current context")
# acquire locks for all keys in the namespace
self._ul = []
for key in self._keys:
lock = self._parent._get_lock_for_key(
self._namespace, key, enable_logging=self._enable_logging
)
await lock.__aenter__()
inc_debug_n_locks_acquired()
self._ul.append(lock)
return self
# ----- exit -----
async def __aexit__(self, exc_type, exc, tb):
# The UnifiedLock takes care of proper release order
for ul, key in zip(reversed(self._ul), reversed(self._keys)):
await ul.__aexit__(exc_type, exc, tb)
self._parent._release_lock_for_key(self._namespace, key)
dec_debug_n_locks_acquired()
self._ul = None
def get_internal_lock(enable_logging: bool = False) -> UnifiedLock:
"""return unified storage lock for data consistency"""
async_lock = _async_locks.get("internal_lock") if _is_multiprocess else None
return UnifiedLock(
lock=_internal_lock,
is_async=not _is_multiprocess,
name="internal_lock",
enable_logging=enable_logging,
async_lock=async_lock,
)
def get_storage_lock(enable_logging: bool = False) -> UnifiedLock:
"""return unified storage lock for data consistency"""
async_lock = _async_locks.get("storage_lock") if _is_multiprocess else None
return UnifiedLock(
lock=_storage_lock,
is_async=not _is_multiprocess,
name="storage_lock",
enable_logging=enable_logging,
async_lock=async_lock,
)
def get_pipeline_status_lock(enable_logging: bool = False) -> UnifiedLock:
"""return unified storage lock for data consistency"""
async_lock = _async_locks.get("pipeline_status_lock") if _is_multiprocess else None
return UnifiedLock(
lock=_pipeline_status_lock,
is_async=not _is_multiprocess,
name="pipeline_status_lock",
enable_logging=enable_logging,
async_lock=async_lock,
)
def get_graph_db_lock(enable_logging: bool = False) -> UnifiedLock:
"""return unified graph database lock for ensuring atomic operations"""
async_lock = _async_locks.get("graph_db_lock") if _is_multiprocess else None
return UnifiedLock(
lock=_graph_db_lock,
is_async=not _is_multiprocess,
name="graph_db_lock",
enable_logging=enable_logging,
async_lock=async_lock,
)
def get_storage_keyed_lock(
keys: str | list[str], namespace: str = "default", enable_logging: bool = False
) -> _KeyedLockContext:
"""Return unified storage keyed lock for ensuring atomic operations across different namespaces"""
global _storage_keyed_lock
if _storage_keyed_lock is None:
raise RuntimeError("Shared-Data is not initialized")
if isinstance(keys, str):
keys = [keys]
return _storage_keyed_lock(namespace, keys, enable_logging=enable_logging)
def get_data_init_lock(enable_logging: bool = False) -> UnifiedLock:
"""return unified data initialization lock for ensuring atomic data initialization"""
async_lock = _async_locks.get("data_init_lock") if _is_multiprocess else None
return UnifiedLock(
lock=_data_init_lock,
is_async=not _is_multiprocess,
name="data_init_lock",
enable_logging=enable_logging,
async_lock=async_lock,
)
def cleanup_keyed_lock() -> Dict[str, Any]:
"""
Force cleanup of expired keyed locks and return comprehensive status information.
This function actively cleans up expired locks for both async and multiprocess locks,
then returns detailed statistics about the cleanup operation and current lock status.
Returns:
Same as cleanup_expired_locks in KeyedUnifiedLock
"""
global _storage_keyed_lock
# Check if shared storage is initialized
if not _initialized or _storage_keyed_lock is None:
return {
"process_id": os.getpid(),
"cleanup_performed": {"mp_cleaned": 0, "async_cleaned": 0},
"current_status": {
"total_mp_locks": 0,
"pending_mp_cleanup": 0,
"total_async_locks": 0,
"pending_async_cleanup": 0,
},
}
return _storage_keyed_lock.cleanup_expired_locks()
def get_keyed_lock_status() -> Dict[str, Any]:
"""
Get current status of keyed locks without performing cleanup.
This function provides a read-only view of the current lock counts
for both multiprocess and async locks, including pending cleanup counts.
Returns:
Same as get_lock_status in KeyedUnifiedLock
"""
global _storage_keyed_lock
# Check if shared storage is initialized
if not _initialized or _storage_keyed_lock is None:
return {
"process_id": os.getpid(),
"total_mp_locks": 0,
"pending_mp_cleanup": 0,
"total_async_locks": 0,
"pending_async_cleanup": 0,
}
status = _storage_keyed_lock.get_lock_status()
status["process_id"] = os.getpid()
return status
def initialize_share_data(workers: int = 1):
"""
Initialize shared storage data for single or multi-process mode.
When used with Gunicorn's preload feature, this function is called once in the
master process before forking worker processes, allowing all workers to share
the same initialized data.
In single-process mode, this function is called in FASTAPI lifespan function.
The function determines whether to use cross-process shared variables for data storage
based on the number of workers. If workers=1, it uses thread locks and local dictionaries.
If workers>1, it uses process locks and shared dictionaries managed by multiprocessing.Manager.
Args:
workers (int): Number of worker processes. If 1, single-process mode is used.
If > 1, multi-process mode with shared memory is used.
"""
global \
_manager, \
_workers, \
_is_multiprocess, \
_storage_lock, \
_lock_registry, \
_lock_registry_count, \
_lock_cleanup_data, \
_registry_guard, \
_internal_lock, \
_pipeline_status_lock, \
_graph_db_lock, \
_data_init_lock, \
_shared_dicts, \
_init_flags, \
_initialized, \
_update_flags, \
_async_locks, \
_storage_keyed_lock, \
_earliest_mp_cleanup_time, \
_last_mp_cleanup_time
# Check if already initialized
if _initialized:
direct_log(
f"Process {os.getpid()} Shared-Data already initialized (multiprocess={_is_multiprocess})"
)
return
_workers = workers
if workers > 1:
_is_multiprocess = True
_manager = Manager()
_lock_registry = _manager.dict()
_lock_registry_count = _manager.dict()
_lock_cleanup_data = _manager.dict()
_registry_guard = _manager.RLock()
_internal_lock = _manager.Lock()
_storage_lock = _manager.Lock()
_pipeline_status_lock = _manager.Lock()
_graph_db_lock = _manager.Lock()
_data_init_lock = _manager.Lock()
_shared_dicts = _manager.dict()
_init_flags = _manager.dict()
_update_flags = _manager.dict()
_storage_keyed_lock = KeyedUnifiedLock()
# Initialize async locks for multiprocess mode
_async_locks = {
"internal_lock": asyncio.Lock(),
"storage_lock": asyncio.Lock(),
"pipeline_status_lock": asyncio.Lock(),
"graph_db_lock": asyncio.Lock(),
"data_init_lock": asyncio.Lock(),
}
direct_log(
f"Process {os.getpid()} Shared-Data created for Multiple Process (workers={workers})"
)
else:
_is_multiprocess = False
_internal_lock = asyncio.Lock()
_storage_lock = asyncio.Lock()
_pipeline_status_lock = asyncio.Lock()
_graph_db_lock = asyncio.Lock()
_data_init_lock = asyncio.Lock()
_shared_dicts = {}
_init_flags = {}
_update_flags = {}
_async_locks = None # No need for async locks in single process mode
_storage_keyed_lock = KeyedUnifiedLock()
direct_log(f"Process {os.getpid()} Shared-Data created for Single Process")
# Initialize multiprocess cleanup times
_earliest_mp_cleanup_time = None
_last_mp_cleanup_time = None
# Mark as initialized
_initialized = True
async def initialize_pipeline_status():
"""
Initialize pipeline namespace with default values.
This function is called during FASTAPI lifespan for each worker.
"""
pipeline_namespace = await get_namespace_data("pipeline_status", first_init=True)
async with get_internal_lock():
# Check if already initialized by checking for required fields
if "busy" in pipeline_namespace:
return
# Create a shared list object for history_messages
history_messages = _manager.list() if _is_multiprocess else []
pipeline_namespace.update(
{
"autoscanned": False, # Auto-scan started
"busy": False, # Control concurrent processes
"job_name": "-", # Current job name (indexing files/indexing texts)
"job_start": None, # Job start time
"docs": 0, # Total number of documents to be indexed
"batchs": 0, # Number of batches for processing documents
"cur_batch": 0, # Current processing batch
"request_pending": False, # Flag for pending request for processing
"latest_message": "", # Latest message from pipeline processing
"history_messages": history_messages, # 使用共享列表对象
}
)
direct_log(f"Process {os.getpid()} Pipeline namespace initialized")
async def get_update_flag(namespace: str):
"""
Create a namespace's update flag for a workers.
Returen the update flag to caller for referencing or reset.
"""
global _update_flags
if _update_flags is None:
raise ValueError("Try to create namespace before Shared-Data is initialized")
async with get_internal_lock():
if namespace not in _update_flags:
if _is_multiprocess and _manager is not None:
_update_flags[namespace] = _manager.list()
else:
_update_flags[namespace] = []
direct_log(
f"Process {os.getpid()} initialized updated flags for namespace: [{namespace}]"
)
if _is_multiprocess and _manager is not None:
new_update_flag = _manager.Value("b", False)
else:
# Create a simple mutable object to store boolean value for compatibility with mutiprocess
class MutableBoolean:
def __init__(self, initial_value=False):
self.value = initial_value
new_update_flag = MutableBoolean(False)
_update_flags[namespace].append(new_update_flag)
return new_update_flag
async def set_all_update_flags(namespace: str):
"""Set all update flag of namespace indicating all workers need to reload data from files"""
global _update_flags
if _update_flags is None:
raise ValueError("Try to create namespace before Shared-Data is initialized")
async with get_internal_lock():
if namespace not in _update_flags:
raise ValueError(f"Namespace {namespace} not found in update flags")
# Update flags for both modes
for i in range(len(_update_flags[namespace])):
_update_flags[namespace][i].value = True
async def clear_all_update_flags(namespace: str):
"""Clear all update flag of namespace indicating all workers need to reload data from files"""
global _update_flags
if _update_flags is None:
raise ValueError("Try to create namespace before Shared-Data is initialized")
async with get_internal_lock():
if namespace not in _update_flags:
raise ValueError(f"Namespace {namespace} not found in update flags")
# Update flags for both modes
for i in range(len(_update_flags[namespace])):
_update_flags[namespace][i].value = False
async def get_all_update_flags_status() -> Dict[str, list]:
"""
Get update flags status for all namespaces.
Returns:
Dict[str, list]: A dictionary mapping namespace names to lists of update flag statuses
"""
if _update_flags is None:
return {}
result = {}
async with get_internal_lock():
for namespace, flags in _update_flags.items():
worker_statuses = []
for flag in flags:
if _is_multiprocess:
worker_statuses.append(flag.value)
else:
worker_statuses.append(flag)
result[namespace] = worker_statuses
return result
async def try_initialize_namespace(namespace: str) -> bool:
"""
Returns True if the current worker(process) gets initialization permission for loading data later.
The worker does not get the permission is prohibited to load data from files.
"""
global _init_flags, _manager
if _init_flags is None:
raise ValueError("Try to create nanmespace before Shared-Data is initialized")
async with get_internal_lock():
if namespace not in _init_flags:
_init_flags[namespace] = True
direct_log(
f"Process {os.getpid()} ready to initialize storage namespace: [{namespace}]"
)
return True
direct_log(
f"Process {os.getpid()} storage namespace already initialized: [{namespace}]"
)
return False
async def get_namespace_data(
namespace: str, first_init: bool = False
) -> Dict[str, Any]:
"""get the shared data reference for specific namespace
Args:
namespace: The namespace to retrieve
allow_create: If True, allows creation of the namespace if it doesn't exist.
Used internally by initialize_pipeline_status().
"""
if _shared_dicts is None:
direct_log(
f"Error: try to getnanmespace before it is initialized, pid={os.getpid()}",
level="ERROR",
)
raise ValueError("Shared dictionaries not initialized")
async with get_internal_lock():
if namespace not in _shared_dicts:
# Special handling for pipeline_status namespace
if namespace == "pipeline_status" and not first_init:
# Check if pipeline_status should have been initialized but wasn't
# This helps users understand they need to call initialize_pipeline_status()
raise PipelineNotInitializedError(namespace)
# For other namespaces or when allow_create=True, create them dynamically
if _is_multiprocess and _manager is not None:
_shared_dicts[namespace] = _manager.dict()
else:
_shared_dicts[namespace] = {}
return _shared_dicts[namespace]
def finalize_share_data():
"""
Release shared resources and clean up.
This function should be called when the application is shutting down
to properly release shared resources and avoid memory leaks.
In multi-process mode, it shuts down the Manager and releases all shared objects.
In single-process mode, it simply resets the global variables.
"""
global \
_manager, \
_is_multiprocess, \
_storage_lock, \
_internal_lock, \
_pipeline_status_lock, \
_graph_db_lock, \
_data_init_lock, \
_shared_dicts, \
_init_flags, \
_initialized, \
_update_flags, \
_async_locks
# Check if already initialized
if not _initialized:
direct_log(
f"Process {os.getpid()} storage data not initialized, nothing to finalize"
)
return
direct_log(
f"Process {os.getpid()} finalizing storage data (multiprocess={_is_multiprocess})"
)
# In multi-process mode, shut down the Manager
if _is_multiprocess and _manager is not None:
try:
# Clear shared resources before shutting down Manager
if _shared_dicts is not None:
# Clear pipeline status history messages first if exists
try:
pipeline_status = _shared_dicts.get("pipeline_status", {})
if "history_messages" in pipeline_status:
pipeline_status["history_messages"].clear()
except Exception:
pass # Ignore any errors during history messages cleanup
_shared_dicts.clear()
if _init_flags is not None:
_init_flags.clear()
if _update_flags is not None:
# Clear each namespace's update flags list and Value objects
try:
for namespace in _update_flags:
flags_list = _update_flags[namespace]
if isinstance(flags_list, list):
# Clear Value objects in the list
for flag in flags_list:
if hasattr(
flag, "value"
): # Check if it's a Value object
flag.value = False
flags_list.clear()
except Exception:
pass # Ignore any errors during update flags cleanup
_update_flags.clear()
# Shut down the Manager - this will automatically clean up all shared resources
_manager.shutdown()
direct_log(f"Process {os.getpid()} Manager shutdown complete")
except Exception as e:
direct_log(
f"Process {os.getpid()} Error shutting down Manager: {e}", level="ERROR"
)
# Reset global variables
_manager = None
_initialized = None
_is_multiprocess = None
_shared_dicts = None
_init_flags = None
_storage_lock = None
_internal_lock = None
_pipeline_status_lock = None
_graph_db_lock = None
_data_init_lock = None
_update_flags = None
_async_locks = None
direct_log(f"Process {os.getpid()} storage data finalization complete")
|