[#36] Add default capacity and price attributes

Default values for capacity and price will be 0, so storage node
won't be present in placement. Add these attributes if they are
not explicitly set up by user config.

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2020-09-22 15:31:13 +03:00
parent 1759ff57f1
commit 8e4416ad4b
2 changed files with 38 additions and 8 deletions

View file

@ -22,8 +22,6 @@ const (
cfgNodeKey = "node.key" cfgNodeKey = "node.key"
cfgBootstrapAddress = "node.address" cfgBootstrapAddress = "node.address"
cfgNodeAttributePrefix = "node.attribute" cfgNodeAttributePrefix = "node.attribute"
cfgNodeCapacity = "node.capacity"
cfgNodePrice = "node.price"
// config keys for cfgGRPC // config keys for cfgGRPC
cfgListenAddress = "grpc.endpoint" cfgListenAddress = "grpc.endpoint"
@ -101,8 +99,8 @@ type BootstrapType uint32
type cfgNodeInfo struct { type cfgNodeInfo struct {
bootType BootstrapType bootType BootstrapType
attributes []string attributes []string
capacity uint64 capacity uint64 // default: 0
price uint64 price uint64 // default: 0
} }
const ( const (
@ -149,8 +147,6 @@ func initCfg(path string) *cfg {
cfgNodeInfo: cfgNodeInfo{ cfgNodeInfo: cfgNodeInfo{
bootType: StorageNode, bootType: StorageNode,
attributes: readAttributes(viperCfg), attributes: readAttributes(viperCfg),
capacity: viperCfg.GetUint64(cfgNodeCapacity),
price: viperCfg.GetUint64(cfgNodePrice),
}, },
} }
} }
@ -179,8 +175,6 @@ func initViper(path string) *viper.Viper {
func defaultConfiguration(v *viper.Viper) { func defaultConfiguration(v *viper.Viper) {
// fixme: all hardcoded private keys must be removed // fixme: all hardcoded private keys must be removed
v.SetDefault(cfgNodeKey, "Kwk6k2eC3L3QuPvD8aiaNyoSXgQ2YL1bwS5CP1oKoA9waeAze97s") v.SetDefault(cfgNodeKey, "Kwk6k2eC3L3QuPvD8aiaNyoSXgQ2YL1bwS5CP1oKoA9waeAze97s")
v.SetDefault(cfgNodeCapacity, "10")
v.SetDefault(cfgNodePrice, "10")
v.SetDefault(cfgBootstrapAddress, "") // address to bootstrap with v.SetDefault(cfgBootstrapAddress, "") // address to bootstrap with
v.SetDefault(cfgMorphRPCAddress, "http://morph_chain.localtest.nspcc.ru:30333/") v.SetDefault(cfgMorphRPCAddress, "http://morph_chain.localtest.nspcc.ru:30333/")

View file

@ -1,6 +1,9 @@
package main package main
import ( import (
"strconv"
sdk "github.com/nspcc-dev/neofs-api-go/pkg/netmap"
v2netmap "github.com/nspcc-dev/neofs-api-go/v2/netmap" v2netmap "github.com/nspcc-dev/neofs-api-go/v2/netmap"
crypto "github.com/nspcc-dev/neofs-crypto" crypto "github.com/nspcc-dev/neofs-crypto"
"github.com/nspcc-dev/neofs-node/pkg/morph/client" "github.com/nspcc-dev/neofs-node/pkg/morph/client"
@ -37,6 +40,8 @@ func bootstrapNode(c *cfg) {
fatalOnErr(errors.Wrap(err, "bootstrap attribute error")) fatalOnErr(errors.Wrap(err, "bootstrap attribute error"))
} }
attrs = addWellKnownAttributes(c, attrs)
peerInfo := new(v2netmap.NodeInfo) peerInfo := new(v2netmap.NodeInfo)
peerInfo.SetAddress(c.viper.GetString(cfgBootstrapAddress)) peerInfo.SetAddress(c.viper.GetString(cfgBootstrapAddress))
peerInfo.SetPublicKey(crypto.MarshalPublicKey(&c.key.PublicKey)) peerInfo.SetPublicKey(crypto.MarshalPublicKey(&c.key.PublicKey))
@ -46,3 +51,34 @@ func bootstrapNode(c *cfg) {
fatalOnErr(errors.Wrap(err, "bootstrap error")) fatalOnErr(errors.Wrap(err, "bootstrap error"))
} }
} }
func addWellKnownAttributes(c *cfg, attrs []*v2netmap.Attribute) []*v2netmap.Attribute {
var hasCapacity, hasPrice bool
// check if user defined capacity and price attributes
for i := range attrs {
if !hasPrice && attrs[i].GetKey() == sdk.PriceAttr {
hasPrice = true
} else if !hasCapacity && attrs[i].GetKey() == sdk.CapacityAttr {
hasCapacity = true
}
}
// do not override user defined capacity and price attributes
if !hasCapacity {
capacity := new(v2netmap.Attribute)
capacity.SetKey(sdk.CapacityAttr)
capacity.SetValue(strconv.FormatUint(c.cfgNodeInfo.capacity, 10))
attrs = append(attrs, capacity)
}
if !hasPrice {
price := new(v2netmap.Attribute)
price.SetKey(sdk.PriceAttr)
price.SetValue(strconv.FormatUint(c.cfgNodeInfo.price, 10))
attrs = append(attrs, price)
}
return attrs
}