Documentation ¶
Index ¶
- Constants
- Variables
- func DistinctScore(labels []string, stores []*core.StoreInfo, other *core.StoreInfo) float64
- func FilterSource(opt Options, store *core.StoreInfo, filters []Filter) bool
- func FilterTarget(opt Options, store *core.StoreInfo, filters []Filter) bool
- func RegisterScheduler(name string, createFn CreateSchedulerFunc)
- type AddPeer
- type BasicCluster
- func (bc *BasicCluster) BlockStore(storeID uint64) error
- func (bc *BasicCluster) GetFollowerStores(region *core.RegionInfo) []*core.StoreInfo
- func (bc *BasicCluster) GetLeaderStore(region *core.RegionInfo) *core.StoreInfo
- func (bc *BasicCluster) GetRegion(regionID uint64) *core.RegionInfo
- func (bc *BasicCluster) GetRegionStores(region *core.RegionInfo) []*core.StoreInfo
- func (bc *BasicCluster) GetStore(storeID uint64) *core.StoreInfo
- func (bc *BasicCluster) GetStores() []*core.StoreInfo
- func (bc *BasicCluster) GetStoresAverageScore(kind core.ResourceKind) float64
- func (bc *BasicCluster) IsRegionHot(id uint64) bool
- func (bc *BasicCluster) PutRegion(region *core.RegionInfo) error
- func (bc *BasicCluster) PutStore(store *core.StoreInfo) error
- func (bc *BasicCluster) RandFollowerRegion(storeID uint64) *core.RegionInfo
- func (bc *BasicCluster) RandLeaderRegion(storeID uint64) *core.RegionInfo
- func (bc *BasicCluster) RegionReadStats() []*core.RegionStat
- func (bc *BasicCluster) RegionWriteStats() []*core.RegionStat
- func (bc *BasicCluster) UnblockStore(storeID uint64)
- func (bc *BasicCluster) UpdateReadStatCache(region *core.RegionInfo, hotRegionThreshold uint64)
- func (bc *BasicCluster) UpdateReadStatus(region *core.RegionInfo)
- func (bc *BasicCluster) UpdateWriteStatCache(region *core.RegionInfo, hotRegionThreshold uint64)
- func (bc *BasicCluster) UpdateWriteStatus(region *core.RegionInfo)
- type Cluster
- type CreateSchedulerFunc
- type Filter
- func NewBlockFilter() Filter
- func NewCacheFilter(cache *cache.TTLUint64) Filter
- func NewDistinctScoreFilter(labels []string, stores []*core.StoreInfo, source *core.StoreInfo) Filter
- func NewExcludedFilter(sources, targets map[uint64]struct{}) Filter
- func NewHealthFilter() Filter
- func NewNamespaceFilter(classifier namespace.Classifier, namespace string) Filter
- func NewPendingPeerCountFilter() Filter
- func NewSnapshotCountFilter() Filter
- func NewStateFilter() Filter
- func NewStorageThresholdFilter() Filter
- type Limiter
- type NamespaceChecker
- func (n *NamespaceChecker) Check(region *core.RegionInfo) *Operator
- func (n *NamespaceChecker) SelectBestPeerToRelocate(region *core.RegionInfo, targets []*core.StoreInfo, filters ...Filter) *metapb.Peer
- func (n *NamespaceChecker) SelectBestStoreToRelocate(region *core.RegionInfo, targets []*core.StoreInfo, filters ...Filter) uint64
- type NamespaceOptions
- type OpInfluence
- type Operator
- func CreateMovePeerOperator(desc string, region *core.RegionInfo, kind OperatorKind, ...) *Operator
- func CreateRemovePeerOperator(desc string, kind OperatorKind, region *core.RegionInfo, storeID uint64) *Operator
- func NewOperator(desc string, regionID uint64, kind OperatorKind, steps ...OperatorStep) *Operator
- func (o *Operator) Check(region *core.RegionInfo) OperatorStep
- func (o *Operator) Desc() string
- func (o *Operator) ElapsedTime() time.Duration
- func (o *Operator) GetPriorityLevel() core.PriorityLevel
- func (o *Operator) History() []OperatorHistory
- func (o *Operator) Influence(opInfluence OpInfluence, region *core.RegionInfo)
- func (o *Operator) IsFinish() bool
- func (o *Operator) IsTimeout() bool
- func (o *Operator) Kind() OperatorKind
- func (o *Operator) Len() int
- func (o *Operator) MarshalJSON() ([]byte, error)
- func (o *Operator) RegionID() uint64
- func (o *Operator) SetPriorityLevel(level core.PriorityLevel)
- func (o *Operator) Step(i int) OperatorStep
- func (o *Operator) String() string
- type OperatorHistory
- type OperatorKind
- type OperatorStep
- type Options
- type RegionScatterer
- type RemovePeer
- type ReplicaChecker
- type Scheduler
- type Selector
- type StoreInfluence
- type TransferLeader
Constants ¶
const MaxOperatorWaitTime = 5 * time.Minute
MaxOperatorWaitTime is the duration that if an operator lives longer that it, the operator is considered timeout.
const (
// RegionHeartBeatReportInterval is the heartbeat report interval of a region
RegionHeartBeatReportInterval = 60
)
Variables ¶
var (
// HotRegionLowThreshold is the low threadshold of hot region
HotRegionLowThreshold = 3
)
var Simulating bool
Simulating is an option to overpass the impact of accelerated time. Should only turned on by the simulator.
Functions ¶
func DistinctScore ¶
DistinctScore returns the score that the other is distinct from the stores. A higher score means the other store is more different from the existed stores.
func FilterSource ¶
FilterSource checks if store can pass all Filters as source store.
func FilterTarget ¶
FilterTarget checks if store can pass all Filters as target store.
func RegisterScheduler ¶
func RegisterScheduler(name string, createFn CreateSchedulerFunc)
RegisterScheduler binds a scheduler creator. It should be called in init() func of a package.
Types ¶
type AddPeer ¶
type AddPeer struct {
ToStore, PeerID uint64
}
AddPeer is an OperatorStep that adds a region peer.
func (AddPeer) Influence ¶
func (ap AddPeer) Influence(opInfluence OpInfluence, region *core.RegionInfo)
Influence calculates the store difference that current step make
type BasicCluster ¶
type BasicCluster struct { Stores *core.StoresInfo Regions *core.RegionsInfo WriteStatistics cache.Cache ReadStatistics cache.Cache }
BasicCluster provides basic data member and interface for a tikv cluster.
func (*BasicCluster) BlockStore ¶
func (bc *BasicCluster) BlockStore(storeID uint64) error
BlockStore stops balancer from selecting the store.
func (*BasicCluster) GetFollowerStores ¶
func (bc *BasicCluster) GetFollowerStores(region *core.RegionInfo) []*core.StoreInfo
GetFollowerStores returns all Stores that contains the region's follower peer.
func (*BasicCluster) GetLeaderStore ¶
func (bc *BasicCluster) GetLeaderStore(region *core.RegionInfo) *core.StoreInfo
GetLeaderStore returns all Stores that contains the region's leader peer.
func (*BasicCluster) GetRegion ¶
func (bc *BasicCluster) GetRegion(regionID uint64) *core.RegionInfo
GetRegion searches for a region by ID.
func (*BasicCluster) GetRegionStores ¶
func (bc *BasicCluster) GetRegionStores(region *core.RegionInfo) []*core.StoreInfo
GetRegionStores returns all Stores that contains the region's peer.
func (*BasicCluster) GetStore ¶
func (bc *BasicCluster) GetStore(storeID uint64) *core.StoreInfo
GetStore searches for a store by ID.
func (*BasicCluster) GetStores ¶
func (bc *BasicCluster) GetStores() []*core.StoreInfo
GetStores returns all Stores in the cluster.
func (*BasicCluster) GetStoresAverageScore ¶
func (bc *BasicCluster) GetStoresAverageScore(kind core.ResourceKind) float64
GetStoresAverageScore returns the total resource score of all StoreInfo.
func (*BasicCluster) IsRegionHot ¶
func (bc *BasicCluster) IsRegionHot(id uint64) bool
IsRegionHot checks if a region is in hot state.
func (*BasicCluster) PutRegion ¶
func (bc *BasicCluster) PutRegion(region *core.RegionInfo) error
PutRegion put a region
func (*BasicCluster) PutStore ¶
func (bc *BasicCluster) PutStore(store *core.StoreInfo) error
PutStore put a store
func (*BasicCluster) RandFollowerRegion ¶
func (bc *BasicCluster) RandFollowerRegion(storeID uint64) *core.RegionInfo
RandFollowerRegion returns a random region that has a follower on the store.
func (*BasicCluster) RandLeaderRegion ¶
func (bc *BasicCluster) RandLeaderRegion(storeID uint64) *core.RegionInfo
RandLeaderRegion returns a random region that has leader on the store.
func (*BasicCluster) RegionReadStats ¶
func (bc *BasicCluster) RegionReadStats() []*core.RegionStat
RegionReadStats returns hot region's read stats.
func (*BasicCluster) RegionWriteStats ¶
func (bc *BasicCluster) RegionWriteStats() []*core.RegionStat
RegionWriteStats returns hot region's write stats.
func (*BasicCluster) UnblockStore ¶
func (bc *BasicCluster) UnblockStore(storeID uint64)
UnblockStore allows balancer to select the store.
func (*BasicCluster) UpdateReadStatCache ¶
func (bc *BasicCluster) UpdateReadStatCache(region *core.RegionInfo, hotRegionThreshold uint64)
UpdateReadStatCache updates statistic for a region if it's hot, or remove it from statistics if it cools down
func (*BasicCluster) UpdateReadStatus ¶
func (bc *BasicCluster) UpdateReadStatus(region *core.RegionInfo)
UpdateReadStatus update the read status
func (*BasicCluster) UpdateWriteStatCache ¶
func (bc *BasicCluster) UpdateWriteStatCache(region *core.RegionInfo, hotRegionThreshold uint64)
UpdateWriteStatCache updates statistic for a region if it's hot, or remove it from statistics if it cools down
func (*BasicCluster) UpdateWriteStatus ¶
func (bc *BasicCluster) UpdateWriteStatus(region *core.RegionInfo)
UpdateWriteStatus update the write status
type Cluster ¶
type Cluster interface { RandFollowerRegion(storeID uint64) *core.RegionInfo RandLeaderRegion(storeID uint64) *core.RegionInfo GetStores() []*core.StoreInfo GetStore(id uint64) *core.StoreInfo GetRegion(id uint64) *core.RegionInfo GetRegionStores(region *core.RegionInfo) []*core.StoreInfo GetFollowerStores(region *core.RegionInfo) []*core.StoreInfo GetLeaderStore(region *core.RegionInfo) *core.StoreInfo GetStoresAverageScore(kind core.ResourceKind) float64 ScanRegions(startKey []byte, limit int) []*core.RegionInfo BlockStore(id uint64) error UnblockStore(id uint64) IsRegionHot(id uint64) bool RegionWriteStats() []*core.RegionStat RegionReadStats() []*core.RegionStat // get config methods GetOpt() NamespaceOptions Options // TODO: it should be removed. Schedulers don't need to know anything // about peers. AllocPeer(storeID uint64) (*metapb.Peer, error) }
Cluster provides an overview of a cluster's regions distribution.
type CreateSchedulerFunc ¶
CreateSchedulerFunc is for creating scheudler.
type Filter ¶
type Filter interface { // Return true if the store should not be used as a source store. FilterSource(opt Options, store *core.StoreInfo) bool // Return true if the store should not be used as a target store. FilterTarget(opt Options, store *core.StoreInfo) bool }
Filter is an interface to filter source and target store.
func NewBlockFilter ¶
func NewBlockFilter() Filter
NewBlockFilter creates a Filter that filters all stores that are blocked from balance.
func NewCacheFilter ¶
NewCacheFilter creates a Filter that filters all stores that are in the cache.
func NewDistinctScoreFilter ¶
func NewDistinctScoreFilter(labels []string, stores []*core.StoreInfo, source *core.StoreInfo) Filter
NewDistinctScoreFilter creates a filter that filters all stores that have lower distinct score than specified store.
func NewExcludedFilter ¶
NewExcludedFilter creates a Filter that filters all specified stores.
func NewHealthFilter ¶
func NewHealthFilter() Filter
NewHealthFilter creates a Filter that filters all stores that are Busy or Down.
func NewNamespaceFilter ¶
func NewNamespaceFilter(classifier namespace.Classifier, namespace string) Filter
NewNamespaceFilter creates a Filter that filters all stores that are not belong to a namespace.
func NewPendingPeerCountFilter ¶
func NewPendingPeerCountFilter() Filter
NewPendingPeerCountFilter creates a Filter that filters all stores that are currently handling too many pengding peers.
func NewSnapshotCountFilter ¶
func NewSnapshotCountFilter() Filter
NewSnapshotCountFilter creates a Filter that filters all stores that are currently handling too many snapshots.
func NewStateFilter ¶
func NewStateFilter() Filter
NewStateFilter creates a Filter that filters all stores that are not UP.
func NewStorageThresholdFilter ¶
func NewStorageThresholdFilter() Filter
NewStorageThresholdFilter creates a Filter that filters all stores that are almost full.
type Limiter ¶
Limiter a counter that limits the number of operators
func (*Limiter) OperatorCount ¶
func (l *Limiter) OperatorCount(mask OperatorKind) uint64
OperatorCount gets the count of operators filtered by mask.
func (*Limiter) UpdateCounts ¶
UpdateCounts updates resouce counts using current pending operators.
type NamespaceChecker ¶
type NamespaceChecker struct {
// contains filtered or unexported fields
}
NamespaceChecker ensures region to go to the right place.
func NewNamespaceChecker ¶
func NewNamespaceChecker(cluster Cluster, classifier namespace.Classifier) *NamespaceChecker
NewNamespaceChecker creates a namespace checker.
func (*NamespaceChecker) Check ¶
func (n *NamespaceChecker) Check(region *core.RegionInfo) *Operator
Check verifies a region's namespace, creating an Operator if need.
func (*NamespaceChecker) SelectBestPeerToRelocate ¶
func (n *NamespaceChecker) SelectBestPeerToRelocate(region *core.RegionInfo, targets []*core.StoreInfo, filters ...Filter) *metapb.Peer
SelectBestPeerToRelocate return a new peer that to be used to move a region
func (*NamespaceChecker) SelectBestStoreToRelocate ¶
func (n *NamespaceChecker) SelectBestStoreToRelocate(region *core.RegionInfo, targets []*core.StoreInfo, filters ...Filter) uint64
SelectBestStoreToRelocate randomly returns the store to relocate
type NamespaceOptions ¶
type NamespaceOptions interface { GetLeaderScheduleLimit(name string) uint64 GetRegionScheduleLimit(name string) uint64 GetReplicaScheduleLimit(name string) uint64 GetMaxReplicas(name string) int }
NamespaceOptions for namespace cluster
type OpInfluence ¶
type OpInfluence map[uint64]*StoreInfluence
OpInfluence is a map of StoreInfluence.
func NewOpInfluence ¶
func NewOpInfluence(operators []*Operator, cluster Cluster) OpInfluence
NewOpInfluence creates a OpInfluence.
func (OpInfluence) GetStoreInfluence ¶
func (m OpInfluence) GetStoreInfluence(id uint64) *StoreInfluence
GetStoreInfluence get storeInfluence of specific store.
type Operator ¶
type Operator struct {
// contains filtered or unexported fields
}
Operator contains execution steps generated by scheduler.
func CreateMovePeerOperator ¶
func CreateMovePeerOperator(desc string, region *core.RegionInfo, kind OperatorKind, oldStore, newStore uint64, peerID uint64) *Operator
CreateMovePeerOperator creates an Operator that replaces an old peer with a new peer. It prevents removing leader by transfer its leadership first.
func CreateRemovePeerOperator ¶
func CreateRemovePeerOperator(desc string, kind OperatorKind, region *core.RegionInfo, storeID uint64) *Operator
CreateRemovePeerOperator creates an Operator that removes a peer from region. It prevents removing leader by tranfer its leadership first.
func NewOperator ¶
func NewOperator(desc string, regionID uint64, kind OperatorKind, steps ...OperatorStep) *Operator
NewOperator creates a new operator.
func (*Operator) Check ¶
func (o *Operator) Check(region *core.RegionInfo) OperatorStep
Check checks if current step is finished, returns next step to take action. It's safe to be called by multiple goroutine concurrently.
func (*Operator) ElapsedTime ¶
ElapsedTime returns duration since it was created.
func (*Operator) GetPriorityLevel ¶
func (o *Operator) GetPriorityLevel() core.PriorityLevel
GetPriorityLevel get the priority level
func (*Operator) History ¶
func (o *Operator) History() []OperatorHistory
History transfers the operator's steps to operator histories.
func (*Operator) Influence ¶
func (o *Operator) Influence(opInfluence OpInfluence, region *core.RegionInfo)
Influence calculates the store difference which unfinished operator steps make
func (*Operator) IsTimeout ¶
IsTimeout checks the operator's create time and determines if it is timeout.
func (*Operator) MarshalJSON ¶
MarshalJSON serialize custom types to JSON
func (*Operator) SetPriorityLevel ¶
func (o *Operator) SetPriorityLevel(level core.PriorityLevel)
SetPriorityLevel set the priority level for operator
type OperatorHistory ¶
type OperatorHistory struct { FinishTime time.Time From, To uint64 Kind core.ResourceKind }
OperatorHistory is used to log and visualize completed operators.
type OperatorKind ¶
type OperatorKind uint32
OperatorKind is a bit field to identify operator types.
const ( OpLeader OperatorKind = 1 << iota // Include leader transfer. OpRegion // Include peer movement. OpAdmin // Initiated by admin. OpHotRegion // Initiated by hot region scheduler. OpAdjacent // Initiated by adjacent region scheduler. OpReplica // Initiated by replica checkers. OpBalance // Initiated by balancers. )
Flags for operators.
func ParseOperatorKind ¶
func ParseOperatorKind(str string) (OperatorKind, error)
ParseOperatorKind converts string (flag name list concat by ',') to OperatorKind.
func (OperatorKind) String ¶
func (k OperatorKind) String() string
type OperatorStep ¶
type OperatorStep interface { fmt.Stringer IsFinish(region *core.RegionInfo) bool Influence(opInfluence OpInfluence, region *core.RegionInfo) }
OperatorStep describes the basic scheduling steps that can not be subdivided.
type Options ¶
type Options interface { GetLeaderScheduleLimit() uint64 GetRegionScheduleLimit() uint64 GetReplicaScheduleLimit() uint64 GetMaxSnapshotCount() uint64 GetMaxPendingPeerCount() uint64 GetMaxStoreDownTime() time.Duration GetMaxReplicas() int GetLocationLabels() []string GetHotRegionLowThreshold() int GetTolerantSizeRatio() float64 }
Options for schedulers.
type RegionScatterer ¶
type RegionScatterer struct {
// contains filtered or unexported fields
}
RegionScatterer scatters regions.
func NewRegionScatterer ¶
func NewRegionScatterer(cluster Cluster, classifier namespace.Classifier) *RegionScatterer
NewRegionScatterer creates a region scatterer.
func (*RegionScatterer) Scatter ¶
func (r *RegionScatterer) Scatter(region *core.RegionInfo) *Operator
Scatter relocates the region.
type RemovePeer ¶
type RemovePeer struct {
FromStore uint64
}
RemovePeer is an OperatorStep that removes a region peer.
func (RemovePeer) Influence ¶
func (rp RemovePeer) Influence(opInfluence OpInfluence, region *core.RegionInfo)
Influence calculates the store difference that current step make
func (RemovePeer) IsFinish ¶
func (rp RemovePeer) IsFinish(region *core.RegionInfo) bool
IsFinish checks if current step is finished.
func (RemovePeer) String ¶
func (rp RemovePeer) String() string
type ReplicaChecker ¶
type ReplicaChecker struct {
// contains filtered or unexported fields
}
ReplicaChecker ensures region has the best replicas.
func NewReplicaChecker ¶
func NewReplicaChecker(cluster Cluster, classifier namespace.Classifier) *ReplicaChecker
NewReplicaChecker creates a replica checker.
func (*ReplicaChecker) Check ¶
func (r *ReplicaChecker) Check(region *core.RegionInfo) *Operator
Check verifies a region's replicas, creating an Operator if need.
func (*ReplicaChecker) SelectBestPeerToAddReplica ¶
func (r *ReplicaChecker) SelectBestPeerToAddReplica(region *core.RegionInfo, filters ...Filter) *metapb.Peer
SelectBestPeerToAddReplica returns a new peer that to be used to add a replica.
func (*ReplicaChecker) SelectBestStoreToAddReplica ¶
func (r *ReplicaChecker) SelectBestStoreToAddReplica(region *core.RegionInfo, filters ...Filter) (uint64, float64)
SelectBestStoreToAddReplica returns the store to add a replica.
type Scheduler ¶
type Scheduler interface { GetName() string // GetType should in accordance with the name passing to schedule.RegisterScheduler() GetType() string GetMinInterval() time.Duration GetNextInterval(interval time.Duration) time.Duration Prepare(cluster Cluster) error Cleanup(cluster Cluster) Schedule(cluster Cluster, opInfluence OpInfluence) *Operator IsScheduleAllowed(cluster Cluster) bool }
Scheduler is an interface to schedule resources.
type Selector ¶
type Selector interface { SelectSource(opt Options, stores []*core.StoreInfo, filters ...Filter) *core.StoreInfo SelectTarget(opt Options, stores []*core.StoreInfo, filters ...Filter) *core.StoreInfo }
Selector is an interface to select source and target store to schedule.
func NewBalanceSelector ¶
func NewBalanceSelector(kind core.ResourceKind, filters []Filter) Selector
NewBalanceSelector creates a Selector that select source/target store by their resource scores.
func NewRandomSelector ¶
NewRandomSelector creates a selector that select store randomly.
type StoreInfluence ¶
StoreInfluence records influences that pending operators will make.
type TransferLeader ¶
type TransferLeader struct {
FromStore, ToStore uint64
}
TransferLeader is an OperatorStep that transfers a region's leader.
func (TransferLeader) Influence ¶
func (tl TransferLeader) Influence(opInfluence OpInfluence, region *core.RegionInfo)
Influence calculates the store difference that current step make
func (TransferLeader) IsFinish ¶
func (tl TransferLeader) IsFinish(region *core.RegionInfo) bool
IsFinish checks if current step is finished.
func (TransferLeader) String ¶
func (tl TransferLeader) String() string