2015-01-24 15:12:48 +03:00
|
|
|
package redis_test
|
|
|
|
|
|
|
|
import (
|
2017-03-24 13:48:43 +03:00
|
|
|
"bytes"
|
2015-11-14 16:54:16 +03:00
|
|
|
"fmt"
|
2015-04-28 18:14:19 +03:00
|
|
|
"net"
|
2015-12-16 17:11:52 +03:00
|
|
|
"strconv"
|
2015-11-14 16:54:16 +03:00
|
|
|
"strings"
|
2015-12-16 17:11:52 +03:00
|
|
|
"sync"
|
2015-04-28 18:14:19 +03:00
|
|
|
"testing"
|
2015-03-18 13:41:24 +03:00
|
|
|
"time"
|
2015-01-24 15:12:48 +03:00
|
|
|
|
2017-02-18 17:42:34 +03:00
|
|
|
"github.com/go-redis/redis"
|
|
|
|
"github.com/go-redis/redis/internal/hashtag"
|
2016-12-16 17:26:48 +03:00
|
|
|
|
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
2015-01-24 15:12:48 +03:00
|
|
|
)
|
|
|
|
|
2015-04-28 18:14:19 +03:00
|
|
|
type clusterScenario struct {
|
|
|
|
ports []string
|
|
|
|
nodeIds []string
|
|
|
|
processes map[string]*redisProcess
|
|
|
|
clients map[string]*redis.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *clusterScenario) masters() []*redis.Client {
|
|
|
|
result := make([]*redis.Client, 3)
|
|
|
|
for pos, port := range s.ports[:3] {
|
|
|
|
result[pos] = s.clients[port]
|
2015-01-24 15:12:48 +03:00
|
|
|
}
|
2015-04-28 18:14:19 +03:00
|
|
|
return result
|
|
|
|
}
|
2015-01-24 15:12:48 +03:00
|
|
|
|
2015-04-28 18:14:19 +03:00
|
|
|
func (s *clusterScenario) slaves() []*redis.Client {
|
|
|
|
result := make([]*redis.Client, 3)
|
|
|
|
for pos, port := range s.ports[3:] {
|
|
|
|
result[pos] = s.clients[port]
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
2015-01-24 15:12:48 +03:00
|
|
|
|
2017-02-17 13:12:06 +03:00
|
|
|
func (s *clusterScenario) addrs() []string {
|
2015-04-28 18:14:19 +03:00
|
|
|
addrs := make([]string, len(s.ports))
|
|
|
|
for i, port := range s.ports {
|
|
|
|
addrs[i] = net.JoinHostPort("127.0.0.1", port)
|
|
|
|
}
|
2017-02-17 13:12:06 +03:00
|
|
|
return addrs
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *clusterScenario) clusterClient(opt *redis.ClusterOptions) *redis.ClusterClient {
|
|
|
|
opt.Addrs = s.addrs()
|
2015-05-10 16:01:38 +03:00
|
|
|
return redis.NewClusterClient(opt)
|
2015-04-28 18:14:19 +03:00
|
|
|
}
|
2015-01-24 15:12:48 +03:00
|
|
|
|
2015-04-28 18:14:19 +03:00
|
|
|
func startCluster(scenario *clusterScenario) error {
|
2015-11-14 16:54:16 +03:00
|
|
|
// Start processes and collect node ids
|
2015-04-28 18:14:19 +03:00
|
|
|
for pos, port := range scenario.ports {
|
|
|
|
process, err := startRedis(port, "--cluster-enabled", "yes")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-01-24 15:12:48 +03:00
|
|
|
}
|
|
|
|
|
2016-03-16 17:57:24 +03:00
|
|
|
client := redis.NewClient(&redis.Options{
|
|
|
|
Addr: ":" + port,
|
|
|
|
})
|
|
|
|
|
2015-04-28 18:14:19 +03:00
|
|
|
info, err := client.ClusterNodes().Result()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-01-24 15:12:48 +03:00
|
|
|
}
|
|
|
|
|
2015-04-28 18:14:19 +03:00
|
|
|
scenario.processes[port] = process
|
|
|
|
scenario.clients[port] = client
|
|
|
|
scenario.nodeIds[pos] = info[:40]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Meet cluster nodes
|
|
|
|
for _, client := range scenario.clients {
|
|
|
|
err := client.ClusterMeet("127.0.0.1", scenario.ports[0]).Err()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-01-24 15:12:48 +03:00
|
|
|
}
|
2015-04-28 18:14:19 +03:00
|
|
|
}
|
2015-01-24 15:12:48 +03:00
|
|
|
|
2015-04-28 18:14:19 +03:00
|
|
|
// Bootstrap masters
|
|
|
|
slots := []int{0, 5000, 10000, 16384}
|
2015-11-14 16:54:16 +03:00
|
|
|
for pos, master := range scenario.masters() {
|
|
|
|
err := master.ClusterAddSlotsRange(slots[pos], slots[pos+1]-1).Err()
|
2015-04-28 18:14:19 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-01-24 15:12:48 +03:00
|
|
|
|
2015-04-28 18:14:19 +03:00
|
|
|
// Bootstrap slaves
|
2015-11-14 16:54:16 +03:00
|
|
|
for idx, slave := range scenario.slaves() {
|
|
|
|
masterId := scenario.nodeIds[idx]
|
|
|
|
|
|
|
|
// Wait until master is available
|
|
|
|
err := eventually(func() error {
|
|
|
|
s := slave.ClusterNodes().Val()
|
|
|
|
wanted := masterId
|
|
|
|
if !strings.Contains(s, wanted) {
|
|
|
|
return fmt.Errorf("%q does not contain %q", s, wanted)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}, 10*time.Second)
|
2015-04-28 18:14:19 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-01-24 15:12:48 +03:00
|
|
|
}
|
|
|
|
|
2015-11-14 16:54:16 +03:00
|
|
|
err = slave.ClusterReplicate(masterId).Err()
|
2015-04-28 18:14:19 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-01-24 15:12:48 +03:00
|
|
|
}
|
2015-04-28 18:14:19 +03:00
|
|
|
}
|
2015-01-24 15:12:48 +03:00
|
|
|
|
2015-11-14 16:54:16 +03:00
|
|
|
// Wait until all nodes have consistent info
|
2015-04-28 18:14:19 +03:00
|
|
|
for _, client := range scenario.clients {
|
2015-11-14 16:54:16 +03:00
|
|
|
err := eventually(func() error {
|
2015-11-22 15:44:38 +03:00
|
|
|
res, err := client.ClusterSlots().Result()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-11-21 14:16:13 +03:00
|
|
|
}
|
2016-04-09 12:52:43 +03:00
|
|
|
wanted := []redis.ClusterSlot{
|
|
|
|
{0, 4999, []redis.ClusterNode{{"", "127.0.0.1:8220"}, {"", "127.0.0.1:8223"}}},
|
|
|
|
{5000, 9999, []redis.ClusterNode{{"", "127.0.0.1:8221"}, {"", "127.0.0.1:8224"}}},
|
|
|
|
{10000, 16383, []redis.ClusterNode{{"", "127.0.0.1:8222"}, {"", "127.0.0.1:8225"}}},
|
2015-11-22 15:44:38 +03:00
|
|
|
}
|
2016-04-09 12:52:43 +03:00
|
|
|
return assertSlotsEqual(res, wanted)
|
2016-03-14 17:51:46 +03:00
|
|
|
}, 30*time.Second)
|
2015-04-28 18:14:19 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-09 12:52:43 +03:00
|
|
|
func assertSlotsEqual(slots, wanted []redis.ClusterSlot) error {
|
|
|
|
outer_loop:
|
|
|
|
for _, s2 := range wanted {
|
|
|
|
for _, s1 := range slots {
|
|
|
|
if slotEqual(s1, s2) {
|
|
|
|
continue outer_loop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fmt.Errorf("%v not found in %v", s2, slots)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func slotEqual(s1, s2 redis.ClusterSlot) bool {
|
|
|
|
if s1.Start != s2.Start {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if s1.End != s2.End {
|
|
|
|
return false
|
|
|
|
}
|
2016-12-16 17:26:48 +03:00
|
|
|
if len(s1.Nodes) != len(s2.Nodes) {
|
|
|
|
return false
|
|
|
|
}
|
2016-04-09 12:52:43 +03:00
|
|
|
for i, n1 := range s1.Nodes {
|
|
|
|
if n1.Addr != s2.Nodes[i].Addr {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-04-28 18:14:19 +03:00
|
|
|
func stopCluster(scenario *clusterScenario) error {
|
|
|
|
for _, client := range scenario.clients {
|
|
|
|
if err := client.Close(); err != nil {
|
|
|
|
return err
|
2015-01-24 15:12:48 +03:00
|
|
|
}
|
2015-04-28 18:14:19 +03:00
|
|
|
}
|
|
|
|
for _, process := range scenario.processes {
|
|
|
|
if err := process.Close(); err != nil {
|
|
|
|
return err
|
2015-01-24 15:12:48 +03:00
|
|
|
}
|
2015-04-28 18:14:19 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2016-10-02 15:44:01 +03:00
|
|
|
var _ = Describe("ClusterClient", func() {
|
2016-12-16 17:26:48 +03:00
|
|
|
var opt *redis.ClusterOptions
|
2016-10-02 15:44:01 +03:00
|
|
|
var client *redis.ClusterClient
|
2015-01-24 15:12:48 +03:00
|
|
|
|
2016-12-16 17:26:48 +03:00
|
|
|
assertClusterClient := func() {
|
2015-01-24 15:12:48 +03:00
|
|
|
It("should CLUSTER SLOTS", func() {
|
2016-10-02 15:44:01 +03:00
|
|
|
res, err := client.ClusterSlots().Result()
|
2015-01-24 15:12:48 +03:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(res).To(HaveLen(3))
|
2016-04-09 12:52:43 +03:00
|
|
|
|
|
|
|
wanted := []redis.ClusterSlot{
|
|
|
|
{0, 4999, []redis.ClusterNode{{"", "127.0.0.1:8220"}, {"", "127.0.0.1:8223"}}},
|
|
|
|
{5000, 9999, []redis.ClusterNode{{"", "127.0.0.1:8221"}, {"", "127.0.0.1:8224"}}},
|
|
|
|
{10000, 16383, []redis.ClusterNode{{"", "127.0.0.1:8222"}, {"", "127.0.0.1:8225"}}},
|
|
|
|
}
|
|
|
|
Expect(assertSlotsEqual(res, wanted)).NotTo(HaveOccurred())
|
2015-01-24 15:12:48 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
It("should CLUSTER NODES", func() {
|
2016-10-02 15:44:01 +03:00
|
|
|
res, err := client.ClusterNodes().Result()
|
2015-01-24 15:12:48 +03:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(len(res)).To(BeNumerically(">", 400))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("should CLUSTER INFO", func() {
|
2016-10-02 15:44:01 +03:00
|
|
|
res, err := client.ClusterInfo().Result()
|
2015-01-24 15:12:48 +03:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(res).To(ContainSubstring("cluster_known_nodes:6"))
|
|
|
|
})
|
|
|
|
|
2015-12-28 19:58:04 +03:00
|
|
|
It("should CLUSTER KEYSLOT", func() {
|
2016-10-02 15:44:01 +03:00
|
|
|
hashSlot, err := client.ClusterKeySlot("somekey").Result()
|
2015-12-28 19:58:04 +03:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2015-12-30 16:53:45 +03:00
|
|
|
Expect(hashSlot).To(Equal(int64(hashtag.Slot("somekey"))))
|
2015-12-29 19:16:14 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
It("should CLUSTER COUNT-FAILURE-REPORTS", func() {
|
2016-10-02 15:44:01 +03:00
|
|
|
n, err := client.ClusterCountFailureReports(cluster.nodeIds[0]).Result()
|
2015-12-29 19:16:14 +03:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(n).To(Equal(int64(0)))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("should CLUSTER COUNTKEYSINSLOT", func() {
|
2016-10-02 15:44:01 +03:00
|
|
|
n, err := client.ClusterCountKeysInSlot(10).Result()
|
2015-12-29 19:16:14 +03:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(n).To(Equal(int64(0)))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("should CLUSTER SAVECONFIG", func() {
|
2016-10-02 15:44:01 +03:00
|
|
|
res, err := client.ClusterSaveConfig().Result()
|
2015-12-29 19:16:14 +03:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(res).To(Equal("OK"))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("should CLUSTER SLAVES", func() {
|
2016-10-02 15:44:01 +03:00
|
|
|
nodesList, err := client.ClusterSlaves(cluster.nodeIds[0]).Result()
|
2015-12-29 19:16:14 +03:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(nodesList).Should(ContainElement(ContainSubstring("slave")))
|
|
|
|
Expect(nodesList).Should(HaveLen(1))
|
|
|
|
})
|
|
|
|
|
2015-01-24 15:12:48 +03:00
|
|
|
It("should GET/SET/DEL", func() {
|
|
|
|
val, err := client.Get("A").Result()
|
|
|
|
Expect(err).To(Equal(redis.Nil))
|
|
|
|
Expect(val).To(Equal(""))
|
|
|
|
|
2015-01-31 12:08:56 +03:00
|
|
|
val, err = client.Set("A", "VALUE", 0).Result()
|
2015-01-24 15:12:48 +03:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(val).To(Equal("OK"))
|
|
|
|
|
|
|
|
val, err = client.Get("A").Result()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(val).To(Equal("VALUE"))
|
|
|
|
|
|
|
|
cnt, err := client.Del("A").Result()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(cnt).To(Equal(int64(1)))
|
|
|
|
})
|
|
|
|
|
2016-06-17 15:09:38 +03:00
|
|
|
It("returns pool stats", func() {
|
2016-01-19 19:36:40 +03:00
|
|
|
Expect(client.PoolStats()).To(BeAssignableToTypeOf(&redis.PoolStats{}))
|
|
|
|
})
|
|
|
|
|
2016-10-02 15:44:01 +03:00
|
|
|
It("removes idle connections", func() {
|
|
|
|
stats := client.PoolStats()
|
|
|
|
Expect(stats.TotalConns).NotTo(BeZero())
|
|
|
|
Expect(stats.FreeConns).NotTo(BeZero())
|
|
|
|
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
|
|
|
|
stats = client.PoolStats()
|
|
|
|
Expect(stats.TotalConns).To(BeZero())
|
|
|
|
Expect(stats.FreeConns).To(BeZero())
|
|
|
|
})
|
|
|
|
|
2016-06-17 15:09:38 +03:00
|
|
|
It("follows redirects", func() {
|
2015-01-31 12:08:56 +03:00
|
|
|
Expect(client.Set("A", "VALUE", 0).Err()).NotTo(HaveOccurred())
|
2015-05-01 10:42:58 +03:00
|
|
|
|
2015-12-30 16:53:45 +03:00
|
|
|
slot := hashtag.Slot("A")
|
2016-05-06 21:12:31 +03:00
|
|
|
Expect(client.SwapSlotNodes(slot)).To(Equal([]string{"127.0.0.1:8224", "127.0.0.1:8221"}))
|
2015-01-24 15:12:48 +03:00
|
|
|
|
|
|
|
val, err := client.Get("A").Result()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(val).To(Equal("VALUE"))
|
2015-03-18 13:41:24 +03:00
|
|
|
})
|
|
|
|
|
2016-06-17 15:09:38 +03:00
|
|
|
It("returns an error when there are no attempts left", func() {
|
2016-10-02 15:44:01 +03:00
|
|
|
opt := redisClusterOptions()
|
|
|
|
opt.MaxRedirects = -1
|
|
|
|
client := cluster.clusterClient(opt)
|
2015-05-10 16:01:38 +03:00
|
|
|
|
2015-12-30 16:53:45 +03:00
|
|
|
slot := hashtag.Slot("A")
|
2016-05-06 21:12:31 +03:00
|
|
|
Expect(client.SwapSlotNodes(slot)).To(Equal([]string{"127.0.0.1:8224", "127.0.0.1:8221"}))
|
2015-05-10 16:01:38 +03:00
|
|
|
|
|
|
|
err := client.Get("A").Err()
|
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
Expect(err.Error()).To(ContainSubstring("MOVED"))
|
2016-06-05 14:30:56 +03:00
|
|
|
|
|
|
|
Expect(client.Close()).NotTo(HaveOccurred())
|
2015-05-10 16:01:38 +03:00
|
|
|
})
|
2015-12-16 17:11:52 +03:00
|
|
|
|
2016-10-09 14:12:32 +03:00
|
|
|
It("distributes keys", func() {
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
err := client.Set(fmt.Sprintf("key%d", i), "value", 0).Err()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
}
|
|
|
|
|
|
|
|
wanted := []string{"keys=31", "keys=29", "keys=40"}
|
|
|
|
for i, master := range cluster.masters() {
|
|
|
|
Expect(master.Info().Val()).To(ContainSubstring(wanted[i]))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
It("distributes keys when using EVAL", func() {
|
|
|
|
script := redis.NewScript(`
|
|
|
|
local r = redis.call('SET', KEYS[1], ARGV[1])
|
|
|
|
return r
|
|
|
|
`)
|
|
|
|
|
|
|
|
var key string
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
key = fmt.Sprintf("key%d", i)
|
|
|
|
err := script.Run(client, []string{key}, "value").Err()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
}
|
|
|
|
|
|
|
|
wanted := []string{"keys=31", "keys=29", "keys=40"}
|
|
|
|
for i, master := range cluster.masters() {
|
|
|
|
Expect(master.Info().Val()).To(ContainSubstring(wanted[i]))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2016-06-17 15:09:38 +03:00
|
|
|
It("supports Watch", func() {
|
2015-12-16 17:11:52 +03:00
|
|
|
var incr func(string) error
|
|
|
|
|
|
|
|
// Transactionally increments key using GET and SET commands.
|
|
|
|
incr = func(key string) error {
|
2016-05-02 15:54:15 +03:00
|
|
|
err := client.Watch(func(tx *redis.Tx) error {
|
|
|
|
n, err := tx.Get(key).Int64()
|
|
|
|
if err != nil && err != redis.Nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-05-02 18:00:53 +03:00
|
|
|
_, err = tx.Pipelined(func(pipe redis.Pipeliner) error {
|
2016-10-13 14:36:15 +03:00
|
|
|
pipe.Set(key, strconv.FormatInt(n+1, 10), 0)
|
2016-05-02 15:54:15 +03:00
|
|
|
return nil
|
|
|
|
})
|
2015-12-16 17:11:52 +03:00
|
|
|
return err
|
2016-05-02 15:54:15 +03:00
|
|
|
}, key)
|
2015-12-16 17:11:52 +03:00
|
|
|
if err == redis.TxFailedErr {
|
|
|
|
return incr(key)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
2016-07-02 15:52:10 +03:00
|
|
|
defer GinkgoRecover()
|
2015-12-16 17:11:52 +03:00
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
err := incr("key")
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
n, err := client.Get("key").Int64()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(n).To(Equal(int64(100)))
|
|
|
|
})
|
2016-04-06 14:01:08 +03:00
|
|
|
|
2016-12-13 18:28:39 +03:00
|
|
|
Describe("pipelining", func() {
|
|
|
|
var pipe *redis.Pipeline
|
2016-04-06 14:01:08 +03:00
|
|
|
|
2016-12-13 18:28:39 +03:00
|
|
|
assertPipeline := func() {
|
2016-12-16 17:26:48 +03:00
|
|
|
keys := []string{"A", "B", "C", "D", "E", "F", "G"}
|
2016-04-06 14:01:08 +03:00
|
|
|
|
2016-12-16 17:26:48 +03:00
|
|
|
It("follows redirects", func() {
|
|
|
|
for _, key := range keys {
|
|
|
|
slot := hashtag.Slot(key)
|
|
|
|
client.SwapSlotNodes(slot)
|
|
|
|
}
|
2016-06-05 14:30:56 +03:00
|
|
|
|
2016-12-13 18:28:39 +03:00
|
|
|
for i, key := range keys {
|
|
|
|
pipe.Set(key, key+"_value", 0)
|
|
|
|
pipe.Expire(key, time.Duration(i+1)*time.Hour)
|
|
|
|
}
|
|
|
|
cmds, err := pipe.Exec()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(cmds).To(HaveLen(14))
|
2016-06-05 14:30:56 +03:00
|
|
|
|
2016-12-16 17:26:48 +03:00
|
|
|
if opt.RouteByLatency {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, key := range keys {
|
|
|
|
slot := hashtag.Slot(key)
|
|
|
|
client.SwapSlotNodes(slot)
|
|
|
|
}
|
|
|
|
|
2016-12-13 18:28:39 +03:00
|
|
|
for _, key := range keys {
|
|
|
|
pipe.Get(key)
|
|
|
|
pipe.TTL(key)
|
|
|
|
}
|
|
|
|
cmds, err = pipe.Exec()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(cmds).To(HaveLen(14))
|
2016-12-16 17:26:48 +03:00
|
|
|
|
|
|
|
for i, key := range keys {
|
|
|
|
get := cmds[i*2].(*redis.StringCmd)
|
|
|
|
Expect(get.Val()).To(Equal(key + "_value"))
|
|
|
|
|
|
|
|
ttl := cmds[(i*2)+1].(*redis.DurationCmd)
|
|
|
|
Expect(ttl.Val()).To(BeNumerically("~", time.Duration(i+1)*time.Hour, time.Second))
|
|
|
|
}
|
2016-12-13 18:28:39 +03:00
|
|
|
})
|
2016-04-06 14:01:08 +03:00
|
|
|
|
2016-12-13 18:28:39 +03:00
|
|
|
It("works with missing keys", func() {
|
2016-12-16 17:26:48 +03:00
|
|
|
pipe.Set("A", "A_value", 0)
|
|
|
|
pipe.Set("C", "C_value", 0)
|
|
|
|
_, err := pipe.Exec()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
|
|
|
a := pipe.Get("A")
|
|
|
|
b := pipe.Get("B")
|
|
|
|
c := pipe.Get("C")
|
|
|
|
cmds, err := pipe.Exec()
|
2016-12-13 18:28:39 +03:00
|
|
|
Expect(err).To(Equal(redis.Nil))
|
|
|
|
Expect(cmds).To(HaveLen(3))
|
|
|
|
|
|
|
|
Expect(a.Err()).NotTo(HaveOccurred())
|
|
|
|
Expect(a.Val()).To(Equal("A_value"))
|
|
|
|
|
|
|
|
Expect(b.Err()).To(Equal(redis.Nil))
|
|
|
|
Expect(b.Val()).To(Equal(""))
|
|
|
|
|
|
|
|
Expect(c.Err()).NotTo(HaveOccurred())
|
|
|
|
Expect(c.Val()).To(Equal("C_value"))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
Describe("Pipeline", func() {
|
|
|
|
BeforeEach(func() {
|
2017-05-01 18:42:58 +03:00
|
|
|
pipe = client.Pipeline().(*redis.Pipeline)
|
2016-10-09 14:12:32 +03:00
|
|
|
})
|
2016-04-06 14:01:08 +03:00
|
|
|
|
2016-12-13 18:28:39 +03:00
|
|
|
AfterEach(func() {
|
|
|
|
Expect(pipe.Close()).NotTo(HaveOccurred())
|
|
|
|
})
|
|
|
|
|
|
|
|
assertPipeline()
|
|
|
|
})
|
2016-04-06 14:01:08 +03:00
|
|
|
|
2016-12-13 18:28:39 +03:00
|
|
|
Describe("TxPipeline", func() {
|
|
|
|
BeforeEach(func() {
|
2017-05-01 18:42:58 +03:00
|
|
|
pipe = client.TxPipeline().(*redis.Pipeline)
|
2016-12-13 18:28:39 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
AfterEach(func() {
|
|
|
|
Expect(pipe.Close()).NotTo(HaveOccurred())
|
|
|
|
})
|
2016-04-06 14:01:08 +03:00
|
|
|
|
2016-12-13 18:28:39 +03:00
|
|
|
assertPipeline()
|
2016-10-09 14:12:32 +03:00
|
|
|
})
|
2016-04-06 14:01:08 +03:00
|
|
|
})
|
2016-06-17 15:09:38 +03:00
|
|
|
|
|
|
|
It("calls fn for every master node", func() {
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
Expect(client.Set(strconv.Itoa(i), "", 0).Err()).NotTo(HaveOccurred())
|
|
|
|
}
|
|
|
|
|
|
|
|
err := client.ForEachMaster(func(master *redis.Client) error {
|
2017-06-17 12:53:16 +03:00
|
|
|
return master.FlushDB().Err()
|
2016-06-17 15:09:38 +03:00
|
|
|
})
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
|
|
|
for _, client := range cluster.masters() {
|
|
|
|
keys, err := client.Keys("*").Result()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(keys).To(HaveLen(0))
|
|
|
|
}
|
|
|
|
})
|
2016-06-05 14:30:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Describe("default ClusterClient", func() {
|
|
|
|
BeforeEach(func() {
|
2016-12-16 17:26:48 +03:00
|
|
|
opt = redisClusterOptions()
|
|
|
|
client = cluster.clusterClient(opt)
|
2016-06-17 15:09:38 +03:00
|
|
|
|
|
|
|
_ = client.ForEachMaster(func(master *redis.Client) error {
|
2017-06-17 12:53:16 +03:00
|
|
|
return master.FlushDB().Err()
|
2016-06-17 15:09:38 +03:00
|
|
|
})
|
2016-06-05 14:30:56 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
AfterEach(func() {
|
|
|
|
Expect(client.Close()).NotTo(HaveOccurred())
|
|
|
|
})
|
|
|
|
|
2016-12-16 17:26:48 +03:00
|
|
|
assertClusterClient()
|
2016-06-05 14:30:56 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
Describe("ClusterClient with RouteByLatency", func() {
|
|
|
|
BeforeEach(func() {
|
2016-12-16 17:26:48 +03:00
|
|
|
opt = redisClusterOptions()
|
2016-10-02 15:44:01 +03:00
|
|
|
opt.RouteByLatency = true
|
|
|
|
client = cluster.clusterClient(opt)
|
2016-06-17 15:09:38 +03:00
|
|
|
|
|
|
|
_ = client.ForEachMaster(func(master *redis.Client) error {
|
2017-06-17 12:53:16 +03:00
|
|
|
return master.FlushDB().Err()
|
2016-06-17 15:09:38 +03:00
|
|
|
})
|
2016-06-05 14:30:56 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
AfterEach(func() {
|
2017-06-17 12:53:16 +03:00
|
|
|
client.FlushDB()
|
2016-06-05 14:30:56 +03:00
|
|
|
Expect(client.Close()).NotTo(HaveOccurred())
|
|
|
|
})
|
|
|
|
|
2016-12-16 17:26:48 +03:00
|
|
|
assertClusterClient()
|
2016-04-06 14:01:08 +03:00
|
|
|
})
|
2016-12-03 18:30:13 +03:00
|
|
|
})
|
2016-09-23 14:52:19 +03:00
|
|
|
|
2016-12-03 18:30:13 +03:00
|
|
|
var _ = Describe("ClusterClient without nodes", func() {
|
|
|
|
var client *redis.ClusterClient
|
|
|
|
|
|
|
|
BeforeEach(func() {
|
|
|
|
client = redis.NewClusterClient(&redis.ClusterOptions{})
|
|
|
|
})
|
|
|
|
|
|
|
|
AfterEach(func() {
|
|
|
|
Expect(client.Close()).NotTo(HaveOccurred())
|
|
|
|
})
|
|
|
|
|
|
|
|
It("returns an error", func() {
|
|
|
|
err := client.Ping().Err()
|
|
|
|
Expect(err).To(MatchError("redis: cluster has no nodes"))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("pipeline returns an error", func() {
|
2017-05-02 18:00:53 +03:00
|
|
|
_, err := client.Pipelined(func(pipe redis.Pipeliner) error {
|
2016-12-03 18:30:13 +03:00
|
|
|
pipe.Ping()
|
|
|
|
return nil
|
2016-10-09 14:12:32 +03:00
|
|
|
})
|
2016-12-03 18:30:13 +03:00
|
|
|
Expect(err).To(MatchError("redis: cluster has no nodes"))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
var _ = Describe("ClusterClient without valid nodes", func() {
|
|
|
|
var client *redis.ClusterClient
|
|
|
|
|
|
|
|
BeforeEach(func() {
|
|
|
|
client = redis.NewClusterClient(&redis.ClusterOptions{
|
|
|
|
Addrs: []string{redisAddr},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
AfterEach(func() {
|
|
|
|
Expect(client.Close()).NotTo(HaveOccurred())
|
|
|
|
})
|
|
|
|
|
|
|
|
It("returns an error", func() {
|
|
|
|
err := client.Ping().Err()
|
|
|
|
Expect(err).To(MatchError("ERR This instance has cluster support disabled"))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("pipeline returns an error", func() {
|
2017-05-02 18:00:53 +03:00
|
|
|
_, err := client.Pipelined(func(pipe redis.Pipeliner) error {
|
2016-12-03 18:30:13 +03:00
|
|
|
pipe.Ping()
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
Expect(err).To(MatchError("ERR This instance has cluster support disabled"))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
var _ = Describe("ClusterClient timeout", func() {
|
|
|
|
var client *redis.ClusterClient
|
2016-10-09 14:12:32 +03:00
|
|
|
|
2016-12-03 18:30:13 +03:00
|
|
|
AfterEach(func() {
|
2017-03-04 14:04:27 +03:00
|
|
|
_ = client.Close()
|
2016-12-03 18:30:13 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
testTimeout := func() {
|
|
|
|
It("Ping timeouts", func() {
|
2016-10-09 14:12:32 +03:00
|
|
|
err := client.Ping().Err()
|
2016-12-03 18:30:13 +03:00
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
Expect(err.(net.Error).Timeout()).To(BeTrue())
|
2016-10-09 14:12:32 +03:00
|
|
|
})
|
|
|
|
|
2016-12-03 18:30:13 +03:00
|
|
|
It("Pipeline timeouts", func() {
|
2017-05-02 18:00:53 +03:00
|
|
|
_, err := client.Pipelined(func(pipe redis.Pipeliner) error {
|
2016-10-09 14:12:32 +03:00
|
|
|
pipe.Ping()
|
|
|
|
return nil
|
|
|
|
})
|
2016-12-03 18:30:13 +03:00
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
Expect(err.(net.Error).Timeout()).To(BeTrue())
|
2016-10-09 14:12:32 +03:00
|
|
|
})
|
|
|
|
|
2016-12-03 18:30:13 +03:00
|
|
|
It("Tx timeouts", func() {
|
|
|
|
err := client.Watch(func(tx *redis.Tx) error {
|
|
|
|
return tx.Ping().Err()
|
2016-09-23 14:52:19 +03:00
|
|
|
})
|
2016-12-03 18:30:13 +03:00
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
Expect(err.(net.Error).Timeout()).To(BeTrue())
|
2016-09-23 14:52:19 +03:00
|
|
|
})
|
|
|
|
|
2016-12-03 18:30:13 +03:00
|
|
|
It("Tx Pipeline timeouts", func() {
|
|
|
|
err := client.Watch(func(tx *redis.Tx) error {
|
2017-05-02 18:00:53 +03:00
|
|
|
_, err := tx.Pipelined(func(pipe redis.Pipeliner) error {
|
2016-12-03 18:30:13 +03:00
|
|
|
pipe.Ping()
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
Expect(err.(net.Error).Timeout()).To(BeTrue())
|
2016-09-23 14:52:19 +03:00
|
|
|
})
|
2016-12-03 18:30:13 +03:00
|
|
|
}
|
2016-10-09 14:12:32 +03:00
|
|
|
|
2016-12-03 18:30:13 +03:00
|
|
|
Context("read timeout", func() {
|
|
|
|
BeforeEach(func() {
|
|
|
|
opt := redisClusterOptions()
|
|
|
|
opt.ReadTimeout = time.Nanosecond
|
|
|
|
opt.WriteTimeout = -1
|
|
|
|
client = cluster.clusterClient(opt)
|
|
|
|
})
|
|
|
|
|
|
|
|
testTimeout()
|
|
|
|
})
|
|
|
|
|
|
|
|
Context("write timeout", func() {
|
|
|
|
BeforeEach(func() {
|
|
|
|
opt := redisClusterOptions()
|
|
|
|
opt.ReadTimeout = time.Nanosecond
|
|
|
|
opt.WriteTimeout = -1
|
|
|
|
client = cluster.clusterClient(opt)
|
2016-10-09 14:12:32 +03:00
|
|
|
})
|
2016-12-03 18:30:13 +03:00
|
|
|
|
|
|
|
testTimeout()
|
2016-09-23 14:52:19 +03:00
|
|
|
})
|
2016-12-12 18:30:08 +03:00
|
|
|
|
|
|
|
Context("network timeout", func() {
|
|
|
|
const pause = time.Second
|
|
|
|
|
|
|
|
BeforeEach(func() {
|
|
|
|
opt := redisClusterOptions()
|
|
|
|
opt.ReadTimeout = 100 * time.Millisecond
|
|
|
|
opt.WriteTimeout = 100 * time.Millisecond
|
|
|
|
opt.MaxRedirects = 1
|
|
|
|
client = cluster.clusterClient(opt)
|
|
|
|
|
|
|
|
err := client.ForEachNode(func(client *redis.Client) error {
|
|
|
|
return client.ClientPause(pause).Err()
|
|
|
|
})
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
})
|
|
|
|
|
|
|
|
AfterEach(func() {
|
|
|
|
Eventually(func() error {
|
|
|
|
return client.ForEachNode(func(client *redis.Client) error {
|
|
|
|
return client.Ping().Err()
|
|
|
|
})
|
2016-12-13 18:28:39 +03:00
|
|
|
}, 2*pause).ShouldNot(HaveOccurred())
|
2016-12-12 18:30:08 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
testTimeout()
|
|
|
|
})
|
2015-01-24 15:12:48 +03:00
|
|
|
})
|
|
|
|
|
2015-04-28 18:14:19 +03:00
|
|
|
//------------------------------------------------------------------------------
|
2015-01-24 15:12:48 +03:00
|
|
|
|
2015-04-28 18:14:19 +03:00
|
|
|
func BenchmarkRedisClusterPing(b *testing.B) {
|
2015-07-18 11:22:42 +03:00
|
|
|
if testing.Short() {
|
|
|
|
b.Skip("skipping in short mode")
|
|
|
|
}
|
|
|
|
|
2015-05-14 16:13:45 +03:00
|
|
|
cluster := &clusterScenario{
|
2015-04-28 18:14:19 +03:00
|
|
|
ports: []string{"8220", "8221", "8222", "8223", "8224", "8225"},
|
|
|
|
nodeIds: make([]string, 6),
|
|
|
|
processes: make(map[string]*redisProcess, 6),
|
|
|
|
clients: make(map[string]*redis.Client, 6),
|
2015-01-24 15:12:48 +03:00
|
|
|
}
|
2016-10-02 15:44:01 +03:00
|
|
|
|
2015-05-14 16:13:45 +03:00
|
|
|
if err := startCluster(cluster); err != nil {
|
2015-04-28 18:14:19 +03:00
|
|
|
b.Fatal(err)
|
2015-01-24 15:12:48 +03:00
|
|
|
}
|
2015-05-14 16:13:45 +03:00
|
|
|
defer stopCluster(cluster)
|
2016-10-02 15:44:01 +03:00
|
|
|
|
|
|
|
client := cluster.clusterClient(redisClusterOptions())
|
2015-04-28 18:14:19 +03:00
|
|
|
defer client.Close()
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
for pb.Next() {
|
|
|
|
if err := client.Ping().Err(); err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2015-01-24 15:12:48 +03:00
|
|
|
}
|
2017-03-24 13:48:43 +03:00
|
|
|
|
|
|
|
func BenchmarkRedisClusterSetString(b *testing.B) {
|
|
|
|
if testing.Short() {
|
|
|
|
b.Skip("skipping in short mode")
|
|
|
|
}
|
|
|
|
|
|
|
|
cluster := &clusterScenario{
|
|
|
|
ports: []string{"8220", "8221", "8222", "8223", "8224", "8225"},
|
|
|
|
nodeIds: make([]string, 6),
|
|
|
|
processes: make(map[string]*redisProcess, 6),
|
|
|
|
clients: make(map[string]*redis.Client, 6),
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := startCluster(cluster); err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
defer stopCluster(cluster)
|
|
|
|
|
|
|
|
client := cluster.clusterClient(redisClusterOptions())
|
|
|
|
defer client.Close()
|
|
|
|
|
|
|
|
value := string(bytes.Repeat([]byte{'1'}, 10000))
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
for pb.Next() {
|
|
|
|
if err := client.Set("key", value, 0).Err(); err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|