From 5c5e1fc68f2e8d7154d7121f4acbd6d458d791d6 Mon Sep 17 00:00:00 2001 From: Syngnat Date: Wed, 18 Mar 2026 15:51:19 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(redis,mongodb):=20=E4=BF=AE?= =?UTF-8?q?=E5=A4=8DRedis=E9=9B=86=E7=BE=A4=E6=95=B0=E6=8D=AE=E6=98=BE?= =?UTF-8?q?=E7=A4=BA=E4=B8=8D=E5=85=A8=E5=92=8CMongoDB=E6=8C=87=E5=AE=9A?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=BA=93=E8=BF=9E=E6=8E=A5=E5=A4=B1=E8=B4=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Redis集群ScanKeys改用ForEachMaster逐master节点扫描合并去重 - MongoDB authSource未显式设置时始终默认admin而非Database值 - 同步修复mongodb_impl.go和mongodb_impl_v1.go - refs #246 --- internal/db/mongodb_impl.go | 3 -- internal/db/mongodb_impl_v1.go | 3 -- internal/redis/redis_impl.go | 59 ++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 6 deletions(-) diff --git a/internal/db/mongodb_impl.go b/internal/db/mongodb_impl.go index bcdd2ce..2e5131c 100644 --- a/internal/db/mongodb_impl.go +++ b/internal/db/mongodb_impl.go @@ -237,9 +237,6 @@ func (m *MongoDB) getURI(config connection.ConnectionConfig) string { params.Set("serverSelectionTimeoutMS", strconv.Itoa(timeout*1000)) authSource := strings.TrimSpace(config.AuthSource) - if authSource == "" && strings.TrimSpace(config.Database) != "" { - authSource = strings.TrimSpace(config.Database) - } if authSource == "" { authSource = "admin" } diff --git a/internal/db/mongodb_impl_v1.go b/internal/db/mongodb_impl_v1.go index 909285b..c746431 100644 --- a/internal/db/mongodb_impl_v1.go +++ b/internal/db/mongodb_impl_v1.go @@ -238,9 +238,6 @@ func (m *MongoDBV1) getURI(config connection.ConnectionConfig) string { params.Set("serverSelectionTimeoutMS", strconv.Itoa(timeout*1000)) authSource := strings.TrimSpace(config.AuthSource) - if authSource == "" && strings.TrimSpace(config.Database) != "" { - authSource = strings.TrimSpace(config.Database) - } if authSource == "" { authSource = "admin" } diff --git a/internal/redis/redis_impl.go b/internal/redis/redis_impl.go index 2662979..8ea032c 100644 --- a/internal/redis/redis_impl.go +++ b/internal/redis/redis_impl.go @@ -388,6 +388,65 @@ func (r *RedisClientImpl) ScanKeys(pattern string, cursor uint64, count int64) ( ctx, cancel := context.WithTimeout(context.Background(), maxDuration+5*time.Second) defer cancel() + // 集群模式:逐 master 节点 SCAN 后合并去重 + if r.isCluster && r.clusterClient != nil { + keys := make([]string, 0, int(targetCount)) + seen := make(map[string]struct{}, int(targetCount)) + var mu sync.Mutex + + err := r.clusterClient.ForEachMaster(ctx, func(nodeCtx context.Context, node *redis.Client) error { + var nodeCursor uint64 + round := 0 + scanStartedAt := time.Now() + for { + if time.Since(scanStartedAt) >= maxDuration { + break + } + mu.Lock() + enough := len(keys) >= int(targetCount) + mu.Unlock() + if enough { + break + } + + batch, nextCursor, err := node.Scan(nodeCtx, nodeCursor, physicalPattern, scanStepCount).Result() + if err != nil { + return err + } + + mu.Lock() + for _, key := range batch { + if _, ok := seen[key]; ok { + continue + } + seen[key] = struct{}{} + keys = append(keys, key) + if len(keys) >= int(targetCount) { + break + } + } + mu.Unlock() + + nodeCursor = nextCursor + round++ + if nodeCursor == 0 || round >= maxRounds { + break + } + } + return nil + }) + if err != nil { + return nil, err + } + + // 集群模式 cursor 无意义,始终返回 "0" 表示扫描完成 + return &RedisScanResult{ + Keys: r.loadRedisKeyInfos(ctx, keys), + Cursor: "0", + }, nil + } + + // 非集群模式:原逻辑 currentCursor := cursor round := 0 scanStartedAt := time.Now()