From 707a0bcb3589ccfad668b6690e9378acebceb75c Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Thu, 3 Dec 2020 13:52:36 +0300 Subject: [PATCH] [#220] netmap: process `REP X` policies correctly For `REP X` select X nodes from the default filter and fail if it cannot be done. Signed-off-by: Evgenii Stratonikov --- pkg/netmap/netmap.go | 13 +++++++++++++ pkg/netmap/selector_test.go | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/pkg/netmap/netmap.go b/pkg/netmap/netmap.go index 4e63e15c..080dd77a 100644 --- a/pkg/netmap/netmap.go +++ b/pkg/netmap/netmap.go @@ -71,6 +71,19 @@ func (m *Netmap) GetContainerNodes(p *PlacementPolicy, pivot []byte) (ContainerN } if r.Selector() == "" { + if len(p.Selectors()) == 0 { + s := new(Selector) + s.SetCount(r.Count()) + s.SetFilter(MainFilterName) + + nodes, err := c.getSelection(p, s) + if err != nil { + return nil, err + } + + result[i] = flattenNodes(nodes) + } + for _, s := range p.Selectors() { result[i] = append(result[i], flattenNodes(c.Selections[s.Name()])...) } diff --git a/pkg/netmap/selector_test.go b/pkg/netmap/selector_test.go index 15ab5726..81fcd58b 100644 --- a/pkg/netmap/selector_test.go +++ b/pkg/netmap/selector_test.go @@ -31,6 +31,43 @@ func TestPlacementPolicy_UnspecifiedClause(t *testing.T) { require.Equal(t, 4, len(v.Flatten())) } +func TestPlacementPolicy_Minimal(t *testing.T) { + nodes := []NodeInfo{ + nodeInfoFromAttributes("City", "Saint-Petersburg"), + nodeInfoFromAttributes("City", "Moscow"), + nodeInfoFromAttributes("City", "Berlin"), + nodeInfoFromAttributes("City", "Paris"), + } + nm, err := NewNetmap(NodesFromInfo(nodes)) + require.NoError(t, err) + + runTest := func(t *testing.T, rep uint32, expectError bool) { + p := newPlacementPolicy(0, + []*Replica{newReplica(rep, "")}, + nil, nil) + + v, err := nm.GetContainerNodes(p, nil) + + if expectError { + require.Error(t, err) + return + } + + require.NoError(t, err) + require.EqualValues(t, rep, len(v.Flatten())) + } + + t.Run("REP 1", func(t *testing.T) { + runTest(t, 1, false) + }) + t.Run("REP 3", func(t *testing.T) { + runTest(t, 3, false) + }) + t.Run("REP 5", func(t *testing.T) { + runTest(t, 5, true) + }) +} + // Issue #215. func TestPlacementPolicy_MultipleREP(t *testing.T) { p := newPlacementPolicy(1,