package service import ( "context" "encoding/json" "fmt" "sync" "time" "github.com/rulego/gflow-engine/dao" "github.com/rulego/gflow-engine/model" "github.com/rulego/gflow-engine/query" "github.com/rulego/rulego" "github.com/rulego/rulego/api/types" "github.com/sirupsen/logrus" ) // runtimeServiceRegistry 已创建的 RuntimeServiceImpl 注册表。enginePool(已装载的 // 规则链)挂在每个服务实例上,失效时须逐个对账;池缓存按 processID 键且不回读 DB // (见 GetExecution 快路径),Update 就地改 definition_json 后若不重载,本副本会 // 继续用旧链推进实例。 var runtimeServiceRegistry sync.Map // *RuntimeServiceImpl -> struct{} // loadedChain 一条已装载链及其所在池:对账需要引擎(热重载)和池(别名核对、 // 硬驱逐回退)两个对象。 type loadedChain struct { pool types.RuleEnginePool engine types.RuleEngine } // enginePoolInvalidateHook 跨副本失效广播钩子(多副本部署由 gflow 注入:PUBLISH 到 // redis),语义与 forkGraph 的同名机制一致(见 fork_aware_resume.go)。 var ( enginePoolHookMu sync.RWMutex enginePoolInvalidateHook func(processID string) ) // SetEnginePoolInvalidateHook 注入跨副本 enginePool 失效广播钩子(gflow 启动时调)。 func SetEnginePoolInvalidateHook(h func(processID string)) { enginePoolHookMu.Lock() defer enginePoolHookMu.Unlock() enginePoolInvalidateHook = h } func getEnginePoolHook() func(processID string) { enginePoolHookMu.RLock() defer enginePoolHookMu.RUnlock() return enginePoolInvalidateHook } // InvalidateExecutionCache 对所有引擎池(含各租户池)中 processID 的已装载链做 // 失效对账,并触发跨副本失效广播。ProcessService.Update / Delete(就地改 // definition_json、processID 不变)调用;Deploy 走新版本新 processID,不命中旧缓存。 // 对账口径见 reconcileLoadedChain:热重载为主,硬驱逐仅限定义已删除。 func InvalidateExecutionCache(processID string) { if processID == "" { return } invalidateExecutionCacheLocal(processID) if hook := getEnginePoolHook(); hook != nil { hook(processID) } } // ApplyRemoteExecutionInvalidate 仅对账本地(不广播),供跨副本订阅方收到远程失效 // 消息时调用,避免广播循环。 func ApplyRemoteExecutionInvalidate(processID string) { if processID == "" { return } invalidateExecutionCacheLocal(processID) } func invalidateExecutionCacheLocal(processID string) { runtimeServiceRegistry.Range(func(k, _ any) bool { if s, ok := k.(*RuntimeServiceImpl); ok && s != nil { s.reconcileLoadedChain(processID) } return true }) } // reconcileLoadedChain 单副本对账一条已装载链。定义仍在时 ReloadSelf 原地热重载: // 只换链不 Stop 引擎,在途驱动不受影响(旧实现 pool.Del 内部会 Stop,超过优雅期 // 就取消引擎 ctx,驱动中的实例滞留 active)。定义已删除时退回 pool.Del 硬驱逐, // Delete 已挡运行实例,此时无在途驱动。定义读取失败保留旧链,等下轮兜底巡检重试。 func (s *RuntimeServiceImpl) reconcileLoadedChain(processID string) { chains := s.loadedChains(processID) if len(chains) == 0 { return // 未装载:懒加载路径本就从库取最新定义,无需处理 } if s.processDAO == nil { // DAO 未装配无法对账,退回硬驱逐(GetExecution 自愈会按需重装载) evictLoadedChains(processID, chains) return } // 对账读限时:本函数同步跑在 Update/Delete 请求处理、广播订阅与巡检 goroutine // 里,DB 挂死不应无限拖住 HTTP 请求;超时按读取失败处理(保留旧链等下轮兜底) ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() processDef, err := s.processDAO.Get(ctx, processID) if err != nil { logrus.WithError(err).WithField("processId", processID). Warn("chain reconcile: read process def failed, keep loaded chain") return } if processDef == nil { evictLoadedChains(processID, chains) return } def := normalizeChainDef(processDef.DefinitionJSON) for _, lc := range chains { if err := lc.engine.ReloadSelf([]byte(def)); err != nil { logrus.WithError(err).WithField("processId", processID). Warn("chain reconcile: reload loaded chain failed, keep old chain") continue } ensurePoolAlias(processID, lc, def) } } // loadedChains 收集持有 processID 的池与引擎(默认池 + 各租户池)。同一引擎只在 // 一个池里,无需去重。Get 主键 miss 会按别名解析,此处要求命中的引擎 Id 与 // processID 一致,防止把他链的同名别名引擎误当本链重载。 func (s *RuntimeServiceImpl) loadedChains(processID string) []loadedChain { var out []loadedChain if e, ok := s.enginePool.Get(processID); ok && e.Id() == processID { out = append(out, loadedChain{pool: s.enginePool, engine: e}) } s.enginePools.Range(func(_, v any) bool { if p, ok := v.(types.RuleEnginePool); ok { if e, ok := p.Get(processID); ok && e.Id() == processID { out = append(out, loadedChain{pool: p, engine: e}) } } return true }) return out } // evictLoadedChains 硬驱逐(pool.Del 内部会 Stop 引擎)。仅用于定义已删除/DAO // 未装配两种无在途驱动的场景。 func evictLoadedChains(processID string, chains []loadedChain) { for _, lc := range chains { lc.pool.Del(processID) } } // ensurePoolAlias 核对热重载后的池别名。别名只在 Pool.New 时登记,ReloadSelf 不 // 重新登记:定义改了 ruleChain.id 后新别名寻址不到引擎,此时退回硬驱逐,让下一次 // GetExecution 走 Pool.New 重新登记。改 ruleChain.id 必经 ProcessService.Update, // Update 已挡运行实例,此回退不涉及在途驱动。 func ensurePoolAlias(processID string, lc loadedChain, def string) { alias := extractRuleChainID(def) if alias == "" || alias == processID { return } if e, ok := lc.pool.Get(alias); ok && e == lc.engine { return // 别名已指向本引擎 } logrus.WithFields(logrus.Fields{ "processId": processID, "newAlias": alias, }).Info("chain reconcile: ruleChain.id changed, evict for alias re-registration") lc.pool.Del(processID) } // normalizeChainDef 装载期兜底归一(补 end 节点 / 补 Default 兜底边),内存态不回写 // DB。initExecution 与热重载共用,两条装载路径产出一致。 func normalizeChainDef(def string) string { defCopy := &model.WfProcess{DefinitionJSON: def} defCopy.EnsureEndNode() defCopy.EnsureSwitchDefaultEdges() return defCopy.DefinitionJSON } // RecentlyUpdatedProcessIDs 返回 updated_at 晚于 since 的流程定义 ID,供宿主做 // enginePool/forkGraph 失效的兜底巡检:失效广播是 pub/sub,副本断连窗口内错过 // 的消息不补发,宿主周期性按时间窗扫近期变更逐条对账(对账是原地热重载, // 不 Stop 引擎,见 reconcileLoadedChain)。 // 经注册表取服务实例的 DAO 查询;生产环境各实例共享同一数据库,任取一个即可, // 查询失败换下一个注册实例重试(容忍实例间瞬时差异)。 func RecentlyUpdatedProcessIDs(ctx context.Context, since time.Time) ([]string, error) { var ids []string var lastErr error runtimeServiceRegistry.Range(func(k, _ any) bool { s, ok := k.(*RuntimeServiceImpl) if !ok || s == nil { return true } db := s.processDAO.Underlying().WfProcess.UnderlyingDB().WithContext(ctx) if err := db.Model(&model.WfProcess{}).Where("updated_at > ?", since).Pluck("id", &ids).Error; err != nil { lastErr = err return true } lastErr = nil return false }) if lastErr != nil { return nil, fmt.Errorf("recently updated process ids: %w", lastErr) } return ids, nil } // GetExecution 根据ID获取执行实例(租户感知:先查 processID→tenant 缓存定位租户池)。 func (s *RuntimeServiceImpl) GetExecution(ctx context.Context, processID string) (types.RuleEngine, error) { // 快速路径:缓存命中(processID→tenant)+ 池命中 → 免 DB 查询 if tenantID, ok := s.processTenants.Load(processID); ok { if engine, ok := s.poolFor(tenantID.(string)).Get(processID); ok { return engine, nil } } // 慢路径:DB 查 processDef(拿 tenant + def 用于自愈注册) processDef, err := s.processDAO.Get(ctx, processID) if err != nil { return nil, fmt.Errorf("failed to get process definition: %w", err) } if processDef == nil { return nil, fmt.Errorf("process definition not found: %s", processID) } s.processTenants.Store(processDef.ID, processDef.TenantID) // 回填缓存 pool := s.poolFor(processDef.TenantID) if engine, ok := pool.Get(processID); ok { return engine, nil } return s.initExecution(processDef.TenantID, processDef.ID, processDef.DefinitionJSON) } // PreloadChain 预加载流程定义的规则链到【租户专属】enginePool,不启动实例。 // 用于让 subProcess 子链在 Deploy 时即进池(父流程用 ruleChain.id 别名 TellFlow 可命中)。 // 幂等:enginePool.New 对已存在的 id 直接返回,故重复调用安全。 func (s *RuntimeServiceImpl) PreloadChain(tenantID, processID, processDef string) error { if processID == "" || processDef == "" { return nil } _, err := s.initExecution(tenantID, processID, processDef) // 注册 subProcess 目标:(tenant|ruleChain.id) -> processID,供 subProcess 节点解析子流程定义。 if tenantID != "" { if rcID := extractRuleChainID(processDef); rcID != "" { s.subProcessTargets.Store(tenantID+"|"+rcID, processID) } } return err } // extractRuleChainID 从流程定义 JSON 解析 ruleChain.id(subProcess targetId 寻址用)。 func extractRuleChainID(def string) string { var rc struct { RuleChain struct { ID string `json:"id"` } `json:"ruleChain"` } if json.Unmarshal([]byte(def), &rc) == nil { return rc.RuleChain.ID } return "" } // poolFor 取(或惰性创建)租户专属引擎池。tenantID 为空时回退到默认 enginePool。 // 每租户独立池 → 别名(ruleChain.id)天然租户内隔离,杜绝跨租户撞。 func (s *RuntimeServiceImpl) poolFor(tenantID string) types.RuleEnginePool { if tenantID == "" { return s.enginePool } if v, ok := s.enginePools.Load(tenantID); ok { return v.(types.RuleEnginePool) } actual, _ := s.enginePools.LoadOrStore(tenantID, rulego.NewRuleGo()) return actual.(types.RuleEnginePool) } // EvictStaleChain 驱逐过期版本链:若 processID 版本【非最新版】且【无 active 实例】, // 从租户池移除其链+别名。池中只保留「最新版」或「有活实例的旧版」。 // best-effort:查询失败/并发竞争都不影响正确性——GetExecution 自愈会按需重注册。 // 只能在事务外调用;事务内(如 TerminateInTx)须改用 evictStaleChain 并传入事务 q, // 走全局连接会与当前事务互等(SQLite 等单写锁数据库)且读不到未提交的删除结果。 func (s *RuntimeServiceImpl) EvictStaleChain(ctx context.Context, tenantID, processID string) { // best-effort:processDAO 未装配时跳过,GetExecution 自愈会按需重注册 if s.processDAO == nil { return } s.evictStaleChain(ctx, s.processDAO.Underlying(), tenantID, processID) } // evictStaleChain 统一驱逐实现;q 事务外传全局 Query,事务内传事务 tx。 func (s *RuntimeServiceImpl) evictStaleChain(ctx context.Context, q *query.Query, tenantID, processID string) { if tenantID == "" || processID == "" || q == nil { return } processDAO := dao.NewProcessDAOWithQuery(q) processDef, err := processDAO.Get(ctx, processID) if err != nil || processDef == nil { return } // 是最新版则保留(新版要随时可启动/subProcess 引用) if latest, err := processDAO.GetLatestByKey(ctx, tenantID, processDef.ProcessKey); err == nil && latest != nil && latest.ID == processID { return } // 仍有 active 实例则保留(在途实例续跑需要) if n, err := dao.NewInstanceDAOWithQuery(q).CountActiveByProcessID(ctx, tenantID, processID); err == nil && n > 0 { return } s.poolFor(tenantID).Del(processID) } func (s *RuntimeServiceImpl) initExecution(tenantID, processID, processDef string) (types.RuleEngine, error) { // 缓存 processID -> tenantID(不可变),供 GetExecution 快速路径免 DB 选池。 if tenantID != "" && processID != "" { s.processTenants.Store(processID, tenantID) } // 引擎装载期兜底(内存态,不回写 DB):部署链路(ProcessService.create)已做 // EnsureEndNode/EnsureSwitchDefaultEdges 归一,但已部署的定义不会重新走部署链 // ——缺 end 节点 / 缺 Default 兜底边的定义在此处对副本做同样处理(先补 end // 再补 Default,Default 边才能指向新 end),保证在途流程实例也能正常装载/ // 完结/不卡死。热重载(reconcileLoadedChain)共用同一归一。 processDef = normalizeChainDef(processDef) c := rulego.NewConfig() // 注册到租户专属池:Pool.New 内部 WithRuleEnginePool(该租户池) 绑给引擎, // 引擎内 ctx.TellFlow(如 subProcess)自动在该租户池内解析别名,天然租户隔离。 return s.poolFor(tenantID).New(processID, []byte(processDef), rulego.WithConfig(c), types.WithAspects(&TaskCreator{ instanceDAO: s.instanceDAO, workflowEngine: s.workflowEngine, })) }