Anton Nikiforov
78e4d71cc0
All checks were successful
Tests and linters / Run gofumpt (pull_request) Successful in 1m30s
DCO action / DCO (pull_request) Successful in 1m42s
Vulncheck / Vulncheck (pull_request) Successful in 2m1s
Pre-commit hooks / Pre-commit (pull_request) Successful in 2m13s
Build / Build Components (pull_request) Successful in 2m25s
Tests and linters / gopls check (pull_request) Successful in 2m42s
Tests and linters / Staticcheck (pull_request) Successful in 2m48s
Tests and linters / Lint (pull_request) Successful in 3m38s
Tests and linters / Tests (pull_request) Successful in 4m18s
Tests and linters / Tests with -race (pull_request) Successful in 5m18s
Signed-off-by: Anton Nikiforov <an.nikiforov@yadro.com>
51 lines
1.2 KiB
Go
51 lines
1.2 KiB
Go
package placement
|
|
|
|
import (
|
|
"errors"
|
|
"strings"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/netmap"
|
|
)
|
|
|
|
const (
|
|
attrPrefix = "$attribute:"
|
|
)
|
|
|
|
type Metric interface {
|
|
CalculateValue(*netmap.NodeInfo, *netmap.NodeInfo) []byte
|
|
}
|
|
|
|
func ValidateMetric(raw string) error {
|
|
if strings.HasPrefix(raw, attrPrefix) {
|
|
return nil
|
|
}
|
|
return errors.New("unsupported priority metric")
|
|
}
|
|
|
|
func ParseMetric(raw string) Metric {
|
|
if attr, found := strings.CutPrefix(raw, attrPrefix); found {
|
|
return NewAttributeMetric(attr)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// attributeMetric describes priority metric based on attribute.
|
|
type attributeMetric struct {
|
|
attribute string
|
|
}
|
|
|
|
// CalculateValue return [0] if from and to contains attribute attributeMetric.attribute and
|
|
// the value of attribute is the same. In other case return [1].
|
|
func (am *attributeMetric) CalculateValue(from *netmap.NodeInfo, to *netmap.NodeInfo) []byte {
|
|
fromAttr := from.Attribute(am.attribute)
|
|
toAttr := to.Attribute(am.attribute)
|
|
if len(fromAttr) > 0 && len(toAttr) > 0 && fromAttr == toAttr {
|
|
return []byte{0}
|
|
}
|
|
return []byte{1}
|
|
}
|
|
|
|
func NewAttributeMetric(raw string) Metric {
|
|
attr, _ := strings.CutPrefix(raw, attrPrefix)
|
|
return &attributeMetric{attribute: attr}
|
|
}
|