Skip to content
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

filter: replace region.Clone in filter #2794

Merged
merged 5 commits into from
Aug 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions server/schedule/filter/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package filter
import (
"fmt"

"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/log"
"github.com/tikv/pd/pkg/slice"
"github.com/tikv/pd/server/core"
Expand Down Expand Up @@ -425,7 +426,9 @@ func (f *ruleFitFilter) Source(opt opt.Options, store *core.StoreInfo) bool {
}

func (f *ruleFitFilter) Target(opt opt.Options, store *core.StoreInfo) bool {
region := f.region.Clone(core.WithReplacePeerStore(f.oldStore, store.GetID()))
region := createRegionForRuleFit(f.region.GetStartKey(), f.region.GetEndKey(),
f.region.GetPeers(), f.region.GetLeader(),
core.WithReplacePeerStore(f.oldStore, store.GetID()))
newFit := f.fitter.FitRegion(region)
return placement.CompareRegionFit(f.oldFit, newFit) <= 0
}
Expand Down Expand Up @@ -468,8 +471,10 @@ func (f *ruleLeaderFitFilter) Target(opt opt.Options, store *core.StoreInfo) boo
log.Warn("ruleLeaderFitFilter couldn't find peer on target Store", zap.Uint64("target-store", store.GetID()))
return false
}
region := f.region.Clone(core.WithLeader(targetPeer))
newFit := f.fitter.FitRegion(region)
copyRegion := createRegionForRuleFit(f.region.GetStartKey(), f.region.GetEndKey(),
f.region.GetPeers(), f.region.GetLeader(),
core.WithLeader(targetPeer))
newFit := f.fitter.FitRegion(copyRegion)
return placement.CompareRegionFit(f.oldFit, newFit) <= 0
}

Expand Down Expand Up @@ -672,3 +677,29 @@ func (f *isolationFilter) Target(opt opt.Options, store *core.StoreInfo) bool {
}
return true
}

// createRegionForRuleFit is used to create a clone region with RegionCreateOptions which is only used for
// FitRegion in filter
func createRegionForRuleFit(startKey, endKey []byte,
peers []*metapb.Peer, leader *metapb.Peer, opts ...core.RegionCreateOption) *core.RegionInfo {
copyLeader := &metapb.Peer{
Id: leader.Id,
StoreId: leader.StoreId,
Role: leader.Role,
}
copyPeers := make([]*metapb.Peer, 0, len(peers))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems we don't have to copy leader. And if the peer list is untouched, we don't need to copy it either.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think for both ruleFitFilter and ruleLeaderFitFilter, they will both touch peers and leaders. I think it is necessary to copy peers and leaders either.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes sense.

for _, p := range peers {
peer := &metapb.Peer{
Id: p.Id,
StoreId: p.StoreId,
Role: p.Role,
}
copyPeers = append(copyPeers, peer)
}
cloneRegion := core.NewRegionInfo(&metapb.Region{
StartKey: startKey,
EndKey: endKey,
Peers: copyPeers,
}, copyLeader, opts...)
return cloneRegion
}
25 changes: 25 additions & 0 deletions server/schedule/filter/filters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,3 +272,28 @@ func (s *testFiltersSuite) TestPlacementGuard(c *C) {
FitsTypeOf,
newRuleFitFilter("", testCluster, region, 1))
}

func BenchmarkCloneRegionTest(b *testing.B) {
epoch := &metapb.RegionEpoch{
ConfVer: 1,
Version: 1,
}
region := core.NewRegionInfo(
&metapb.Region{
Id: 4,
StartKey: []byte("x"),
EndKey: []byte(""),
Peers: []*metapb.Peer{
{Id: 108, StoreId: 4},
},
RegionEpoch: epoch,
},
&metapb.Peer{Id: 108, StoreId: 4},
core.SetApproximateSize(50),
core.SetApproximateKeys(20),
)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = createRegionForRuleFit(region.GetStartKey(), region.GetEndKey(), region.GetPeers(), region.GetLeader())
}
}