Debian Codegraph @

ring_test.go

raw
1 package redis_test
2
3 import (
4 "crypto/rand"
5 "fmt"
6 "time"
7
8 . "github.com/onsi/ginkgo"
9 . "github.com/onsi/gomega"
10
11 "gopkg.in/redis.v5"
12 )
13
14 var _ = Describe("Redis Ring", func() {
15 const heartbeat = 100 * time.Millisecond
16
17 var ring *redis.Ring
18
19 setRingKeys := func() {
20 for i := 0; i < 100; i++ {
21 err := ring.Set(fmt.Sprintf("key%d", i), "value", 0).Err()
22 Expect(err).NotTo(HaveOccurred())
23 }
24 }
25
26 BeforeEach(func() {
27 opt := redisRingOptions()
28 opt.HeartbeatFrequency = heartbeat
29 ring = redis.NewRing(opt)
30
31 err := ring.ForEachShard(func(cl *redis.Client) error {
32 return cl.FlushDb().Err()
33 })
34 Expect(err).NotTo(HaveOccurred())
35 })
36
37 AfterEach(func() {
38 Expect(ring.Close()).NotTo(HaveOccurred())
39 })
40
41 It("distributes keys", func() {
42 setRingKeys()
43
44 // Both shards should have some keys now.
45 Expect(ringShard1.Info().Val()).To(ContainSubstring("keys=57"))
46 Expect(ringShard2.Info().Val()).To(ContainSubstring("keys=43"))
47 })
48
49 It("distributes keys when using EVAL", func() {
50 script := redis.NewScript(`
51 local r = redis.call('SET', KEYS[1], ARGV[1])
52 return r
53 `)
54
55 var key string
56 for i := 0; i < 100; i++ {
57 key = fmt.Sprintf("key%d", i)
58 err := script.Run(ring, []string{key}, "value").Err()
59 Expect(err).NotTo(HaveOccurred())
60 }
61
62 Expect(ringShard1.Info().Val()).To(ContainSubstring("keys=57"))
63 Expect(ringShard2.Info().Val()).To(ContainSubstring("keys=43"))
64 })
65
66 It("uses single shard when one of the shards is down", func() {
67 // Stop ringShard2.
68 Expect(ringShard2.Close()).NotTo(HaveOccurred())
69
70 // Ring needs 3 * heartbeat time to detect that node is down.
71 // Give it more to be sure.
72 time.Sleep(2 * 3 * heartbeat)
73
74 setRingKeys()
75
76 // RingShard1 should have all keys.
77 Expect(ringShard1.Info().Val()).To(ContainSubstring("keys=100"))
78
79 // Start ringShard2.
80 var err error
81 ringShard2, err = startRedis(ringShard2Port)
82 Expect(err).NotTo(HaveOccurred())
83
84 // Wait for ringShard2 to come up.
85 Eventually(func() error {
86 return ringShard2.Ping().Err()
87 }, "1s").ShouldNot(HaveOccurred())
88
89 // Ring needs heartbeat time to detect that node is up.
90 // Give it more to be sure.
91 time.Sleep(heartbeat + heartbeat)
92
93 setRingKeys()
94
95 // RingShard2 should have its keys.
96 Expect(ringShard2.Info().Val()).To(ContainSubstring("keys=43"))
97 })
98
99 It("supports hash tags", func() {
100 for i := 0; i < 100; i++ {
101 err := ring.Set(fmt.Sprintf("key%d{tag}", i), "value", 0).Err()
102 Expect(err).NotTo(HaveOccurred())
103 }
104
105 Expect(ringShard1.Info().Val()).ToNot(ContainSubstring("keys="))
106 Expect(ringShard2.Info().Val()).To(ContainSubstring("keys=100"))
107 })
108
109 Describe("pipeline", func() {
110 It("distributes keys", func() {
111 pipe := ring.Pipeline()
112 for i := 0; i < 100; i++ {
113 err := pipe.Set(fmt.Sprintf("key%d", i), "value", 0).Err()
114 Expect(err).NotTo(HaveOccurred())
115 }
116 cmds, err := pipe.Exec()
117 Expect(err).NotTo(HaveOccurred())
118 Expect(cmds).To(HaveLen(100))
119 Expect(pipe.Close()).NotTo(HaveOccurred())
120
121 for _, cmd := range cmds {
122 Expect(cmd.Err()).NotTo(HaveOccurred())
123 Expect(cmd.(*redis.StatusCmd).Val()).To(Equal("OK"))
124 }
125
126 // Both shards should have some keys now.
127 Expect(ringShard1.Info().Val()).To(ContainSubstring("keys=57"))
128 Expect(ringShard2.Info().Val()).To(ContainSubstring("keys=43"))
129 })
130
131 It("is consistent with ring", func() {
132 var keys []string
133 for i := 0; i < 100; i++ {
134 key := make([]byte, 64)
135 _, err := rand.Read(key)
136 Expect(err).NotTo(HaveOccurred())
137 keys = append(keys, string(key))
138 }
139
140 _, err := ring.Pipelined(func(pipe *redis.Pipeline) error {
141 for _, key := range keys {
142 pipe.Set(key, "value", 0).Err()
143 }
144 return nil
145 })
146 Expect(err).NotTo(HaveOccurred())
147
148 for _, key := range keys {
149 val, err := ring.Get(key).Result()
150 Expect(err).NotTo(HaveOccurred())
151 Expect(val).To(Equal("value"))
152 }
153 })
154
155 It("supports hash tags", func() {
156 _, err := ring.Pipelined(func(pipe *redis.Pipeline) error {
157 for i := 0; i < 100; i++ {
158 pipe.Set(fmt.Sprintf("key%d{tag}", i), "value", 0).Err()
159 }
160 return nil
161 })
162 Expect(err).NotTo(HaveOccurred())
163
164 Expect(ringShard1.Info().Val()).ToNot(ContainSubstring("keys="))
165 Expect(ringShard2.Info().Val()).To(ContainSubstring("keys=100"))
166 })
167 })
168 })
169
170 var _ = Describe("empty Redis Ring", func() {
171 var ring *redis.Ring
172
173 BeforeEach(func() {
174 ring = redis.NewRing(&redis.RingOptions{})
175 })
176
177 AfterEach(func() {
178 Expect(ring.Close()).NotTo(HaveOccurred())
179 })
180
181 It("returns an error", func() {
182 err := ring.Ping().Err()
183 Expect(err).To(MatchError("redis: all ring shards are down"))
184 })
185
186 It("pipeline returns an error", func() {
187 _, err := ring.Pipelined(func(pipe *redis.Pipeline) error {
188 pipe.Ping()
189 return nil
190 })
191 Expect(err).To(MatchError("redis: all ring shards are down"))
192 })
193 })