2014-07-02 19:04:38 +04:00
|
|
|
package redis_test
|
|
|
|
|
|
|
|
import (
|
2020-09-09 17:39:13 +03:00
|
|
|
"net"
|
|
|
|
|
2015-01-15 18:51:22 +03:00
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
2021-09-08 16:00:52 +03:00
|
|
|
|
|
|
|
"github.com/go-redis/redis/v8"
|
2014-07-02 19:04:38 +04:00
|
|
|
)
|
|
|
|
|
2015-01-15 18:51:22 +03:00
|
|
|
var _ = Describe("Sentinel", func() {
|
2015-05-14 16:13:45 +03:00
|
|
|
var client *redis.Client
|
2020-09-09 17:39:13 +03:00
|
|
|
var master *redis.Client
|
|
|
|
var masterPort string
|
2021-03-12 14:06:11 +03:00
|
|
|
var sentinel *redis.SentinelClient
|
2014-11-13 15:26:14 +03:00
|
|
|
|
2015-05-14 16:13:45 +03:00
|
|
|
BeforeEach(func() {
|
|
|
|
client = redis.NewFailoverClient(&redis.FailoverOptions{
|
|
|
|
MasterName: sentinelName,
|
2020-09-03 22:11:56 +03:00
|
|
|
SentinelAddrs: sentinelAddrs,
|
2020-09-11 11:24:38 +03:00
|
|
|
MaxRetries: -1,
|
2015-01-15 18:51:22 +03:00
|
|
|
})
|
2020-03-11 17:26:42 +03:00
|
|
|
Expect(client.FlushDB(ctx).Err()).NotTo(HaveOccurred())
|
2020-09-09 17:39:13 +03:00
|
|
|
|
2021-03-12 14:06:11 +03:00
|
|
|
sentinel = redis.NewSentinelClient(&redis.Options{
|
2020-09-11 11:24:38 +03:00
|
|
|
Addr: ":" + sentinelPort1,
|
|
|
|
MaxRetries: -1,
|
2020-09-09 17:39:13 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
addr, err := sentinel.GetMasterAddrByName(ctx, sentinelName).Result()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
|
|
|
master = redis.NewClient(&redis.Options{
|
2020-09-11 11:24:38 +03:00
|
|
|
Addr: net.JoinHostPort(addr[0], addr[1]),
|
|
|
|
MaxRetries: -1,
|
2020-09-09 17:39:13 +03:00
|
|
|
})
|
|
|
|
masterPort = addr[1]
|
2020-09-11 11:24:38 +03:00
|
|
|
|
|
|
|
// Wait until slaves are picked up by sentinel.
|
|
|
|
Eventually(func() string {
|
|
|
|
return sentinel1.Info(ctx).Val()
|
|
|
|
}, "15s", "100ms").Should(ContainSubstring("slaves=2"))
|
|
|
|
Eventually(func() string {
|
|
|
|
return sentinel2.Info(ctx).Val()
|
|
|
|
}, "15s", "100ms").Should(ContainSubstring("slaves=2"))
|
|
|
|
Eventually(func() string {
|
|
|
|
return sentinel3.Info(ctx).Val()
|
|
|
|
}, "15s", "100ms").Should(ContainSubstring("slaves=2"))
|
2015-05-14 16:13:45 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
AfterEach(func() {
|
2020-09-10 09:19:07 +03:00
|
|
|
_ = client.Close()
|
|
|
|
_ = master.Close()
|
2021-03-12 14:06:11 +03:00
|
|
|
_ = sentinel.Close()
|
2015-05-14 16:13:45 +03:00
|
|
|
})
|
2015-01-15 18:51:22 +03:00
|
|
|
|
2015-05-14 16:13:45 +03:00
|
|
|
It("should facilitate failover", func() {
|
2017-06-29 16:53:49 +03:00
|
|
|
// Set value on master.
|
2020-03-11 17:26:42 +03:00
|
|
|
err := client.Set(ctx, "foo", "master", 0).Err()
|
2015-01-15 18:51:22 +03:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
2017-06-29 16:53:49 +03:00
|
|
|
// Verify.
|
2020-09-09 16:20:58 +03:00
|
|
|
val, err := client.Get(ctx, "foo").Result()
|
2015-01-15 18:51:22 +03:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(val).To(Equal("master"))
|
|
|
|
|
2021-03-12 14:06:11 +03:00
|
|
|
// Verify master->slaves sync.
|
|
|
|
var slavesAddr []string
|
|
|
|
Eventually(func() []string {
|
2021-03-12 17:08:58 +03:00
|
|
|
slavesAddr = redis.GetSlavesAddrByName(ctx, sentinel, sentinelName)
|
2021-03-12 14:06:11 +03:00
|
|
|
return slavesAddr
|
|
|
|
}, "15s", "100ms").Should(HaveLen(2))
|
|
|
|
Eventually(func() bool {
|
|
|
|
sync := true
|
|
|
|
for _, addr := range slavesAddr {
|
|
|
|
slave := redis.NewClient(&redis.Options{
|
|
|
|
Addr: addr,
|
|
|
|
MaxRetries: -1,
|
|
|
|
})
|
|
|
|
sync = slave.Get(ctx, "foo").Val() == "master"
|
|
|
|
_ = slave.Close()
|
|
|
|
}
|
|
|
|
return sync
|
|
|
|
}, "15s", "100ms").Should(BeTrue())
|
|
|
|
|
2017-06-29 16:53:49 +03:00
|
|
|
// Create subscription.
|
2021-03-12 14:06:11 +03:00
|
|
|
pub := client.Subscribe(ctx, "foo")
|
|
|
|
ch := pub.Channel()
|
2017-06-29 16:53:49 +03:00
|
|
|
|
2015-01-15 18:51:22 +03:00
|
|
|
// Kill master.
|
2020-09-09 17:39:13 +03:00
|
|
|
err = master.Shutdown(ctx).Err()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2015-01-15 18:51:22 +03:00
|
|
|
Eventually(func() error {
|
2020-09-09 17:39:13 +03:00
|
|
|
return master.Ping(ctx).Err()
|
2020-08-15 16:23:14 +03:00
|
|
|
}, "15s", "100ms").Should(HaveOccurred())
|
2015-01-15 18:51:22 +03:00
|
|
|
|
|
|
|
// Check that client picked up new master.
|
2020-09-11 11:24:38 +03:00
|
|
|
Eventually(func() string {
|
|
|
|
return client.Get(ctx, "foo").Val()
|
|
|
|
}, "15s", "100ms").Should(Equal("master"))
|
2017-06-29 16:53:49 +03:00
|
|
|
|
2020-09-05 10:56:09 +03:00
|
|
|
// Check if subscription is renewed.
|
2017-06-29 16:53:49 +03:00
|
|
|
var msg *redis.Message
|
2020-09-05 10:56:09 +03:00
|
|
|
Eventually(func() <-chan *redis.Message {
|
|
|
|
_ = client.Publish(ctx, "foo", "hello").Err()
|
|
|
|
return ch
|
2020-09-09 15:27:17 +03:00
|
|
|
}, "15s", "100ms").Should(Receive(&msg))
|
2017-06-29 16:53:49 +03:00
|
|
|
Expect(msg.Channel).To(Equal("foo"))
|
|
|
|
Expect(msg.Payload).To(Equal("hello"))
|
2021-03-12 14:06:11 +03:00
|
|
|
Expect(pub.Close()).NotTo(HaveOccurred())
|
2020-09-09 16:20:58 +03:00
|
|
|
|
2020-09-09 17:39:13 +03:00
|
|
|
_, err = startRedis(masterPort)
|
2020-09-09 16:20:58 +03:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2014-07-02 19:04:38 +04:00
|
|
|
})
|
|
|
|
|
2015-05-14 16:13:45 +03:00
|
|
|
It("supports DB selection", func() {
|
|
|
|
Expect(client.Close()).NotTo(HaveOccurred())
|
|
|
|
|
|
|
|
client = redis.NewFailoverClient(&redis.FailoverOptions{
|
|
|
|
MasterName: sentinelName,
|
2020-09-03 22:11:56 +03:00
|
|
|
SentinelAddrs: sentinelAddrs,
|
2015-05-14 16:13:45 +03:00
|
|
|
DB: 1,
|
|
|
|
})
|
2020-03-11 17:26:42 +03:00
|
|
|
err := client.Ping(ctx).Err()
|
2015-05-14 16:13:45 +03:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
})
|
2015-01-15 18:51:22 +03:00
|
|
|
})
|
2020-09-09 15:27:17 +03:00
|
|
|
|
|
|
|
var _ = Describe("NewFailoverClusterClient", func() {
|
|
|
|
var client *redis.ClusterClient
|
2020-09-09 17:39:13 +03:00
|
|
|
var master *redis.Client
|
|
|
|
var masterPort string
|
2020-09-09 15:27:17 +03:00
|
|
|
|
|
|
|
BeforeEach(func() {
|
|
|
|
client = redis.NewFailoverClusterClient(&redis.FailoverOptions{
|
|
|
|
MasterName: sentinelName,
|
|
|
|
SentinelAddrs: sentinelAddrs,
|
2020-09-11 15:52:38 +03:00
|
|
|
|
|
|
|
RouteRandomly: true,
|
2020-09-09 15:27:17 +03:00
|
|
|
})
|
|
|
|
Expect(client.FlushDB(ctx).Err()).NotTo(HaveOccurred())
|
2020-09-09 17:39:13 +03:00
|
|
|
|
|
|
|
sentinel := redis.NewSentinelClient(&redis.Options{
|
2020-09-11 11:24:38 +03:00
|
|
|
Addr: ":" + sentinelPort1,
|
|
|
|
MaxRetries: -1,
|
2020-09-09 17:39:13 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
addr, err := sentinel.GetMasterAddrByName(ctx, sentinelName).Result()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
|
|
|
master = redis.NewClient(&redis.Options{
|
2020-09-11 11:24:38 +03:00
|
|
|
Addr: net.JoinHostPort(addr[0], addr[1]),
|
|
|
|
MaxRetries: -1,
|
2020-09-09 17:39:13 +03:00
|
|
|
})
|
|
|
|
masterPort = addr[1]
|
2020-09-11 11:24:38 +03:00
|
|
|
|
|
|
|
// Wait until slaves are picked up by sentinel.
|
|
|
|
Eventually(func() string {
|
|
|
|
return sentinel1.Info(ctx).Val()
|
|
|
|
}, "15s", "100ms").Should(ContainSubstring("slaves=2"))
|
|
|
|
Eventually(func() string {
|
|
|
|
return sentinel2.Info(ctx).Val()
|
|
|
|
}, "15s", "100ms").Should(ContainSubstring("slaves=2"))
|
|
|
|
Eventually(func() string {
|
|
|
|
return sentinel3.Info(ctx).Val()
|
|
|
|
}, "15s", "100ms").Should(ContainSubstring("slaves=2"))
|
2020-09-09 15:27:17 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
AfterEach(func() {
|
2020-09-10 09:19:07 +03:00
|
|
|
_ = client.Close()
|
|
|
|
_ = master.Close()
|
2020-09-09 15:27:17 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
It("should facilitate failover", func() {
|
2020-09-09 17:39:13 +03:00
|
|
|
// Set value.
|
2020-09-09 15:27:17 +03:00
|
|
|
err := client.Set(ctx, "foo", "master", 0).Err()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
2020-09-11 15:52:38 +03:00
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
// Verify.
|
2020-09-11 16:56:02 +03:00
|
|
|
Eventually(func() string {
|
|
|
|
return client.Get(ctx, "foo").Val()
|
|
|
|
}, "15s", "1ms").Should(Equal("master"))
|
2020-09-11 15:52:38 +03:00
|
|
|
}
|
2020-09-09 15:27:17 +03:00
|
|
|
|
|
|
|
// Create subscription.
|
2021-04-27 10:04:46 +03:00
|
|
|
sub := client.Subscribe(ctx, "foo")
|
|
|
|
ch := sub.Channel()
|
2020-09-09 15:27:17 +03:00
|
|
|
|
|
|
|
// Kill master.
|
2020-09-09 17:39:13 +03:00
|
|
|
err = master.Shutdown(ctx).Err()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2020-09-09 15:27:17 +03:00
|
|
|
Eventually(func() error {
|
2022-06-04 15:15:43 +03:00
|
|
|
return master.Ping(ctx).Err()
|
2020-09-09 15:27:17 +03:00
|
|
|
}, "15s", "100ms").Should(HaveOccurred())
|
|
|
|
|
|
|
|
// Check that client picked up new master.
|
2020-09-11 11:24:38 +03:00
|
|
|
Eventually(func() string {
|
|
|
|
return client.Get(ctx, "foo").Val()
|
|
|
|
}, "15s", "100ms").Should(Equal("master"))
|
2020-09-09 15:27:17 +03:00
|
|
|
|
|
|
|
// Check if subscription is renewed.
|
|
|
|
var msg *redis.Message
|
|
|
|
Eventually(func() <-chan *redis.Message {
|
|
|
|
_ = client.Publish(ctx, "foo", "hello").Err()
|
|
|
|
return ch
|
|
|
|
}, "15s", "100ms").Should(Receive(&msg))
|
|
|
|
Expect(msg.Channel).To(Equal("foo"))
|
|
|
|
Expect(msg.Payload).To(Equal("hello"))
|
2021-04-27 10:04:46 +03:00
|
|
|
Expect(sub.Close()).NotTo(HaveOccurred())
|
2020-09-09 16:20:58 +03:00
|
|
|
|
2020-09-09 17:39:13 +03:00
|
|
|
_, err = startRedis(masterPort)
|
2020-09-09 16:20:58 +03:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2020-09-09 15:27:17 +03:00
|
|
|
})
|
|
|
|
})
|
2021-09-18 09:39:26 +03:00
|
|
|
|
|
|
|
var _ = Describe("SentinelAclAuth", func() {
|
2021-09-19 08:36:40 +03:00
|
|
|
const (
|
|
|
|
aclSentinelUsername = "sentinel-user"
|
|
|
|
aclSentinelPassword = "sentinel-pass"
|
|
|
|
)
|
|
|
|
|
2021-09-18 09:39:26 +03:00
|
|
|
var client *redis.Client
|
|
|
|
var sentinel *redis.SentinelClient
|
2022-06-04 15:15:43 +03:00
|
|
|
sentinels := func() []*redisProcess {
|
2021-09-23 16:31:21 +03:00
|
|
|
return []*redisProcess{sentinel1, sentinel2, sentinel3}
|
2021-09-19 08:36:40 +03:00
|
|
|
}
|
2021-09-18 09:39:26 +03:00
|
|
|
|
|
|
|
BeforeEach(func() {
|
2021-09-19 08:36:40 +03:00
|
|
|
authCmd := redis.NewStatusCmd(ctx, "ACL", "SETUSER", aclSentinelUsername, "ON",
|
2021-09-23 16:31:21 +03:00
|
|
|
">"+aclSentinelPassword, "-@all", "+auth", "+client|getname", "+client|id", "+client|setname",
|
2021-09-19 08:36:40 +03:00
|
|
|
"+command", "+hello", "+ping", "+role", "+sentinel|get-master-addr-by-name", "+sentinel|master",
|
|
|
|
"+sentinel|myid", "+sentinel|replicas", "+sentinel|sentinels")
|
|
|
|
|
|
|
|
for _, process := range sentinels() {
|
|
|
|
err := process.Client.Process(ctx, authCmd)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
}
|
|
|
|
|
2021-09-18 09:39:26 +03:00
|
|
|
client = redis.NewFailoverClient(&redis.FailoverOptions{
|
2021-09-23 16:31:21 +03:00
|
|
|
MasterName: sentinelName,
|
|
|
|
SentinelAddrs: sentinelAddrs,
|
|
|
|
MaxRetries: -1,
|
2021-09-18 09:39:26 +03:00
|
|
|
SentinelUsername: aclSentinelUsername,
|
|
|
|
SentinelPassword: aclSentinelPassword,
|
|
|
|
})
|
|
|
|
|
|
|
|
Expect(client.FlushDB(ctx).Err()).NotTo(HaveOccurred())
|
|
|
|
|
|
|
|
sentinel = redis.NewSentinelClient(&redis.Options{
|
2021-09-23 16:31:21 +03:00
|
|
|
Addr: sentinelAddrs[0],
|
2021-09-18 09:39:26 +03:00
|
|
|
MaxRetries: -1,
|
2021-09-23 16:31:21 +03:00
|
|
|
Username: aclSentinelUsername,
|
|
|
|
Password: aclSentinelPassword,
|
2021-09-18 09:39:26 +03:00
|
|
|
})
|
|
|
|
|
2021-09-19 08:36:40 +03:00
|
|
|
_, err := sentinel.GetMasterAddrByName(ctx, sentinelName).Result()
|
2021-09-18 09:39:26 +03:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
|
|
|
// Wait until sentinels are picked up by each other.
|
2021-09-19 08:36:40 +03:00
|
|
|
for _, process := range sentinels() {
|
|
|
|
Eventually(func() string {
|
|
|
|
return process.Info(ctx).Val()
|
|
|
|
}, "15s", "100ms").Should(ContainSubstring("sentinels=3"))
|
|
|
|
}
|
2021-09-18 09:39:26 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
AfterEach(func() {
|
2021-09-19 08:36:40 +03:00
|
|
|
unauthCommand := redis.NewStatusCmd(ctx, "ACL", "DELUSER", aclSentinelUsername)
|
|
|
|
|
|
|
|
for _, process := range sentinels() {
|
|
|
|
err := process.Client.Process(ctx, unauthCommand)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
}
|
|
|
|
|
2021-09-18 09:39:26 +03:00
|
|
|
_ = client.Close()
|
|
|
|
_ = sentinel.Close()
|
|
|
|
})
|
|
|
|
|
|
|
|
It("should still facilitate operations", func() {
|
|
|
|
err := client.Set(ctx, "wow", "acl-auth", 0).Err()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
|
|
|
val, err := client.Get(ctx, "wow").Result()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(val).To(Equal("acl-auth"))
|
|
|
|
})
|
2021-09-23 16:31:21 +03:00
|
|
|
})
|