Skip to content

Commit

Permalink
Fix/Placement sorting (#480)
Browse files Browse the repository at this point in the history
Closes #438.
  • Loading branch information
roman-khimov authored Aug 1, 2023
2 parents af656fb + 6e3c516 commit e3b3936
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 8 deletions.
12 changes: 8 additions & 4 deletions netmap/aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type (
}

minAgg struct {
min float64
min *float64
}

meanIQRAgg struct {
Expand Down Expand Up @@ -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) {
Expand Down
69 changes: 69 additions & 0 deletions netmap/aggregator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
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())
}
}

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())
}
}
17 changes: 13 additions & 4 deletions netmap/json_tests/hrw_sort.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "HRW ordering",
"nodes": [
{
"public_key": "AsGTU2KO25PRUWal5HzInv3LLqBedankF/scMr7VpVR/",
"attributes": [
{
"key": "Country",
Expand All @@ -18,6 +19,7 @@
]
},
{
"public_key": "AltaQMNJ8k3awpg9q8Z+CFMXHt+io4ChddgBuRw8QSN+",
"attributes": [
{
"key": "Country",
Expand All @@ -34,6 +36,7 @@
]
},
{
"public_key": "AoV8bh005WYoMAGpucDOZ1O704ySFlypTWP+pJP1trBo",
"attributes": [
{
"key": "Country",
Expand All @@ -50,6 +53,7 @@
]
},
{
"public_key": "ApQN0UPXMeBcCiiw+UA3XRe5EESOFbu545lEaThaZ+4L",
"attributes": [
{
"key": "Country",
Expand All @@ -66,6 +70,7 @@
]
},
{
"public_key": "AzG1GtS58K92j6dDrGXTzu3EKJ+9GFLZLMK6lpH+mIzE",
"attributes": [
{
"key": "Country",
Expand All @@ -82,6 +87,7 @@
]
},
{
"public_key": "AzCRU8cw/WQRPZV/f5PjxXXFSZ7dakRvzlwwEVFQdaz4",
"attributes": [
{
"key": "Country",
Expand All @@ -94,6 +100,7 @@
]
},
{
"public_key": "As8NhDYv70g2ivJ95guRdxtDuqzONOv0nlU36xuXI2Mx",
"attributes": [
{
"key": "Country",
Expand All @@ -110,6 +117,7 @@
]
},
{
"public_key": "Ax9daczuWTOYdrsmGrqWRkDonY86GD1OE1WUrnsOjE9e",
"attributes": [
{
"key": "Country",
Expand All @@ -126,6 +134,7 @@
]
},
{
"public_key": "AorNy+ku9CwiGdQO/TjcGHXYmvGks6ZIS9JqVO1+KXze",
"attributes": [
{
"key": "Country",
Expand All @@ -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]]
}
}
}
Expand Down

0 comments on commit e3b3936

Please sign in to comment.