frostfs-node/pkg/services/object_manager/placement/metrics.go
Anton Nikiforov dd8d28732b
All checks were successful
DCO action / DCO (pull_request) Successful in 29s
Vulncheck / Vulncheck (pull_request) Successful in 1m23s
Build / Build Components (pull_request) Successful in 1m47s
Pre-commit hooks / Pre-commit (pull_request) Successful in 1m52s
Tests and linters / Run gofumpt (pull_request) Successful in 2m53s
Tests and linters / Lint (pull_request) Successful in 3m12s
Tests and linters / Staticcheck (pull_request) Successful in 3m27s
Tests and linters / Tests (pull_request) Successful in 3m40s
Tests and linters / gopls check (pull_request) Successful in 4m39s
Tests and linters / Tests with -race (pull_request) Successful in 5m4s
[#1640] object: Add priority metric based on geo distance
Change-Id: I3a7ea4fc4807392bf50e6ff1389c61367c953074
Signed-off-by: Anton Nikiforov <an.nikiforov@yadro.com>
2025-03-31 20:27:34 +03:00

189 lines
4.6 KiB
Go

package placement
import (
"errors"
"fmt"
"maps"
"math"
"strings"
"sync"
"sync/atomic"
locodedb "git.frostfs.info/TrueCloudLab/frostfs-locode-db/pkg/locode/db"
locodebolt "git.frostfs.info/TrueCloudLab/frostfs-locode-db/pkg/locode/db/boltdb"
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/netmap"
)
const (
attrPrefix = "$attribute:"
geoDistance = "$geoDistance"
)
type Metric interface {
CalculateValue(*netmap.NodeInfo, *netmap.NodeInfo) int
}
type metricsParser struct {
locodeDBPath string
locodes map[string]locodedb.Point
nodeLocode string
}
type MetricParser interface {
ParseMetrics([]string) ([]Metric, error)
}
func NewMetricsParser(locodeDBPath string, nodeLocode string) (MetricParser, error) {
return &metricsParser{
locodeDBPath: locodeDBPath,
nodeLocode: nodeLocode,
}, nil
}
func (p *metricsParser) initLocodes() error {
if len(p.locodes) != 0 {
return nil
}
if len(p.locodeDBPath) > 0 {
p.locodes = make(map[string]locodedb.Point)
locodeDB := locodebolt.New(locodebolt.Prm{
Path: p.locodeDBPath,
},
locodebolt.ReadOnly(),
)
err := locodeDB.Open()
if err != nil {
return err
}
defer locodeDB.Close()
err = locodeDB.IterateOverLocodes(func(k string, v locodedb.Point) {
p.locodes[k] = v
})
if err != nil {
return err
}
return nil
}
return errors.New("set path to locode database")
}
func (p *metricsParser) ParseMetrics(priority []string) ([]Metric, error) {
var metrics []Metric
for _, raw := range priority {
if attr, found := strings.CutPrefix(raw, attrPrefix); found {
metrics = append(metrics, NewAttributeMetric(attr))
} else if raw == geoDistance {
err := p.initLocodes()
if err != nil {
return nil, err
}
if len(p.locodes) == 0 {
return nil, fmt.Errorf("provide locodes database for metric %s", raw)
}
if len(p.nodeLocode) == 0 {
return nil, errors.New("set locode for node")
}
point, ok := p.locodes[p.nodeLocode]
if !ok {
return nil, fmt.Errorf("not found geo coordinates for locode %s", raw)
}
m := NewGeoDistanceMetric(p.locodes, point.Latitude(), point.Longitude())
metrics = append(metrics, m)
} else {
return nil, fmt.Errorf("unsupported priority metric %s", raw)
}
}
return metrics, 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) int {
fromAttr := from.Attribute(am.attribute)
toAttr := to.Attribute(am.attribute)
if len(fromAttr) > 0 && len(toAttr) > 0 && fromAttr == toAttr {
return 0
}
return 1
}
func NewAttributeMetric(attr string) Metric {
return &attributeMetric{attribute: attr}
}
// geoDistanceMetric describes priority metric based on attribute.
type geoDistanceMetric struct {
locodes map[string]locodedb.Point
distance *atomic.Pointer[map[string]int]
mtx sync.Mutex
lat float64
long float64
}
func NewGeoDistanceMetric(locodes map[string]locodedb.Point, lat float64, long float64) Metric {
d := atomic.Pointer[map[string]int]{}
m := make(map[string]int)
d.Store(&m)
gm := &geoDistanceMetric{
locodes: locodes,
lat: lat,
long: long,
distance: &d,
}
return gm
}
// CalculateValue return distance in kilometers between current node and provided,
// if coordinates for provided node found. In other case return math.MaxInt.
func (gm *geoDistanceMetric) CalculateValue(_ *netmap.NodeInfo, to *netmap.NodeInfo) int {
tl := to.LOCODE()
m := gm.distance.Load()
if v, ok := (*m)[tl]; ok {
return v
}
return gm.calculateDistance(tl)
}
func (gm *geoDistanceMetric) calculateDistance(to string) int {
gm.mtx.Lock()
defer gm.mtx.Unlock()
od := gm.distance.Load()
if v, ok := (*od)[to]; ok {
return v
}
nd := maps.Clone(*od)
pointTo, ok := gm.locodes[to]
if ok {
nd[to] = int(distance(gm.lat, gm.long, pointTo.Latitude(), pointTo.Longitude()))
} else {
nd[to] = math.MaxInt
}
gm.distance.Store(&nd)
return nd[to]
}
// distance return amount of KM between two points.
// Parameters are latitude and longitude of point 1 and 2 in decimal degrees.
func distance(lt1 float64, ln1 float64, lt2 float64, ln2 float64) float64 {
radLat1 := math.Pi * lt1 / 180
radLat2 := math.Pi * lt2 / 180
radTheta := math.Pi * (ln1 - ln2) / 180
dist := math.Sin(radLat1)*math.Sin(radLat2) + math.Cos(radLat1)*math.Cos(radLat2)*math.Cos(radTheta)
if dist > 1 {
dist = 1
}
dist = math.Acos(dist)
dist = dist * 180 / math.Pi
dist = dist * 60 * 1.1515 * 1.609344
return dist
}