From bdf1680021b29643f595802bc1644e0d755997bf Mon Sep 17 00:00:00 2001 From: lysu Date: Fri, 11 Oct 2019 22:54:04 +0800 Subject: [PATCH] mocktikv: fix data race in mocktikv split (#12414) --- store/mockstore/mocktikv/cluster.go | 6 ++++-- store/mockstore/mocktikv/rpc.go | 4 +--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/store/mockstore/mocktikv/cluster.go b/store/mockstore/mocktikv/cluster.go index 672e88f14bb28..f941e749145f0 100644 --- a/store/mockstore/mocktikv/cluster.go +++ b/store/mockstore/mocktikv/cluster.go @@ -371,13 +371,15 @@ func (c *Cluster) Split(regionID, newRegionID uint64, key []byte, peerIDs []uint } // SplitRaw splits a Region at the key (not encoded) and creates new Region. -func (c *Cluster) SplitRaw(regionID, newRegionID uint64, rawKey []byte, peerIDs []uint64, leaderPeerID uint64) *Region { +func (c *Cluster) SplitRaw(regionID, newRegionID uint64, rawKey []byte, peerIDs []uint64, leaderPeerID uint64) *metapb.Region { c.Lock() defer c.Unlock() newRegion := c.regions[regionID].split(newRegionID, rawKey, peerIDs, leaderPeerID) c.regions[newRegionID] = newRegion - return newRegion + // The mocktikv should return a deep copy of meta info to avoid data race + meta := proto.Clone(newRegion.Meta) + return meta.(*metapb.Region) } // Merge merges 2 regions, their key ranges should be adjacent. diff --git a/store/mockstore/mocktikv/rpc.go b/store/mockstore/mocktikv/rpc.go index 32dff89fd0427..09e3c6b8d057f 100755 --- a/store/mockstore/mocktikv/rpc.go +++ b/store/mockstore/mocktikv/rpc.go @@ -624,9 +624,7 @@ func (h *rpcHandler) handleSplitRegion(req *kvrpcpb.SplitRegionRequest) *kvrpcpb } newRegionID, newPeerIDs := h.cluster.AllocID(), h.cluster.AllocIDs(len(region.Peers)) newRegion := h.cluster.SplitRaw(region.GetId(), newRegionID, k, newPeerIDs, newPeerIDs[0]) - // The mocktikv should return a deep copy of meta info to avoid data race - metaCloned := proto.Clone(newRegion.Meta) - resp.Regions = append(resp.Regions, metaCloned.(*metapb.Region)) + resp.Regions = append(resp.Regions, newRegion) } return resp }