forked from TrueCloudLab/frostfs-sdk-go
[#229] netmap: make context private
It isn't used anywhere outside of the package. Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
parent
60ef026923
commit
1f7fe6864d
6 changed files with 20 additions and 20 deletions
|
@ -6,8 +6,8 @@ import (
|
||||||
"github.com/nspcc-dev/hrw"
|
"github.com/nspcc-dev/hrw"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Context contains references to named filters and cached numeric values.
|
// context contains references to named filters and cached numeric values.
|
||||||
type Context struct {
|
type context struct {
|
||||||
// Netmap is a netmap structure to operate on.
|
// Netmap is a netmap structure to operate on.
|
||||||
Netmap *Netmap
|
Netmap *Netmap
|
||||||
// Filters stores processed filters.
|
// Filters stores processed filters.
|
||||||
|
@ -47,10 +47,10 @@ var (
|
||||||
ErrUnnamedTopFilter = errors.New("netmap: all filters on top level must be named")
|
ErrUnnamedTopFilter = errors.New("netmap: all filters on top level must be named")
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewContext creates new context. It contains various caches.
|
// newContext creates new context. It contains various caches.
|
||||||
// In future it may create hierarchical netmap structure to work with.
|
// In future it may create hierarchical netmap structure to work with.
|
||||||
func NewContext(nm *Netmap) *Context {
|
func newContext(nm *Netmap) *context {
|
||||||
return &Context{
|
return &context{
|
||||||
Netmap: nm,
|
Netmap: nm,
|
||||||
Filters: make(map[string]*Filter),
|
Filters: make(map[string]*Filter),
|
||||||
Selectors: make(map[string]*Selector),
|
Selectors: make(map[string]*Selector),
|
||||||
|
@ -63,14 +63,14 @@ func NewContext(nm *Netmap) *Context {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Context) setPivot(pivot []byte) {
|
func (c *context) setPivot(pivot []byte) {
|
||||||
if len(pivot) != 0 {
|
if len(pivot) != 0 {
|
||||||
c.pivot = pivot
|
c.pivot = pivot
|
||||||
c.pivotHash = hrw.Hash(pivot)
|
c.pivotHash = hrw.Hash(pivot)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Context) setCBF(cbf uint32) {
|
func (c *context) setCBF(cbf uint32) {
|
||||||
if cbf == 0 {
|
if cbf == 0 {
|
||||||
c.cbf = defaultCBF
|
c.cbf = defaultCBF
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -15,12 +15,12 @@ type Filter netmap.Filter
|
||||||
const MainFilterName = "*"
|
const MainFilterName = "*"
|
||||||
|
|
||||||
// applyFilter applies named filter to b.
|
// applyFilter applies named filter to b.
|
||||||
func (c *Context) applyFilter(name string, b *Node) bool {
|
func (c *context) applyFilter(name string, b *Node) bool {
|
||||||
return name == MainFilterName || c.match(c.Filters[name], b)
|
return name == MainFilterName || c.match(c.Filters[name], b)
|
||||||
}
|
}
|
||||||
|
|
||||||
// processFilters processes filters and returns error is any of them is invalid.
|
// processFilters processes filters and returns error is any of them is invalid.
|
||||||
func (c *Context) processFilters(p *PlacementPolicy) error {
|
func (c *context) processFilters(p *PlacementPolicy) error {
|
||||||
filters := p.Filters()
|
filters := p.Filters()
|
||||||
for i := range filters {
|
for i := range filters {
|
||||||
if err := c.processFilter(&filters[i], true); err != nil {
|
if err := c.processFilter(&filters[i], true); err != nil {
|
||||||
|
@ -31,7 +31,7 @@ func (c *Context) processFilters(p *PlacementPolicy) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Context) processFilter(f *Filter, top bool) error {
|
func (c *context) processFilter(f *Filter, top bool) error {
|
||||||
if f == nil {
|
if f == nil {
|
||||||
return fmt.Errorf("%w: FILTER", ErrMissingField)
|
return fmt.Errorf("%w: FILTER", ErrMissingField)
|
||||||
}
|
}
|
||||||
|
@ -86,7 +86,7 @@ func (c *Context) processFilter(f *Filter, top bool) error {
|
||||||
// match matches f against b. It returns no errors because
|
// match matches f against b. It returns no errors because
|
||||||
// filter should have been parsed during context creation
|
// filter should have been parsed during context creation
|
||||||
// and missing node properties are considered as a regular fail.
|
// and missing node properties are considered as a regular fail.
|
||||||
func (c *Context) match(f *Filter, b *Node) bool {
|
func (c *context) match(f *Filter, b *Node) bool {
|
||||||
switch f.Operation() {
|
switch f.Operation() {
|
||||||
case OpAND, OpOR:
|
case OpAND, OpOR:
|
||||||
for _, lf := range f.InnerFilters() {
|
for _, lf := range f.InnerFilters() {
|
||||||
|
@ -106,7 +106,7 @@ func (c *Context) match(f *Filter, b *Node) bool {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Context) matchKeyValue(f *Filter, b *Node) bool {
|
func (c *context) matchKeyValue(f *Filter, b *Node) bool {
|
||||||
switch f.Operation() {
|
switch f.Operation() {
|
||||||
case OpEQ:
|
case OpEQ:
|
||||||
return b.Attribute(f.Key()) == f.Value()
|
return b.Attribute(f.Key()) == f.Value()
|
||||||
|
|
|
@ -19,7 +19,7 @@ func TestContext_ProcessFilters(t *testing.T) {
|
||||||
}
|
}
|
||||||
nm, err := NewNetmap(nil)
|
nm, err := NewNetmap(nil)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
c := NewContext(nm)
|
c := newContext(nm)
|
||||||
p := newPlacementPolicy(1, nil, nil, fs)
|
p := newPlacementPolicy(1, nil, nil, fs)
|
||||||
require.NoError(t, c.processFilters(p))
|
require.NoError(t, c.processFilters(p))
|
||||||
require.Equal(t, 3, len(c.Filters))
|
require.Equal(t, 3, len(c.Filters))
|
||||||
|
@ -72,7 +72,7 @@ func TestContext_ProcessFiltersInvalid(t *testing.T) {
|
||||||
}
|
}
|
||||||
for _, tc := range errTestCases {
|
for _, tc := range errTestCases {
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
c := NewContext(new(Netmap))
|
c := newContext(new(Netmap))
|
||||||
p := newPlacementPolicy(1, nil, nil, []Filter{tc.filter})
|
p := newPlacementPolicy(1, nil, nil, []Filter{tc.filter})
|
||||||
err := c.processFilters(p)
|
err := c.processFilters(p)
|
||||||
require.True(t, errors.Is(err, tc.err), "got: %v", err)
|
require.True(t, errors.Is(err, tc.err), "got: %v", err)
|
||||||
|
@ -87,7 +87,7 @@ func TestFilter_MatchSimple_InvalidOp(t *testing.T) {
|
||||||
}}
|
}}
|
||||||
|
|
||||||
f := newFilter("Main", "Rating", "5", OpEQ)
|
f := newFilter("Main", "Rating", "5", OpEQ)
|
||||||
c := NewContext(new(Netmap))
|
c := newContext(new(Netmap))
|
||||||
p := newPlacementPolicy(1, nil, nil, []Filter{f})
|
p := newPlacementPolicy(1, nil, nil, []Filter{f})
|
||||||
require.NoError(t, c.processFilters(p))
|
require.NoError(t, c.processFilters(p))
|
||||||
|
|
||||||
|
|
|
@ -48,7 +48,7 @@ func (m *Netmap) GetPlacementVectors(cnt ContainerNodes, pivot []byte) ([]Nodes,
|
||||||
// Order of returned nodes corresponds to order of replicas in p.
|
// Order of returned nodes corresponds to order of replicas in p.
|
||||||
// pivot is a seed for HRW sorting.
|
// pivot is a seed for HRW sorting.
|
||||||
func (m *Netmap) GetContainerNodes(p *PlacementPolicy, pivot []byte) (ContainerNodes, error) {
|
func (m *Netmap) GetContainerNodes(p *PlacementPolicy, pivot []byte) (ContainerNodes, error) {
|
||||||
c := NewContext(m)
|
c := newContext(m)
|
||||||
c.setPivot(pivot)
|
c.setPivot(pivot)
|
||||||
c.setCBF(p.ContainerBackupFactor())
|
c.setCBF(p.ContainerBackupFactor())
|
||||||
|
|
||||||
|
|
|
@ -13,7 +13,7 @@ import (
|
||||||
type Selector netmap.Selector
|
type Selector netmap.Selector
|
||||||
|
|
||||||
// processSelectors processes selectors and returns error is any of them is invalid.
|
// processSelectors processes selectors and returns error is any of them is invalid.
|
||||||
func (c *Context) processSelectors(p *PlacementPolicy) error {
|
func (c *context) processSelectors(p *PlacementPolicy) error {
|
||||||
selectors := p.Selectors()
|
selectors := p.Selectors()
|
||||||
for i, s := range selectors {
|
for i, s := range selectors {
|
||||||
if s.Filter() != MainFilterName {
|
if s.Filter() != MainFilterName {
|
||||||
|
@ -49,7 +49,7 @@ func GetNodesCount(_ *PlacementPolicy, s *Selector) (int, int) {
|
||||||
|
|
||||||
// getSelection returns nodes grouped by s.attribute.
|
// getSelection returns nodes grouped by s.attribute.
|
||||||
// Last argument specifies if more buckets can be used to fulfill CBF.
|
// Last argument specifies if more buckets can be used to fulfill CBF.
|
||||||
func (c *Context) getSelection(p *PlacementPolicy, s *Selector) ([]Nodes, error) {
|
func (c *context) getSelection(p *PlacementPolicy, s *Selector) ([]Nodes, error) {
|
||||||
bucketCount, nodesInBucket := GetNodesCount(p, s)
|
bucketCount, nodesInBucket := GetNodesCount(p, s)
|
||||||
buckets := c.getSelectionBase(p.SubnetID(), s)
|
buckets := c.getSelectionBase(p.SubnetID(), s)
|
||||||
|
|
||||||
|
@ -124,7 +124,7 @@ type nodeAttrPair struct {
|
||||||
|
|
||||||
// getSelectionBase returns nodes grouped by selector attribute.
|
// getSelectionBase returns nodes grouped by selector attribute.
|
||||||
// It it guaranteed that each pair will contain at least one node.
|
// It it guaranteed that each pair will contain at least one node.
|
||||||
func (c *Context) getSelectionBase(subnetID *subnetid.ID, s *Selector) []nodeAttrPair {
|
func (c *context) getSelectionBase(subnetID *subnetid.ID, s *Selector) []nodeAttrPair {
|
||||||
f := c.Filters[s.Filter()]
|
f := c.Filters[s.Filter()]
|
||||||
isMain := s.Filter() == MainFilterName
|
isMain := s.Filter() == MainFilterName
|
||||||
result := []nodeAttrPair{}
|
result := []nodeAttrPair{}
|
||||||
|
|
|
@ -214,7 +214,7 @@ func TestPlacementPolicy_ProcessSelectors(t *testing.T) {
|
||||||
|
|
||||||
nm, err := NewNetmap(NodesFromInfo(nodes))
|
nm, err := NewNetmap(NodesFromInfo(nodes))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
c := NewContext(nm)
|
c := newContext(nm)
|
||||||
c.setCBF(p.ContainerBackupFactor())
|
c.setCBF(p.ContainerBackupFactor())
|
||||||
require.NoError(t, c.processFilters(p))
|
require.NoError(t, c.processFilters(p))
|
||||||
require.NoError(t, c.processSelectors(p))
|
require.NoError(t, c.processSelectors(p))
|
||||||
|
|
Loading…
Reference in a new issue