diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/core/FlowExecutor.java b/liteflow-core/src/main/java/com/yomahub/liteflow/core/FlowExecutor.java index 9f247c324..1e2ff507c 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/core/FlowExecutor.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/core/FlowExecutor.java @@ -661,7 +661,10 @@ public class FlowExecutor { boolean exist = lifeCycleList.stream() .anyMatch(lifeCycle -> lifeCycle instanceof RuleCacheLifeCycle); if (!exist) { - RuleCacheLifeCycle.initIfAbsent(capacity); + boolean success = RuleCacheLifeCycle.initIfAbsent(capacity); + if (!success) { + throw new FlowExecutorNotInitException("Initialization of RuleCacheLifeCycle failed"); + } LifeCycleHolder.addLifeCycle(RuleCacheLifeCycle.getLifeCycle()); } @@ -679,7 +682,8 @@ public class FlowExecutor { int chainNum = FlowBus.getChainMap().size(); double threshold = chainNum * 0.3; if (capacity < threshold) { - LOG.warn("The rule cache capacity {} is too small, it is recommended to be greater than 30% of the number of chains", capacity); + LOG.warn("The rule cache capacity {} is too small, the current total number of chains is {}, " + +"it is recommended to be greater than 30% of the number of chains", capacity, chainNum); } } } \ No newline at end of file diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/Chain.java b/liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/Chain.java index 427478046..7060e798a 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/Chain.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/Chain.java @@ -11,6 +11,7 @@ package com.yomahub.liteflow.flow.element; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.util.BooleanUtil; import cn.hutool.core.util.IdUtil; +import cn.hutool.core.util.StrUtil; import com.yomahub.liteflow.builder.el.LiteFlowChainELBuilder; import com.yomahub.liteflow.common.ChainConstant; import com.yomahub.liteflow.enums.ExecuteableTypeEnum; @@ -108,10 +109,10 @@ public class Chain implements Executable{ // 因为在正式执行condition之前,this.conditionList有可能被其他线程置空 // 比如,该chain在规则缓存中被淘汰 List conditionListRef = this.conditionList; - // 但在编译后到拿到引用之前,this.conditionList可能已经被置空了 + // 但在编译后到拿到引用之前,this.conditionList还是有可能已经被置空了 if (CollUtil.isEmpty(conditionListRef)) { // 如果conditionListRef为空, - // 构建临时conditionList确保本次一定可以执行 + // 尝试构建临时conditionList确保本次一定可以执行 conditionListRef = buildTemporaryConditionList(); } Slot slot = DataBus.getSlot(slotIndex); @@ -248,6 +249,10 @@ public class Chain implements Executable{ // 构建临时的ConditionList private List buildTemporaryConditionList() { + if (StrUtil.isBlank(el)) { + // 无法构建 + throw new FlowSystemException("no conditionList in this chain[" + chainId + "]"); + } // 构建临时chain String tempChainId = chainId + "_temp_" + IdUtil.simpleUUID(); Chain tempChain = new Chain(tempChainId); @@ -261,10 +266,6 @@ public class Chain implements Executable{ // 移除临时chain FlowBus.removeChain(tempChainId); - if (CollUtil.isEmpty(tempConditionList)) { - throw new FlowSystemException("no conditionList in this chain[" + chainId + "]"); - } - // 打印警告,可用于排查临时chain与已有chain重名(几乎不可能发生)而将已有chain覆盖的情况 LOG.warn("The conditionList of chain[{}] is empty, " + "temporarily using chain[{}] (now removed) to build it.", chainId, tempChainId); diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/lifecycle/impl/RuleCacheLifeCycle.java b/liteflow-core/src/main/java/com/yomahub/liteflow/lifecycle/impl/RuleCacheLifeCycle.java index 67eb86bc3..52e3dca35 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/lifecycle/impl/RuleCacheLifeCycle.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/lifecycle/impl/RuleCacheLifeCycle.java @@ -48,7 +48,7 @@ public class RuleCacheLifeCycle implements PostProcessChainExecuteLifeCycle { @Override public void postProcessAfterChainExecute(String chainId, Slot slot) { - // 不在缓存中、或出于非活跃状态,但未被清理 + // 检测是否有不在缓存中、或处于非活跃状态而未被清理的 Chain if (!isActive(chainId) && !isCleaned(chainId)) { cleanChain(chainId); } @@ -155,11 +155,14 @@ public class RuleCacheLifeCycle implements PostProcessChainExecuteLifeCycle { /** * 初始化生命周期实例 * @param capacity 缓存容量 + * @return 成功 true,失败返回 false */ - public synchronized static void initIfAbsent(int capacity) { + public synchronized static boolean initIfAbsent(int capacity) { if (ObjectUtil.isNull(INSTANCE)) { INSTANCE = new RuleCacheLifeCycle(capacity); + return true; } + return false; } /**