From 5d48af9013f32f3f7986d5be2f4818d8c0eeb74e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Fri, 22 Apr 2016 14:02:03 +0000 Subject: [PATCH] remove scale option to by --- handler/lib.go | 21 +++++++++++---------- handler/lib_test.go | 6 +++--- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/handler/lib.go b/handler/lib.go index 0bdb300..629932c 100644 --- a/handler/lib.go +++ b/handler/lib.go @@ -13,7 +13,7 @@ import ( ) type Handler struct { - Id, When, Scale string + Id, When, By string MinInstances int64 MaxInstances int64 Cooldown time.Duration @@ -37,11 +37,12 @@ func (*Handler) Info() (*udf.InfoResponse, error) { Options: map[string]*udf.OptionInfo{ "id": {ValueTypes: []udf.ValueType{udf.ValueType_STRING}}, "when": {ValueTypes: []udf.ValueType{udf.ValueType_STRING}}, - "scale": {ValueTypes: []udf.ValueType{udf.ValueType_STRING}}, + "by": {ValueTypes: []udf.ValueType{udf.ValueType_STRING}}, "min_instances": {ValueTypes: []udf.ValueType{udf.ValueType_INT}}, "max_instances": {ValueTypes: []udf.ValueType{udf.ValueType_INT}}, "cooldown": {ValueTypes: []udf.ValueType{udf.ValueType_STRING}}, "simulate": {ValueTypes: []udf.ValueType{udf.ValueType_BOOL}}, + "debug": {ValueTypes: []udf.ValueType{udf.ValueType_BOOL}}, }, } return info, nil @@ -59,7 +60,7 @@ func (h *Handler) Init(r *udf.InitRequest) (*udf.InitResponse, error) { h.Debug = false h.Simulate = false h.Cooldown = time.Minute - h.Scale = "current + 1" + h.By = "current + 1" h.MinInstances = 1 h.MaxInstances = 3 @@ -68,8 +69,8 @@ func (h *Handler) Init(r *udf.InitRequest) (*udf.InitResponse, error) { switch opt.Name { case "when": h.When = opt.Values[0].Value.(*udf.OptionValue_StringValue).StringValue - case "scale": - h.Scale = opt.Values[0].Value.(*udf.OptionValue_StringValue).StringValue + case "by": + h.By = opt.Values[0].Value.(*udf.OptionValue_StringValue).StringValue case "min_instances": h.MinInstances = opt.Values[0].Value.(*udf.OptionValue_IntValue).IntValue case "max_instances": @@ -89,9 +90,9 @@ func (h *Handler) Init(r *udf.InitRequest) (*udf.InitResponse, error) { init.Success = false init.Error += " must supply `when` expression;" } - if h.Scale == "" { + if h.By == "" { init.Success = false - init.Error += " must supply `scale` expression;" + init.Error += " must supply `by` expression;" } if h.MinInstances < 0 { init.Success = false @@ -155,10 +156,10 @@ func (h *Handler) evaluateWhen(p *udf.Point) (bool, error) { return doScale, nil } -func (h *Handler) evaluateScale(s *scaling.Service) (int64, error) { +func (h *Handler) evaluateBy(s *scaling.Service) (int64, error) { scaleContext := make(map[string]interface{}) scaleContext["current"] = s.CurrentInstances - res := gostr.Evaluate(h.Scale, scaleContext).(string) + res := gostr.Evaluate(h.By, scaleContext).(string) amount, err := strconv.ParseInt(res, 10, 64) if err != nil { return -1, fmt.Errorf("the expression `scale` should evaluate to an integer value, got %s (tipp: there is an ROUND() method)", res) @@ -186,7 +187,7 @@ func (h *Handler) Point(p *udf.Point) error { return nil } defer service.Unlock() - to, err := h.evaluateScale(service) + to, err := h.evaluateBy(service) if err != nil { return err } diff --git a/handler/lib_test.go b/handler/lib_test.go index 69fad42..7762d7d 100644 --- a/handler/lib_test.go +++ b/handler/lib_test.go @@ -123,7 +123,7 @@ func intOpt(name string, value int64) *udf.Option { var options = []*udf.Option{ strOpt("id", "abc"), strOpt("when", "cpu_usage > 8"), - strOpt("scale", "current + 1"), + strOpt("by", "current + 2"), intOpt("min_instances", 1), intOpt("max_instances", 10), strOpt("cooldown", "1m"), @@ -184,8 +184,8 @@ func TestHandler(t *testing.T) { t.Fatalf("expect to receive a point") } val := point.Point.GetFieldsInt()["scale"] - if val != 2 { - t.Fatalf("expected scale to be 2, got %d", val) + if val != 3 { + t.Fatalf("expected scale to be 3, got %d", val) } server.writePoint(udfPoint) go func() {