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 2 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
32 changes: 29 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,7 @@ 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 := cloneRegion(f.region, core.WithReplacePeerStore(f.oldStore, store.GetID()))
newFit := f.fitter.FitRegion(region)
return placement.CompareRegionFit(f.oldFit, newFit) <= 0
}
Expand Down Expand Up @@ -468,8 +469,8 @@ 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 := cloneRegion(f.region, core.WithLeader(targetPeer))
newFit := f.fitter.FitRegion(copyRegion)
return placement.CompareRegionFit(f.oldFit, newFit) <= 0
}

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

// cloneRegion is used to replace region.Clone in filter which call the FitRegion,
// as FitRegion only needs partial information(startKey, endKey, peers, leader peer)
func cloneRegion(region *core.RegionInfo, opts ...core.RegionCreateOption) *core.RegionInfo {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do not clone the downPeers, pendingPeers? maybe rename to a suitable name?

Copy link
Contributor

Choose a reason for hiding this comment

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

How about createRegionForRuleFit(peers []*metapb.Peer, leader *metapb.Peer) *core.RegionInfo?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@nolouch currently FitRegion didn't consider the downPeers and pending peers, after discussed with @disksing , I create a tracking issue #2796

@disksing good advice, updated.

copyLeader := &metapb.Peer{
Id: region.GetLeader().Id,
StoreId: region.GetLeader().StoreId,
Role: region.GetLeader().Role,
}
copyPeers := make([]*metapb.Peer, 0, len(region.GetPeers()))
for _, p := range region.GetPeers() {
peer := &metapb.Peer{
Id: p.Id,
StoreId: p.StoreId,
Role: p.Role,
}
copyPeers = append(copyPeers, peer)
}
cloneRegion := core.NewRegionInfo(&metapb.Region{
StartKey: region.GetStartKey(),
EndKey: region.GetEndKey(),
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++ {
_ = cloneRegion(region)
}
}