2021-11-18 16:33:32 +00:00
|
|
|
package netmap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2022-12-09 11:16:24 +00:00
|
|
|
"github.com/TrueCloudLab/frostfs-api-go/v2/refs"
|
2021-11-18 16:33:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// prefix of keys to subnet attributes.
|
|
|
|
const attrSubnetPrefix = "__NEOFS__SUBNET_"
|
|
|
|
|
|
|
|
const (
|
|
|
|
// subnet attribute's value denoting subnet entry
|
|
|
|
attrSubnetValEntry = "True"
|
|
|
|
|
|
|
|
// subnet attribute's value denoting subnet exit
|
|
|
|
attrSubnetValExit = "False"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NodeSubnetInfo groups information about subnet which can be written to NodeInfo.
|
|
|
|
//
|
|
|
|
// Zero value represents entry to zero subnet.
|
|
|
|
type NodeSubnetInfo struct {
|
|
|
|
exit bool
|
|
|
|
|
|
|
|
id *refs.SubnetID
|
|
|
|
}
|
|
|
|
|
2021-11-23 10:48:37 +00:00
|
|
|
// Enabled returns true iff subnet membership is enabled for the node.
|
|
|
|
func (x NodeSubnetInfo) Enabled() bool {
|
2021-11-18 16:33:32 +00:00
|
|
|
return !x.exit
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetEntryFlag sets the subnet entry flag.
|
|
|
|
func (x *NodeSubnetInfo) SetEntryFlag(enters bool) {
|
|
|
|
x.exit = !enters
|
|
|
|
}
|
|
|
|
|
|
|
|
// ID returns identifier of the subnet.
|
|
|
|
func (x NodeSubnetInfo) ID() *refs.SubnetID {
|
|
|
|
return x.id
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetID sets identifier of the subnet.
|
|
|
|
func (x *NodeSubnetInfo) SetID(id *refs.SubnetID) {
|
|
|
|
x.id = id
|
|
|
|
}
|
|
|
|
|
|
|
|
func subnetAttributeKey(id *refs.SubnetID) string {
|
|
|
|
txt, _ := id.MarshalText() // never returns an error
|
|
|
|
|
|
|
|
return attrSubnetPrefix + string(txt)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteSubnetInfo writes NodeSubnetInfo to NodeInfo via attributes. NodeInfo must not be nil.
|
|
|
|
//
|
2021-11-23 10:48:37 +00:00
|
|
|
// Existing subnet attributes are expected to be key-unique, otherwise undefined behavior.
|
|
|
|
//
|
2021-11-18 16:33:32 +00:00
|
|
|
// Does not add (removes existing) attribute if node:
|
2022-09-15 05:20:13 +00:00
|
|
|
// - disables non-zero subnet;
|
|
|
|
// - enables zero subnet.
|
2021-11-18 16:33:32 +00:00
|
|
|
//
|
|
|
|
// Attribute key is calculated from ID using format `__NEOFS__SUBNET_%s`.
|
|
|
|
// Attribute Value is:
|
2022-09-15 05:20:13 +00:00
|
|
|
// - `True` if node enters the subnet;
|
|
|
|
// - `False`, otherwise.
|
2021-11-18 16:33:32 +00:00
|
|
|
func WriteSubnetInfo(node *NodeInfo, info NodeSubnetInfo) {
|
|
|
|
attrs := node.GetAttributes()
|
|
|
|
|
|
|
|
id := info.ID()
|
2021-11-23 10:48:37 +00:00
|
|
|
enters := info.Enabled()
|
2021-11-18 16:33:32 +00:00
|
|
|
|
|
|
|
// calculate attribute key
|
|
|
|
key := subnetAttributeKey(id)
|
|
|
|
|
|
|
|
if refs.IsZeroSubnet(id) == enters {
|
|
|
|
for i := range attrs {
|
|
|
|
if attrs[i].GetKey() == key {
|
|
|
|
attrs = append(attrs[:i], attrs[i+1:]...)
|
2021-11-23 10:48:37 +00:00
|
|
|
break // attributes are expected to be key-unique
|
2021-11-18 16:33:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
var val string
|
|
|
|
|
|
|
|
if enters {
|
|
|
|
val = attrSubnetValEntry
|
|
|
|
} else {
|
|
|
|
val = attrSubnetValExit
|
|
|
|
}
|
|
|
|
|
|
|
|
presented := false
|
|
|
|
|
|
|
|
for i := range attrs {
|
|
|
|
if attrs[i].GetKey() == key {
|
|
|
|
attrs[i].SetValue(val)
|
|
|
|
presented = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !presented {
|
2022-03-02 07:40:22 +00:00
|
|
|
index := len(attrs)
|
|
|
|
attrs = append(attrs, Attribute{})
|
|
|
|
attrs[index].SetKey(key)
|
|
|
|
attrs[index].SetValue(val)
|
2021-11-18 16:33:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
node.SetAttributes(attrs)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ErrRemoveSubnet is returned when a node needs to leave the subnet.
|
|
|
|
var ErrRemoveSubnet = errors.New("remove subnet")
|
|
|
|
|
2021-11-18 18:26:29 +00:00
|
|
|
var errNoSubnets = errors.New("no subnets")
|
|
|
|
|
2021-11-18 16:33:32 +00:00
|
|
|
// IterateSubnets iterates over all subnets the node belongs to and passes the IDs to f.
|
|
|
|
// Handler must not be nil.
|
|
|
|
//
|
2021-11-22 10:32:36 +00:00
|
|
|
// Subnet attributes are expected to be key-unique, otherwise undefined behavior.
|
|
|
|
//
|
2021-11-18 18:26:29 +00:00
|
|
|
// If f returns ErrRemoveSubnet, then removes subnet entry. Note that this leads to an instant mutation of NodeInfo.
|
|
|
|
// Breaks on any other non-nil error and returns it.
|
2021-11-18 16:33:32 +00:00
|
|
|
//
|
|
|
|
// Returns an error if any subnet attribute has wrong format.
|
2021-11-18 18:26:29 +00:00
|
|
|
// Returns an error if the node is not included in any subnet by the end of the loop.
|
2021-11-18 16:33:32 +00:00
|
|
|
func IterateSubnets(node *NodeInfo, f func(refs.SubnetID) error) error {
|
|
|
|
attrs := node.GetAttributes()
|
|
|
|
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
id refs.SubnetID
|
2021-11-18 18:26:29 +00:00
|
|
|
entries uint
|
2021-11-19 16:50:29 +00:00
|
|
|
|
2021-11-22 12:05:21 +00:00
|
|
|
zeroEntry = true
|
2021-11-18 16:33:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
for i := 0; i < len(attrs); i++ { // range must not be used because of attrs mutation in body
|
|
|
|
key := attrs[i].GetKey()
|
|
|
|
|
|
|
|
// cut subnet ID string
|
|
|
|
idTxt := strings.TrimPrefix(key, attrSubnetPrefix)
|
2021-11-23 10:48:37 +00:00
|
|
|
if len(idTxt) == len(key) {
|
2021-11-18 16:33:32 +00:00
|
|
|
// not a subnet attribute
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// check value
|
2021-11-19 16:50:29 +00:00
|
|
|
val := attrs[i].GetValue()
|
|
|
|
if val != attrSubnetValExit && val != attrSubnetValEntry {
|
2021-11-18 16:33:32 +00:00
|
|
|
return fmt.Errorf("invalid attribute value: %s", val)
|
|
|
|
}
|
|
|
|
|
|
|
|
// decode subnet ID
|
|
|
|
if err = id.UnmarshalText([]byte(idTxt)); err != nil {
|
|
|
|
return fmt.Errorf("invalid ID text: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-11-19 16:50:29 +00:00
|
|
|
// update status of zero subnet
|
|
|
|
isZero := refs.IsZeroSubnet(&id)
|
|
|
|
|
2021-11-22 12:05:21 +00:00
|
|
|
if isZero {
|
|
|
|
zeroEntry = val == attrSubnetValEntry
|
2021-11-19 16:50:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// continue to process only the subnets to which the node belongs
|
|
|
|
if val == attrSubnetValExit {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-11-18 16:33:32 +00:00
|
|
|
// pass ID to the handler
|
|
|
|
err = f(id)
|
|
|
|
|
|
|
|
isRemoveErr := errors.Is(err, ErrRemoveSubnet)
|
|
|
|
|
|
|
|
if err != nil && !isRemoveErr {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-19 16:50:29 +00:00
|
|
|
if isRemoveErr {
|
|
|
|
if isZero {
|
|
|
|
// we can't remove attribute of zero subnet because it means entry
|
2021-11-18 16:33:32 +00:00
|
|
|
attrs[i].SetValue(attrSubnetValExit)
|
2021-11-19 16:50:29 +00:00
|
|
|
} else {
|
|
|
|
// we can set False or remove attribute, latter is more memory/network efficient.
|
|
|
|
attrs = append(attrs[:i], attrs[i+1:]...)
|
|
|
|
i--
|
2021-11-18 16:33:32 +00:00
|
|
|
}
|
|
|
|
|
2021-11-18 18:26:29 +00:00
|
|
|
continue
|
2021-11-18 16:33:32 +00:00
|
|
|
}
|
2021-11-18 18:26:29 +00:00
|
|
|
|
|
|
|
entries++
|
2021-11-18 16:33:32 +00:00
|
|
|
}
|
|
|
|
|
2021-11-22 12:05:21 +00:00
|
|
|
if zeroEntry {
|
2021-11-18 16:33:32 +00:00
|
|
|
// missing attribute of zero subnet equivalent to entry
|
|
|
|
refs.MakeZeroSubnet(&id)
|
|
|
|
|
|
|
|
err = f(id)
|
2021-11-23 14:11:15 +00:00
|
|
|
if err != nil {
|
|
|
|
if !errors.Is(err, ErrRemoveSubnet) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-18 16:33:32 +00:00
|
|
|
// zero subnet should be clearly removed with False value
|
2022-03-02 07:40:22 +00:00
|
|
|
index := len(attrs)
|
|
|
|
attrs = append(attrs, Attribute{})
|
|
|
|
attrs[index].SetKey(subnetAttributeKey(&id))
|
|
|
|
attrs[index].SetValue(attrSubnetValExit)
|
2021-11-18 18:26:29 +00:00
|
|
|
} else {
|
|
|
|
entries++
|
2021-11-18 16:33:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-18 18:26:29 +00:00
|
|
|
if entries <= 0 {
|
|
|
|
return errNoSubnets
|
|
|
|
}
|
|
|
|
|
2021-11-18 16:33:32 +00:00
|
|
|
node.SetAttributes(attrs)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|