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

placement: add manager and FitRegion interface #1865

Merged
merged 7 commits into from
Nov 4, 2019
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
7 changes: 7 additions & 0 deletions pkg/mock/mockcluster/mockcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/pingcap/pd/pkg/mock/mockoption"
"github.com/pingcap/pd/server/core"
"github.com/pingcap/pd/server/namespace"
"github.com/pingcap/pd/server/schedule/placement"
"github.com/pingcap/pd/server/statistics"
"go.uber.org/zap"
)
Expand All @@ -34,6 +35,7 @@ type Cluster struct {
*core.BasicCluster
*mockid.IDAllocator
*mockoption.ScheduleOptions
*placement.RuleManager
*statistics.HotCache
*statistics.StoresStats
ID uint64
Expand Down Expand Up @@ -114,6 +116,11 @@ func (mc *Cluster) AllocPeer(storeID uint64) (*metapb.Peer, error) {
return peer, nil
}

// FitRegion fits a region to the rules it matches.
func (mc *Cluster) FitRegion(region *core.RegionInfo) *placement.RegionFit {
return mc.RuleManager.FitRegion(mc.BasicCluster, region)
}

// SetStoreUp sets store state to be up.
func (mc *Cluster) SetStoreUp(storeID uint64) {
store := mc.GetStore(storeID)
Expand Down
15 changes: 15 additions & 0 deletions server/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
syncer "github.com/pingcap/pd/server/region_syncer"
"github.com/pingcap/pd/server/schedule"
"github.com/pingcap/pd/server/schedule/checker"
"github.com/pingcap/pd/server/schedule/placement"
"github.com/pingcap/pd/server/statistics"
"github.com/pkg/errors"
"go.uber.org/zap"
Expand Down Expand Up @@ -84,6 +85,8 @@ type RaftCluster struct {
wg sync.WaitGroup
quit chan struct{}
regionSyncer *syncer.RegionSyncer

ruleManager *placement.RuleManager
}

// ClusterStatus saves some state information
Expand Down Expand Up @@ -1429,6 +1432,18 @@ func (c *RaftCluster) putRegion(region *core.RegionInfo) error {
return nil
}

// GetRuleManager returns the rule manager reference.
func (c *RaftCluster) GetRuleManager() *placement.RuleManager {
c.RLock()
defer c.RUnlock()
return c.ruleManager
}

// FitRegion tries to fit the region with placement rules.
func (c *RaftCluster) FitRegion(region *core.RegionInfo) *placement.RegionFit {
return c.GetRuleManager().FitRegion(c, region)
}

type prepareChecker struct {
reactiveRegions map[uint64]int
start time.Time
Expand Down
3 changes: 3 additions & 0 deletions server/schedule/opt/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/pd/server/core"
"github.com/pingcap/pd/server/namespace"
"github.com/pingcap/pd/server/schedule/placement"
"github.com/pingcap/pd/server/statistics"
)

Expand Down Expand Up @@ -84,4 +85,6 @@ type Cluster interface {
// TODO: it should be removed. Schedulers don't need to know anything
// about peers.
AllocPeer(storeID uint64) (*metapb.Peer, error)

FitRegion(*core.RegionInfo) *placement.RegionFit
}
104 changes: 104 additions & 0 deletions server/schedule/placement/fit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// Copyright 2019 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package placement

import (
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/pd/server/core"
)

// RegionFit is the result of fitting a region's peers to rule list.
type RegionFit struct {
Luffbee marked this conversation as resolved.
Show resolved Hide resolved
RuleFits []*RuleFit
OrphanPeers []*metapb.Peer
rleungx marked this conversation as resolved.
Show resolved Hide resolved
}

// IsSatisfied returns if the rules are properly satisfied.
func (f *RegionFit) IsSatisfied() bool {
if len(f.RuleFits) == 0 {
return false
}
for _, r := range f.RuleFits {
if !r.IsSatisfied() {
return false
}
}
return len(f.OrphanPeers) == 0
}

// GetRuleFit returns the RuleFit that contains the peer.
func (f *RegionFit) GetRuleFit(peerID uint64) *RuleFit {
for _, rf := range f.RuleFits {
for _, p := range rf.Peers {
if p.GetId() == peerID {
return rf
}
}
}
return nil
}

// CompareRegionFit determines the superiority of 2 fits.
func CompareRegionFit(a, b *RegionFit) int {
for i := range a.RuleFits {
if cmp := compareRuleFit(a.RuleFits[i], b.RuleFits[i]); cmp != 0 {
return cmp
}
}
switch {
case len(a.OrphanPeers) < len(b.OrphanPeers):
return 1
case len(a.OrphanPeers) > len(b.OrphanPeers):
return -1
default:
return 0
}
}

// RuleFit is the result of fitting status of a Rule.
type RuleFit struct {
Rule *Rule
Peers []*metapb.Peer
LooseMatchedPeers []*metapb.Peer
Luffbee marked this conversation as resolved.
Show resolved Hide resolved
IsolationLevel int
}

// IsSatisfied returns if the rule is properly satisfied.
func (f *RuleFit) IsSatisfied() bool {
return len(f.Peers) == f.Rule.Count && len(f.LooseMatchedPeers) == 0
}

func compareRuleFit(a, b *RuleFit) int {
switch {
case len(a.Peers) < len(b.Peers):
return -1
nolouch marked this conversation as resolved.
Show resolved Hide resolved
case len(a.Peers) > len(b.Peers):
return 1
case len(a.LooseMatchedPeers) > len(b.LooseMatchedPeers):
return -1
case len(a.LooseMatchedPeers) < len(b.LooseMatchedPeers):
disksing marked this conversation as resolved.
Show resolved Hide resolved
return 1
case a.IsolationLevel < b.IsolationLevel:
return -1
case a.IsolationLevel > b.IsolationLevel:
return 1
default:
return 0
}
}

// FitRegion tries to fit peers of a region to the rules.
func FitRegion(stores core.StoreSetInformer, region *core.RegionInfo, rules []*Rule) *RegionFit {
return nil
}
Loading