From 17e8439f2f77d8264b384efb26f7cf61f49e89b4 Mon Sep 17 00:00:00 2001 From: Vladimir Mihailenco Date: Sat, 24 Nov 2018 13:16:21 +0200 Subject: [PATCH] Remove Redis Cluster state check --- cluster.go | 23 ++++------------------- cluster_test.go | 24 +----------------------- export_test.go | 25 +++++++++++++++++++++++++ 3 files changed, 30 insertions(+), 42 deletions(-) diff --git a/cluster.go b/cluster.go index 03c186c..3f140a6 100644 --- a/cluster.go +++ b/cluster.go @@ -582,13 +582,6 @@ func (c *clusterState) slotNodes(slot int) []*clusterNode { return nil } -func (c *clusterState) IsConsistent() bool { - if c.nodes.opt.ClusterSlots != nil { - return true - } - return len(c.Masters) <= len(c.Slaves) -} - //------------------------------------------------------------------------------ type clusterStateHolder struct { @@ -613,9 +606,6 @@ func (c *clusterStateHolder) Reload() (*clusterState, error) { if err != nil { return nil, err } - if !state.IsConsistent() { - time.AfterFunc(time.Second, c.LazyReload) - } return state, nil } @@ -640,16 +630,11 @@ func (c *clusterStateHolder) LazyReload() { go func() { defer atomic.StoreUint32(&c.reloading, 0) - for { - state, err := c.reload() - if err != nil { - return - } - time.Sleep(100 * time.Millisecond) - if state.IsConsistent() { - return - } + _, err := c.reload() + if err != nil { + return } + time.Sleep(100 * time.Millisecond) }() } diff --git a/cluster_test.go b/cluster_test.go index b8003ba..cdbb733 100644 --- a/cluster_test.go +++ b/cluster_test.go @@ -49,8 +49,6 @@ func (s *clusterScenario) addrs() []string { } func (s *clusterScenario) clusterClient(opt *redis.ClusterOptions) *redis.ClusterClient { - var errBadState = fmt.Errorf("cluster state is not consistent") - opt.Addrs = s.addrs() client := redis.NewClusterClient(opt) @@ -65,27 +63,7 @@ func (s *clusterScenario) clusterClient(opt *redis.ClusterOptions) *redis.Cluste } if !state.IsConsistent() { - return errBadState - } - - if len(state.Masters) < 3 { - return errBadState - } - for _, master := range state.Masters { - s := master.Client.Info("replication").Val() - if !strings.Contains(s, "role:master") { - return errBadState - } - } - - if len(state.Slaves) < 3 { - return errBadState - } - for _, slave := range state.Slaves { - s := slave.Client.Info("replication").Val() - if !strings.Contains(s, "role:slave") { - return errBadState - } + return fmt.Errorf("cluster state is not consistent") } return nil diff --git a/export_test.go b/export_test.go index fab91e2..4dc0c62 100644 --- a/export_test.go +++ b/export_test.go @@ -3,6 +3,7 @@ package redis import ( "fmt" "net" + "strings" "github.com/go-redis/redis/internal/hashtag" "github.com/go-redis/redis/internal/pool" @@ -55,3 +56,27 @@ func (c *ClusterClient) SwapNodes(key string) error { nodes[0], nodes[1] = nodes[1], nodes[0] return nil } + +func (state *clusterState) IsConsistent() bool { + if len(state.Masters) < 3 { + return false + } + for _, master := range state.Masters { + s := master.Client.Info("replication").Val() + if !strings.Contains(s, "role:master") { + return false + } + } + + if len(state.Slaves) < 3 { + return false + } + for _, slave := range state.Slaves { + s := slave.Client.Info("replication").Val() + if !strings.Contains(s, "role:slave") { + return false + } + } + + return true +}