2015-05-25 16:22:27 +03:00
|
|
|
package redis_test
|
|
|
|
|
|
|
|
import (
|
2019-07-04 11:18:06 +03:00
|
|
|
"context"
|
2015-06-24 15:37:41 +03:00
|
|
|
"crypto/rand"
|
2015-05-25 16:22:27 +03:00
|
|
|
"fmt"
|
2019-04-22 12:48:06 +03:00
|
|
|
"net"
|
|
|
|
"strconv"
|
|
|
|
"sync"
|
2015-05-25 16:22:27 +03:00
|
|
|
"time"
|
|
|
|
|
2020-03-11 17:29:16 +03:00
|
|
|
"github.com/go-redis/redis/v8"
|
2017-02-18 17:42:34 +03:00
|
|
|
|
2015-05-25 16:22:27 +03:00
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
)
|
|
|
|
|
2016-10-02 15:44:01 +03:00
|
|
|
var _ = Describe("Redis Ring", func() {
|
2016-08-09 16:32:08 +03:00
|
|
|
const heartbeat = 100 * time.Millisecond
|
2016-10-02 15:44:01 +03:00
|
|
|
|
2015-05-25 16:22:27 +03:00
|
|
|
var ring *redis.Ring
|
|
|
|
|
|
|
|
setRingKeys := func() {
|
|
|
|
for i := 0; i < 100; i++ {
|
2020-03-11 17:26:42 +03:00
|
|
|
err := ring.Set(ctx, fmt.Sprintf("key%d", i), "value", 0).Err()
|
2015-05-25 16:22:27 +03:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BeforeEach(func() {
|
2016-10-02 15:44:01 +03:00
|
|
|
opt := redisRingOptions()
|
|
|
|
opt.HeartbeatFrequency = heartbeat
|
|
|
|
ring = redis.NewRing(opt)
|
2015-05-25 16:22:27 +03:00
|
|
|
|
2020-03-11 17:26:42 +03:00
|
|
|
err := ring.ForEachShard(ctx, func(ctx context.Context, cl *redis.Client) error {
|
|
|
|
return cl.FlushDB(ctx).Err()
|
2016-10-02 15:44:01 +03:00
|
|
|
})
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2015-05-25 16:22:27 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
AfterEach(func() {
|
|
|
|
Expect(ring.Close()).NotTo(HaveOccurred())
|
|
|
|
})
|
|
|
|
|
2020-03-11 17:26:42 +03:00
|
|
|
It("supports context", func() {
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
2019-07-04 11:18:06 +03:00
|
|
|
cancel()
|
|
|
|
|
2020-03-11 17:26:42 +03:00
|
|
|
err := ring.Ping(ctx).Err()
|
2019-07-04 11:18:06 +03:00
|
|
|
Expect(err).To(MatchError("context canceled"))
|
|
|
|
})
|
|
|
|
|
2016-10-09 14:12:32 +03:00
|
|
|
It("distributes keys", func() {
|
2015-05-25 16:22:27 +03:00
|
|
|
setRingKeys()
|
|
|
|
|
|
|
|
// Both shards should have some keys now.
|
2020-06-08 12:42:43 +03:00
|
|
|
Expect(ringShard1.Info(ctx, "keyspace").Val()).To(ContainSubstring("keys=56"))
|
|
|
|
Expect(ringShard2.Info(ctx, "keyspace").Val()).To(ContainSubstring("keys=44"))
|
2015-05-25 16:22:27 +03:00
|
|
|
})
|
|
|
|
|
2016-10-09 14:12:32 +03:00
|
|
|
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)
|
2020-03-11 17:26:42 +03:00
|
|
|
err := script.Run(ctx, ring, []string{key}, "value").Err()
|
2016-10-09 14:12:32 +03:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
}
|
|
|
|
|
2020-06-08 12:42:43 +03:00
|
|
|
Expect(ringShard1.Info(ctx, "keyspace").Val()).To(ContainSubstring("keys=56"))
|
|
|
|
Expect(ringShard2.Info(ctx, "keyspace").Val()).To(ContainSubstring("keys=44"))
|
2016-10-09 14:12:32 +03:00
|
|
|
})
|
|
|
|
|
2015-12-22 12:44:49 +03:00
|
|
|
It("uses single shard when one of the shards is down", func() {
|
2015-05-25 16:22:27 +03:00
|
|
|
// Stop ringShard2.
|
|
|
|
Expect(ringShard2.Close()).NotTo(HaveOccurred())
|
|
|
|
|
2018-07-23 15:55:13 +03:00
|
|
|
Eventually(func() int {
|
|
|
|
return ring.Len()
|
|
|
|
}, "30s").Should(Equal(1))
|
2015-05-25 16:22:27 +03:00
|
|
|
|
|
|
|
setRingKeys()
|
|
|
|
|
|
|
|
// RingShard1 should have all keys.
|
2020-03-11 17:26:42 +03:00
|
|
|
Expect(ringShard1.Info(ctx, "keyspace").Val()).To(ContainSubstring("keys=100"))
|
2015-05-25 16:22:27 +03:00
|
|
|
|
|
|
|
// Start ringShard2.
|
|
|
|
var err error
|
|
|
|
ringShard2, err = startRedis(ringShard2Port)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
2018-07-23 15:55:13 +03:00
|
|
|
Eventually(func() int {
|
|
|
|
return ring.Len()
|
|
|
|
}, "30s").Should(Equal(2))
|
2015-05-25 16:22:27 +03:00
|
|
|
|
|
|
|
setRingKeys()
|
|
|
|
|
|
|
|
// RingShard2 should have its keys.
|
2020-06-08 12:42:43 +03:00
|
|
|
Expect(ringShard2.Info(ctx, "keyspace").Val()).To(ContainSubstring("keys=44"))
|
2015-05-25 16:22:27 +03:00
|
|
|
})
|
2015-06-04 11:50:24 +03:00
|
|
|
|
2015-06-24 15:37:41 +03:00
|
|
|
It("supports hash tags", func() {
|
|
|
|
for i := 0; i < 100; i++ {
|
2020-03-11 17:26:42 +03:00
|
|
|
err := ring.Set(ctx, fmt.Sprintf("key%d{tag}", i), "value", 0).Err()
|
2015-06-24 15:37:41 +03:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
}
|
|
|
|
|
2020-03-11 17:26:42 +03:00
|
|
|
Expect(ringShard1.Info(ctx, "keyspace").Val()).ToNot(ContainSubstring("keys="))
|
|
|
|
Expect(ringShard2.Info(ctx, "keyspace").Val()).To(ContainSubstring("keys=100"))
|
2015-06-24 15:37:41 +03:00
|
|
|
})
|
|
|
|
|
2016-10-09 14:12:32 +03:00
|
|
|
Describe("pipeline", func() {
|
|
|
|
It("distributes keys", func() {
|
2015-06-24 13:41:50 +03:00
|
|
|
pipe := ring.Pipeline()
|
|
|
|
for i := 0; i < 100; i++ {
|
2020-03-11 17:26:42 +03:00
|
|
|
err := pipe.Set(ctx, fmt.Sprintf("key%d", i), "value", 0).Err()
|
2015-06-24 13:41:50 +03:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
}
|
2020-03-11 17:26:42 +03:00
|
|
|
cmds, err := pipe.Exec(ctx)
|
2015-06-04 11:50:24 +03:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2015-06-24 13:41:50 +03:00
|
|
|
Expect(cmds).To(HaveLen(100))
|
|
|
|
Expect(pipe.Close()).NotTo(HaveOccurred())
|
2015-06-04 11:50:24 +03:00
|
|
|
|
2015-06-24 13:41:50 +03:00
|
|
|
for _, cmd := range cmds {
|
|
|
|
Expect(cmd.Err()).NotTo(HaveOccurred())
|
|
|
|
Expect(cmd.(*redis.StatusCmd).Val()).To(Equal("OK"))
|
|
|
|
}
|
2015-06-04 11:50:24 +03:00
|
|
|
|
2015-06-24 13:41:50 +03:00
|
|
|
// Both shards should have some keys now.
|
2020-06-08 12:42:43 +03:00
|
|
|
Expect(ringShard1.Info(ctx).Val()).To(ContainSubstring("keys=56"))
|
|
|
|
Expect(ringShard2.Info(ctx).Val()).To(ContainSubstring("keys=44"))
|
2015-06-24 13:41:50 +03:00
|
|
|
})
|
|
|
|
|
2015-06-24 15:37:41 +03:00
|
|
|
It("is consistent with ring", func() {
|
|
|
|
var keys []string
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
key := make([]byte, 64)
|
|
|
|
_, err := rand.Read(key)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
keys = append(keys, string(key))
|
|
|
|
}
|
|
|
|
|
2020-03-11 17:26:42 +03:00
|
|
|
_, err := ring.Pipelined(ctx, func(pipe redis.Pipeliner) error {
|
2015-06-24 15:37:41 +03:00
|
|
|
for _, key := range keys {
|
2020-03-11 17:26:42 +03:00
|
|
|
pipe.Set(ctx, key, "value", 0).Err()
|
2015-06-24 15:37:41 +03:00
|
|
|
}
|
2015-06-24 13:41:50 +03:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
2015-06-24 15:37:41 +03:00
|
|
|
for _, key := range keys {
|
2020-03-11 17:26:42 +03:00
|
|
|
val, err := ring.Get(ctx, key).Result()
|
2015-06-24 15:37:41 +03:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(val).To(Equal("value"))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
It("supports hash tags", func() {
|
2020-03-11 17:26:42 +03:00
|
|
|
_, err := ring.Pipelined(ctx, func(pipe redis.Pipeliner) error {
|
2015-06-24 15:37:41 +03:00
|
|
|
for i := 0; i < 100; i++ {
|
2020-03-11 17:26:42 +03:00
|
|
|
pipe.Set(ctx, fmt.Sprintf("key%d{tag}", i), "value", 0).Err()
|
2015-06-24 15:37:41 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2015-06-24 13:41:50 +03:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2015-06-24 15:37:41 +03:00
|
|
|
|
2020-03-11 17:26:42 +03:00
|
|
|
Expect(ringShard1.Info(ctx).Val()).ToNot(ContainSubstring("keys="))
|
|
|
|
Expect(ringShard2.Info(ctx).Val()).To(ContainSubstring("keys=100"))
|
2015-06-24 13:41:50 +03:00
|
|
|
})
|
2015-06-04 11:50:24 +03:00
|
|
|
})
|
2019-07-09 22:00:10 +03:00
|
|
|
|
2020-03-27 16:48:18 +03:00
|
|
|
Describe("new client callback", func() {
|
|
|
|
It("can be initialized with a new client callback", func() {
|
|
|
|
opts := redisRingOptions()
|
|
|
|
opts.NewClient = func(name string, opt *redis.Options) *redis.Client {
|
|
|
|
opt.Password = "password1"
|
|
|
|
return redis.NewClient(opt)
|
|
|
|
}
|
|
|
|
ring = redis.NewRing(opts)
|
|
|
|
|
2020-03-11 17:26:42 +03:00
|
|
|
err := ring.Ping(ctx).Err()
|
2020-06-05 09:30:21 +03:00
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
Expect(err.Error()).To(ContainSubstring("ERR AUTH"))
|
2019-07-09 22:00:10 +03:00
|
|
|
})
|
|
|
|
})
|
2020-02-14 15:30:07 +03:00
|
|
|
|
|
|
|
It("supports Process hook", func() {
|
2020-03-11 17:26:42 +03:00
|
|
|
err := ring.Ping(ctx).Err()
|
2020-02-14 15:30:07 +03:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
|
|
|
var stack []string
|
|
|
|
|
|
|
|
ring.AddHook(&hook{
|
|
|
|
beforeProcess: func(ctx context.Context, cmd redis.Cmder) (context.Context, error) {
|
|
|
|
Expect(cmd.String()).To(Equal("ping: "))
|
|
|
|
stack = append(stack, "ring.BeforeProcess")
|
|
|
|
return ctx, nil
|
|
|
|
},
|
|
|
|
afterProcess: func(ctx context.Context, cmd redis.Cmder) error {
|
|
|
|
Expect(cmd.String()).To(Equal("ping: PONG"))
|
|
|
|
stack = append(stack, "ring.AfterProcess")
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2020-03-11 17:26:42 +03:00
|
|
|
ring.ForEachShard(ctx, func(ctx context.Context, shard *redis.Client) error {
|
2020-02-14 15:30:07 +03:00
|
|
|
shard.AddHook(&hook{
|
|
|
|
beforeProcess: func(ctx context.Context, cmd redis.Cmder) (context.Context, error) {
|
|
|
|
Expect(cmd.String()).To(Equal("ping: "))
|
|
|
|
stack = append(stack, "shard.BeforeProcess")
|
|
|
|
return ctx, nil
|
|
|
|
},
|
|
|
|
afterProcess: func(ctx context.Context, cmd redis.Cmder) error {
|
|
|
|
Expect(cmd.String()).To(Equal("ping: PONG"))
|
|
|
|
stack = append(stack, "shard.AfterProcess")
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2020-03-11 17:26:42 +03:00
|
|
|
err = ring.Ping(ctx).Err()
|
2020-02-14 15:30:07 +03:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(stack).To(Equal([]string{
|
|
|
|
"ring.BeforeProcess",
|
|
|
|
"shard.BeforeProcess",
|
|
|
|
"shard.AfterProcess",
|
|
|
|
"ring.AfterProcess",
|
|
|
|
}))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("supports Pipeline hook", func() {
|
2020-03-11 17:26:42 +03:00
|
|
|
err := ring.Ping(ctx).Err()
|
2020-02-14 15:30:07 +03:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
|
|
|
var stack []string
|
|
|
|
|
|
|
|
ring.AddHook(&hook{
|
|
|
|
beforeProcessPipeline: func(ctx context.Context, cmds []redis.Cmder) (context.Context, error) {
|
|
|
|
Expect(cmds).To(HaveLen(1))
|
|
|
|
Expect(cmds[0].String()).To(Equal("ping: "))
|
|
|
|
stack = append(stack, "ring.BeforeProcessPipeline")
|
|
|
|
return ctx, nil
|
|
|
|
},
|
|
|
|
afterProcessPipeline: func(ctx context.Context, cmds []redis.Cmder) error {
|
|
|
|
Expect(cmds).To(HaveLen(1))
|
|
|
|
Expect(cmds[0].String()).To(Equal("ping: PONG"))
|
|
|
|
stack = append(stack, "ring.AfterProcessPipeline")
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2020-03-11 17:26:42 +03:00
|
|
|
ring.ForEachShard(ctx, func(ctx context.Context, shard *redis.Client) error {
|
2020-02-14 15:30:07 +03:00
|
|
|
shard.AddHook(&hook{
|
|
|
|
beforeProcessPipeline: func(ctx context.Context, cmds []redis.Cmder) (context.Context, error) {
|
|
|
|
Expect(cmds).To(HaveLen(1))
|
|
|
|
Expect(cmds[0].String()).To(Equal("ping: "))
|
|
|
|
stack = append(stack, "shard.BeforeProcessPipeline")
|
|
|
|
return ctx, nil
|
|
|
|
},
|
|
|
|
afterProcessPipeline: func(ctx context.Context, cmds []redis.Cmder) error {
|
|
|
|
Expect(cmds).To(HaveLen(1))
|
|
|
|
Expect(cmds[0].String()).To(Equal("ping: PONG"))
|
|
|
|
stack = append(stack, "shard.AfterProcessPipeline")
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2020-03-11 17:26:42 +03:00
|
|
|
_, err = ring.Pipelined(ctx, func(pipe redis.Pipeliner) error {
|
|
|
|
pipe.Ping(ctx)
|
2020-02-14 15:30:07 +03:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(stack).To(Equal([]string{
|
|
|
|
"ring.BeforeProcessPipeline",
|
|
|
|
"shard.BeforeProcessPipeline",
|
|
|
|
"shard.AfterProcessPipeline",
|
|
|
|
"ring.AfterProcessPipeline",
|
|
|
|
}))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("supports TxPipeline hook", func() {
|
2020-03-11 17:26:42 +03:00
|
|
|
err := ring.Ping(ctx).Err()
|
2020-02-14 15:30:07 +03:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
|
|
|
var stack []string
|
|
|
|
|
|
|
|
ring.AddHook(&hook{
|
|
|
|
beforeProcessPipeline: func(ctx context.Context, cmds []redis.Cmder) (context.Context, error) {
|
|
|
|
Expect(cmds).To(HaveLen(1))
|
|
|
|
Expect(cmds[0].String()).To(Equal("ping: "))
|
|
|
|
stack = append(stack, "ring.BeforeProcessPipeline")
|
|
|
|
return ctx, nil
|
|
|
|
},
|
|
|
|
afterProcessPipeline: func(ctx context.Context, cmds []redis.Cmder) error {
|
|
|
|
Expect(cmds).To(HaveLen(1))
|
|
|
|
Expect(cmds[0].String()).To(Equal("ping: PONG"))
|
|
|
|
stack = append(stack, "ring.AfterProcessPipeline")
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2020-03-11 17:26:42 +03:00
|
|
|
ring.ForEachShard(ctx, func(ctx context.Context, shard *redis.Client) error {
|
2020-02-14 15:30:07 +03:00
|
|
|
shard.AddHook(&hook{
|
|
|
|
beforeProcessPipeline: func(ctx context.Context, cmds []redis.Cmder) (context.Context, error) {
|
2020-02-14 16:37:35 +03:00
|
|
|
Expect(cmds).To(HaveLen(3))
|
|
|
|
Expect(cmds[1].String()).To(Equal("ping: "))
|
2020-02-14 15:30:07 +03:00
|
|
|
stack = append(stack, "shard.BeforeProcessPipeline")
|
|
|
|
return ctx, nil
|
|
|
|
},
|
|
|
|
afterProcessPipeline: func(ctx context.Context, cmds []redis.Cmder) error {
|
2020-02-14 16:37:35 +03:00
|
|
|
Expect(cmds).To(HaveLen(3))
|
|
|
|
Expect(cmds[1].String()).To(Equal("ping: PONG"))
|
2020-02-14 15:30:07 +03:00
|
|
|
stack = append(stack, "shard.AfterProcessPipeline")
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2020-03-11 17:26:42 +03:00
|
|
|
_, err = ring.TxPipelined(ctx, func(pipe redis.Pipeliner) error {
|
|
|
|
pipe.Ping(ctx)
|
2020-02-14 15:30:07 +03:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(stack).To(Equal([]string{
|
|
|
|
"ring.BeforeProcessPipeline",
|
|
|
|
"shard.BeforeProcessPipeline",
|
|
|
|
"shard.AfterProcessPipeline",
|
|
|
|
"ring.AfterProcessPipeline",
|
|
|
|
}))
|
|
|
|
})
|
2015-05-25 16:22:27 +03:00
|
|
|
})
|
2016-10-09 14:12:32 +03:00
|
|
|
|
|
|
|
var _ = Describe("empty Redis Ring", func() {
|
|
|
|
var ring *redis.Ring
|
|
|
|
|
|
|
|
BeforeEach(func() {
|
|
|
|
ring = redis.NewRing(&redis.RingOptions{})
|
|
|
|
})
|
|
|
|
|
|
|
|
AfterEach(func() {
|
|
|
|
Expect(ring.Close()).NotTo(HaveOccurred())
|
|
|
|
})
|
|
|
|
|
|
|
|
It("returns an error", func() {
|
2020-03-11 17:26:42 +03:00
|
|
|
err := ring.Ping(ctx).Err()
|
2016-10-09 14:12:32 +03:00
|
|
|
Expect(err).To(MatchError("redis: all ring shards are down"))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("pipeline returns an error", func() {
|
2020-03-11 17:26:42 +03:00
|
|
|
_, err := ring.Pipelined(ctx, func(pipe redis.Pipeliner) error {
|
|
|
|
pipe.Ping(ctx)
|
2016-10-09 14:12:32 +03:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
Expect(err).To(MatchError("redis: all ring shards are down"))
|
|
|
|
})
|
|
|
|
})
|
2019-04-22 12:48:06 +03:00
|
|
|
|
|
|
|
var _ = Describe("Ring watch", func() {
|
|
|
|
const heartbeat = 100 * time.Millisecond
|
|
|
|
|
|
|
|
var ring *redis.Ring
|
|
|
|
|
|
|
|
BeforeEach(func() {
|
|
|
|
opt := redisRingOptions()
|
|
|
|
opt.HeartbeatFrequency = heartbeat
|
|
|
|
ring = redis.NewRing(opt)
|
|
|
|
|
2020-03-11 17:26:42 +03:00
|
|
|
err := ring.ForEachShard(ctx, func(ctx context.Context, cl *redis.Client) error {
|
|
|
|
return cl.FlushDB(ctx).Err()
|
2019-04-22 12:48:06 +03:00
|
|
|
})
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
})
|
|
|
|
|
|
|
|
AfterEach(func() {
|
|
|
|
Expect(ring.Close()).NotTo(HaveOccurred())
|
|
|
|
})
|
|
|
|
|
|
|
|
It("should Watch", func() {
|
|
|
|
var incr func(string) error
|
|
|
|
|
|
|
|
// Transactionally increments key using GET and SET commands.
|
|
|
|
incr = func(key string) error {
|
2020-03-11 17:26:42 +03:00
|
|
|
err := ring.Watch(ctx, func(tx *redis.Tx) error {
|
|
|
|
n, err := tx.Get(ctx, key).Int64()
|
2019-04-22 12:48:06 +03:00
|
|
|
if err != nil && err != redis.Nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-11 17:26:42 +03:00
|
|
|
_, err = tx.TxPipelined(ctx, func(pipe redis.Pipeliner) error {
|
|
|
|
pipe.Set(ctx, key, strconv.FormatInt(n+1, 10), 0)
|
2019-04-22 12:48:06 +03:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}, key)
|
|
|
|
if err == redis.TxFailedErr {
|
|
|
|
return incr(key)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer GinkgoRecover()
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
err := incr("key")
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
|
2020-03-11 17:26:42 +03:00
|
|
|
n, err := ring.Get(ctx, "key").Int64()
|
2019-04-22 12:48:06 +03:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(n).To(Equal(int64(100)))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("should discard", func() {
|
2020-03-11 17:26:42 +03:00
|
|
|
err := ring.Watch(ctx, func(tx *redis.Tx) error {
|
|
|
|
cmds, err := tx.TxPipelined(ctx, func(pipe redis.Pipeliner) error {
|
2020-06-08 12:42:43 +03:00
|
|
|
pipe.Set(ctx, "{shard}key1", "hello1", 0)
|
2019-04-22 12:48:06 +03:00
|
|
|
pipe.Discard()
|
2020-06-08 12:42:43 +03:00
|
|
|
pipe.Set(ctx, "{shard}key2", "hello2", 0)
|
2019-04-22 12:48:06 +03:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(cmds).To(HaveLen(1))
|
|
|
|
return err
|
2020-06-08 12:42:43 +03:00
|
|
|
}, "{shard}key1", "{shard}key2")
|
2019-04-22 12:48:06 +03:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
2020-06-08 12:42:43 +03:00
|
|
|
get := ring.Get(ctx, "{shard}key1")
|
2019-04-22 12:48:06 +03:00
|
|
|
Expect(get.Err()).To(Equal(redis.Nil))
|
|
|
|
Expect(get.Val()).To(Equal(""))
|
|
|
|
|
2020-06-08 12:42:43 +03:00
|
|
|
get = ring.Get(ctx, "{shard}key2")
|
2019-04-22 12:48:06 +03:00
|
|
|
Expect(get.Err()).NotTo(HaveOccurred())
|
|
|
|
Expect(get.Val()).To(Equal("hello2"))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("returns no error when there are no commands", func() {
|
2020-03-11 17:26:42 +03:00
|
|
|
err := ring.Watch(ctx, func(tx *redis.Tx) error {
|
|
|
|
_, err := tx.TxPipelined(ctx, func(redis.Pipeliner) error { return nil })
|
2019-04-22 12:48:06 +03:00
|
|
|
return err
|
|
|
|
}, "key")
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
2020-03-11 17:26:42 +03:00
|
|
|
v, err := ring.Ping(ctx).Result()
|
2019-04-22 12:48:06 +03:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(v).To(Equal("PONG"))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("should exec bulks", func() {
|
|
|
|
const N = 20000
|
|
|
|
|
2020-03-11 17:26:42 +03:00
|
|
|
err := ring.Watch(ctx, func(tx *redis.Tx) error {
|
|
|
|
cmds, err := tx.TxPipelined(ctx, func(pipe redis.Pipeliner) error {
|
2019-04-22 12:48:06 +03:00
|
|
|
for i := 0; i < N; i++ {
|
2020-03-11 17:26:42 +03:00
|
|
|
pipe.Incr(ctx, "key")
|
2019-04-22 12:48:06 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(len(cmds)).To(Equal(N))
|
|
|
|
for _, cmd := range cmds {
|
|
|
|
Expect(cmd.Err()).NotTo(HaveOccurred())
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}, "key")
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
2020-03-11 17:26:42 +03:00
|
|
|
num, err := ring.Get(ctx, "key").Int64()
|
2019-04-22 12:48:06 +03:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(num).To(Equal(int64(N)))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("should Watch/Unwatch", func() {
|
|
|
|
var C, N int
|
|
|
|
|
2020-03-11 17:26:42 +03:00
|
|
|
err := ring.Set(ctx, "key", "0", 0).Err()
|
2019-04-22 12:48:06 +03:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
|
|
|
perform(C, func(id int) {
|
|
|
|
for i := 0; i < N; i++ {
|
2020-03-11 17:26:42 +03:00
|
|
|
err := ring.Watch(ctx, func(tx *redis.Tx) error {
|
|
|
|
val, err := tx.Get(ctx, "key").Result()
|
2019-04-22 12:48:06 +03:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(val).NotTo(Equal(redis.Nil))
|
|
|
|
|
|
|
|
num, err := strconv.ParseInt(val, 10, 64)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
2020-03-11 17:26:42 +03:00
|
|
|
cmds, err := tx.TxPipelined(ctx, func(pipe redis.Pipeliner) error {
|
|
|
|
pipe.Set(ctx, "key", strconv.FormatInt(num+1, 10), 0)
|
2019-04-22 12:48:06 +03:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
Expect(cmds).To(HaveLen(1))
|
|
|
|
return err
|
|
|
|
}, "key")
|
|
|
|
if err == redis.TxFailedErr {
|
|
|
|
i--
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-03-11 17:26:42 +03:00
|
|
|
val, err := ring.Get(ctx, "key").Int64()
|
2019-04-22 12:48:06 +03:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(val).To(Equal(int64(C * N)))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("should close Tx without closing the client", func() {
|
2020-03-11 17:26:42 +03:00
|
|
|
err := ring.Watch(ctx, func(tx *redis.Tx) error {
|
|
|
|
_, err := tx.TxPipelined(ctx, func(pipe redis.Pipeliner) error {
|
|
|
|
pipe.Ping(ctx)
|
2019-04-22 12:48:06 +03:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}, "key")
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
2020-03-11 17:26:42 +03:00
|
|
|
Expect(ring.Ping(ctx).Err()).NotTo(HaveOccurred())
|
2019-04-22 12:48:06 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
It("respects max size on multi", func() {
|
2021-02-25 06:43:41 +03:00
|
|
|
//this test checks the number of "pool.conn"
|
|
|
|
//if the health check is performed at the same time
|
|
|
|
//conn will be used, resulting in an abnormal number of "pool.conn".
|
|
|
|
//
|
|
|
|
//redis.NewRing() does not have an option to prohibit health checks.
|
|
|
|
//set a relatively large time here to avoid health checks.
|
|
|
|
opt := redisRingOptions()
|
|
|
|
opt.HeartbeatFrequency = 72 * time.Hour
|
|
|
|
ring = redis.NewRing(opt)
|
|
|
|
|
2019-04-22 12:48:06 +03:00
|
|
|
perform(1000, func(id int) {
|
|
|
|
var ping *redis.StatusCmd
|
|
|
|
|
2020-03-11 17:26:42 +03:00
|
|
|
err := ring.Watch(ctx, func(tx *redis.Tx) error {
|
|
|
|
cmds, err := tx.TxPipelined(ctx, func(pipe redis.Pipeliner) error {
|
|
|
|
ping = pipe.Ping(ctx)
|
2019-04-22 12:48:06 +03:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(cmds).To(HaveLen(1))
|
|
|
|
return err
|
|
|
|
}, "key")
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
|
|
|
Expect(ping.Err()).NotTo(HaveOccurred())
|
|
|
|
Expect(ping.Val()).To(Equal("PONG"))
|
|
|
|
})
|
|
|
|
|
2020-03-11 17:26:42 +03:00
|
|
|
ring.ForEachShard(ctx, func(ctx context.Context, cl *redis.Client) error {
|
2019-06-16 14:47:24 +03:00
|
|
|
defer GinkgoRecover()
|
|
|
|
|
2019-04-22 12:48:06 +03:00
|
|
|
pool := cl.Pool()
|
|
|
|
Expect(pool.Len()).To(BeNumerically("<=", 10))
|
|
|
|
Expect(pool.IdleLen()).To(BeNumerically("<=", 10))
|
|
|
|
Expect(pool.Len()).To(Equal(pool.IdleLen()))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
var _ = Describe("Ring Tx timeout", func() {
|
|
|
|
const heartbeat = 100 * time.Millisecond
|
|
|
|
|
|
|
|
var ring *redis.Ring
|
|
|
|
|
|
|
|
AfterEach(func() {
|
|
|
|
_ = ring.Close()
|
|
|
|
})
|
|
|
|
|
|
|
|
testTimeout := func() {
|
|
|
|
It("Tx timeouts", func() {
|
2020-03-11 17:26:42 +03:00
|
|
|
err := ring.Watch(ctx, func(tx *redis.Tx) error {
|
|
|
|
return tx.Ping(ctx).Err()
|
2019-04-22 12:48:06 +03:00
|
|
|
}, "foo")
|
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
Expect(err.(net.Error).Timeout()).To(BeTrue())
|
|
|
|
})
|
|
|
|
|
|
|
|
It("Tx Pipeline timeouts", func() {
|
2020-03-11 17:26:42 +03:00
|
|
|
err := ring.Watch(ctx, func(tx *redis.Tx) error {
|
|
|
|
_, err := tx.TxPipelined(ctx, func(pipe redis.Pipeliner) error {
|
|
|
|
pipe.Ping(ctx)
|
2019-04-22 12:48:06 +03:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}, "foo")
|
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
Expect(err.(net.Error).Timeout()).To(BeTrue())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const pause = 5 * time.Second
|
|
|
|
|
|
|
|
Context("read/write timeout", func() {
|
|
|
|
BeforeEach(func() {
|
|
|
|
opt := redisRingOptions()
|
|
|
|
opt.ReadTimeout = 250 * time.Millisecond
|
|
|
|
opt.WriteTimeout = 250 * time.Millisecond
|
|
|
|
opt.HeartbeatFrequency = heartbeat
|
|
|
|
ring = redis.NewRing(opt)
|
|
|
|
|
2020-03-11 17:26:42 +03:00
|
|
|
err := ring.ForEachShard(ctx, func(ctx context.Context, client *redis.Client) error {
|
|
|
|
return client.ClientPause(ctx, pause).Err()
|
2019-04-22 12:48:06 +03:00
|
|
|
})
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
})
|
|
|
|
|
|
|
|
AfterEach(func() {
|
2020-03-11 17:26:42 +03:00
|
|
|
_ = ring.ForEachShard(ctx, func(ctx context.Context, client *redis.Client) error {
|
2019-04-22 12:48:06 +03:00
|
|
|
defer GinkgoRecover()
|
|
|
|
Eventually(func() error {
|
2020-03-11 17:26:42 +03:00
|
|
|
return client.Ping(ctx).Err()
|
2019-04-22 12:48:06 +03:00
|
|
|
}, 2*pause).ShouldNot(HaveOccurred())
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
testTimeout()
|
|
|
|
})
|
|
|
|
})
|