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"
|
2017-04-17 15:43:58 +03:00
|
|
|
"net"
|
2018-11-24 14:16:21 +03:00
|
|
|
"strings"
|
2016-03-17 19:00:47 +03:00
|
|
|
|
2018-05-17 16:09:56 +03:00
|
|
|
"github.com/go-redis/redis/internal/hashtag"
|
2017-02-18 17:42:34 +03:00
|
|
|
"github.com/go-redis/redis/internal/pool"
|
2016-03-17 19:00:47 +03:00
|
|
|
)
|
2015-05-10 15:33:04 +03:00
|
|
|
|
2016-03-12 11:52:13 +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
|
|
|
|
2017-04-17 15:43:58 +03:00
|
|
|
func (c *PubSub) SetNetConn(netConn net.Conn) {
|
|
|
|
c.cn = pool.NewConn(netConn)
|
2016-03-01 13:31:06 +03:00
|
|
|
}
|
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()
|
|
|
|
}
|
|
|
|
|
2016-11-08 12:46:44 +03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2016-11-08 12:46:44 +03:00
|
|
|
var addrs []string
|
2017-08-31 15:22:47 +03:00
|
|
|
for _, n := range state.slotNodes(slot) {
|
2016-11-08 12:46:44 +03:00
|
|
|
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
|
2016-11-08 12:46:44 +03:00
|
|
|
}
|
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
|
|
|
|
}
|