placement

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2021 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CompareResourceFit

func CompareResourceFit(a, b *ResourceFit) int

CompareResourceFit determines the superiority of 2 fits. It returns 1 when the first fit result is better.

func MatchLabelConstraints

func MatchLabelConstraints(container *core.CachedContainer, constraints []LabelConstraint) bool

MatchLabelConstraints checks if a container matches label constraints list.

Types

type ContainerSet

type ContainerSet interface {
	GetContainers() []*core.CachedContainer
	GetContainer(id uint64) *core.CachedContainer
}

ContainerSet represents the container.

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.

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 container when trying to place peer of a resource.

func (*LabelConstraint) MatchContainer

func (c *LabelConstraint) MatchContainer(container *core.CachedContainer) bool

MatchContainer checks if a container matches the constraint.

type LabelConstraintOp

type LabelConstraintOp string

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

const (
	// In restricts the container label value should in the value list.
	// If label does not exist, `in` is always false.
	In LabelConstraintOp = "in"
	// NotIn restricts the container label value should not in the value list.
	// If label does not exist, `notIn` is always true.
	NotIn LabelConstraintOp = "notIn"
	// Exists restricts the container should have the label.
	Exists LabelConstraintOp = "exists"
	// NotExists restricts the container 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 ResourceFit

type ResourceFit struct {
	RuleFits    []*RuleFit
	OrphanPeers []metapb.Peer
}

ResourceFit is the result of fitting a resource'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 FitResource

func FitResource(containers ContainerSet, res *core.CachedResource, rules []*Rule) *ResourceFit

FitResource tries to fit peers of a resource to the rules.

func (*ResourceFit) GetRuleFit

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

GetRuleFit returns the RuleFit that contains the peer.

func (*ResourceFit) IsSatisfied

func (f *ResourceFit) IsSatisfied() bool

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

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
	Count            int               `json:"count"`                       // expected count of the peers
	LabelConstraints []LabelConstraint `json:"label_constraints,omitempty"` // used to select containers 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
	// contains filtered or unexported fields
}

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

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 container.

func (*Rule) String

func (r *Rule) String() string

type RuleFit

type RuleFit struct {
	Rule *Rule
	// Peers of the Resource that are divided to this Rule.
	Peers []metapb.Peer
	// 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
	// IsolationScore indicates at which level of labeling these Peers are
	// isolated. A larger value is better.
	IsolationScore float64
}

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.

func (*RuleGroup) String

func (g *RuleGroup) String() string

type RuleManager

type RuleManager struct {
	sync.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 storage.Storage) *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) FitResource

func (m *RuleManager) FitResource(containers ContainerSet, res *core.CachedResource) *ResourceFit

FitResource fits a resource 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) GetRulesForApplyResource

func (m *RuleManager) GetRulesForApplyResource(res *core.CachedResource) []*Rule

GetRulesForApplyResource returns the rules list that should be applied to a resource.

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) 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) 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) 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
	Action           RuleOpType `json:"action"`              // the operation type
	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"
)

Jump to

Keyboard shortcuts

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