score

package
v5.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2025 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultHalfLifeTime = time.Hour
	MaxHalfTime         = 3 * time.Hour

	DefaultWeight     float64 = 1
	ProbeUpdateWeight float64 = 0.25
	RelayUpdateWeight float64 = 1

	// TODO: find actual numbers from info of latencies of high/mid/low CU from "stats.lavanet.xyz".
	// Do a distribution and find average factor to multiply the failure cost by.
	DefaultCuLatencyFactor = LowCuLatencyFactor
	HighCuLatencyFactor    = 0.01       // for cu > HighCuThreshold
	MidCuLatencyFactor     = 0.1        // for MidCuThreshold < cu < HighCuThreshold
	LowCuLatencyFactor     = float64(1) // for cu < MidCuThreshold

	HighCuThreshold = uint64(100)
	MidCuThreshold  = uint64(50)
)
View Source
const (
	DecPrecision         int64 = 8
	InitialDataStaleness       = 24 * time.Hour
)
View Source
const (
	DefaultLatencyNum      float64 = 0.01
	DefaultSyncNum         float64 = 0.1
	DefaultAvailabilityNum float64 = 1

	LatencyScoreType      = "latency"
	SyncScoreType         = "sync"
	AvailabilityScoreType = "availability"
	TotalScoreType        = "total"

	// Worst score results for each QoS excellence metric for truncation
	WorstLatencyScore      float64 = 30      // seconds
	WorstSyncScore         float64 = 20 * 60 // seconds
	WorstAvailabilityScore float64 = 0.00001 // very small value to avoid score = 0
)

Variables

View Source
var TimeConflictingScoresError = sdkerrors.New("TimeConflictingScoreStoreError", 5183, "ScoreStore has a more recent sample than the one provided")

Functions

func ConvertToDec

func ConvertToDec(val float64) sdk.Dec

func GetLatencyFactor

func GetLatencyFactor(cu uint64) float64

GetLatencyFactor returns the appropriate latency factor by the CU amount

Types

type AvailabilityScoreStore

type AvailabilityScoreStore struct {
	*ScoreStore
}

func (*AvailabilityScoreStore) Resolve

func (as *AvailabilityScoreStore) Resolve() (float64, error)

func (*AvailabilityScoreStore) Update

func (as *AvailabilityScoreStore) Update(sample float64, sampleTime time.Time) error

Update updates the availability ScoreStore's numerator and denominator with a new sample. The new sample must be 0 or 1.

type Config

type Config struct {
	Weight          float64
	HalfLife        time.Duration
	LatencyCuFactor float64 // should only be used for latency samples
}

func (Config) String

func (c Config) String() string

String prints a Config's fields

func (Config) Validate

func (c Config) Validate() error

Validate validates the Config's fields hold valid values

type LatencyScoreStore

type LatencyScoreStore struct {
	*ScoreStore
}

func (*LatencyScoreStore) Update

func (ls *LatencyScoreStore) Update(sample float64, sampleTime time.Time) error

Update updates the Latency ScoreStore's numerator and denominator with a new sample.

type Option

type Option func(*Config)

Option is used as a generic and elegant way to configure a new ScoreStore

func WithDecayHalfLife

func WithDecayHalfLife(halfLife time.Duration) Option

func WithLatencyCuFactor

func WithLatencyCuFactor(factor float64) Option

func WithWeight

func WithWeight(weight float64) Option

type ScoreStore

type ScoreStore struct {
	Name   string
	Num    float64 // using float64 and not math/big for performance
	Denom  float64
	Time   time.Time
	Config Config
	// contains filtered or unexported fields
}

ScoreStore is a decaying weighted average object that is used to collect providers performance metrics samples (see QoS excellence comment below). These are used to calculate the providers QoS excellence score, used by the provider optimizer when choosing providers to be paired with a consumer.

ScoreStore holds a score's numerator and denominator, last update timestamp, and a configuration object. When a ScoreStore updates it uses a decay exponent to lower the weight of old average samples and a weight parameter to determine the influence of the new sample.

Resolving the ScoreStore's num and denom means to divide the num by the denom to get the score. Keeping the score as a fracture helps calculating and updating weighted average calculations on the go.

func (*ScoreStore) GetConfig

func (ss *ScoreStore) GetConfig() Config

func (*ScoreStore) GetDenom

func (ss *ScoreStore) GetDenom() float64

func (*ScoreStore) GetLastUpdateTime

func (ss *ScoreStore) GetLastUpdateTime() time.Time

func (*ScoreStore) GetName

func (ss *ScoreStore) GetName() string

func (*ScoreStore) GetNum

func (ss *ScoreStore) GetNum() float64

func (*ScoreStore) Resolve

func (ss *ScoreStore) Resolve() (float64, error)

Resolve resolves the ScoreStore's frac by dividing the numerator by the denominator

func (*ScoreStore) String

func (ss *ScoreStore) String() string

String prints a ScoreStore's fields

func (*ScoreStore) Update

func (ss *ScoreStore) Update(sample float64, sampleTime time.Time) error

update updates the ScoreStore's numerator and denominator with a new sample. The ScoreStore's score is calculated as a weighted average with a decay factor. The new sample is added by the following formula:

num = num * decay_factor + sample * weight
denom = denom * decay_factor + weight
decay_factor = exp(-time_since_last_update / half_life_time)

func (*ScoreStore) UpdateConfig

func (ss *ScoreStore) UpdateConfig(opts ...Option) error

UpdateConfig updates the configuration of a ScoreStore

func (*ScoreStore) Validate

func (ss *ScoreStore) Validate() error

Validate validates the ScoreStore's fields hold valid values

type ScoreStorer

type ScoreStorer interface {
	Update(sample float64, sampleTime time.Time) error
	Resolve() (float64, error)
	Validate() error
	String() string
	UpdateConfig(opts ...Option) error

	GetName() string
	GetNum() float64
	GetDenom() float64
	GetLastUpdateTime() time.Time
	GetConfig() Config
}

ScoreStorer defines the interface for all score stores

func NewCustomScoreStore

func NewCustomScoreStore(scoreType string, num, denom float64, t time.Time, opts ...Option) (ScoreStorer, error)

NewCustomScoreStore creates a new custom ScoreStorer based on the score type

func NewScoreStore

func NewScoreStore(scoreType string) ScoreStorer

NewScoreStore creates a new default ScoreStorer based on the score type

type SyncScoreStore

type SyncScoreStore struct {
	*ScoreStore
}

func (*SyncScoreStore) Update

func (ss *SyncScoreStore) Update(sample float64, sampleTime time.Time) error

Update updates the Sync ScoreStore's numerator and denominator with a new sample.

Jump to

Keyboard shortcuts

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