2021-10-27 10:00:35 +00:00
|
|
|
package netmap
|
|
|
|
|
|
|
|
import (
|
2022-06-07 08:25:34 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2023-03-07 11:20:03 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/netmap"
|
|
|
|
frostfscrypto "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/crypto"
|
|
|
|
"git.frostfs.info/TrueCloudLab/hrw"
|
2021-10-27 10:00:35 +00:00
|
|
|
)
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// NodeInfo groups information about FrostFS storage node which is reflected
|
|
|
|
// in the FrostFS network map. Storage nodes advertise this information when
|
|
|
|
// registering with the FrostFS network. After successful registration, information
|
2022-06-07 08:25:34 +00:00
|
|
|
// about the nodes is available to all network participants to work with the network
|
|
|
|
// map (mainly to comply with container storage policies).
|
|
|
|
//
|
2023-03-07 11:20:03 +00:00
|
|
|
// NodeInfo is mutually compatible with git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/netmap.NodeInfo
|
2022-06-07 08:25:34 +00:00
|
|
|
// message. See ReadFromV2 / WriteToV2 methods.
|
|
|
|
//
|
|
|
|
// Instances can be created using built-in var declaration.
|
2022-06-07 02:12:39 +00:00
|
|
|
type NodeInfo struct {
|
2023-02-28 11:04:19 +00:00
|
|
|
m netmap.NodeInfo
|
|
|
|
hash uint64
|
2022-06-07 02:12:39 +00:00
|
|
|
}
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// reads NodeInfo from netmap.NodeInfo message. If checkFieldPresence is set,
|
|
|
|
// returns an error on absence of any protocol-required field. Verifies format of any
|
2022-12-29 10:46:18 +00:00
|
|
|
// presented field according to FrostFS API V2 protocol.
|
2022-06-07 08:25:34 +00:00
|
|
|
func (x *NodeInfo) readFromV2(m netmap.NodeInfo, checkFieldPresence bool) error {
|
|
|
|
var err error
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
binPublicKey := m.GetPublicKey()
|
|
|
|
if checkFieldPresence && len(binPublicKey) == 0 {
|
|
|
|
return errors.New("missing public key")
|
|
|
|
}
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
if checkFieldPresence && m.NumberOfAddresses() <= 0 {
|
|
|
|
return errors.New("missing network endpoints")
|
|
|
|
}
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
attributes := m.GetAttributes()
|
|
|
|
mAttr := make(map[string]struct{}, len(attributes))
|
|
|
|
for i := range attributes {
|
|
|
|
key := attributes[i].GetKey()
|
|
|
|
if key == "" {
|
|
|
|
return fmt.Errorf("empty key of the attribute #%d", i)
|
|
|
|
} else if _, ok := mAttr[key]; ok {
|
|
|
|
return fmt.Errorf("duplicated attbiuted %s", key)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case key == attrCapacity:
|
|
|
|
_, err = strconv.ParseUint(attributes[i].GetValue(), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid %s attribute: %w", attrCapacity, err)
|
|
|
|
}
|
|
|
|
case key == attrPrice:
|
|
|
|
var err error
|
|
|
|
_, err = strconv.ParseUint(attributes[i].GetValue(), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid %s attribute: %w", attrPrice, err)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
if attributes[i].GetValue() == "" {
|
2022-06-14 14:27:51 +00:00
|
|
|
return fmt.Errorf("empty value of the attribute %s", key)
|
2022-06-07 08:25:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
x.m = m
|
2023-02-28 11:04:19 +00:00
|
|
|
x.hash = hrw.Hash(binPublicKey)
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// ReadFromV2 reads NodeInfo from the netmap.NodeInfo message. Checks if the
|
2022-12-29 10:46:18 +00:00
|
|
|
// message conforms to FrostFS API V2 protocol.
|
2022-06-07 08:25:34 +00:00
|
|
|
//
|
|
|
|
// See also WriteToV2.
|
|
|
|
func (x *NodeInfo) ReadFromV2(m netmap.NodeInfo) error {
|
|
|
|
return x.readFromV2(m, true)
|
|
|
|
}
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// WriteToV2 writes NodeInfo to the netmap.NodeInfo message. The message MUST NOT
|
|
|
|
// be nil.
|
|
|
|
//
|
|
|
|
// See also ReadFromV2.
|
|
|
|
func (x NodeInfo) WriteToV2(m *netmap.NodeInfo) {
|
|
|
|
*m = x.m
|
|
|
|
}
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// Marshal encodes NodeInfo into a binary format of the FrostFS API protocol
|
2022-06-07 08:25:34 +00:00
|
|
|
// (Protocol Buffers with direct field order).
|
|
|
|
//
|
|
|
|
// See also Unmarshal.
|
|
|
|
func (x NodeInfo) Marshal() []byte {
|
|
|
|
var m netmap.NodeInfo
|
|
|
|
x.WriteToV2(&m)
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
return m.StableMarshal(nil)
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// Unmarshal decodes FrostFS API protocol binary format into the NodeInfo
|
2022-06-07 08:25:34 +00:00
|
|
|
// (Protocol Buffers with direct field order). Returns an error describing
|
|
|
|
// a format violation.
|
|
|
|
//
|
|
|
|
// See also Marshal.
|
|
|
|
func (x *NodeInfo) Unmarshal(data []byte) error {
|
|
|
|
var m netmap.NodeInfo
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
err := m.Unmarshal(data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
2022-06-07 08:25:34 +00:00
|
|
|
|
|
|
|
return x.readFromV2(m, false)
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// MarshalJSON encodes NodeInfo into a JSON format of the FrostFS API protocol
|
2022-06-07 08:25:34 +00:00
|
|
|
// (Protocol Buffers JSON).
|
2021-10-27 10:00:35 +00:00
|
|
|
//
|
2022-06-07 08:25:34 +00:00
|
|
|
// See also UnmarshalJSON.
|
|
|
|
func (x NodeInfo) MarshalJSON() ([]byte, error) {
|
|
|
|
var m netmap.NodeInfo
|
|
|
|
x.WriteToV2(&m)
|
|
|
|
|
|
|
|
return m.MarshalJSON()
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// UnmarshalJSON decodes FrostFS API protocol JSON format into the NodeInfo
|
2022-06-07 08:25:34 +00:00
|
|
|
// (Protocol Buffers JSON). Returns an error describing a format violation.
|
2021-10-27 10:00:35 +00:00
|
|
|
//
|
2022-06-07 08:25:34 +00:00
|
|
|
// See also MarshalJSON.
|
|
|
|
func (x *NodeInfo) UnmarshalJSON(data []byte) error {
|
|
|
|
var m netmap.NodeInfo
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
err := m.UnmarshalJSON(data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
return x.readFromV2(m, false)
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// SetPublicKey sets binary-encoded public key bound to the node. The key
|
|
|
|
// authenticates the storage node, so it MUST be unique within the network.
|
|
|
|
//
|
|
|
|
// Argument MUST NOT be mutated, make a copy first.
|
2021-10-27 10:00:35 +00:00
|
|
|
//
|
2022-06-07 08:25:34 +00:00
|
|
|
// See also PublicKey.
|
|
|
|
func (x *NodeInfo) SetPublicKey(key []byte) {
|
|
|
|
x.m.SetPublicKey(key)
|
2023-02-28 11:04:19 +00:00
|
|
|
x.hash = hrw.Hash(x.m.GetPublicKey())
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// PublicKey returns value set using SetPublicKey.
|
2021-10-27 10:00:35 +00:00
|
|
|
//
|
2022-06-07 08:25:34 +00:00
|
|
|
// Zero NodeInfo has no public key, which is incorrect according to
|
2022-12-29 10:46:18 +00:00
|
|
|
// FrostFS system requirements.
|
2022-06-07 08:25:34 +00:00
|
|
|
//
|
|
|
|
// Return value MUST not be mutated, make a copy first.
|
|
|
|
func (x NodeInfo) PublicKey() []byte {
|
|
|
|
return x.m.GetPublicKey()
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-10-12 11:46:48 +00:00
|
|
|
// StringifyPublicKey returns HEX representation of PublicKey.
|
|
|
|
func StringifyPublicKey(node NodeInfo) string {
|
2022-12-13 14:36:35 +00:00
|
|
|
return frostfscrypto.StringifyKeyBinary(node.PublicKey())
|
2022-10-12 11:46:48 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// SetNetworkEndpoints sets list to the announced node's network endpoints.
|
|
|
|
// Node MUSt have at least one announced endpoint. List MUST be unique.
|
2022-12-29 10:46:18 +00:00
|
|
|
// Endpoints are used for communication with the storage node within FrostFS
|
2022-06-07 08:25:34 +00:00
|
|
|
// network. It is expected that node serves storage node services on these
|
|
|
|
// endpoints (it also adds a wait on their network availability).
|
|
|
|
//
|
|
|
|
// Argument MUST NOT be mutated, make a copy first.
|
2021-10-27 10:00:35 +00:00
|
|
|
//
|
2022-06-07 08:25:34 +00:00
|
|
|
// See also IterateNetworkEndpoints.
|
|
|
|
func (x *NodeInfo) SetNetworkEndpoints(v ...string) {
|
|
|
|
x.m.SetAddresses(v...)
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// NumberOfNetworkEndpoints returns number of network endpoints announced by the node.
|
|
|
|
//
|
|
|
|
// See also SetNetworkEndpoints.
|
|
|
|
func (x NodeInfo) NumberOfNetworkEndpoints() int {
|
|
|
|
return x.m.NumberOfAddresses()
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// IterateNetworkEndpoints iterates over network endpoints announced by the
|
|
|
|
// node and pass them into f. Breaks iteration on f's true return. Handler
|
|
|
|
// MUST NOT be nil.
|
|
|
|
//
|
|
|
|
// Zero NodeInfo contains no endpoints which is incorrect according to
|
2022-12-29 10:46:18 +00:00
|
|
|
// FrostFS system requirements.
|
2022-06-07 08:25:34 +00:00
|
|
|
//
|
|
|
|
// See also SetNetworkEndpoints.
|
|
|
|
func (x NodeInfo) IterateNetworkEndpoints(f func(string) bool) {
|
|
|
|
x.m.IterateAddresses(f)
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// IterateNetworkEndpoints is an extra-sugared function over IterateNetworkEndpoints
|
|
|
|
// method which allows to unconditionally iterate over all node's network endpoints.
|
|
|
|
func IterateNetworkEndpoints(node NodeInfo, f func(string)) {
|
|
|
|
node.IterateNetworkEndpoints(func(addr string) bool {
|
|
|
|
f(addr)
|
|
|
|
return false
|
|
|
|
})
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// assert NodeInfo type provides hrw.Hasher required for HRW sorting.
|
|
|
|
var _ hrw.Hasher = NodeInfo{}
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// Hash implements hrw.Hasher interface.
|
|
|
|
//
|
|
|
|
// Hash is needed to support weighted HRW therefore sort function sorts nodes
|
|
|
|
// based on their public key. Hash isn't expected to be used directly.
|
|
|
|
func (x NodeInfo) Hash() uint64 {
|
2023-02-28 11:04:19 +00:00
|
|
|
if x.hash != 0 {
|
|
|
|
return x.hash
|
|
|
|
}
|
2022-06-07 08:25:34 +00:00
|
|
|
return hrw.Hash(x.m.GetPublicKey())
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// less declares "less than" comparison between two NodeInfo instances:
|
|
|
|
// x1 is less than x2 if it has less Hash().
|
|
|
|
//
|
|
|
|
// Method is needed for internal placement needs.
|
|
|
|
func less(x1, x2 NodeInfo) bool {
|
|
|
|
return x1.Hash() < x2.Hash()
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
func (x *NodeInfo) setNumericAttribute(key string, num uint64) {
|
|
|
|
x.SetAttribute(key, strconv.FormatUint(num, 10))
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// SetPrice sets the storage cost declared by the node. By default, zero
|
|
|
|
// price is announced.
|
|
|
|
func (x *NodeInfo) SetPrice(price uint64) {
|
|
|
|
x.setNumericAttribute(attrPrice, price)
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// Price returns price set using SetPrice.
|
|
|
|
//
|
|
|
|
// Zero NodeInfo has zero price.
|
|
|
|
func (x NodeInfo) Price() uint64 {
|
|
|
|
val := x.Attribute(attrPrice)
|
|
|
|
if val == "" {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
price, err := strconv.ParseUint(val, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("unexpected price parsing error %s: %v", val, err))
|
|
|
|
}
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
return price
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// SetCapacity sets the storage capacity declared by the node. By default, zero
|
|
|
|
// capacity is announced.
|
|
|
|
func (x *NodeInfo) SetCapacity(capacity uint64) {
|
|
|
|
x.setNumericAttribute(attrCapacity, capacity)
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// capacity returns capacity set using SetCapacity.
|
2021-10-27 10:00:35 +00:00
|
|
|
//
|
2022-06-07 08:25:34 +00:00
|
|
|
// Zero NodeInfo has zero capacity.
|
|
|
|
func (x NodeInfo) capacity() uint64 {
|
|
|
|
val := x.Attribute(attrCapacity)
|
|
|
|
if val == "" {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
capacity, err := strconv.ParseUint(val, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("unexpected capacity parsing error %s: %v", val, err))
|
|
|
|
}
|
2022-06-07 02:12:39 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
return capacity
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
const attrUNLOCODE = "UN-LOCODE"
|
|
|
|
|
|
|
|
// SetLOCODE specifies node's geographic location in UN/LOCODE format. Each
|
2022-12-29 10:46:18 +00:00
|
|
|
// storage node MUST declare it for entrance to the FrostFS network. Node MAY
|
2022-06-07 08:25:34 +00:00
|
|
|
// declare the code of the nearest location as needed, for example, when it is
|
|
|
|
// impossible to unambiguously attribute the node to any location from UN/LOCODE
|
|
|
|
// database.
|
2021-10-27 10:00:35 +00:00
|
|
|
//
|
2022-06-07 08:25:34 +00:00
|
|
|
// See also LOCODE.
|
|
|
|
func (x *NodeInfo) SetLOCODE(locode string) {
|
|
|
|
x.SetAttribute(attrUNLOCODE, locode)
|
|
|
|
}
|
2022-06-07 02:12:39 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// LOCODE returns node's location code set using SetLOCODE.
|
|
|
|
//
|
|
|
|
// Zero NodeInfo has empty location code which is invalid according to
|
2022-12-29 10:46:18 +00:00
|
|
|
// FrostFS API system requirement.
|
2022-06-07 08:25:34 +00:00
|
|
|
func (x NodeInfo) LOCODE() string {
|
|
|
|
return x.Attribute(attrUNLOCODE)
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// SetCountryCode sets code of the country in ISO 3166-1_alpha-2 to which
|
|
|
|
// storage node belongs (or the closest one).
|
|
|
|
//
|
|
|
|
// SetCountryCode is intended only for processing the network registration
|
|
|
|
// request by the Inner Ring. Other parties SHOULD NOT use it.
|
|
|
|
func (x *NodeInfo) SetCountryCode(countryCode string) {
|
|
|
|
x.SetAttribute("CountryCode", countryCode)
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// SetCountryName sets short name of the country in ISO-3166 format to which
|
|
|
|
// storage node belongs (or the closest one).
|
|
|
|
//
|
|
|
|
// SetCountryName is intended only for processing the network registration
|
|
|
|
// request by the Inner Ring. Other parties SHOULD NOT use it.
|
|
|
|
func (x *NodeInfo) SetCountryName(country string) {
|
|
|
|
x.SetAttribute("Country", country)
|
|
|
|
}
|
2022-06-07 02:12:39 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// SetLocationName sets storage node's location name from "NameWoDiacritics"
|
|
|
|
// column in the UN/LOCODE record corresponding to the specified LOCODE.
|
|
|
|
//
|
|
|
|
// SetLocationName is intended only for processing the network registration
|
|
|
|
// request by the Inner Ring. Other parties SHOULD NOT use it.
|
|
|
|
func (x *NodeInfo) SetLocationName(location string) {
|
|
|
|
x.SetAttribute("Location", location)
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// SetSubdivisionCode sets storage node's subdivision code from "SubDiv" column in
|
|
|
|
// the UN/LOCODE record corresponding to the specified LOCODE.
|
|
|
|
//
|
|
|
|
// SetSubdivisionCode is intended only for processing the network registration
|
|
|
|
// request by the Inner Ring. Other parties SHOULD NOT use it.
|
|
|
|
func (x *NodeInfo) SetSubdivisionCode(subDiv string) {
|
|
|
|
x.SetAttribute("SubDivCode", subDiv)
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// SetSubdivisionName sets storage node's subdivision name in ISO 3166-2 format.
|
2021-10-27 10:00:35 +00:00
|
|
|
//
|
2022-06-07 08:25:34 +00:00
|
|
|
// SetSubdivisionName is intended only for processing the network registration
|
|
|
|
// request by the Inner Ring. Other parties SHOULD NOT use it.
|
|
|
|
func (x *NodeInfo) SetSubdivisionName(subDiv string) {
|
|
|
|
x.SetAttribute("SubDiv", subDiv)
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// SetContinentName sets name of the storage node's continent from
|
|
|
|
// Seven-Continent model.
|
|
|
|
//
|
|
|
|
// SetContinentName is intended only for processing the network registration
|
|
|
|
// request by the Inner Ring. Other parties SHOULD NOT use it.
|
|
|
|
func (x *NodeInfo) SetContinentName(continent string) {
|
|
|
|
x.SetAttribute("Continent", continent)
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// Enumeration of well-known attributes.
|
|
|
|
const (
|
|
|
|
// attrPrice is a key to the node attribute that indicates the
|
|
|
|
// price in GAS tokens for storing one GB of data during one Epoch.
|
|
|
|
attrPrice = "Price"
|
|
|
|
|
|
|
|
// attrCapacity is a key to the node attribute that indicates the
|
|
|
|
// total available disk space in Gigabytes.
|
|
|
|
attrCapacity = "Capacity"
|
2022-09-24 10:37:08 +00:00
|
|
|
|
|
|
|
// attrExternalAddr is a key for the attribute storing node external addresses.
|
|
|
|
attrExternalAddr = "ExternalAddr"
|
|
|
|
// sepExternalAddr is a separator for multi-value ExternalAddr attribute.
|
|
|
|
sepExternalAddr = ","
|
2022-06-07 08:25:34 +00:00
|
|
|
)
|
2022-06-07 02:12:39 +00:00
|
|
|
|
2022-09-24 10:37:08 +00:00
|
|
|
// SetExternalAddresses sets multi-addresses to use
|
|
|
|
// to connect to this node from outside.
|
|
|
|
//
|
|
|
|
// Panics if addr is an empty list.
|
|
|
|
func (x *NodeInfo) SetExternalAddresses(addr ...string) {
|
|
|
|
x.SetAttribute(attrExternalAddr, strings.Join(addr, sepExternalAddr))
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExternalAddresses returns list of multi-addresses to use
|
|
|
|
// to connect to this node from outside.
|
|
|
|
func (x NodeInfo) ExternalAddresses() []string {
|
|
|
|
a := x.Attribute(attrExternalAddr)
|
|
|
|
if len(a) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Split(a, sepExternalAddr)
|
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// NumberOfAttributes returns number of attributes announced by the node.
|
|
|
|
//
|
|
|
|
// See also SetAttribute.
|
|
|
|
func (x NodeInfo) NumberOfAttributes() int {
|
|
|
|
return len(x.m.GetAttributes())
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// IterateAttributes iterates over all node attributes and passes the into f.
|
|
|
|
// Handler MUST NOT be nil.
|
|
|
|
func (x NodeInfo) IterateAttributes(f func(key, value string)) {
|
|
|
|
a := x.m.GetAttributes()
|
|
|
|
for i := range a {
|
|
|
|
f(a[i].GetKey(), a[i].GetValue())
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
2022-06-07 08:25:34 +00:00
|
|
|
}
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// SetAttribute sets value of the node attribute value by the given key.
|
|
|
|
// Both key and value MUST NOT be empty.
|
|
|
|
func (x *NodeInfo) SetAttribute(key, value string) {
|
|
|
|
if key == "" {
|
|
|
|
panic("empty key in SetAttribute")
|
|
|
|
} else if value == "" {
|
|
|
|
panic("empty value in SetAttribute")
|
|
|
|
}
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
a := x.m.GetAttributes()
|
|
|
|
for i := range a {
|
|
|
|
if a[i].GetKey() == key {
|
|
|
|
a[i].SetValue(value)
|
|
|
|
return
|
|
|
|
}
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
a = append(a, netmap.Attribute{})
|
|
|
|
a[len(a)-1].SetKey(key)
|
|
|
|
a[len(a)-1].SetValue(value)
|
|
|
|
|
|
|
|
x.m.SetAttributes(a)
|
|
|
|
}
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// Attribute returns value of the node attribute set using SetAttribute by the
|
|
|
|
// given key. Returns empty string if attribute is missing.
|
|
|
|
func (x NodeInfo) Attribute(key string) string {
|
|
|
|
a := x.m.GetAttributes()
|
|
|
|
for i := range a {
|
|
|
|
if a[i].GetKey() == key {
|
|
|
|
return a[i].GetValue()
|
|
|
|
}
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
return ""
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// SortAttributes sorts node attributes set using SetAttribute lexicographically.
|
|
|
|
// The method is only needed to make NodeInfo consistent, e.g. for signing.
|
|
|
|
func (x *NodeInfo) SortAttributes() {
|
|
|
|
as := x.m.GetAttributes()
|
|
|
|
if len(as) == 0 {
|
|
|
|
return
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
sort.Slice(as, func(i, j int) bool {
|
|
|
|
switch strings.Compare(as[i].GetKey(), as[j].GetKey()) {
|
|
|
|
case -1:
|
|
|
|
return true
|
|
|
|
case 1:
|
|
|
|
return false
|
|
|
|
default:
|
|
|
|
return as[i].GetValue() < as[j].GetValue()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
x.m.SetAttributes(as)
|
|
|
|
}
|
2022-06-07 02:12:39 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// SetOffline sets the state of the node to "offline". When a node updates
|
|
|
|
// information about itself in the network map, this action is interpreted as
|
|
|
|
// an intention to leave the network.
|
|
|
|
func (x *NodeInfo) SetOffline() {
|
|
|
|
x.m.SetState(netmap.Offline)
|
|
|
|
}
|
2022-06-07 02:12:39 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// IsOffline checks if the node is in the "offline" state.
|
|
|
|
//
|
|
|
|
// Zero NodeInfo has undefined state which is not offline (note that it does not
|
|
|
|
// mean online).
|
|
|
|
//
|
|
|
|
// See also SetOffline.
|
|
|
|
func (x NodeInfo) IsOffline() bool {
|
|
|
|
return x.m.GetState() == netmap.Offline
|
|
|
|
}
|
2022-06-07 02:12:39 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// SetOnline sets the state of the node to "online". When a node updates
|
|
|
|
// information about itself in the network map, this
|
|
|
|
// action is interpreted as an intention to enter the network.
|
|
|
|
//
|
|
|
|
// See also IsOnline.
|
|
|
|
func (x *NodeInfo) SetOnline() {
|
|
|
|
x.m.SetState(netmap.Online)
|
|
|
|
}
|
2022-06-07 02:12:39 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// IsOnline checks if the node is in the "online" state.
|
|
|
|
//
|
|
|
|
// Zero NodeInfo has undefined state which is not online (note that it does not
|
|
|
|
// mean offline).
|
|
|
|
//
|
|
|
|
// See also SetOnline.
|
|
|
|
func (x NodeInfo) IsOnline() bool {
|
|
|
|
return x.m.GetState() == netmap.Online
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
2022-09-15 05:51:24 +00:00
|
|
|
|
|
|
|
// SetMaintenance sets the state of the node to "maintenance". When a node updates
|
|
|
|
// information about itself in the network map, this
|
|
|
|
// state declares temporal unavailability for a node.
|
|
|
|
//
|
|
|
|
// See also IsMaintenance.
|
|
|
|
func (x *NodeInfo) SetMaintenance() {
|
|
|
|
x.m.SetState(netmap.Maintenance)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsMaintenance checks if the node is in the "maintenance" state.
|
|
|
|
//
|
|
|
|
// Zero NodeInfo has undefined state.
|
|
|
|
//
|
|
|
|
// See also SetMaintenance.
|
|
|
|
func (x NodeInfo) IsMaintenance() bool {
|
|
|
|
return x.m.GetState() == netmap.Maintenance
|
|
|
|
}
|