[Bug]: agentStateStore does not support Redis Cluster.
Describe the bug
Redis Cluster 模式下,多 key 的 Lua 脚本 (EVAL) 要求所有 key 哈希到同一 slot,否则抛出 CROSSSLOT Keys in request don't hash to the same slot。
- redis cluster
- agentStateStore.save(userId, conversationId, "agent_state", state);
- org.redisson.client.RedisException: CROSSSLOT Keys in request don't hash to the same slot.
To Reproduce Steps to reproduce the behavior:
@Bean public AgentStateStore agentStateStore(RedissonClient redissonClient){ return RedisAgentStateStore.builder() .redissonClient(redissonClient) .keyPrefix("sa:session:") .build(); }
agentStateStore.save(userId, conversationId, "agent_state", state);
Environment (please complete the following information):
- AgentScope-Java Version: 2.0.1-SNAPSHOT
- Java Version: 21
- OS: windows
Redis Cluster CROSSSLOT 错误修复方案
问题
Redis Cluster 模式下,多 key 的 Lua 脚本 (EVAL) 要求所有 key 哈希到同一 slot,否则抛出:
org.redisson.client.RedisException: CROSSSLOT Keys in request don't hash to the same slot.
根因
两个组件的多 key Lua 脚本违反了同 slot 约束:
| 组件 | 脚本 | 涉及的 KEYS | 问题 |
|---|---|---|---|
RedisStore |
PUT_SCRIPT / PUT_IF_VERSION_SCRIPT / DELETE_SCRIPT |
itemKey (item: 前缀) + idxKey (idx: 前缀) |
不同前缀 → 不同 slot |
RedisAgentStateStore |
SAVE_SCRIPT |
payloadKey + versionKey + keysKey(不同后缀) | 不同后缀 → 不同 slot |
修复方案:Redis Hash Tag {...}
Redis Cluster 仅根据 key 中 {...} 内的子串计算 slot。将相关 key 的公共部分用 {} 包裹,确保哈希到同一 slot。
修改清单(共 4 处,分布在 2 个文件)
文件 1 — RedisAgentStateStore.java
1. slotId() — 核心修改
返回值加 {},所有派生 key(payloadKey、versionKey、keysKey、listKey、hashKey)自动继承同一 hash tag,getStateKey/getListKey/getKeysKey 无需改动。
private static String slotId(String userId, String sessionId) {
if (sessionId == null || sessionId.isBlank()) {
throw new IllegalArgumentException("sessionId must not be blank");
}
return "{" + normalizeUser(userId) + "/" + sessionId + "}";
}
效果:以 saveVersioned 为例,三个 key 共享 hash tag {user/session} → 同一 slot:
redisKey = prefix{user/session}:key
versionKey = prefix{user/session}:key:ver
keysKey = prefix{user/session}:_keys
2. listSessionIds() — 适配新 key 格式
更新 SCAN pattern 和解析逻辑以匹配 {prefix}{{user}/{sessionId}}:_keys 格式:
String pattern = keyPrefix + "{" + userSegment + "/*}" + KEYS_SUFFIX;
String openTag = keyPrefix + "{" + userSegment + "/";
String closeTag = "}" + KEYS_SUFFIX;
3. clearAllSessions() — 改为逐个删除 key
避免跨 session 多 key DEL 触发 CROSSSLOT:
for (String key : keys) {
client.deleteKeys(key);
}
文件 2 — RedisStore.java
4. itemKey() / indexKey() — 独立组件
hash tag 基于 namespace(与 slotId 无关):
private String itemKey(List<String> namespace, String key) {
return keyPrefix + "item:{" + namespacePath(namespace) + "}" + NS_SEPARATOR + key;
}
private String indexKey(List<String> namespace) {
return keyPrefix + "idx:{" + namespacePath(namespace) + "}";
}
未修改的组件(无需修改)
RedisSandboxExecutionGuard—RELEASE_SCRIPT只用 1 个 key,无 CROSSSLOT 风险。RedisRemoteSnapshotClient— 不使用 Lua 脚本。RedissonClientAdapter/JedisClientAdapter/LettuceClientAdapter— 适配层,不构造 key。
验证
- 编译通过(无 diagnostics 错误)。
- Standalone 模式下
{}仅作为 key 名的一部分,不影响功能。 - Cluster 模式下相关 key 保证同 slot,Lua 脚本可正常执行。
注意事项
- Key 格式变更:这是一次 breaking change,升级时需清理旧数据重新写入。
- 错误消息:部分异常消息会带花括号(如
"Failed to delete session: {user/session}"),仅展示层小瑕疵,不影响功能。
最初也想过 先判断Redis是否是集群模式 如果是再增加Hash Tag {...},否则保持原样, 后面想了想,复杂度高(Jedis, Lettuce, Redisson都要考虑),也完全没有必要。
Same area, one more cluster-related point: RedisAgentStateStore.listSessionIds enumerates sessions with JedisClientAdapter.findKeysByPattern, a SCAN loop on a single cursor. On a native Redis Cluster a SCAN has no key, so Jedis (checked on 7.4.1: ClusterConnectionProvider.getConnection returns a random primary when the slot is < 0) sends each call to a random primary, so the result is incomplete and the cursor can be mixed up across nodes. We use listSessionIds to enumerate a user's sessions for deletion; our deployment is a proxy-mode cluster, where it works. If native cluster support is added to the state store, the session enumeration needs to scan every primary or use an index key.
Closing as fixed by #3129 (merged): agent state Redis keys no longer require the same hashslot, enabling Redis Cluster deployments. Reopen if cluster mode still fails.