Tendis icon indicating copy to clipboard operation
Tendis copied to clipboard

使用go-redis库访问Tendis,Scan命令持续返回重复的key

Open illidanzheng opened this issue 2 years ago • 8 comments

image 如图所示,scan次数,表示代码中调用scan命令的次数。keys,表示被scan命令扫出来的key的数量。

scan次数持续增大,但是key数量不变

illidanzheng avatar Nov 28 '22 13:11 illidanzheng

你问题的描述感觉不够清晰,不知道你是怎么用和怎么测的,我们这边测试是没有问题的。返回的cusor为0之后,就表示数据scan结束了。

takenliu avatar Nov 29 '22 06:11 takenliu

127.0.0.1:52000> scan 0 match a* count 2
1) "3"
2) 1) "a1"
   2) "a3"
127.0.0.1:52000> scan 3 match a* count 2
1) "5"
2) 1) "a2"
   2) "a5"
127.0.0.1:52000> scan 5 match a* count 2
1) "0"
2) 1) "a4"
127.0.0.1:52000> keys a*
1) "a1"
2) "a3"
3) "a2"
4) "a5"
5) "a4"

takenliu avatar Nov 29 '22 06:11 takenliu

image

我们是通过代码调用的,如下图: image

illidanzheng avatar Nov 29 '22 06:11 illidanzheng

img_v2_8cb7a31c-349b-41c1-aaf6-e12c07d572eg 有查到文档中说scan会有这种问题,是否有解决方案?

illidanzheng avatar Nov 29 '22 06:11 illidanzheng

好,我先看看

另外,我看了系统监控,我们对tendis的写入qps,不超过100,应该算小的了

illidanzheng avatar Nov 29 '22 07:11 illidanzheng

嗯,跟你贴的这个问题应该不是一件事情。 执行下这个看看: redis-cli -p 54005 info dataset | grep rocksdb.estimate-num-key

takenliu avatar Nov 29 '22 07:11 takenliu

原因是客户端代码有误,修改之后问题得到解决:

func GetKeys(redisCli *redis.ClusterClient) error {
    err := redisCli.ForEachMaster(context.Background(), func(ctx context.Context, client *redis.Client) error {
        for true {
            scanCmd := redisCli.Scan(ctx, cursor, "*", 1000)  // redisCli(集群客户端)需要改为client(单节点客户端)
            keys, cursor, err = scanCmd.Result()
        }
    }
}

takenliu avatar Nov 29 '22 11:11 takenliu

经过沟通,确认为Go代码有误。

raffertyyu avatar Nov 30 '22 07:11 raffertyyu