forked from TrueCloudLab/frostfs-sdk-go
[#210] subnet: Refactor and document package functionality
Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
parent
6709b00c89
commit
eb3b990812
13 changed files with 276 additions and 216 deletions
|
@ -36,13 +36,31 @@ func (p *PlacementPolicy) ToV2() *netmap.PlacementPolicy {
|
|||
|
||||
// SubnetID returns subnet to select nodes from.
|
||||
func (p *PlacementPolicy) SubnetID() *subnetid.ID {
|
||||
return (*subnetid.ID)(
|
||||
(*netmap.PlacementPolicy)(p).GetSubnetID())
|
||||
idv2 := (*netmap.PlacementPolicy)(p).GetSubnetID()
|
||||
if idv2 == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
var id subnetid.ID
|
||||
|
||||
err := id.ReadFromV2(*idv2)
|
||||
if err != nil {
|
||||
panic(err) // will disappear after netmap package refactor
|
||||
}
|
||||
|
||||
return &id
|
||||
}
|
||||
|
||||
// SetSubnetID sets subnet to select nodes from.
|
||||
func (p *PlacementPolicy) SetSubnetID(subnet *subnetid.ID) {
|
||||
(*netmap.PlacementPolicy)(p).SetSubnetID((*refs.SubnetID)(subnet))
|
||||
var idv2 *refs.SubnetID
|
||||
|
||||
if subnet != nil {
|
||||
idv2 = new(refs.SubnetID)
|
||||
subnet.WriteToV2(idv2)
|
||||
}
|
||||
|
||||
(*netmap.PlacementPolicy)(p).SetSubnetID(idv2)
|
||||
}
|
||||
|
||||
// Replicas returns list of object replica descriptors.
|
||||
|
|
|
@ -2,6 +2,7 @@ package netmap
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/nspcc-dev/neofs-api-go/v2/netmap"
|
||||
"github.com/nspcc-dev/neofs-api-go/v2/refs"
|
||||
|
@ -48,9 +49,12 @@ func (i *NodeInfo) IterateSubnets(f func(subnetid.ID) error) error {
|
|||
var id subnetid.ID
|
||||
|
||||
return netmap.IterateSubnets((*netmap.NodeInfo)(i), func(idv2 refs.SubnetID) error {
|
||||
id.FromV2(idv2)
|
||||
err := id.ReadFromV2(idv2)
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid subnet: %w", err)
|
||||
}
|
||||
|
||||
err := f(id)
|
||||
err = f(id)
|
||||
if errors.Is(err, ErrRemoveSubnet) {
|
||||
return netmap.ErrRemoveSubnet
|
||||
}
|
||||
|
@ -66,7 +70,7 @@ var errAbortSubnetIter = errors.New("abort subnet iterator")
|
|||
// Function is NPE-safe: nil NodeInfo always belongs to zero subnet only.
|
||||
func BelongsToSubnet(node *NodeInfo, id subnetid.ID) bool {
|
||||
err := node.IterateSubnets(func(id_ subnetid.ID) error {
|
||||
if id.Equals(&id_) {
|
||||
if id.Equals(id_) {
|
||||
return errAbortSubnetIter
|
||||
}
|
||||
|
||||
|
|
|
@ -12,7 +12,7 @@ func TestNodeInfoSubnets(t *testing.T) {
|
|||
t.Run("enter subnet", func(t *testing.T) {
|
||||
var id subnetid.ID
|
||||
|
||||
id.SetNumber(13)
|
||||
id.SetNumeric(13)
|
||||
|
||||
var node netmap.NodeInfo
|
||||
|
||||
|
@ -35,8 +35,8 @@ func TestNodeInfoSubnets(t *testing.T) {
|
|||
t.Run("not last", func(t *testing.T) {
|
||||
var id, idrm subnetid.ID
|
||||
|
||||
id.SetNumber(13)
|
||||
idrm.SetNumber(23)
|
||||
id.SetNumeric(13)
|
||||
idrm.SetNumeric(23)
|
||||
|
||||
var node netmap.NodeInfo
|
||||
|
||||
|
@ -44,7 +44,7 @@ func TestNodeInfoSubnets(t *testing.T) {
|
|||
node.EnterSubnet(idrm)
|
||||
|
||||
err := node.IterateSubnets(func(id subnetid.ID) error {
|
||||
if subnetid.IsZero(id) || id.Equals(&idrm) {
|
||||
if subnetid.IsZero(id) || id.Equals(idrm) {
|
||||
return netmap.ErrRemoveSubnet
|
||||
}
|
||||
|
||||
|
@ -100,7 +100,7 @@ func TestEnterSubnet(t *testing.T) {
|
|||
node.ExitSubnet(id)
|
||||
require.False(t, netmap.BelongsToSubnet(&node, id))
|
||||
|
||||
id.SetNumber(10)
|
||||
id.SetNumeric(10)
|
||||
node.EnterSubnet(id)
|
||||
require.True(t, netmap.BelongsToSubnet(&node, id))
|
||||
require.False(t, netmap.BelongsToSubnet(&node, subnetid.ID{}))
|
||||
|
@ -113,8 +113,8 @@ func TestEnterSubnet(t *testing.T) {
|
|||
func TestBelongsToSubnet(t *testing.T) {
|
||||
var id, idMiss, idZero subnetid.ID
|
||||
|
||||
id.SetNumber(13)
|
||||
idMiss.SetNumber(23)
|
||||
id.SetNumeric(13)
|
||||
idMiss.SetNumeric(23)
|
||||
|
||||
var node netmap.NodeInfo
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue