From 44ea6310dcc85bfa14b94dec38c9e6d07e72b0bb Mon Sep 17 00:00:00 2001 From: Aleksey Savchuk Date: Tue, 3 Sep 2024 17:17:25 +0300 Subject: [PATCH] [#217] netmap: Return node netmap state directly Signed-off-by: Aleksey Savchuk --- netmap/node_info.go | 72 ++++++++++++++++++++++------------------ netmap/node_info_test.go | 48 ++++++++++++++++++++------- 2 files changed, 75 insertions(+), 45 deletions(-) diff --git a/netmap/node_info.go b/netmap/node_info.go index bd37388..ac468a7 100644 --- a/netmap/node_info.go +++ b/netmap/node_info.go @@ -457,6 +457,45 @@ func (x *NodeInfo) SortAttributes() { 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 // information about itself in the network map, this action is interpreted as // an intention to leave the network. @@ -464,49 +503,16 @@ func (x *NodeInfo) SetOffline() { 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 // 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) } -// 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 // 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 -} diff --git a/netmap/node_info_test.go b/netmap/node_info_test.go index 1d1c018..468ca99 100644 --- a/netmap/node_info_test.go +++ b/netmap/node_info_test.go @@ -3,6 +3,7 @@ package netmap import ( "testing" + "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/netmap" "github.com/stretchr/testify/require" ) @@ -23,27 +24,50 @@ func TestNodeInfo_SetAttribute(t *testing.T) { 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) { var n NodeInfo - require.False(t, n.IsOnline()) - require.False(t, n.IsOffline()) - require.False(t, n.IsMaintenance()) + require.False(t, n.Status().IsOnline()) + require.False(t, n.Status().IsOffline()) + require.False(t, n.Status().IsMaintenance()) n.SetOnline() - require.True(t, n.IsOnline()) - require.False(t, n.IsOffline()) - require.False(t, n.IsMaintenance()) + require.True(t, n.Status().IsOnline()) + require.False(t, n.Status().IsOffline()) + require.False(t, n.Status().IsMaintenance()) n.SetOffline() - require.True(t, n.IsOffline()) - require.False(t, n.IsOnline()) - require.False(t, n.IsMaintenance()) + require.False(t, n.Status().IsOnline()) + require.True(t, n.Status().IsOffline()) + require.False(t, n.Status().IsMaintenance()) n.SetMaintenance() - require.True(t, n.IsMaintenance()) - require.False(t, n.IsOnline()) - require.False(t, n.IsOffline()) + require.False(t, n.Status().IsOnline()) + require.False(t, n.Status().IsOffline()) + require.True(t, n.Status().IsMaintenance()) } func TestNodeInfo_ExternalAddr(t *testing.T) {