redis/export_test.go

96 lines
2.0 KiB
Go
Raw Normal View History

2013-11-07 18:20:15 +04:00
package redis
2016-03-17 19:00:47 +03:00
import (
2020-03-11 17:26:42 +03:00
"context"
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
2021-03-12 17:08:58 +03:00
"github.com/go-redis/redis/v8/internal"
2020-03-11 17:29:16 +03:00
"github.com/go-redis/redis/v8/internal/hashtag"
"github.com/go-redis/redis/v8/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
2020-03-11 17:26:42 +03:00
func (c *ClusterClient) LoadState(ctx context.Context) (*clusterState, error) {
2020-09-05 11:34:37 +03:00
// return c.state.Reload(ctx)
2020-03-11 17:26:42 +03:00
return c.loadState(ctx)
2018-05-17 16:09:56 +03:00
}
2020-03-11 17:26:42 +03:00
func (c *ClusterClient) SlotAddrs(ctx context.Context, slot int) []string {
state, err := c.state.Get(ctx)
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
}
2020-03-11 17:26:42 +03:00
func (c *ClusterClient) Nodes(ctx context.Context, key string) ([]*clusterNode, error) {
state, err := c.state.Reload(ctx)
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
}
2020-03-11 17:26:42 +03:00
func (c *ClusterClient) SwapNodes(ctx context.Context, key string) error {
nodes, err := c.Nodes(ctx, key)
2018-05-17 16:09:56 +03:00
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 (c *clusterState) IsConsistent(ctx context.Context) bool {
if len(c.Masters) < 3 {
2018-11-24 14:16:21 +03:00
return false
}
for _, master := range c.Masters {
2020-03-11 17:26:42 +03:00
s := master.Client.Info(ctx, "replication").Val()
2018-11-24 14:16:21 +03:00
if !strings.Contains(s, "role:master") {
return false
}
}
if len(c.Slaves) < 3 {
2018-11-24 14:16:21 +03:00
return false
}
for _, slave := range c.Slaves {
2020-03-11 17:26:42 +03:00
s := slave.Client.Info(ctx, "replication").Val()
2018-11-24 14:16:21 +03:00
if !strings.Contains(s, "role:slave") {
return false
}
}
return true
}
2021-03-12 17:08:58 +03:00
func GetSlavesAddrByName(ctx context.Context, c *SentinelClient, name string) []string {
addrs, err := c.Slaves(ctx, name).Result()
if err != nil {
internal.Logger.Printf(ctx, "sentinel: Slaves name=%q failed: %s",
name, err)
return []string{}
}
return parseSlaveAddrs(addrs, false)
}