Revert "Informer/disk: deprecate RepoSize metric"

This reverts commit 3e54c4c695.
This commit is contained in:
Hector Sanjuan 2021-09-10 18:53:59 +02:00
parent 3e54c4c695
commit 4060f4196b
4 changed files with 96 additions and 11 deletions

View File

@ -15,18 +15,32 @@ const envConfigKey = "cluster_disk"
// Default values for disk Config
const (
DefaultMetricTTL = 30 * time.Second
DefaultMetricType = MetricFreeSpace
)
// String returns a string representation for MetricType.
func (t MetricType) String() string {
switch t {
case MetricFreeSpace:
return "freespace"
case MetricRepoSize:
return "reposize"
}
return ""
}
// Config is used to initialize an Informer and customize
// the type and parameters of the metric it produces.
type Config struct {
config.Saver
MetricTTL time.Duration
MetricType MetricType
}
type jsonConfig struct {
MetricTTL string `json:"metric_ttl"`
MetricType string `json:"metric_type"`
}
// ConfigKey returns a human-friendly identifier for this type of Metric.
@ -37,6 +51,7 @@ func (cfg *Config) ConfigKey() string {
// Default initializes this Config with sensible values.
func (cfg *Config) Default() error {
cfg.MetricTTL = DefaultMetricTTL
cfg.MetricType = DefaultMetricType
return nil
}
@ -60,6 +75,9 @@ func (cfg *Config) Validate() error {
return errors.New("disk.metric_ttl is invalid")
}
if cfg.MetricType.String() == "" {
return errors.New("disk.metric_type is invalid")
}
return nil
}
@ -82,6 +100,15 @@ func (cfg *Config) applyJSONConfig(jcfg *jsonConfig) error {
t, _ := time.ParseDuration(jcfg.MetricTTL)
cfg.MetricTTL = t
switch jcfg.MetricType {
case "reposize":
cfg.MetricType = MetricRepoSize
case "freespace":
cfg.MetricType = MetricFreeSpace
default:
return errors.New("disk.metric_type is invalid")
}
return cfg.Validate()
}
@ -97,6 +124,7 @@ func (cfg *Config) ToJSON() (raw []byte, err error) {
func (cfg *Config) toJSONConfig() *jsonConfig {
return &jsonConfig{
MetricTTL: cfg.MetricTTL.String(),
MetricType: cfg.MetricType.String(),
}
}

View File

@ -30,6 +30,25 @@ func TestLoadJSON(t *testing.T) {
if err == nil {
t.Error("expected error decoding metric_ttl")
}
j = &jsonConfig{}
json.Unmarshal(cfgJSON, j)
j.MetricType = "abc"
tst, _ = json.Marshal(j)
err = cfg.LoadJSON(tst)
if err == nil {
t.Error("expected error decoding check_interval")
}
j = &jsonConfig{}
json.Unmarshal(cfgJSON, j)
j.MetricType = "reposize"
tst, _ = json.Marshal(j)
err = cfg.LoadJSON(tst)
if err != nil {
t.Error("reposize should be a valid type")
}
}
func TestToJSON(t *testing.T) {
@ -57,6 +76,12 @@ func TestDefault(t *testing.T) {
if cfg.Validate() == nil {
t.Fatal("expected error validating")
}
cfg.Default()
cfg.MetricType = MetricRepoSize
if cfg.Validate() != nil {
t.Fatal("MetricRepoSize is a valid type")
}
}
func TestApplyEnvVars(t *testing.T) {

View File

@ -50,7 +50,7 @@ func NewInformer(cfg *Config) (*Informer, error) {
// Name returns the user-facing name of this informer.
func (disk *Informer) Name() string {
return "reposize"
return disk.config.MetricType.String()
}
// SetClient provides us with an rpc.Client which allows
@ -108,6 +108,8 @@ func (disk *Informer) GetMetric(ctx context.Context) *api.Metric {
logger.Error(err)
valid = false
} else {
switch disk.config.MetricType {
case MetricFreeSpace:
size := repoStat.RepoSize
total := repoStat.StorageMax
if size < total {
@ -115,6 +117,9 @@ func (disk *Informer) GetMetric(ctx context.Context) *api.Metric {
} else { // Make sure we don't underflow
metric = 0
}
case MetricRepoSize:
metric = repoStat.RepoSize
}
}
m := &api.Metric{

View File

@ -52,6 +52,7 @@ func TestFreeSpace(t *testing.T) {
ctx := context.Background()
cfg := &Config{}
cfg.Default()
cfg.MetricType = MetricFreeSpace
inf, err := NewInformer(cfg)
if err != nil {
@ -73,6 +74,32 @@ func TestFreeSpace(t *testing.T) {
}
}
func TestRepoSize(t *testing.T) {
ctx := context.Background()
cfg := &Config{}
cfg.Default()
cfg.MetricType = MetricRepoSize
inf, err := NewInformer(cfg)
if err != nil {
t.Fatal(err)
}
defer inf.Shutdown(ctx)
m := inf.GetMetric(ctx)
if m.Valid {
t.Error("metric should be invalid")
}
inf.SetClient(test.NewMockRPCClient(t))
m = inf.GetMetric(ctx)
if !m.Valid {
t.Error("metric should be valid")
}
// The mock client reports 100KB and 2 pins of 1 KB
if m.Value != "2000" {
t.Error("bad metric value")
}
}
func TestWithErrors(t *testing.T) {
ctx := context.Background()
cfg := &Config{}