redis/export_test.go

83 lines
1.5 KiB
Go
Raw Permalink Normal View History

2013-11-07 18:20:15 +04:00
package redis
2016-03-17 19:00:47 +03:00
import (
2018-05-17 16:09:56 +03:00
"fmt"
"net"
2018-11-24 14:16:21 +03:00
"strings"
2016-03-17 19:00:47 +03:00
2019-08-08 14:29:44 +03:00
"github.com/go-redis/redis/v7/internal/hashtag"
"github.com/go-redis/redis/v7/internal/pool"
2016-03-17 19:00:47 +03:00
)
func (c *baseClient) Pool() pool.Pooler {
2013-11-07 18:20:15 +04:00
return c.connPool
}
2015-03-23 11:23:33 +03:00
func (c *PubSub) SetNetConn(netConn net.Conn) {
c.cn = pool.NewConn(netConn)
}
2016-03-17 19:00:47 +03:00
2018-05-17 16:09:56 +03:00
func (c *ClusterClient) LoadState() (*clusterState, error) {
return c.loadState()
}
func (c *ClusterClient) SlotAddrs(slot int) []string {
2018-03-06 18:10:01 +03:00
state, err := c.state.Get()
2017-08-31 15:22:47 +03:00
if err != nil {
panic(err)
}
var addrs []string
2017-08-31 15:22:47 +03:00
for _, n := range state.slotNodes(slot) {
addrs = append(addrs, n.Client.getAddr())
}
return addrs
}
2018-05-17 16:09:56 +03:00
func (c *ClusterClient) Nodes(key string) ([]*clusterNode, error) {
state, err := c.state.Reload()
2017-08-31 15:22:47 +03:00
if err != nil {
2018-05-17 16:09:56 +03:00
return nil, err
2017-08-31 15:22:47 +03:00
}
2018-05-17 16:09:56 +03:00
slot := hashtag.Slot(key)
2018-07-22 10:50:26 +03:00
nodes := state.slotNodes(slot)
2018-05-17 16:09:56 +03:00
if len(nodes) != 2 {
return nil, fmt.Errorf("slot=%d does not have enough nodes: %v", slot, nodes)
}
return nodes, nil
}
func (c *ClusterClient) SwapNodes(key string) error {
nodes, err := c.Nodes(key)
if err != nil {
return err
2017-07-09 13:10:07 +03:00
}
2018-05-17 16:09:56 +03:00
nodes[0], nodes[1] = nodes[1], nodes[0]
return nil
}
2018-11-24 14:16:21 +03:00
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
}