4daece2b98
This new component broadcasts metrics about the current size of the pinqueue, which can in turn be used to inform allocations. It has a weight_bucket_size option that serves to divide the actual size by a given factor. This allows considering peers with similar queue sizes to have the same weight. Additionally, some changes have been made to the balanced allocator so that a combination of tags, pinqueue sizes and free-spaces can be used. When allocating by [<tag>, pinqueue, freespace], the allocator will prioritize choosing peers with the smallest pin queue weight first, and of those with the same weight, it will allocate based on freespace.
77 lines
1.2 KiB
Go
77 lines
1.2 KiB
Go
package pinqueue
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
var cfgJSON = []byte(`
|
|
{
|
|
"metric_ttl": "1s"
|
|
}
|
|
`)
|
|
|
|
func TestLoadJSON(t *testing.T) {
|
|
cfg := &Config{}
|
|
err := cfg.LoadJSON(cfgJSON)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
j := &jsonConfig{}
|
|
|
|
json.Unmarshal(cfgJSON, j)
|
|
j.MetricTTL = "-10"
|
|
tst, _ := json.Marshal(j)
|
|
err = cfg.LoadJSON(tst)
|
|
if err == nil {
|
|
t.Error("expected error decoding metric_ttl")
|
|
}
|
|
}
|
|
|
|
func TestToJSON(t *testing.T) {
|
|
cfg := &Config{}
|
|
cfg.LoadJSON(cfgJSON)
|
|
newjson, err := cfg.ToJSON()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
cfg = &Config{}
|
|
err = cfg.LoadJSON(newjson)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestDefault(t *testing.T) {
|
|
cfg := &Config{}
|
|
cfg.Default()
|
|
if cfg.Validate() != nil {
|
|
t.Fatal("error validating")
|
|
}
|
|
|
|
cfg.MetricTTL = 0
|
|
if cfg.Validate() == nil {
|
|
t.Fatal("expected error validating")
|
|
}
|
|
|
|
cfg.Default()
|
|
cfg.WeightBucketSize = -2
|
|
if cfg.Validate() == nil {
|
|
t.Fatal("expected error validating")
|
|
}
|
|
|
|
}
|
|
|
|
func TestApplyEnvVars(t *testing.T) {
|
|
os.Setenv("CLUSTER_PINQUEUE_METRICTTL", "22s")
|
|
cfg := &Config{}
|
|
cfg.ApplyEnvVars()
|
|
|
|
if cfg.MetricTTL != 22*time.Second {
|
|
t.Fatal("failed to override metric_ttl with env var")
|
|
}
|
|
}
|