mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-06 02:57:37 +01:00
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 <noreply@anthropic.com>
This commit is contained in:
1 parent
4d44f288c9
commit
bce4934cca
4 files changed
+61
-10
No files matched your search
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user