Skip to content

[Bug]: agentStateStore does not support Redis Cluster. #2579

Closed

Description

@amyaxy

Describe the bug

Redis Cluster 模式下,多 key 的 Lua 脚本 (EVAL) 要求所有 key 哈希到同一 slot,否则抛出 CROSSSLOT Keys in request don't hash to the same slot。

  1. redis cluster
  2. agentStateStore.save(userId, conversationId, "agent_state", state);
  3. 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);

Image Image Image

Environment (please complete the following information):

  • AgentScope-Java Version: 2.0.1-SNAPSHOT
  • Java Version: 21
  • OS: windows

Activity

  1. added
    bug
    on Aug 6, 2026
  2. changed the title [-][Bug]:[/-] [+][Bug]: agentStateStore does not support Redis Cluster.[/+] on Aug 6, 2026
  3. amyaxy commented on Aug 6, 2026

    @amyaxy
    ContributorAuthor

    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}"),仅展示层小瑕疵,不影响功能。
  4. amyaxy commented on Aug 6, 2026

    @amyaxy
    ContributorAuthor

    最初也想过
    先判断Redis是否是集群模式
    如果是再增加Hash Tag {...},否则保持原样,
    后面想了想,复杂度高(Jedis, Lettuce, Redisson都要考虑),也完全没有必要。

  5. added
    area/build
    area/core/memory
    on Aug 11, 2026
  6. moved this from Backlog to In progress in AgentScope Javaon Aug 11, 2026
  7. added 4 commits that reference this issue on Sep 12, 2026
    b46d135
    dd80d23
    e6e47fc
    e1bbb79
  8. xuchaoguo1983 commented on Oct 2, 2026

    @xuchaoguo1983

    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.

  9. added 3 commits that reference this issue on Oct 8, 2026
    708b6d1
    63fca52
    4efae85
  10. juruoO-o commented on Nov 12, 2026

    @juruoO-o
    Collaborator

    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.

  11. moved this from In progress to Done in AgentScope Javaon Nov 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/buildarea/core/memorybug

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions