From 5756bf81469232e405aaba6fc95e7d9bb910b4cd Mon Sep 17 00:00:00 2001 From: xhe Date: Wed, 30 Dec 2020 11:13:14 +0800 Subject: [PATCH] ddl: fix inconsistent rule cache (#22051) Signed-off-by: xhe --- infoschema/infoschema.go | 11 +++++++---- infoschema/infoschema_test.go | 14 +++++++++++++- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/infoschema/infoschema.go b/infoschema/infoschema.go index d01e42d768..4e2643ba3f 100644 --- a/infoschema/infoschema.go +++ b/infoschema/infoschema.go @@ -428,17 +428,20 @@ func GetBundle(h InfoSchema, ids []int64) *placement.Bundle { } } + newRules := []*placement.Rule{} + b, ok := h.BundleByName(placement.PDBundleID) if ok { - newRules := b.Rules[:0] for _, rule := range b.Rules { if rule.StartKeyHex == "" && rule.EndKeyHex == "" { newRules = append(newRules, rule.Clone()) } } - b.Rules = newRules - return b } - return &placement.Bundle{ID: placement.GroupID(ids[0])} + id := int64(-1) + if len(ids) > 0 { + id = ids[0] + } + return &placement.Bundle{ID: placement.GroupID(id), Rules: newRules} } diff --git a/infoschema/infoschema_test.go b/infoschema/infoschema_test.go index 38cb62a786..578d149e73 100644 --- a/infoschema/infoschema_test.go +++ b/infoschema/infoschema_test.go @@ -369,7 +369,11 @@ func (*testSuite) TestGetBundle(c *C) { bundles[placement.PDBundleID] = bundle b := infoschema.GetBundle(is, []int64{}) - c.Assert(b, DeepEquals, bundle) + c.Assert(b.Rules, DeepEquals, bundle.Rules) + + // bundle itself is cloned + b.ID = "test" + c.Assert(bundle.ID, Equals, placement.PDBundleID) ptID := placement.GroupID(3) bundle = &placement.Bundle{ @@ -388,6 +392,10 @@ func (*testSuite) TestGetBundle(c *C) { b = infoschema.GetBundle(is, []int64{2, 3}) c.Assert(b, DeepEquals, bundle) + // bundle itself is cloned + b.ID = "test" + c.Assert(bundle.ID, Equals, ptID) + ptID = placement.GroupID(1) bundle = &placement.Bundle{ ID: ptID, @@ -404,4 +412,8 @@ func (*testSuite) TestGetBundle(c *C) { b = infoschema.GetBundle(is, []int64{1, 2, 3}) c.Assert(b, DeepEquals, bundle) + + // bundle itself is cloned + b.ID = "test" + c.Assert(bundle.ID, Equals, ptID) }