From fd512ade58200201e75c69bc4876e473213a8e04 Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Mon, 31 Jul 2023 15:39:58 +0300 Subject: [PATCH 1/2] netmap: Add mean aggregator test Signed-off-by: Pavel Karpy --- netmap/aggregator_test.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 netmap/aggregator_test.go diff --git a/netmap/aggregator_test.go b/netmap/aggregator_test.go new file mode 100644 index 00000000..26789925 --- /dev/null +++ b/netmap/aggregator_test.go @@ -0,0 +1,36 @@ +package netmap + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestMeanAgg(t *testing.T) { + tt := []struct { + vals []float64 + res float64 + }{ + { + vals: []float64{0, 1, 3, 4, 5}, + res: 2.6, + }, + { + vals: []float64{0, 0, 0, 0}, + res: 0, + }, + { + vals: []float64{1, 1, 1, 1}, + res: 1, + }, + } + + for _, test := range tt { + a := newMeanAgg() + for _, val := range test.vals { + a.Add(val) + } + + require.Equal(t, test.res, a.Compute()) + } +} From 6e3c5161f380570a630911019d58dd8d3fc4efd1 Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Mon, 31 Jul 2023 15:46:19 +0300 Subject: [PATCH 2/2] netmap: Fix min aggregator Do not return next element if `0` is being facing. Also, add a public key to the test nodes. It is used in `Hash()` method and, therefore, is the main start point in the HWR sorting, without it, every node is considered the same, so it is hard to pick a good test value, cause sorting is random for the same hashes. Closes #438. Signed-off-by: Pavel Karpy --- netmap/aggregator.go | 12 ++++++++---- netmap/aggregator_test.go | 33 +++++++++++++++++++++++++++++++++ netmap/json_tests/hrw_sort.json | 17 +++++++++++++---- 3 files changed, 54 insertions(+), 8 deletions(-) diff --git a/netmap/aggregator.go b/netmap/aggregator.go index c018e6b1..3365fe31 100644 --- a/netmap/aggregator.go +++ b/netmap/aggregator.go @@ -23,7 +23,7 @@ type ( } minAgg struct { - min float64 + min *float64 } meanIQRAgg struct { @@ -102,13 +102,17 @@ func (a *meanAgg) Compute() float64 { } func (a *minAgg) Add(n float64) { - if a.min == 0 || n < a.min { - a.min = n + if a.min == nil || n < *a.min { + a.min = &n } } func (a *minAgg) Compute() float64 { - return a.min + if a.min == nil { + return 0 + } + + return *a.min } func (a *meanIQRAgg) Add(n float64) { diff --git a/netmap/aggregator_test.go b/netmap/aggregator_test.go index 26789925..20ff4d87 100644 --- a/netmap/aggregator_test.go +++ b/netmap/aggregator_test.go @@ -34,3 +34,36 @@ func TestMeanAgg(t *testing.T) { require.Equal(t, test.res, a.Compute()) } } + +func TestMinAgg(t *testing.T) { + tt := []struct { + vals []float64 + res float64 + }{ + { + vals: []float64{1, 2, 3, 4, 0, 100}, + res: 0, + }, + { + vals: []float64{0, 1, 3, 4, 5}, + res: 0, + }, + { + vals: []float64{0, 0, 0, 0}, + res: 0, + }, + { + vals: []float64{1, 1, 1, 1}, + res: 1, + }, + } + + for _, test := range tt { + a := newMinAgg() + for _, val := range test.vals { + a.Add(val) + } + + require.Equal(t, test.res, a.Compute()) + } +} diff --git a/netmap/json_tests/hrw_sort.json b/netmap/json_tests/hrw_sort.json index 521a852e..31283566 100644 --- a/netmap/json_tests/hrw_sort.json +++ b/netmap/json_tests/hrw_sort.json @@ -2,6 +2,7 @@ "name": "HRW ordering", "nodes": [ { + "public_key": "AsGTU2KO25PRUWal5HzInv3LLqBedankF/scMr7VpVR/", "attributes": [ { "key": "Country", @@ -18,6 +19,7 @@ ] }, { + "public_key": "AltaQMNJ8k3awpg9q8Z+CFMXHt+io4ChddgBuRw8QSN+", "attributes": [ { "key": "Country", @@ -34,6 +36,7 @@ ] }, { + "public_key": "AoV8bh005WYoMAGpucDOZ1O704ySFlypTWP+pJP1trBo", "attributes": [ { "key": "Country", @@ -50,6 +53,7 @@ ] }, { + "public_key": "ApQN0UPXMeBcCiiw+UA3XRe5EESOFbu545lEaThaZ+4L", "attributes": [ { "key": "Country", @@ -66,6 +70,7 @@ ] }, { + "public_key": "AzG1GtS58K92j6dDrGXTzu3EKJ+9GFLZLMK6lpH+mIzE", "attributes": [ { "key": "Country", @@ -82,6 +87,7 @@ ] }, { + "public_key": "AzCRU8cw/WQRPZV/f5PjxXXFSZ7dakRvzlwwEVFQdaz4", "attributes": [ { "key": "Country", @@ -94,6 +100,7 @@ ] }, { + "public_key": "As8NhDYv70g2ivJ95guRdxtDuqzONOv0nlU36xuXI2Mx", "attributes": [ { "key": "Country", @@ -110,6 +117,7 @@ ] }, { + "public_key": "Ax9daczuWTOYdrsmGrqWRkDonY86GD1OE1WUrnsOjE9e", "attributes": [ { "key": "Country", @@ -126,6 +134,7 @@ ] }, { + "public_key": "AorNy+ku9CwiGdQO/TjcGHXYmvGks6ZIS9JqVO1+KXze", "attributes": [ { "key": "Country", @@ -146,19 +155,19 @@ "select 3 nodes in 3 distinct countries, same placement": { "policy": {"replicas":[{"count":1,"selector":"Main"}],"containerBackupFactor":1,"selectors":[{"name":"Main","count":3,"clause":"DISTINCT","attribute":"Country","filter":"*"}],"filters":[],"subnetId":null}, "pivot": "Y29udGFpbmVySUQ=", - "result": [[4, 0, 7]], + "result": [[1, 7, 5]], "placement": { "pivot": "b2JqZWN0SUQ=", - "result": [[4, 0, 7]] + "result": [[1, 7, 5]] } }, "select 6 nodes in 3 distinct countries, different placement": { "policy": {"replicas":[{"count":1,"selector":"Main"}],"containerBackupFactor":2,"selectors":[{"name":"Main","count":3,"clause":"DISTINCT","attribute":"Country","filter":"*"}],"filters":[],"subnetId":null}, "pivot": "Y29udGFpbmVySUQ=", - "result": [[4, 3, 0, 1, 7, 2]], + "result": [[1, 6, 0, 7, 5, 3]], "placement": { "pivot": "b2JqZWN0SUQ=", - "result": [[4, 3, 0, 7, 2, 1]] + "result": [[1, 6, 0, 7, 5, 3]] } } }