Skip to content

Commit 4c635cc

Browse files
authored
fix(txpipeline): should return error on multi/exec on multiple slots [CAE-1028] (#3408)
* fix(txpipeline): should return error on multi/exec on multiple slots * fix(txpipeline): test normal tx pipeline behaviour * chore(err): Extract crossslot err and add test * fix(txpipeline): short curcuit the tx if there are no commands * chore(tests): validate keys are in different slots
1 parent 6871741 commit 4c635cc

File tree

3 files changed

+49
-16
lines changed

3 files changed

+49
-16
lines changed

error.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ var ErrPoolExhausted = pool.ErrPoolExhausted
2222
// ErrPoolTimeout timed out waiting to get a connection from the connection pool.
2323
var ErrPoolTimeout = pool.ErrPoolTimeout
2424

25+
// ErrCrossSlot is returned when keys are used in the same Redis command and
26+
// the keys are not in the same hash slot. This error is returned by Redis
27+
// Cluster and will be returned by the client when TxPipeline or TxPipelined
28+
// is used on a ClusterClient with keys in different slots.
29+
var ErrCrossSlot = proto.RedisError("CROSSSLOT Keys in request don't hash to the same slot")
30+
2531
// HasErrorPrefix checks if the err is a Redis error and the message contains a prefix.
2632
func HasErrorPrefix(err error, prefix string) bool {
2733
var rErr Error

osscluster.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,13 +1497,23 @@ func (c *ClusterClient) processTxPipeline(ctx context.Context, cmds []Cmder) err
14971497
// Trim multi .. exec.
14981498
cmds = cmds[1 : len(cmds)-1]
14991499

1500+
if len(cmds) == 0 {
1501+
return nil
1502+
}
1503+
15001504
state, err := c.state.Get(ctx)
15011505
if err != nil {
15021506
setCmdsErr(cmds, err)
15031507
return err
15041508
}
15051509

15061510
cmdsMap := c.mapCmdsBySlot(cmds)
1511+
// TxPipeline does not support cross slot transaction.
1512+
if len(cmdsMap) > 1 {
1513+
setCmdsErr(cmds, ErrCrossSlot)
1514+
return ErrCrossSlot
1515+
}
1516+
15071517
for slot, cmds := range cmdsMap {
15081518
node, err := state.slotMasterNode(slot)
15091519
if err != nil {

osscluster_test.go

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -462,8 +462,7 @@ var _ = Describe("ClusterClient", func() {
462462
Describe("pipelining", func() {
463463
var pipe *redis.Pipeline
464464

465-
assertPipeline := func() {
466-
keys := []string{"A", "B", "C", "D", "E", "F", "G"}
465+
assertPipeline := func(keys []string) {
467466

468467
It("follows redirects", func() {
469468
if !failover {
@@ -482,13 +481,12 @@ var _ = Describe("ClusterClient", func() {
482481
Expect(err).NotTo(HaveOccurred())
483482
Expect(cmds).To(HaveLen(14))
484483

485-
_ = client.ForEachShard(ctx, func(ctx context.Context, node *redis.Client) error {
486-
defer GinkgoRecover()
487-
Eventually(func() int64 {
488-
return node.DBSize(ctx).Val()
489-
}, 30*time.Second).ShouldNot(BeZero())
490-
return nil
491-
})
484+
// Check that all keys are set.
485+
for _, key := range keys {
486+
Eventually(func() string {
487+
return client.Get(ctx, key).Val()
488+
}, 30*time.Second).Should(Equal(key + "_value"))
489+
}
492490

493491
if !failover {
494492
for _, key := range keys {
@@ -517,14 +515,14 @@ var _ = Describe("ClusterClient", func() {
517515
})
518516

519517
It("works with missing keys", func() {
520-
pipe.Set(ctx, "A", "A_value", 0)
521-
pipe.Set(ctx, "C", "C_value", 0)
518+
pipe.Set(ctx, "A{s}", "A_value", 0)
519+
pipe.Set(ctx, "C{s}", "C_value", 0)
522520
_, err := pipe.Exec(ctx)
523521
Expect(err).NotTo(HaveOccurred())
524522

525-
a := pipe.Get(ctx, "A")
526-
b := pipe.Get(ctx, "B")
527-
c := pipe.Get(ctx, "C")
523+
a := pipe.Get(ctx, "A{s}")
524+
b := pipe.Get(ctx, "B{s}")
525+
c := pipe.Get(ctx, "C{s}")
528526
cmds, err := pipe.Exec(ctx)
529527
Expect(err).To(Equal(redis.Nil))
530528
Expect(cmds).To(HaveLen(3))
@@ -547,7 +545,8 @@ var _ = Describe("ClusterClient", func() {
547545

548546
AfterEach(func() {})
549547

550-
assertPipeline()
548+
keys := []string{"A", "B", "C", "D", "E", "F", "G"}
549+
assertPipeline(keys)
551550

552551
It("doesn't fail node with context.Canceled error", func() {
553552
ctx, cancel := context.WithCancel(context.Background())
@@ -590,7 +589,25 @@ var _ = Describe("ClusterClient", func() {
590589

591590
AfterEach(func() {})
592591

593-
assertPipeline()
592+
// TxPipeline doesn't support cross slot commands.
593+
// Use hashtag to force all keys to the same slot.
594+
keys := []string{"A{s}", "B{s}", "C{s}", "D{s}", "E{s}", "F{s}", "G{s}"}
595+
assertPipeline(keys)
596+
597+
// make sure CrossSlot error is returned
598+
It("returns CrossSlot error", func() {
599+
pipe.Set(ctx, "A{s}", "A_value", 0)
600+
pipe.Set(ctx, "B{t}", "B_value", 0)
601+
Expect(hashtag.Slot("A{s}")).NotTo(Equal(hashtag.Slot("B{t}")))
602+
_, err := pipe.Exec(ctx)
603+
Expect(err).To(MatchError(redis.ErrCrossSlot))
604+
})
605+
606+
// doesn't fail when no commands are queued
607+
It("returns no error when there are no commands", func() {
608+
_, err := pipe.Exec(ctx)
609+
Expect(err).NotTo(HaveOccurred())
610+
})
594611
})
595612
})
596613

0 commit comments

Comments
 (0)