[#217] netmap: Return node netmap state directly
All checks were successful
DCO / DCO (pull_request) Successful in 52s
Tests and linters / Tests (1.22) (pull_request) Successful in 1m7s
Tests and linters / Tests (1.23) (pull_request) Successful in 1m6s
Tests and linters / Lint (pull_request) Successful in 1m47s

Signed-off-by: Aleksey Savchuk <a.savchuk@yadro.com>
This commit is contained in:
Aleksey Savchuk 2024-09-03 17:17:25 +03:00
parent 8f751d9dd0
commit 44ea6310dc
No known key found for this signature in database
2 changed files with 75 additions and 45 deletions

View file

@ -457,6 +457,45 @@ func (x *NodeInfo) SortAttributes() {
x.m.SetAttributes(as) x.m.SetAttributes(as)
} }
type NodeState netmap.NodeState
const (
UnspecifiedState = NodeState(netmap.UnspecifiedState)
Online = NodeState(netmap.Online)
Offline = NodeState(netmap.Offline)
Maintenance = NodeState(netmap.Maintenance)
)
// ToV2 converts NodeState to v2.
func (ns NodeState) ToV2() netmap.NodeState {
return netmap.NodeState(ns)
}
// FromV2 reads NodeState to v2.
func (ns *NodeState) FromV2(state netmap.NodeState) {
*ns = NodeState(state)
}
// Status returns the current state of the node in the network map.
//
// Zero NodeInfo has an undefined state, neither online nor offline.
func (x NodeInfo) Status() NodeState {
return NodeState(x.m.GetState())
}
// IsOnline checks if the current state is "online".
func (ns NodeState) IsOnline() bool { return ns == Online }
// IsOffline checks if the current state is "offline".
func (ns NodeState) IsOffline() bool { return ns == Offline }
// IsMaintenance checks if the current state is "maintenance".
func (ns NodeState) IsMaintenance() bool { return ns == Maintenance }
func (ns NodeState) String() string {
return netmap.NodeState(ns).String()
}
// SetOffline sets the state of the node to "offline". When a node updates // 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 // information about itself in the network map, this action is interpreted as
// an intention to leave the network. // an intention to leave the network.
@ -464,49 +503,16 @@ func (x *NodeInfo) SetOffline() {
x.m.SetState(netmap.Offline) x.m.SetState(netmap.Offline)
} }
// 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
}
// SetOnline sets the state of the node to "online". When a node updates // SetOnline sets the state of the node to "online". When a node updates
// information about itself in the network map, this // information about itself in the network map, this
// action is interpreted as an intention to enter the network. // action is interpreted as an intention to enter the network.
//
// See also IsOnline.
func (x *NodeInfo) SetOnline() { func (x *NodeInfo) SetOnline() {
x.m.SetState(netmap.Online) x.m.SetState(netmap.Online)
} }
// 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
}
// SetMaintenance sets the state of the node to "maintenance". When a node updates // SetMaintenance sets the state of the node to "maintenance". When a node updates
// information about itself in the network map, this // information about itself in the network map, this
// state declares temporal unavailability for a node. // state declares temporal unavailability for a node.
//
// See also IsMaintenance.
func (x *NodeInfo) SetMaintenance() { func (x *NodeInfo) SetMaintenance() {
x.m.SetState(netmap.Maintenance) 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
}

View file

@ -3,6 +3,7 @@ package netmap
import ( import (
"testing" "testing"
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/netmap"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -23,27 +24,50 @@ func TestNodeInfo_SetAttribute(t *testing.T) {
require.Equal(t, val, n.Attribute(key)) require.Equal(t, val, n.Attribute(key))
} }
func TestNodeState(t *testing.T) {
m := map[NodeState]netmap.NodeState{
UnspecifiedState: netmap.UnspecifiedState,
Online: netmap.Online,
Offline: netmap.Offline,
Maintenance: netmap.Maintenance,
}
t.Run("from sdk to v2", func(t *testing.T) {
for stateSDK, stateV2 := range m {
require.Equal(t, stateV2, stateSDK.ToV2())
}
})
t.Run("from v2 to sdk", func(t *testing.T) {
for stateSDK, stateV2 := range m {
var state NodeState
state.FromV2(stateV2)
require.Equal(t, stateSDK, state)
}
})
}
func TestNodeInfo_Status(t *testing.T) { func TestNodeInfo_Status(t *testing.T) {
var n NodeInfo var n NodeInfo
require.False(t, n.IsOnline()) require.False(t, n.Status().IsOnline())
require.False(t, n.IsOffline()) require.False(t, n.Status().IsOffline())
require.False(t, n.IsMaintenance()) require.False(t, n.Status().IsMaintenance())
n.SetOnline() n.SetOnline()
require.True(t, n.IsOnline()) require.True(t, n.Status().IsOnline())
require.False(t, n.IsOffline()) require.False(t, n.Status().IsOffline())
require.False(t, n.IsMaintenance()) require.False(t, n.Status().IsMaintenance())
n.SetOffline() n.SetOffline()
require.True(t, n.IsOffline()) require.False(t, n.Status().IsOnline())
require.False(t, n.IsOnline()) require.True(t, n.Status().IsOffline())
require.False(t, n.IsMaintenance()) require.False(t, n.Status().IsMaintenance())
n.SetMaintenance() n.SetMaintenance()
require.True(t, n.IsMaintenance()) require.False(t, n.Status().IsOnline())
require.False(t, n.IsOnline()) require.False(t, n.Status().IsOffline())
require.False(t, n.IsOffline()) require.True(t, n.Status().IsMaintenance())
} }
func TestNodeInfo_ExternalAddr(t *testing.T) { func TestNodeInfo_ExternalAddr(t *testing.T) {