-
Notifications
You must be signed in to change notification settings - Fork 725
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
server: remove extra region clone when handle region heartbeat #1195
Changes from 2 commits
eb6781c
d71ccfc
fff05bb
2f74e7a
0a43341
c24527f
b480825
43b34da
ea72914
1f8ca97
643fccd
acf87a2
0cfb64b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -446,7 +446,6 @@ func (c *clusterInfo) updateStoreStatusLocked(id uint64) { | |
|
||
// handleRegionHeartbeat updates the region information. | ||
func (c *clusterInfo) handleRegionHeartbeat(region *core.RegionInfo) error { | ||
region = region.Clone() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The region is used after calling this function. Are you sure that it is safe to not clone? See: https://github.com/pingcap/pd/blob/9b0763fb0474c657c1abbef8238871f3d1284817/server/cluster_worker.go#L28-L41 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, all function only read it. More reassuring, I clone the region which has operator now. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It does not make sense to clone the region. Cloning the region reads the region too. I'm not so sure that the region is inmutable. I think you can propose another PR to change all fields of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. got it. |
||
c.RLock() | ||
origin := c.core.Regions.GetRegion(region.GetId()) | ||
isWriteUpdate, writeItem := c.core.CheckWriteStatus(region) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -353,12 +353,12 @@ func (s *testClusterInfoSuite) TestRegionHeartbeat(c *C) { | |
|
||
for i, region := range regions { | ||
// region does not exist. | ||
c.Assert(cluster.handleRegionHeartbeat(region), IsNil) | ||
c.Assert(cluster.handleRegionHeartbeat(region.Clone()), IsNil) | ||
checkRegions(c, cluster.core.Regions, regions[:i+1]) | ||
checkRegionsKV(c, cluster.kv, regions[:i+1]) | ||
|
||
// region is the same, not updated. | ||
c.Assert(cluster.handleRegionHeartbeat(region), IsNil) | ||
c.Assert(cluster.handleRegionHeartbeat(region.Clone()), IsNil) | ||
checkRegions(c, cluster.core.Regions, regions[:i+1]) | ||
checkRegionsKV(c, cluster.kv, regions[:i+1]) | ||
|
||
|
@@ -368,7 +368,7 @@ func (s *testClusterInfoSuite) TestRegionHeartbeat(c *C) { | |
region.RegionEpoch = &metapb.RegionEpoch{ | ||
Version: epoch.GetVersion() + 1, | ||
} | ||
c.Assert(cluster.handleRegionHeartbeat(region), IsNil) | ||
c.Assert(cluster.handleRegionHeartbeat(region.Clone()), IsNil) | ||
checkRegions(c, cluster.core.Regions, regions[:i+1]) | ||
checkRegionsKV(c, cluster.kv, regions[:i+1]) | ||
|
||
|
@@ -386,7 +386,7 @@ func (s *testClusterInfoSuite) TestRegionHeartbeat(c *C) { | |
Version: epoch.GetVersion() + 1, | ||
ConfVer: epoch.GetConfVer() + 1, | ||
} | ||
c.Assert(cluster.handleRegionHeartbeat(region), IsNil) | ||
c.Assert(cluster.handleRegionHeartbeat(region.Clone()), IsNil) | ||
checkRegions(c, cluster.core.Regions, regions[:i+1]) | ||
checkRegionsKV(c, cluster.kv, regions[:i+1]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. duplicated? |
||
|
||
|
@@ -406,33 +406,33 @@ func (s *testClusterInfoSuite) TestRegionHeartbeat(c *C) { | |
DownSeconds: 42, | ||
}, | ||
} | ||
c.Assert(cluster.handleRegionHeartbeat(region), IsNil) | ||
c.Assert(cluster.handleRegionHeartbeat(region.Clone()), IsNil) | ||
checkRegions(c, cluster.core.Regions, regions[:i+1]) | ||
|
||
// Add a pending peer. | ||
region.PendingPeers = []*metapb.Peer{region.Peers[rand.Intn(len(region.Peers))]} | ||
c.Assert(cluster.handleRegionHeartbeat(region), IsNil) | ||
c.Assert(cluster.handleRegionHeartbeat(region.Clone()), IsNil) | ||
checkRegions(c, cluster.core.Regions, regions[:i+1]) | ||
|
||
// Clear down peers. | ||
region.DownPeers = nil | ||
c.Assert(cluster.handleRegionHeartbeat(region), IsNil) | ||
c.Assert(cluster.handleRegionHeartbeat(region.Clone()), IsNil) | ||
checkRegions(c, cluster.core.Regions, regions[:i+1]) | ||
|
||
// Clear pending peers. | ||
region.PendingPeers = nil | ||
c.Assert(cluster.handleRegionHeartbeat(region), IsNil) | ||
c.Assert(cluster.handleRegionHeartbeat(region.Clone()), IsNil) | ||
checkRegions(c, cluster.core.Regions, regions[:i+1]) | ||
|
||
// Remove peers. | ||
origin := region.Clone() | ||
region.Peers = region.Peers[:1] | ||
c.Assert(cluster.handleRegionHeartbeat(region), IsNil) | ||
c.Assert(cluster.handleRegionHeartbeat(region.Clone()), IsNil) | ||
checkRegions(c, cluster.core.Regions, regions[:i+1]) | ||
checkRegionsKV(c, cluster.kv, regions[:i+1]) | ||
// Add peers. | ||
region.Peers = origin.Peers | ||
c.Assert(cluster.handleRegionHeartbeat(region), IsNil) | ||
c.Assert(cluster.handleRegionHeartbeat(region.Clone()), IsNil) | ||
checkRegions(c, cluster.core.Regions, regions[:i+1]) | ||
checkRegionsKV(c, cluster.kv, regions[:i+1]) | ||
} | ||
|
@@ -538,29 +538,29 @@ func (s *testClusterInfoSuite) TestHeartbeatSplit(c *C) { | |
|
||
// 1: [nil, nil) | ||
region1 := core.NewRegionInfo(&metapb.Region{Id: 1, RegionEpoch: &metapb.RegionEpoch{Version: 1, ConfVer: 1}}, nil) | ||
c.Assert(cluster.handleRegionHeartbeat(region1), IsNil) | ||
c.Assert(cluster.handleRegionHeartbeat(region1.Clone()), IsNil) | ||
checkRegion(c, cluster.searchRegion([]byte("foo")), region1) | ||
|
||
// split 1 to 2: [nil, m) 1: [m, nil), sync 2 first. | ||
region1.StartKey, region1.RegionEpoch.Version = []byte("m"), 2 | ||
region2 := core.NewRegionInfo(&metapb.Region{Id: 2, EndKey: []byte("m"), RegionEpoch: &metapb.RegionEpoch{Version: 1, ConfVer: 1}}, nil) | ||
c.Assert(cluster.handleRegionHeartbeat(region2), IsNil) | ||
c.Assert(cluster.handleRegionHeartbeat(region2.Clone()), IsNil) | ||
checkRegion(c, cluster.searchRegion([]byte("a")), region2) | ||
// [m, nil) is missing before r1's heartbeat. | ||
c.Assert(cluster.searchRegion([]byte("z")), IsNil) | ||
|
||
c.Assert(cluster.handleRegionHeartbeat(region1), IsNil) | ||
c.Assert(cluster.handleRegionHeartbeat(region1.Clone()), IsNil) | ||
checkRegion(c, cluster.searchRegion([]byte("z")), region1) | ||
|
||
// split 1 to 3: [m, q) 1: [q, nil), sync 1 first. | ||
region1.StartKey, region1.RegionEpoch.Version = []byte("q"), 3 | ||
region3 := core.NewRegionInfo(&metapb.Region{Id: 3, StartKey: []byte("m"), EndKey: []byte("q"), RegionEpoch: &metapb.RegionEpoch{Version: 1, ConfVer: 1}}, nil) | ||
c.Assert(cluster.handleRegionHeartbeat(region1), IsNil) | ||
c.Assert(cluster.handleRegionHeartbeat(region1.Clone()), IsNil) | ||
checkRegion(c, cluster.searchRegion([]byte("z")), region1) | ||
checkRegion(c, cluster.searchRegion([]byte("a")), region2) | ||
// [m, q) is missing before r3's heartbeat. | ||
c.Assert(cluster.searchRegion([]byte("n")), IsNil) | ||
c.Assert(cluster.handleRegionHeartbeat(region3), IsNil) | ||
c.Assert(cluster.handleRegionHeartbeat(region3.Clone()), IsNil) | ||
checkRegion(c, cluster.searchRegion([]byte("n")), region3) | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we remove the
Clone
method?