fix: 强化工作流与插件运行状态处理
- 显式传播工作流缓存故障并补充仓储测试 - 防止状态轮询重入和过期响应,规范执行记录无权限响应
This commit is contained in:
@@ -1,27 +1,75 @@
|
||||
package tech.easyflow.ai.easyagentsflow.repository;
|
||||
|
||||
import com.alicp.jetcache.Cache;
|
||||
import com.alicp.jetcache.CacheException;
|
||||
import com.alicp.jetcache.CacheGetResult;
|
||||
import com.alicp.jetcache.CacheResult;
|
||||
import com.alicp.jetcache.CacheResultCode;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 工作流运行状态缓存仓储基类。
|
||||
*/
|
||||
public class BaseRepository {
|
||||
|
||||
@Resource(name = "defaultCache")
|
||||
private Cache<String, Object> cache;
|
||||
|
||||
/**
|
||||
* chain 的相关状态缓存三天
|
||||
* 保存工作流运行状态,缓存有效期为三天。
|
||||
*
|
||||
* @param key 缓存键
|
||||
* @param value 缓存值
|
||||
* @throws CacheException 缓存写入失败时抛出
|
||||
*/
|
||||
protected void putCache(String key, Object value) {
|
||||
cache.put(key, value, 3, TimeUnit.DAYS);
|
||||
CacheResult result = cache.PUT(key, value, 3, TimeUnit.DAYS);
|
||||
if (!result.isSuccess()) {
|
||||
throw cacheOperationException("写入", key, result);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取并校验工作流运行状态缓存。
|
||||
*
|
||||
* @param key 缓存键
|
||||
* @param clazz 期望的缓存值类型
|
||||
* @param <T> 缓存值类型
|
||||
* @return 命中的缓存值;缓存不存在或已过期时返回 null
|
||||
* @throws CacheException 缓存读取失败时抛出
|
||||
* @throws ClassCastException 缓存值类型与期望类型不一致时抛出
|
||||
*/
|
||||
protected <T> T getCache(String key, Class<T> clazz) {
|
||||
Object value = cache.get(key);
|
||||
CacheGetResult<Object> result = cache.GET(key);
|
||||
CacheResultCode resultCode = result.getResultCode();
|
||||
if (resultCode == CacheResultCode.NOT_EXISTS || resultCode == CacheResultCode.EXPIRED) {
|
||||
return null;
|
||||
}
|
||||
if (!result.isSuccess()) {
|
||||
throw cacheOperationException("读取", key, result);
|
||||
}
|
||||
Object value = result.getValue();
|
||||
if (value == null) {
|
||||
return null;
|
||||
}
|
||||
return clazz.cast(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建包含缓存操作上下文的异常。
|
||||
*
|
||||
* @param operation 操作名称
|
||||
* @param key 缓存键
|
||||
* @param result JetCache 操作结果
|
||||
* @return 缓存操作异常
|
||||
*/
|
||||
private CacheException cacheOperationException(String operation, String key, CacheResult result) {
|
||||
return new CacheException(
|
||||
"工作流状态缓存" + operation + "失败,key=" + key
|
||||
+ ",resultCode=" + result.getResultCode()
|
||||
+ ",message=" + result.getMessage()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,185 @@
|
||||
package tech.easyflow.ai.easyagentsflow.repository;
|
||||
|
||||
import com.alicp.jetcache.Cache;
|
||||
import com.alicp.jetcache.CacheException;
|
||||
import com.alicp.jetcache.CacheGetResult;
|
||||
import com.alicp.jetcache.CacheResult;
|
||||
import com.alicp.jetcache.CacheResultCode;
|
||||
import com.alicp.jetcache.support.CacheEncodeException;
|
||||
import com.easyagents.flow.core.chain.ChainState;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* {@link ChainStateRepositoryImpl} 缓存异常处理回归测试。
|
||||
*/
|
||||
public class ChainStateRepositoryImplTest {
|
||||
|
||||
/**
|
||||
* 验证缓存解码失败时抛出异常且不创建空工作流状态。
|
||||
*
|
||||
* @throws Exception 缓存依赖注入失败时抛出
|
||||
*/
|
||||
@Test
|
||||
public void loadShouldFailWithoutOverwritingStateWhenCacheDecodeFails() throws Exception {
|
||||
String instanceId = "decode-failed-instance";
|
||||
CacheGetResult<Object> failure = new CacheGetResult<>(new CacheEncodeException(
|
||||
"decode error",
|
||||
new ClassNotFoundException("com.alicp.jetcache.CacheValueHolder")
|
||||
));
|
||||
RecordingCache cache = new RecordingCache(failure, CacheResult.SUCCESS_WITHOUT_MSG);
|
||||
ChainStateRepositoryImpl repository = repository(cache.asCache());
|
||||
|
||||
try {
|
||||
repository.load(instanceId);
|
||||
Assert.fail("cache decode failure should be propagated");
|
||||
} catch (CacheException expected) {
|
||||
Assert.assertTrue(expected.getMessage().contains("工作流状态缓存读取失败"));
|
||||
Assert.assertTrue(expected.getMessage().contains(instanceId));
|
||||
}
|
||||
|
||||
Assert.assertEquals(0, cache.getPutCount());
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证缓存未命中时创建并持久化新的工作流状态。
|
||||
*
|
||||
* @throws Exception 缓存依赖注入失败时抛出
|
||||
*/
|
||||
@Test
|
||||
public void loadShouldCreateStateWhenCacheDoesNotExist() throws Exception {
|
||||
String instanceId = "new-instance";
|
||||
RecordingCache cache = new RecordingCache(
|
||||
new CacheGetResult<>(CacheResultCode.NOT_EXISTS, null, null),
|
||||
CacheResult.SUCCESS_WITHOUT_MSG
|
||||
);
|
||||
ChainStateRepositoryImpl repository = repository(cache.asCache());
|
||||
|
||||
ChainState state = repository.load(instanceId);
|
||||
|
||||
Assert.assertEquals(instanceId, state.getInstanceId());
|
||||
Assert.assertEquals(1, cache.getPutCount());
|
||||
Assert.assertSame(state, cache.getLastPutValue());
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证缓存写入失败时不会返回未持久化的工作流状态。
|
||||
*
|
||||
* @throws Exception 缓存依赖注入失败时抛出
|
||||
*/
|
||||
@Test
|
||||
public void loadShouldFailWhenNewStateCannotBePersisted() throws Exception {
|
||||
RecordingCache cache = new RecordingCache(
|
||||
new CacheGetResult<>(CacheResultCode.NOT_EXISTS, null, null),
|
||||
new CacheResult(new IllegalStateException("redis unavailable"))
|
||||
);
|
||||
ChainStateRepositoryImpl repository = repository(cache.asCache());
|
||||
|
||||
try {
|
||||
repository.load("write-failed-instance");
|
||||
Assert.fail("cache write failure should be propagated");
|
||||
} catch (CacheException expected) {
|
||||
Assert.assertTrue(expected.getMessage().contains("工作流状态缓存写入失败"));
|
||||
}
|
||||
|
||||
Assert.assertEquals(1, cache.getPutCount());
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建工作流状态仓储并注入缓存。
|
||||
*
|
||||
* @param cache 测试缓存
|
||||
* @return 已完成依赖注入的工作流状态仓储
|
||||
* @throws Exception 反射注入失败时抛出
|
||||
*/
|
||||
private ChainStateRepositoryImpl repository(Cache<String, Object> cache) throws Exception {
|
||||
ChainStateRepositoryImpl repository = new ChainStateRepositoryImpl();
|
||||
Field field = BaseRepository.class.getDeclaredField("cache");
|
||||
field.setAccessible(true);
|
||||
field.set(repository, cache);
|
||||
return repository;
|
||||
}
|
||||
|
||||
/**
|
||||
* 仅实现当前仓储测试所需操作的 JetCache 调用记录器。
|
||||
*/
|
||||
private static final class RecordingCache implements InvocationHandler {
|
||||
|
||||
private final CacheGetResult<Object> getResult;
|
||||
private final CacheResult putResult;
|
||||
private int putCount;
|
||||
private Object lastPutValue;
|
||||
|
||||
/**
|
||||
* 创建缓存调用记录器。
|
||||
*
|
||||
* @param getResult 读取操作结果
|
||||
* @param putResult 写入操作结果
|
||||
*/
|
||||
private RecordingCache(CacheGetResult<Object> getResult, CacheResult putResult) {
|
||||
this.getResult = getResult;
|
||||
this.putResult = putResult;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建实现 JetCache 接口的 JDK 动态代理。
|
||||
*
|
||||
* @return JetCache 测试代理
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
private Cache<String, Object> asCache() {
|
||||
return (Cache<String, Object>) Proxy.newProxyInstance(
|
||||
Cache.class.getClassLoader(),
|
||||
new Class<?>[]{Cache.class},
|
||||
this
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理仓储发起的缓存读写操作。
|
||||
*
|
||||
* @param proxy 代理对象
|
||||
* @param method 被调用方法
|
||||
* @param args 调用参数
|
||||
* @return 预设的缓存操作结果
|
||||
*/
|
||||
@Override
|
||||
public Object invoke(Object proxy, Method method, Object[] args) {
|
||||
if ("GET".equals(method.getName())) {
|
||||
return getResult;
|
||||
}
|
||||
if ("PUT".equals(method.getName()) && args != null && args.length == 4) {
|
||||
putCount++;
|
||||
lastPutValue = args[1];
|
||||
Assert.assertEquals(3L, args[2]);
|
||||
Assert.assertEquals(TimeUnit.DAYS, args[3]);
|
||||
return putResult;
|
||||
}
|
||||
throw new UnsupportedOperationException("unsupported cache method: " + method.getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取写入调用次数。
|
||||
*
|
||||
* @return 写入调用次数
|
||||
*/
|
||||
private int getPutCount() {
|
||||
return putCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取最后一次写入的缓存值。
|
||||
*
|
||||
* @return 最后一次写入的缓存值
|
||||
*/
|
||||
private Object getLastPutValue() {
|
||||
return lastPutValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user