redis/export_test.go

84 lines
1.7 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
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
2020-03-11 17:26:42 +03:00
func (c *ClusterClient) LoadState(ctx context.Context) (*clusterState, error) {
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
2020-03-11 17:26:42 +03:00
func (state *clusterState) IsConsistent(ctx context.Context) bool {
2018-11-24 14:16:21 +03:00
if len(state.Masters) < 3 {
return false
}
for _, master := range state.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(state.Slaves) < 3 {
return false
}
for _, slave := range state.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
}