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"
|
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
|
|
|
|
2022-06-04 17:39:21 +03:00
|
|
|
"github.com/go-redis/redis/v9/internal"
|
|
|
|
"github.com/go-redis/redis/v9/internal/hashtag"
|
|
|
|
"github.com/go-redis/redis/v9/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
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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
|
2016-11-08 12:46:44 +03:00
|
|
|
}
|
2018-11-24 14:16:21 +03:00
|
|
|
|
2021-08-02 14:01:01 +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
|
|
|
|
}
|
2021-08-02 14:01:01 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-02 14:01:01 +03:00
|
|
|
if len(c.Slaves) < 3 {
|
2018-11-24 14:16:21 +03:00
|
|
|
return false
|
|
|
|
}
|
2021-08-02 14:01:01 +03:00
|
|
|
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 {
|
2022-06-04 17:25:12 +03:00
|
|
|
addrs, err := c.Replicas(ctx, name).Result()
|
2021-03-12 17:08:58 +03:00
|
|
|
if err != nil {
|
2022-06-04 17:25:12 +03:00
|
|
|
internal.Logger.Printf(ctx, "sentinel: Replicas name=%q failed: %s",
|
2021-03-12 17:08:58 +03:00
|
|
|
name, err)
|
|
|
|
return []string{}
|
|
|
|
}
|
2022-06-04 17:25:12 +03:00
|
|
|
return parseReplicaAddrs(addrs, false)
|
2021-03-12 17:08:58 +03:00
|
|
|
}
|