placement

package
v1.1.0-beta.0...-efb03ec Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 1, 2024 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MatchLabelConstraints

func MatchLabelConstraints(store *core.StoreInfo, constraints []LabelConstraint) bool

MatchLabelConstraints checks if a store matches label constraints list.

func ValidateFit

func ValidateFit(fit *RegionFit) bool

ValidateFit checks whether regionFit is valid

func ValidateRegion

func ValidateRegion(region *core.RegionInfo) bool

ValidateRegion checks whether region is healthy

func ValidateStores

func ValidateStores(stores []*core.StoreInfo) bool

ValidateStores checks whether store isn't offline, unhealthy and disconnected. Only Up store should be cached in RegionFitCache

Types

type GroupBundle

type GroupBundle struct {
	ID       string  `json:"group_id"`
	Index    int     `json:"group_index"`
	Override bool    `json:"group_override"`
	Rules    []*Rule `json:"rules"`
}

GroupBundle represents a rule group and all rules belong to the group. NOTE: This type is exported by HTTP API. Please pay more attention when modifying it.

func (GroupBundle) String

func (g GroupBundle) String() string

type LabelConstraint

type LabelConstraint struct {
	Key    string            `json:"key,omitempty"`
	Op     LabelConstraintOp `json:"op,omitempty"`
	Values []string          `json:"values,omitempty"`
}

LabelConstraint is used to filter store when trying to place peer of a region.

func (*LabelConstraint) MatchStore

func (c *LabelConstraint) MatchStore(store *core.StoreInfo) bool

MatchStore checks if a store matches the constraint.

type LabelConstraintOp

type LabelConstraintOp string

LabelConstraintOp defines how a LabelConstraint matches a store. It can be one of 'in', 'notIn', 'exists', or 'notExists'.

const (
	// In restricts the store label value should in the value list.
	// If label does not exist, `in` is always false.
	In LabelConstraintOp = "in"
	// NotIn restricts the store label value should not in the value list.
	// If label does not exist, `notIn` is always true.
	NotIn LabelConstraintOp = "notIn"
	// Exists restricts the store should have the label.
	Exists LabelConstraintOp = "exists"
	// NotExists restricts the store should not have the label.
	NotExists LabelConstraintOp = "notExists"
)

type PeerRoleType

type PeerRoleType string

PeerRoleType is the expected peer type of the placement rule.

const (
	// Voter can either match a leader peer or follower peer
	Voter PeerRoleType = "voter"
	// Leader matches a leader.
	Leader PeerRoleType = "leader"
	// Follower matches a follower.
	Follower PeerRoleType = "follower"
	// Learner matches a learner.
	Learner PeerRoleType = "learner"
)

func (PeerRoleType) MetaPeerRole

func (s PeerRoleType) MetaPeerRole() metapb.PeerRole

MetaPeerRole converts placement.PeerRoleType to metapb.PeerRole.

type RegionFit

type RegionFit struct {
	RuleFits    []*RuleFit     `json:"rule-fits"`
	OrphanPeers []*metapb.Peer `json:"orphan-peers"`
	// contains filtered or unexported fields
}

RegionFit is the result of fitting a region's peers to rule list. All peers are divided into corresponding rules according to the matching rules, and the remaining Peers are placed in the OrphanPeers list.

func (*RegionFit) ExtraCount

func (f *RegionFit) ExtraCount() int

ExtraCount return the extra count.

func (*RegionFit) GetRegionStores

func (f *RegionFit) GetRegionStores() []*core.StoreInfo

GetRegionStores returns region's stores

func (*RegionFit) GetRuleFit

func (f *RegionFit) GetRuleFit(peerID uint64) *RuleFit

GetRuleFit returns the RuleFit that contains the peer.

func (*RegionFit) IsCached

func (f *RegionFit) IsCached() bool

IsCached indicates whether this result is fetched from caches

func (*RegionFit) IsSatisfied

func (f *RegionFit) IsSatisfied() bool

IsSatisfied returns if the rules are properly satisfied. It means all Rules are fulfilled and there is no orphan peers.

func (*RegionFit) Replace

func (f *RegionFit) Replace(srcStoreID uint64, dstStore *core.StoreInfo) bool

Replace return true if the replacement store is fit all constraints and isolation score is not less than the origin.

func (*RegionFit) SetCached

func (f *RegionFit) SetCached(cached bool)

SetCached indicates this RegionFit is fetch form cache

type RegionRuleFitCache

type RegionRuleFitCache struct {
	// contains filtered or unexported fields
}

RegionRuleFitCache stores regions RegionFit result and involving variables

func (*RegionRuleFitCache) IsUnchanged

func (cache *RegionRuleFitCache) IsUnchanged(region *core.RegionInfo, rules []*Rule, stores []*core.StoreInfo) bool

IsUnchanged checks whether the region and rules unchanged for the cache

type RegionRuleFitCacheManager

type RegionRuleFitCacheManager struct {
	// contains filtered or unexported fields
}

RegionRuleFitCacheManager stores each region's RegionFit Result and involving variables only when the RegionFit result is satisfied with its rules RegionRuleFitCacheManager caches RegionFit result for each region only when: 1. region have no down peers 2. RegionFit is satisfied RegionRuleFitCacheManager will invalid the cache for the region only when: 1. region peer topology is changed 2. region have down peers 3. region leader is changed 4. any involved rule is changed 5. stores topology is changed 6. any store label is changed 7. any store state is changed

func NewRegionRuleFitCacheManager

func NewRegionRuleFitCacheManager() *RegionRuleFitCacheManager

NewRegionRuleFitCacheManager returns RegionRuleFitCacheManager

func (*RegionRuleFitCacheManager) CheckAndGetCache

func (manager *RegionRuleFitCacheManager) CheckAndGetCache(region *core.RegionInfo,
	rules []*Rule,
	stores []*core.StoreInfo) (bool, *RegionFit)

CheckAndGetCache checks whether the region and rules are changed for the stored cache If the check pass, it will return the cache

func (*RegionRuleFitCacheManager) Invalid

func (manager *RegionRuleFitCacheManager) Invalid(regionID uint64)

Invalid invalid cache by regionID

func (*RegionRuleFitCacheManager) SetCache

func (manager *RegionRuleFitCacheManager) SetCache(region *core.RegionInfo, fit *RegionFit)

SetCache stores RegionFit cache

type Rule

type Rule struct {
	GroupID          string            `json:"group_id"`                    // mark the source that add the rule
	ID               string            `json:"id"`                          // unique ID within a group
	Index            int               `json:"index,omitempty"`             // rule apply order in a group, rule with less ID is applied first when indexes are equal
	Override         bool              `json:"override,omitempty"`          // when it is true, all rules with less indexes are disabled
	StartKey         []byte            `json:"-"`                           // range start key
	StartKeyHex      string            `json:"start_key"`                   // hex format start key, for marshal/unmarshal
	EndKey           []byte            `json:"-"`                           // range end key
	EndKeyHex        string            `json:"end_key"`                     // hex format end key, for marshal/unmarshal
	Role             PeerRoleType      `json:"role"`                        // expected role of the peers
	IsWitness        bool              `json:"is_witness"`                  // when it is true, it means the role is also a witness
	Count            int               `json:"count"`                       // expected count of the peers
	LabelConstraints []LabelConstraint `json:"label_constraints,omitempty"` // used to select stores to place peers
	LocationLabels   []string          `json:"location_labels,omitempty"`   // used to make peers isolated physically
	IsolationLevel   string            `json:"isolation_level,omitempty"`   // used to isolate replicas explicitly and forcibly
	Version          uint64            `json:"version,omitempty"`           // only set at runtime, add 1 each time rules updated, begin from 0.
	CreateTimestamp  uint64            `json:"create_timestamp,omitempty"`  // only set at runtime, recorded rule create timestamp
	// contains filtered or unexported fields
}

Rule is the placement rule that can be checked against a region. When applying rules (apply means schedule regions to match selected rules), the apply order is defined by the tuple [GroupIndex, GroupID, Index, ID].

NOTE: This type is exported by HTTP API. Please pay more attention when modifying it.

func (*Rule) Clone

func (r *Rule) Clone() *Rule

Clone returns a copy of Rule.

func (*Rule) Key

func (r *Rule) Key() [2]string

Key returns (groupID, ID) as the global unique key of a rule.

func (*Rule) StoreKey

func (r *Rule) StoreKey() string

StoreKey returns the rule's key for persistent store.

func (*Rule) String

func (r *Rule) String() string

type RuleFit

type RuleFit struct {
	Rule *Rule `json:"rule"`
	// Peers of the Region that are divided to this Rule.
	Peers []*metapb.Peer `json:"peers"`
	// PeersWithDifferentRole is subset of `Peers`. It contains all Peers that have
	// different Role from configuration (the Role can be migrated to target role
	// by scheduling).
	PeersWithDifferentRole []*metapb.Peer `json:"peers-different-role"`
	// IsolationScore indicates at which level of labeling these Peers are
	// isolated. A larger value is better.
	IsolationScore float64 `json:"isolation-score"`
	// contains filtered or unexported fields
}

RuleFit is the result of fitting status of a Rule.

func (*RuleFit) IsSatisfied

func (f *RuleFit) IsSatisfied() bool

IsSatisfied returns if the rule is properly satisfied.

type RuleGroup

type RuleGroup struct {
	ID       string `json:"id,omitempty"`
	Index    int    `json:"index,omitempty"`
	Override bool   `json:"override,omitempty"`
}

RuleGroup defines properties of a rule group. NOTE: This type is exported by HTTP API. Please pay more attention when modifying it.

func (*RuleGroup) String

func (g *RuleGroup) String() string

type RuleManager

type RuleManager struct {
	syncutil.RWMutex
	// contains filtered or unexported fields
}

RuleManager is responsible for the lifecycle of all placement Rules. It is thread safe.

func NewRuleManager

func NewRuleManager(storage endpoint.RuleStorage, storeSetInformer core.StoreSetInformer, opt *config.PersistOptions) *RuleManager

NewRuleManager creates a RuleManager instance.

func (*RuleManager) Batch

func (m *RuleManager) Batch(todo []RuleOp) error

Batch executes a series of actions at once.

func (*RuleManager) DeleteGroupBundle

func (m *RuleManager) DeleteGroupBundle(id string, regex bool) error

DeleteGroupBundle removes a Group and all rules belong to it. If `regex` is true, `id` is a regexp expression.

func (*RuleManager) DeleteRule

func (m *RuleManager) DeleteRule(group, id string) error

DeleteRule removes a Rule.

func (*RuleManager) DeleteRuleGroup

func (m *RuleManager) DeleteRuleGroup(id string) error

DeleteRuleGroup removes a RuleGroup.

func (*RuleManager) FitRegion

func (m *RuleManager) FitRegion(storeSet StoreSet, region *core.RegionInfo) *RegionFit

FitRegion fits a region to the rules it matches.

func (*RuleManager) GetAllGroupBundles

func (m *RuleManager) GetAllGroupBundles() []GroupBundle

GetAllGroupBundles returns all rules and groups configuration. Rules are grouped by groups.

func (*RuleManager) GetAllRules

func (m *RuleManager) GetAllRules() []*Rule

GetAllRules returns sorted all rules.

func (*RuleManager) GetGroupBundle

func (m *RuleManager) GetGroupBundle(id string) (b GroupBundle)

GetGroupBundle returns a group and all rules belong to it.

func (*RuleManager) GetRule

func (m *RuleManager) GetRule(group, id string) *Rule

GetRule returns the Rule with the same (group, id).

func (*RuleManager) GetRuleGroup

func (m *RuleManager) GetRuleGroup(id string) *RuleGroup

GetRuleGroup returns a RuleGroup configuration.

func (*RuleManager) GetRuleGroups

func (m *RuleManager) GetRuleGroups() []*RuleGroup

GetRuleGroups returns all RuleGroup configuration.

func (*RuleManager) GetRulesByGroup

func (m *RuleManager) GetRulesByGroup(group string) []*Rule

GetRulesByGroup returns sorted rules of a group.

func (*RuleManager) GetRulesByKey

func (m *RuleManager) GetRulesByKey(key []byte) []*Rule

GetRulesByKey returns sorted rules that affects a key.

func (*RuleManager) GetRulesForApplyRange

func (m *RuleManager) GetRulesForApplyRange(start, end []byte) []*Rule

GetRulesForApplyRange returns the rules list that should be applied to a range.

func (*RuleManager) GetRulesForApplyRegion

func (m *RuleManager) GetRulesForApplyRegion(region *core.RegionInfo) []*Rule

GetRulesForApplyRegion returns the rules list that should be applied to a region.

func (*RuleManager) GetSplitKeys

func (m *RuleManager) GetSplitKeys(start, end []byte) [][]byte

GetSplitKeys returns all split keys in the range (start, end).

func (*RuleManager) Initialize

func (m *RuleManager) Initialize(maxReplica int, locationLabels []string, isolationLevel string) error

Initialize loads rules from storage. If Placement Rules feature is never enabled, it creates default rule that is compatible with previous configuration.

func (*RuleManager) InvalidCache

func (m *RuleManager) InvalidCache(regionID uint64)

InvalidCache invalids the cache.

func (*RuleManager) IsInitialized

func (m *RuleManager) IsInitialized() bool

IsInitialized returns whether the rule manager is initialized.

func (*RuleManager) SetAllGroupBundles

func (m *RuleManager) SetAllGroupBundles(groups []GroupBundle, override bool) error

SetAllGroupBundles resets configuration. If override is true, all old configurations are dropped.

func (*RuleManager) SetGroupBundle

func (m *RuleManager) SetGroupBundle(group GroupBundle) error

SetGroupBundle resets a Group and all rules belong to it. All old rules belong to the Group are dropped.

func (*RuleManager) SetKeyType

func (m *RuleManager) SetKeyType(h string) *RuleManager

SetKeyType will update keyType for adjustRule()

func (*RuleManager) SetRegionFitCache

func (m *RuleManager) SetRegionFitCache(region *core.RegionInfo, fit *RegionFit)

SetRegionFitCache sets RegionFitCache

func (*RuleManager) SetRule

func (m *RuleManager) SetRule(rule *Rule) error

SetRule inserts or updates a Rule.

func (*RuleManager) SetRuleGroup

func (m *RuleManager) SetRuleGroup(group *RuleGroup) error

SetRuleGroup updates a RuleGroup.

func (*RuleManager) SetRules

func (m *RuleManager) SetRules(rules []*Rule) error

SetRules inserts or updates lots of Rules at once.

type RuleOp

type RuleOp struct {
	*Rule                       // information of the placement rule to add/delete the operation type
	Action           RuleOpType `json:"action"`
	DeleteByIDPrefix bool       `json:"delete_by_id_prefix"` // if action == delete, delete by the prefix of id
}

RuleOp is for batching placement rule actions. The action type is distinguished by the field `Action`.

func (RuleOp) String

func (r RuleOp) String() string

type RuleOpType

type RuleOpType string

RuleOpType indicates the operation type

const (
	// RuleOpAdd a placement rule, only need to specify the field *Rule
	RuleOpAdd RuleOpType = "add"
	// RuleOpDel a placement rule, only need to specify the field `GroupID`, `ID`, `MatchID`
	RuleOpDel RuleOpType = "del"
)

type StoreSet

type StoreSet interface {
	GetStores() []*core.StoreInfo
	GetStore(id uint64) *core.StoreInfo
}

StoreSet represents the store container.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL