sycommon-python-lib 0.2.7a1__py3-none-any.whl → 0.2.7a2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- sycommon/database/pg_checkpoint_service.py +18 -4
- sycommon/database/resilient_pg_saver.py +159 -0
- {sycommon_python_lib-0.2.7a1.dist-info → sycommon_python_lib-0.2.7a2.dist-info}/METADATA +1 -1
- {sycommon_python_lib-0.2.7a1.dist-info → sycommon_python_lib-0.2.7a2.dist-info}/RECORD +7 -6
- {sycommon_python_lib-0.2.7a1.dist-info → sycommon_python_lib-0.2.7a2.dist-info}/WHEEL +0 -0
- {sycommon_python_lib-0.2.7a1.dist-info → sycommon_python_lib-0.2.7a2.dist-info}/entry_points.txt +0 -0
- {sycommon_python_lib-0.2.7a1.dist-info → sycommon_python_lib-0.2.7a2.dist-info}/top_level.txt +0 -0
|
@@ -73,7 +73,6 @@ class PgCheckpointService(metaclass=SingletonMeta):
|
|
|
73
73
|
cls._config = PgConfig.from_dict(config)
|
|
74
74
|
|
|
75
75
|
from psycopg_pool import AsyncConnectionPool
|
|
76
|
-
from langgraph.checkpoint.postgres.aio import AsyncPostgresSaver
|
|
77
76
|
|
|
78
77
|
conn_string = cls._config.dsn
|
|
79
78
|
|
|
@@ -95,7 +94,14 @@ class PgCheckpointService(metaclass=SingletonMeta):
|
|
|
95
94
|
|
|
96
95
|
await cls._pool.open()
|
|
97
96
|
|
|
98
|
-
|
|
97
|
+
# 🔑 使用自愈型 ResilientPostgresSaver 包装类(而非直接 AsyncPostgresSaver)
|
|
98
|
+
# 关键:reload() 会 close 旧池并新建池,但旧 agent 持有的 checkpointer
|
|
99
|
+
# 对象不会自动更换。ResilientPostgresSaver 的 .conn 动态读取 _pool,
|
|
100
|
+
# 池被关闭时自动重建重试,避免 PoolClosed 异常透传到上层。
|
|
101
|
+
# 复用已有包装实例(isinstance 守卫):reload 时只换池,不换 checkpointer 对象。
|
|
102
|
+
from sycommon.database.resilient_pg_saver import ResilientPostgresSaver
|
|
103
|
+
if not isinstance(cls._checkpointer, ResilientPostgresSaver):
|
|
104
|
+
cls._checkpointer = ResilientPostgresSaver()
|
|
99
105
|
await cls._checkpointer.setup()
|
|
100
106
|
|
|
101
107
|
cls._initialized = True
|
|
@@ -151,8 +157,10 @@ class PgCheckpointService(metaclass=SingletonMeta):
|
|
|
151
157
|
except Exception as e:
|
|
152
158
|
logging.error(f"关闭 PgCheckpointService 连接池失败: {e}")
|
|
153
159
|
finally:
|
|
160
|
+
# 🔑 只清 _pool,保留 _checkpointer(ResilientPostgresSaver 包装对象)。
|
|
161
|
+
# reload() 重建后,包装对象的 .conn 动态读取新 _pool;
|
|
162
|
+
# 旧 agent 持有的包装引用无需更换即可继续工作。
|
|
154
163
|
cls._pool = None
|
|
155
|
-
cls._checkpointer = None
|
|
156
164
|
cls._initialized = False
|
|
157
165
|
|
|
158
166
|
@classmethod
|
|
@@ -164,6 +172,8 @@ class PgCheckpointService(metaclass=SingletonMeta):
|
|
|
164
172
|
"""
|
|
165
173
|
if not cls._initialized:
|
|
166
174
|
return
|
|
175
|
+
old_pool = cls._pool
|
|
176
|
+
old_pool_id = id(old_pool)
|
|
167
177
|
try:
|
|
168
178
|
await cls.close()
|
|
169
179
|
except Exception as e:
|
|
@@ -173,5 +183,9 @@ class PgCheckpointService(metaclass=SingletonMeta):
|
|
|
173
183
|
cls._initialized = False
|
|
174
184
|
try:
|
|
175
185
|
await cls.setup()
|
|
186
|
+
new_pool_id = id(cls._pool) if cls._pool else None
|
|
187
|
+
logging.info(
|
|
188
|
+
f"PgCheckpointService reload 完成: old_pool_id={old_pool_id} "
|
|
189
|
+
f"-> new_pool_id={new_pool_id}, initialized={cls._initialized}")
|
|
176
190
|
except Exception as e:
|
|
177
|
-
logging.error(f"PgCheckpointService reload 重建失败: {e}", exc_info=True)
|
|
191
|
+
logging.error(f"PgCheckpointService reload 重建失败: {e}", exc_info=True)
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
"""自愈型 PostgreSQL Checkpoint Saver
|
|
2
|
+
|
|
3
|
+
解决 ``psycopg_pool.PoolClosed: the pool 'pool-1' is already closed`` 问题。
|
|
4
|
+
|
|
5
|
+
根因:``PgCheckpointService.reload()`` 在 Nacos 配置热更新时会
|
|
6
|
+
``close()`` 旧连接池(``_pool=None``)再 ``setup()`` 新建一个全新的
|
|
7
|
+
``AsyncConnectionPool``。但此前创建的 agent(缓存在内存 sessions 中)已经
|
|
8
|
+
捕获了旧的 ``AsyncPostgresSaver`` 对象,其 ``.conn`` 指向已关闭的旧池。
|
|
9
|
+
LangGraph ``Pregel`` 永久持有该 checkpointer 引用,下次 ``aget_tuple`` 即
|
|
10
|
+
抛 ``PoolClosed``。
|
|
11
|
+
|
|
12
|
+
本类把"checkpointer 对象"与"连接池"解耦:对象只有一个(永久存活),
|
|
13
|
+
``.conn`` 每次调用时动态读 ``PgCheckpointService._pool``;reload 只换池、
|
|
14
|
+
不换对象。即便池处于被关闭的瞬态,热路径方法捕获 ``PoolClosed`` 后触发
|
|
15
|
+
``_ensure_pool_alive`` 重建并精确重试一次,对上层透明。
|
|
16
|
+
|
|
17
|
+
已对照 langgraph-checkpoint-postgres 3.1.0 源码验证:
|
|
18
|
+
- ``AsyncPostgresSaver.__init__``(aio.py:57)执行 ``self.conn = conn``,
|
|
19
|
+
用 property + no-op setter 吸收,构造可存活。
|
|
20
|
+
- ``self.conn`` 在 aio.py 中仅 ``_cursor``(aio.py:374)一处方法内读取,
|
|
21
|
+
所有热路径方法都只经由 ``self._cursor()`` 取池,无构造期缓存。
|
|
22
|
+
"""
|
|
23
|
+
import asyncio
|
|
24
|
+
|
|
25
|
+
from psycopg_pool import PoolClosed
|
|
26
|
+
from langgraph.checkpoint.postgres.aio import AsyncPostgresSaver
|
|
27
|
+
|
|
28
|
+
from sycommon.logging.kafka_log import SYLogger
|
|
29
|
+
|
|
30
|
+
# 自愈并发去重锁(模块级单例)。reload 跑在 Services 主 loop 上,多个并发
|
|
31
|
+
# aget_tuple 同时撞上已关闭池时,只允许一个真正执行重建,其余进锁后双重
|
|
32
|
+
# 检查发现池已恢复即直接返回。
|
|
33
|
+
_heal_lock = asyncio.Lock()
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class ResilientPostgresSaver(AsyncPostgresSaver):
|
|
37
|
+
"""跟随 ``PgCheckpointService._pool`` 的自愈型 checkpointer。
|
|
38
|
+
|
|
39
|
+
- 永久存活,跨 reload 不重建(见 ``PgCheckpointService.setup`` 的
|
|
40
|
+
``isinstance`` 守卫)。
|
|
41
|
+
- ``conn`` property 动态返回当前活跃池;池被关闭时热路径方法自动重建。
|
|
42
|
+
"""
|
|
43
|
+
|
|
44
|
+
def __init__(self) -> None:
|
|
45
|
+
# 父类 __init__ 会执行 self.conn = conn;property + no-op setter
|
|
46
|
+
# 吸收该赋值。传 conn=None/pipe=None 避开父类 "pool + pipe 不能同时
|
|
47
|
+
# 非空" 的 guard(aio.py:52)。self.lock/self.loop 由父类设置,跨
|
|
48
|
+
# reload 安全(主 loop 不变)。
|
|
49
|
+
super().__init__(conn=None, pipe=None)
|
|
50
|
+
|
|
51
|
+
# ------------------------------------------------------------------ conn
|
|
52
|
+
@property
|
|
53
|
+
def conn(self): # type: ignore[override]
|
|
54
|
+
"""动态返回当前活跃连接池。
|
|
55
|
+
|
|
56
|
+
池不存在或已关闭时 raise ``PoolClosed``,保持与底层一致的失败语义,
|
|
57
|
+
交由热路径方法的 try/except 捕获后自愈。
|
|
58
|
+
"""
|
|
59
|
+
from sycommon.database.pg_checkpoint_service import PgCheckpointService
|
|
60
|
+
pool = PgCheckpointService._pool
|
|
61
|
+
if pool is None or getattr(pool, "closed", True):
|
|
62
|
+
raise PoolClosed(
|
|
63
|
+
f"the pool {getattr(pool, 'name', 'pool-1')!r} is already closed "
|
|
64
|
+
f"(or not initialized)"
|
|
65
|
+
)
|
|
66
|
+
return pool
|
|
67
|
+
|
|
68
|
+
@conn.setter
|
|
69
|
+
def conn(self, value): # noqa: D401
|
|
70
|
+
"""no-op:吸收父类 ``__init__`` 的 ``self.conn = conn`` 赋值。
|
|
71
|
+
|
|
72
|
+
实际连接池永远通过 getter 动态从 ``PgCheckpointService`` 读取,
|
|
73
|
+
不缓存任何传入值。
|
|
74
|
+
"""
|
|
75
|
+
return
|
|
76
|
+
|
|
77
|
+
# ------------------------------------------------------------- 自愈核心
|
|
78
|
+
async def _ensure_pool_alive(self) -> None:
|
|
79
|
+
"""若连接池已关闭/未初始化,触发一次重建(并发去重)。
|
|
80
|
+
|
|
81
|
+
双重检查:进锁后再查一次池状态,避免多个并发调用重复 reload。
|
|
82
|
+
"""
|
|
83
|
+
from sycommon.database.pg_checkpoint_service import PgCheckpointService
|
|
84
|
+
|
|
85
|
+
# 锁外快查:池可用就直接返回,绝大多数请求走这条快路径
|
|
86
|
+
pool = PgCheckpointService._pool
|
|
87
|
+
if pool is not None and not getattr(pool, "closed", True):
|
|
88
|
+
return
|
|
89
|
+
|
|
90
|
+
async with _heal_lock:
|
|
91
|
+
# 锁内再次检查:可能已有并发协程完成了重建
|
|
92
|
+
pool = PgCheckpointService._pool
|
|
93
|
+
if pool is not None and not getattr(pool, "closed", True):
|
|
94
|
+
return
|
|
95
|
+
old_pool_id = id(pool)
|
|
96
|
+
SYLogger.info(
|
|
97
|
+
f"[ResilientPgSaver] pool closed, triggering self-heal "
|
|
98
|
+
f"(old_pool_id={old_pool_id})")
|
|
99
|
+
try:
|
|
100
|
+
await PgCheckpointService.reload()
|
|
101
|
+
except Exception as e:
|
|
102
|
+
SYLogger.error(
|
|
103
|
+
f"[ResilientPgSaver] self-heal reload 失败: {e}",
|
|
104
|
+
exc_info=True)
|
|
105
|
+
raise
|
|
106
|
+
new_pool = PgCheckpointService._pool
|
|
107
|
+
SYLogger.info(
|
|
108
|
+
f"[ResilientPgSaver] self-heal 完成 "
|
|
109
|
+
f"(old_pool_id={old_pool_id} -> new_pool_id={id(new_pool)})")
|
|
110
|
+
|
|
111
|
+
# ----------------------------------------------------- 热路径方法重试
|
|
112
|
+
# 统一模式:捕获 PoolClosed -> 自愈 -> 精确一次重试。
|
|
113
|
+
# PoolClosed 实际只在打开 cursor(首次从池里取连接)时出现,重试是安全的。
|
|
114
|
+
async def aget_tuple(self, config):
|
|
115
|
+
try:
|
|
116
|
+
return await super().aget_tuple(config)
|
|
117
|
+
except PoolClosed:
|
|
118
|
+
await self._ensure_pool_alive()
|
|
119
|
+
return await super().aget_tuple(config)
|
|
120
|
+
|
|
121
|
+
async def aput(self, config, checkpoint, metadata, new_versions):
|
|
122
|
+
try:
|
|
123
|
+
return await super().aput(config, checkpoint, metadata, new_versions)
|
|
124
|
+
except PoolClosed:
|
|
125
|
+
await self._ensure_pool_alive()
|
|
126
|
+
return await super().aput(config, checkpoint, metadata, new_versions)
|
|
127
|
+
|
|
128
|
+
async def aput_writes(self, config, writes, task_id, task_path=""):
|
|
129
|
+
try:
|
|
130
|
+
return await super().aput_writes(config, writes, task_id, task_path)
|
|
131
|
+
except PoolClosed:
|
|
132
|
+
await self._ensure_pool_alive()
|
|
133
|
+
return await super().aput_writes(config, writes, task_id, task_path)
|
|
134
|
+
|
|
135
|
+
async def adelete_thread(self, thread_id):
|
|
136
|
+
try:
|
|
137
|
+
return await super().adelete_thread(thread_id)
|
|
138
|
+
except PoolClosed:
|
|
139
|
+
await self._ensure_pool_alive()
|
|
140
|
+
return await super().adelete_thread(thread_id)
|
|
141
|
+
|
|
142
|
+
async def alist(self, config, *, filter=None, before=None, limit=None):
|
|
143
|
+
"""异步生成器:首条 PoolClosed 自愈后重新遍历整条流。
|
|
144
|
+
|
|
145
|
+
PoolClosed 只在 cursor 打开(首次取连接)时出现,因此重建后重头
|
|
146
|
+
遍历是安全且语义正确的契约(不会漏放或重放 checkpoint tuple)。
|
|
147
|
+
"""
|
|
148
|
+
try:
|
|
149
|
+
async for item in super().alist(
|
|
150
|
+
config, filter=filter, before=before, limit=limit
|
|
151
|
+
):
|
|
152
|
+
yield item
|
|
153
|
+
return
|
|
154
|
+
except PoolClosed:
|
|
155
|
+
await self._ensure_pool_alive()
|
|
156
|
+
async for item in super().alist(
|
|
157
|
+
config, filter=filter, before=before, limit=limit
|
|
158
|
+
):
|
|
159
|
+
yield item
|
|
@@ -181,8 +181,9 @@ sycommon/database/async_database_service.py,sha256=fL3_W-cBwyg0-TlwKEMqcX5dHWnri
|
|
|
181
181
|
sycommon/database/base_db_service.py,sha256=urdAibaCUt3hxwwKzEMBceWkIwsw3ja_WDJ1QKc8nLU,4116
|
|
182
182
|
sycommon/database/database_service.py,sha256=svkg6MBxcar6nWSepBv40mSfpZi5tVecloTDu1O1Tns,6099
|
|
183
183
|
sycommon/database/elasticsearch_service.py,sha256=BJnBcoeSDtK1a1IcCyLbzQaOMb5hCfIh0wgBRQ9XmpU,4257
|
|
184
|
-
sycommon/database/pg_checkpoint_service.py,sha256=
|
|
184
|
+
sycommon/database/pg_checkpoint_service.py,sha256=lR73iZTDJDZc7tfx4_D6vjdlJAUTDe49zAOa3QIkBsg,7342
|
|
185
185
|
sycommon/database/redis_service.py,sha256=_Sr9RRSsiMh_xPSyxKxjfDkECVf5Lii2yGBzKZRi_hE,21473
|
|
186
|
+
sycommon/database/resilient_pg_saver.py,sha256=S5hOoF7gGTa0b4-P3dM_iLy1GFesG_jEnIxv-WRwd3s,7148
|
|
186
187
|
sycommon/database/token_usage_db_service.py,sha256=wU9jSUyOafLSI_45gbfv7_kOU0nG2bmwOmEQnN2mLpU,7913
|
|
187
188
|
sycommon/health/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
188
189
|
sycommon/health/health_check.py,sha256=EhfbhspRpQiKJaxdtE-PzpKQO_ucaFKtQxIm16F5Mpk,391
|
|
@@ -300,8 +301,8 @@ sycommon/tools/syemail.py,sha256=BDFhgf7WDOQeTcjxJEQdu0dQhnHFPO_p3eI0-Ni3LhQ,561
|
|
|
300
301
|
sycommon/tools/timing.py,sha256=OiiE7P07lRoMzX9kzb8sZU9cDb0zNnqIlY5pWqHcnkY,2064
|
|
301
302
|
sycommon/xxljob/__init__.py,sha256=7eoBlQxv-B39IfRSCY2bkqdGYs1QRe1umAWd88VMEEM,86
|
|
302
303
|
sycommon/xxljob/xxljob_service.py,sha256=1yifwIBNGsCIxLnQjHKiBlbsigc_zvPH-dMTZcNxe-Q,7649
|
|
303
|
-
sycommon_python_lib-0.2.
|
|
304
|
-
sycommon_python_lib-0.2.
|
|
305
|
-
sycommon_python_lib-0.2.
|
|
306
|
-
sycommon_python_lib-0.2.
|
|
307
|
-
sycommon_python_lib-0.2.
|
|
304
|
+
sycommon_python_lib-0.2.7a2.dist-info/METADATA,sha256=x4OmFalEYfAU0nrNre6A5qapQMYp1xFCnyr50icHOdc,7960
|
|
305
|
+
sycommon_python_lib-0.2.7a2.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
|
|
306
|
+
sycommon_python_lib-0.2.7a2.dist-info/entry_points.txt,sha256=gsR4SssKxDWjRU8ggidzNcdMXDPRSKRS7UaGyNP84Qg,92
|
|
307
|
+
sycommon_python_lib-0.2.7a2.dist-info/top_level.txt,sha256=RgphKrg7nJyZ7irJqbxFr-5H2LUYTvI7ivoWZH2hcD0,29
|
|
308
|
+
sycommon_python_lib-0.2.7a2.dist-info/RECORD,,
|
|
File without changes
|
{sycommon_python_lib-0.2.7a1.dist-info → sycommon_python_lib-0.2.7a2.dist-info}/entry_points.txt
RENAMED
|
File without changes
|
{sycommon_python_lib-0.2.7a1.dist-info → sycommon_python_lib-0.2.7a2.dist-info}/top_level.txt
RENAMED
|
File without changes
|