balanced

package
v1.0.8 Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2024 License: Apache-2.0, MIT, Apache-2.0, + 1 more Imports: 11 Imported by: 3

Documentation

Overview

Package balanced implements an allocator that can sort allocations based on multiple metrics, where metrics may be an arbitrary way to partition a set of peers.

For example, allocating by ["tag:region", "disk"] the resulting peer candidate order will balanced between regions and ordered by the value of the weight of the disk metric.

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultAllocateBy = []string{"tag:group", "freespace"}
)

These are the default values for a Config.

Functions

This section is empty.

Types

type Allocator

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

Allocator is an allocator that partitions metrics and orders the final list of allocation by selecting for each partition.

func New

func New(cfg *Config) (*Allocator, error)

New returns an initialized Allocator.

func (*Allocator) Allocate

func (a *Allocator) Allocate(
	ctx context.Context,
	c api.Cid,
	current, candidates, priority api.MetricsSet,
) ([]peer.ID, error)

Allocate produces a sorted list of cluster peer IDs based on different metrics provided for those peer IDs. It works as follows:

  • First, it buckets each peer metrics based on the AllocateBy list. The metric name must match the bucket name, otherwise they are put at the end.
  • Second, based on the AllocateBy order, it orders the first bucket and groups peers by ordered value.
  • Third, it selects metrics on the second bucket for the most prioritary peers of the first bucket and orders their metrics. Then for the peers in second position etc.
  • It repeats the process until there is no more buckets to sort.
  • Finally, it returns the first peer of the first
  • Third, based on the AllocateBy order, it select the first metric

func (*Allocator) Metrics

func (a *Allocator) Metrics() []string

Metrics returns the names of the metrics that have been registered with this allocator.

func (*Allocator) SetClient

func (a *Allocator) SetClient(c *rpc.Client)

SetClient provides us with an rpc.Client which allows contacting other components in the cluster.

func (*Allocator) Shutdown

func (a *Allocator) Shutdown(ctx context.Context) error

Shutdown is called on cluster shutdown. We just invalidate any metrics from this point.

type Config

type Config struct {
	config.Saver

	AllocateBy []string
}

Config allows to initialize the Allocator.

func (*Config) ApplyEnvVars

func (cfg *Config) ApplyEnvVars() error

ApplyEnvVars fills in any Config fields found as environment variables.

func (*Config) ConfigKey

func (cfg *Config) ConfigKey() string

ConfigKey returns a human-friendly identifier for this Config's type.

func (*Config) Default

func (cfg *Config) Default() error

Default initializes this Config with sensible values.

func (*Config) LoadJSON

func (cfg *Config) LoadJSON(raw []byte) error

LoadJSON parses a raw JSON byte-slice as generated by ToJSON().

func (*Config) ToDisplayJSON

func (cfg *Config) ToDisplayJSON() ([]byte, error)

ToDisplayJSON returns JSON config as a string.

func (*Config) ToJSON

func (cfg *Config) ToJSON() ([]byte, error)

ToJSON generates a human-friendly JSON representation of this Config.

func (*Config) Validate

func (cfg *Config) Validate() error

Validate checks that the fields of this configuration have sensible values.

Jump to

Keyboard shortcuts

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