Remove Redis Cluster state check

This commit is contained in:
Vladimir Mihailenco 2018-11-24 13:16:21 +02:00
parent 78a66f0e5f
commit 17e8439f2f
3 changed files with 30 additions and 42 deletions

View File

@ -582,13 +582,6 @@ func (c *clusterState) slotNodes(slot int) []*clusterNode {
return nil 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 { type clusterStateHolder struct {
@ -613,9 +606,6 @@ func (c *clusterStateHolder) Reload() (*clusterState, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
if !state.IsConsistent() {
time.AfterFunc(time.Second, c.LazyReload)
}
return state, nil return state, nil
} }
@ -640,16 +630,11 @@ func (c *clusterStateHolder) LazyReload() {
go func() { go func() {
defer atomic.StoreUint32(&c.reloading, 0) defer atomic.StoreUint32(&c.reloading, 0)
for { _, err := c.reload()
state, err := c.reload() if err != nil {
if err != nil { return
return
}
time.Sleep(100 * time.Millisecond)
if state.IsConsistent() {
return
}
} }
time.Sleep(100 * time.Millisecond)
}() }()
} }

View File

@ -49,8 +49,6 @@ func (s *clusterScenario) addrs() []string {
} }
func (s *clusterScenario) clusterClient(opt *redis.ClusterOptions) *redis.ClusterClient { func (s *clusterScenario) clusterClient(opt *redis.ClusterOptions) *redis.ClusterClient {
var errBadState = fmt.Errorf("cluster state is not consistent")
opt.Addrs = s.addrs() opt.Addrs = s.addrs()
client := redis.NewClusterClient(opt) client := redis.NewClusterClient(opt)
@ -65,27 +63,7 @@ func (s *clusterScenario) clusterClient(opt *redis.ClusterOptions) *redis.Cluste
} }
if !state.IsConsistent() { if !state.IsConsistent() {
return errBadState return fmt.Errorf("cluster state is not consistent")
}
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 nil return nil

View File

@ -3,6 +3,7 @@ package redis
import ( import (
"fmt" "fmt"
"net" "net"
"strings"
"github.com/go-redis/redis/internal/hashtag" "github.com/go-redis/redis/internal/hashtag"
"github.com/go-redis/redis/internal/pool" "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] nodes[0], nodes[1] = nodes[1], nodes[0]
return nil 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
}