From bce4934ccac03178263fc4db850cde836323a7b0 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Fri, 4 Dec 2026 16:52:43 -0800 Subject: [PATCH] redis3: read the directory list from the masters when changing it With useReadOnly or routeByLatency, redis_cluster3 sent the reads of a list change to replicas. A lagging replica could miss a node's other names, and the delete that read it dropped the node with them; a listing or a lookup that removes an expired name now runs that delete too. List changes and the EXISTS checks before them go through a second cluster client that reads from the masters, created only when the store reads from replicas. Co-Authored-By: Claude Opus 5.5 --- weed/filer/redis3/kv_directory_children.go | 6 ++-- weed/filer/redis3/redis_cluster_store.go | 5 +++ weed/filer/redis3/redis_cluster_store_test.go | 33 +++++++++++++++++++ weed/filer/redis3/universal_redis_store.go | 27 +++++++++++---- 4 files changed, 61 insertions(+), 10 deletions(-) create mode 100644 weed/filer/redis3/redis_cluster_store_test.go diff --git a/weed/filer/redis3/kv_directory_children.go b/weed/filer/redis3/kv_directory_children.go index 2f373eeeed..62b5ae6345 100644 --- a/weed/filer/redis3/kv_directory_children.go +++ b/weed/filer/redis3/kv_directory_children.go @@ -21,7 +21,7 @@ func insertChild(ctx context.Context, redisStore *UniversalRedis3Store, key stri mutex.Unlock() }() - client := redisStore.Client + client := redisStore.onMaster() data, err := client.Get(ctx, key).Result() if err != nil { if err != redis.Nil { @@ -65,7 +65,7 @@ func removeChild(ctx context.Context, redisStore *UniversalRedis3Store, key stri } } - client := redisStore.Client + client := redisStore.onMaster() data, err := client.Get(ctx, key).Result() if err != nil { if err != redis.Nil { @@ -110,7 +110,7 @@ func removeChildren(ctx context.Context, redisStore *UniversalRedis3Store, key s } defer mutex.Unlock() - client := redisStore.Client + client := redisStore.onMaster() data, err := client.Get(ctx, key).Result() if err != nil { if err != redis.Nil { diff --git a/weed/filer/redis3/redis_cluster_store.go b/weed/filer/redis3/redis_cluster_store.go index c74f544ab3..7b2a9ac83f 100644 --- a/weed/filer/redis3/redis_cluster_store.go +++ b/weed/filer/redis3/redis_cluster_store.go @@ -59,6 +59,11 @@ func (store *RedisCluster3Store) initialize(addresses []string, username string, TLSConfig: tlsConfig, } settings.ApplyToCluster(options) + if readOnly || routeByLatency { + masterOptions := *options + masterOptions.ReadOnly, masterOptions.RouteByLatency = false, false + store.masterClient = redis.NewClusterClient(&masterOptions) + } store.Client = redis.NewClusterClient(options) store.redsync = redsync.New(goredis.NewPool(store.Client)) store.keyPrefix = keyPrefix diff --git a/weed/filer/redis3/redis_cluster_store_test.go b/weed/filer/redis3/redis_cluster_store_test.go new file mode 100644 index 0000000000..ad42c97fdd --- /dev/null +++ b/weed/filer/redis3/redis_cluster_store_test.go @@ -0,0 +1,33 @@ +package redis3 + +import ( + "testing" + + "github.com/redis/go-redis/v9" + "github.com/seaweedfs/seaweedfs/weed/filer/redis_conf" +) + +func TestClusterStoreChangesListsOnMasters(t *testing.T) { + for _, routing := range []struct{ readOnly, routeByLatency bool }{{true, false}, {false, true}} { + store := &RedisCluster3Store{} + if err := store.initialize([]string{"127.0.0.1:1"}, "", "", "", routing.readOnly, routing.routeByLatency, nil, redis_conf.Settings{}); err != nil { + t.Fatalf("initialize %+v: %v", routing, err) + } + if !store.Client.(*redis.ClusterClient).Options().ReadOnly { + t.Errorf("%+v: Client does not read from replicas", routing) + } + if options := store.onMaster().(*redis.ClusterClient).Options(); options.ReadOnly || options.RouteByLatency || options.RouteRandomly { + t.Errorf("%+v: list changes read from replicas", routing) + } + store.Shutdown() + } + + store := &RedisCluster3Store{} + if err := store.initialize([]string{"127.0.0.1:1"}, "", "", "", false, false, nil, redis_conf.Settings{}); err != nil { + t.Fatalf("initialize: %v", err) + } + if store.onMaster() != store.Client { + t.Errorf("a second client without replica reads") + } + store.Shutdown() +} diff --git a/weed/filer/redis3/universal_redis_store.go b/weed/filer/redis3/universal_redis_store.go index c17cec6685..184df38f61 100644 --- a/weed/filer/redis3/universal_redis_store.go +++ b/weed/filer/redis3/universal_redis_store.go @@ -19,9 +19,20 @@ const ( ) type UniversalRedis3Store struct { - Client redis.UniversalClient - redsync *redsync.Redsync - keyPrefix string + Client redis.UniversalClient + // set when Client reads from replicas + masterClient redis.UniversalClient + redsync *redsync.Redsync + keyPrefix string +} + +// onMaster returns a client that reads every key from its master. A list change +// decided on a lagging replica's read can drop names the master holds. +func (store *UniversalRedis3Store) onMaster() redis.UniversalClient { + if store.masterClient != nil { + return store.masterClient + } + return store.Client } func (store *UniversalRedis3Store) getKey(key string) string { @@ -308,11 +319,10 @@ func (store *UniversalRedis3Store) removeOrphanedDirectoryListMember(ctx context return removed, nil } -// existsOnMaster runs EXISTS as a script, which always runs on the key's master: -// with replica reads (useReadOnly, routeByLatency) a plain EXISTS can hit a lagging -// replica and misread a live value as absent. +// existsOnMaster checks the key's master: a lagging replica can misread a live +// value as absent. func (store *UniversalRedis3Store) existsOnMaster(ctx context.Context, key string) (int64, error) { - return store.Client.Eval(ctx, "return redis.call('EXISTS', KEYS[1])", []string{key}).Int64() + return store.onMaster().Exists(ctx, key).Result() } func isLogicallyExpired(entry *filer.Entry) bool { @@ -360,4 +370,7 @@ func genDirectoryListKey(dir string) (dirList string) { func (store *UniversalRedis3Store) Shutdown() { store.Client.Close() + if store.masterClient != nil { + store.masterClient.Close() + } }