Skip to content

Commit

Permalink
BACKUP
Browse files Browse the repository at this point in the history
Signed-off-by: Leonard Lyubich <[email protected]>
  • Loading branch information
cthulhu-rider committed Dec 11, 2024
1 parent 22f5589 commit fb12af3
Show file tree
Hide file tree
Showing 14 changed files with 653 additions and 600 deletions.
5 changes: 4 additions & 1 deletion checksum/checksum.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type Checksum struct {

// Type represents the enumeration
// of checksum types.
type Type uint32
type Type int32

const (
Unknown Type = iota // Deprecated: use 0 instead.
Expand Down Expand Up @@ -99,6 +99,9 @@ func NewFromData(typ Type, data []byte) (Checksum, error) {
//
// See also [Checksum.ProtoMessage].
func (c *Checksum) FromProtoMessage(m *refs.Checksum) error {
if m.Type < 0 {
return fmt.Errorf("negative type %d", m.Type)
}
if len(m.Sum) == 0 {
return errors.New("missing value")
}
Expand Down
6 changes: 3 additions & 3 deletions checksum/checksum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ func TestChecksumDecodingFailures(t *testing.T) {
}

func TestNew(t *testing.T) {
typ := checksum.Type(rand.Uint32())
typ := checksum.Type(rand.Int31())
val := make([]byte, 128)
//nolint:staticcheck
rand.Read(val)
cs := checksum.New(typ, val)
require.Equal(t, typ, cs.Type())
require.Equal(t, val, cs.Value())

otherTyp := checksum.Type(rand.Uint32())
otherTyp := checksum.Type(rand.Int31())
otherVal := make([]byte, 128)
//nolint:staticcheck
rand.Read(otherVal)
Expand Down Expand Up @@ -155,7 +155,7 @@ func TestNewFromHash(t *testing.T) {
h.Write([]byte("Hello, world!"))
hb := []byte{32, 94, 4, 138}

typ := checksum.Type(rand.Uint32())
typ := checksum.Type(rand.Int31())
cs := checksum.NewFromHash(typ, h)
require.Equal(t, typ, cs.Type())
require.Equal(t, hb, cs.Value())
Expand Down
9 changes: 4 additions & 5 deletions netmap/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"

"github.com/nspcc-dev/hrw/v2"
"github.com/nspcc-dev/neofs-api-go/v2/netmap"
)

// context of a placement build process.
Expand All @@ -13,10 +12,10 @@ type context struct {
netMap NetMap

// cache of processed filters
processedFilters map[string]*netmap.Filter
processedFilters map[string]*Filter

// cache of processed selectors
processedSelectors map[string]*netmap.Selector
processedSelectors map[string]*Selector

// stores results of selector processing
selections map[string][]nodes
Expand Down Expand Up @@ -55,8 +54,8 @@ var (
func newContext(nm NetMap) *context {
return &context{
netMap: nm,
processedFilters: make(map[string]*netmap.Filter),
processedSelectors: make(map[string]*netmap.Selector),
processedFilters: make(map[string]*Filter),
processedSelectors: make(map[string]*Selector),
selections: make(map[string][]nodes),

numCache: make(map[string]uint64),
Expand Down
71 changes: 36 additions & 35 deletions netmap/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package netmap
import (
"fmt"
"strconv"

"github.com/nspcc-dev/neofs-api-go/v2/netmap"
)

// mainFilterName is a name of the filter
Expand All @@ -15,15 +13,15 @@ const mainFilterName = "*"
func (c *context) processFilters(p PlacementPolicy) error {
for i := range p.filters {
if err := c.processFilter(p.filters[i], true); err != nil {
return fmt.Errorf("process filter #%d (%s): %w", i, p.filters[i].GetName(), err)
return fmt.Errorf("process filter #%d (%s): %w", i, p.filters[i].Name(), err)
}
}

return nil
}

func (c *context) processFilter(f netmap.Filter, top bool) error {
fName := f.GetName()
func (c *context) processFilter(f Filter, top bool) error {
fName := f.Name()
if fName == mainFilterName {
return fmt.Errorf("%w: '%s' is reserved", errInvalidFilterName, mainFilterName)
}
Expand All @@ -36,10 +34,10 @@ func (c *context) processFilter(f netmap.Filter, top bool) error {
return errFilterNotFound
}

inner := f.GetFilters()
inner := f.SubFilters()

switch op := f.GetOp(); op {
case netmap.AND, netmap.OR:
switch op := f.Op(); op {
case FilterOpAND, FilterOpOR:
for i := range inner {
if err := c.processFilter(inner[i], false); err != nil {
return fmt.Errorf("process inner filter #%d: %w", i, err)
Expand All @@ -53,12 +51,12 @@ func (c *context) processFilter(f netmap.Filter, top bool) error {
}

switch op {
case netmap.EQ, netmap.NE:
case netmap.GT, netmap.GE, netmap.LT, netmap.LE:
val := f.GetValue()
case FilterOpEQ, FilterOpNE:
case FilterOpGT, FilterOpGE, FilterOpLT, FilterOpLE:
val := f.Value()
n, err := strconv.ParseUint(val, 10, 64)
if err != nil {
return fmt.Errorf("%w: '%s'", errInvalidNumber, f.GetValue())
return fmt.Errorf("%w: '%s'", errInvalidNumber, val)
}

c.numCache[val] = n
Expand All @@ -77,46 +75,49 @@ func (c *context) processFilter(f netmap.Filter, top bool) error {
// match matches f against b. It returns no errors because
// filter should have been parsed during context creation
// and missing node properties are considered as a regular fail.
func (c *context) match(f *netmap.Filter, b NodeInfo) bool {
switch f.GetOp() {
case netmap.AND, netmap.OR:
inner := f.GetFilters()
func (c *context) match(f *Filter, b NodeInfo) bool {
if f == nil {
return false
}
switch f.Op() {
case FilterOpAND, FilterOpOR:
inner := f.SubFilters()
for i := range inner {
fSub := &inner[i]
if name := inner[i].GetName(); name != "" {
if name := inner[i].Name(); name != "" {
fSub = c.processedFilters[name]
}

ok := c.match(fSub, b)
if ok == (f.GetOp() == netmap.OR) {
if ok == (f.Op() == FilterOpOR) {
return ok
}
}

return f.GetOp() == netmap.AND
return f.Op() == FilterOpAND
default:
return c.matchKeyValue(f, b)
}
}

func (c *context) matchKeyValue(f *netmap.Filter, b NodeInfo) bool {
switch op := f.GetOp(); op {
case netmap.EQ:
return b.Attribute(f.GetKey()) == f.GetValue()
case netmap.NE:
return b.Attribute(f.GetKey()) != f.GetValue()
func (c *context) matchKeyValue(f *Filter, b NodeInfo) bool {
switch op := f.Op(); op {
case FilterOpEQ:
return b.Attribute(f.Key()) == f.Value()
case FilterOpNE:
return b.Attribute(f.Key()) != f.Value()
default:
var attr uint64

switch f.GetKey() {
switch f.Key() {
case attrPrice:
attr = b.Price()
case attrCapacity:
attr = b.capacity()
default:
var err error

attr, err = strconv.ParseUint(b.Attribute(f.GetKey()), 10, 64)
attr, err = strconv.ParseUint(b.Attribute(f.Key()), 10, 64)
if err != nil {
// Note: because filters are somewhat independent from nodes attributes,
// We don't report an error here, and fail filter instead.
Expand All @@ -125,14 +126,14 @@ func (c *context) matchKeyValue(f *netmap.Filter, b NodeInfo) bool {
}

switch op {
case netmap.GT:
return attr > c.numCache[f.GetValue()]
case netmap.GE:
return attr >= c.numCache[f.GetValue()]
case netmap.LT:
return attr < c.numCache[f.GetValue()]
case netmap.LE:
return attr <= c.numCache[f.GetValue()]
case FilterOpGT:
return attr > c.numCache[f.Value()]
case FilterOpGE:
return attr >= c.numCache[f.Value()]
case FilterOpLT:
return attr < c.numCache[f.Value()]
case FilterOpLE:
return attr <= c.numCache[f.Value()]
default:
// do nothing and return false
}
Expand Down
31 changes: 15 additions & 16 deletions netmap/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@ import (
"errors"
"testing"

"github.com/nspcc-dev/neofs-api-go/v2/netmap"
"github.com/stretchr/testify/require"
)

func TestContext_ProcessFilters(t *testing.T) {
fs := []Filter{
newFilter("StorageSSD", "Storage", "SSD", netmap.EQ),
newFilter("GoodRating", "Rating", "4", netmap.GE),
newFilter("Main", "", "", netmap.AND,
newFilter("StorageSSD", "Storage", "SSD", FilterOpEQ),
newFilter("GoodRating", "Rating", "4", FilterOpGE),
newFilter("Main", "", "", FilterOpAND,
newFilter("StorageSSD", "", "", 0),
newFilter("", "IntField", "123", netmap.LT),
newFilter("", "IntField", "123", FilterOpLT),
newFilter("GoodRating", "", "", 0)),
}

Expand All @@ -23,11 +22,11 @@ func TestContext_ProcessFilters(t *testing.T) {
require.NoError(t, c.processFilters(p))
require.Equal(t, 3, len(c.processedFilters))
for _, f := range fs {
require.Equal(t, f.m, *c.processedFilters[f.m.GetName()])
require.Equal(t, f, *c.processedFilters[f.Name()])
}

require.Equal(t, uint64(4), c.numCache[fs[1].m.GetValue()])
require.Equal(t, uint64(123), c.numCache[fs[2].m.GetFilters()[1].GetValue()])
require.Equal(t, uint64(4), c.numCache[fs[1].Value()])
require.Equal(t, uint64(123), c.numCache[fs[2].SubFilters()[1].Value()])
}

func TestContext_ProcessFiltersInvalid(t *testing.T) {
Expand All @@ -38,24 +37,24 @@ func TestContext_ProcessFiltersInvalid(t *testing.T) {
}{
{
"UnnamedTop",
newFilter("", "Storage", "SSD", netmap.EQ),
newFilter("", "Storage", "SSD", FilterOpEQ),
errUnnamedTopFilter,
},
{
"InvalidReference",
newFilter("Main", "", "", netmap.AND,
newFilter("Main", "", "", FilterOpAND,
newFilter("StorageSSD", "", "", 0)),
errFilterNotFound,
},
{
"NonEmptyKeyed",
newFilter("Main", "Storage", "SSD", netmap.EQ,
newFilter("Main", "Storage", "SSD", FilterOpEQ,
newFilter("StorageSSD", "", "", 0)),
errNonEmptyFilters,
},
{
"InvalidNumber",
newFilter("Main", "Rating", "three", netmap.GE),
newFilter("Main", "Rating", "three", FilterOpGE),
errInvalidNumber,
},
{
Expand All @@ -65,7 +64,7 @@ func TestContext_ProcessFiltersInvalid(t *testing.T) {
},
{
"InvalidName",
newFilter("*", "Rating", "3", netmap.GE),
newFilter("*", "Rating", "3", FilterOpGE),
errInvalidFilterName,
},
}
Expand All @@ -84,12 +83,12 @@ func TestFilter_MatchSimple_InvalidOp(t *testing.T) {
b.SetAttribute("Rating", "4")
b.SetAttribute("Country", "Germany")

f := newFilter("Main", "Rating", "5", netmap.EQ)
f := newFilter("Main", "Rating", "5", FilterOpEQ)
c := newContext(NetMap{})
p := newPlacementPolicy(1, nil, nil, []Filter{f})
require.NoError(t, c.processFilters(p))

// just for the coverage
f.m.SetOp(0)
require.False(t, c.match(&f.m, b))
f.op = 0
require.False(t, c.match(&f, b))
}
18 changes: 5 additions & 13 deletions netmap/helper_test.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
package netmap

import (
"github.com/nspcc-dev/neofs-api-go/v2/netmap"
)

func newFilter(name string, k, v string, op netmap.Operation, fs ...Filter) (f Filter) {
func newFilter(name string, k, v string, op FilterOp, fs ...Filter) (f Filter) {
f.SetName(name)
f.m.SetKey(k)
f.m.SetOp(op)
f.m.SetValue(v)
inner := make([]netmap.Filter, len(fs))
for i := range fs {
inner[i] = fs[i].m
}
f.m.SetFilters(inner)
f.key = k
f.op = op
f.val = v
f.subs = fs
return f
}

Expand Down
10 changes: 5 additions & 5 deletions netmap/netmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,12 @@ func (m NetMap) ContainerNodes(p PlacementPolicy, containerID cid.ID) ([][]NodeI
result := make([][]NodeInfo, len(p.replicas))

for i := range p.replicas {
sName := p.replicas[i].GetSelector()
sName := p.replicas[i].SelectorName()
if sName == "" {
if len(p.selectors) == 0 {
var s netmap.Selector
s.SetCount(p.replicas[i].GetCount())
s.SetFilter(mainFilterName)
var s Selector
s.SetNumberOfNodes(p.replicas[i].NumberOfObjects())
s.SetFilterName(mainFilterName)

nodes, err := c.getSelection(p, s)
if err != nil {
Expand All @@ -212,7 +212,7 @@ func (m NetMap) ContainerNodes(p PlacementPolicy, containerID cid.ID) ([][]NodeI
}

for i := range p.selectors {
result[i] = append(result[i], flattenNodes(c.selections[p.selectors[i].GetName()])...)
result[i] = append(result[i], flattenNodes(c.selections[p.selectors[i].Name()])...)
}

continue
Expand Down
Loading

0 comments on commit fb12af3

Please sign in to comment.