fix(vprop): persistVProp 改增量写,消除每次 recall 9530 次 Redis 往返(p50 192ms→4.2ms)
根因:persistVProp() 全量遍历 decisions(7592)+memVValues(1938),每条一次 HSET, 且在全局写锁内 ⇒ 单次 recall 9530 次往返≈133ms,随运行时长线性恶化。 修法:只写本次 decision + 本次引用的 memVValues(语义等价,历史条目已在 Redis)。
This commit is contained in:
@@ -72,18 +72,25 @@ func (vp *VPropagator) loadVPropFromRedis() {
|
||||
}
|
||||
}
|
||||
|
||||
func (vp *VPropagator) persistVProp() {
|
||||
func (vp *VPropagator) persistVProp(d *VDecision, memIDs []string) {
|
||||
if vp.redisClient == nil {
|
||||
return
|
||||
}
|
||||
for id, d := range vp.decisions {
|
||||
data, _ := json.Marshal(d)
|
||||
vp.redisClient.HSet(vpropRedisKey, id, string(data))
|
||||
// 增量持久化:只写本次变更的条目。
|
||||
// 此前是全量遍历 decisions(7592 条)+memVValues(1938 条)、每条一次 HSET
|
||||
// ⇒ 单次 recall 9530 次 Redis 往返 ≈133ms,且随运行时长线性恶化(p50 4ms→192ms)。
|
||||
// 历史条目早已在 Redis(LoadFromRedis 全量载入 + 每条写入时都增量落过),故增量写语义等价。
|
||||
if d != nil {
|
||||
if data, err := json.Marshal(d); err == nil {
|
||||
vp.redisClient.HSet(vpropRedisKey, d.ID, string(data))
|
||||
}
|
||||
}
|
||||
for _, memID := range memIDs {
|
||||
if v, ok := vp.memVValues[memID]; ok {
|
||||
vp.redisClient.HSet(vpropValsRedisKey, memID, strconv.FormatFloat(v, 'f', 4, 64))
|
||||
}
|
||||
}
|
||||
vp.redisClient.Expire(vpropRedisKey, 90*24*time.Hour)
|
||||
for memID, v := range vp.memVValues {
|
||||
vp.redisClient.HSet(vpropValsRedisKey, memID, strconv.FormatFloat(v, 'f', 4, 64))
|
||||
}
|
||||
vp.redisClient.Expire(vpropValsRedisKey, 90*24*time.Hour)
|
||||
}
|
||||
|
||||
@@ -135,7 +142,7 @@ func (vp *VPropagator) RecordDecision(id string, memoryIDs []string, action, out
|
||||
}
|
||||
}
|
||||
|
||||
vp.persistVProp()
|
||||
vp.persistVProp(d, memoryIDs)
|
||||
return d
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user