From cb9a1ca9a5bdadc54600bb4599f4013c9fdcf503 Mon Sep 17 00:00:00 2001 From: rewby Date: Mon, 28 Aug 2023 22:20:31 +0200 Subject: [PATCH] Rewrote the dispatcher to be better TM --- .dockerignore | 4 +- .gitignore | 2 + Dockerfile | 32 ++++-- cmd/dispatcher/config.go | 36 ++++++ cmd/dispatcher/main.go | 80 ++++++++++++++ cmd/dispatcher/state.go | 233 +++++++++++++++++++++++++++++++++++++++ compose-dev.yaml | 12 -- config.example.py | 22 ---- config.example.yaml | 8 ++ go.mod | 50 +++++++++ go.sum | 137 +++++++++++++++++++++++ main.py | 38 ------- requirements.txt | 3 - 13 files changed, 571 insertions(+), 86 deletions(-) create mode 100644 cmd/dispatcher/config.go create mode 100644 cmd/dispatcher/main.go create mode 100644 cmd/dispatcher/state.go delete mode 100644 compose-dev.yaml delete mode 100644 config.example.py create mode 100644 config.example.yaml create mode 100644 go.mod create mode 100644 go.sum delete mode 100644 main.py delete mode 100644 requirements.txt diff --git a/.dockerignore b/.dockerignore index cbbacc3..adef977 100644 --- a/.dockerignore +++ b/.dockerignore @@ -330,4 +330,6 @@ pip-selfcheck.json # End of https://www.toptal.com/developers/gitignore/api/pycharm+all,intellij+all,python,virtualenv /venv -/config.py \ No newline at end of file +/config.py +/config.yaml +/dispatcher \ No newline at end of file diff --git a/.gitignore b/.gitignore index 6ad176c..adef977 100644 --- a/.gitignore +++ b/.gitignore @@ -331,3 +331,5 @@ pip-selfcheck.json # End of https://www.toptal.com/developers/gitignore/api/pycharm+all,intellij+all,python,virtualenv /venv /config.py +/config.yaml +/dispatcher \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index b9bf5a1..f1e56ff 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,14 +1,26 @@ -FROM python:3.11-bookworm +FROM golang:1.21 AS build-stage -RUN apt update && apt install -y tini ca-certificates && \ - apt-get clean && \ - rm -rf /var/lib/apt/lists/* +WORKDIR /app -WORKDIR /dispatcher -COPY requirements.txt . -RUN pip install -r requirements.txt +COPY go.mod go.sum ./ +RUN go mod download -COPY main.py . +COPY *.go cmd ./ -ENV DISPATCHER_SETTINGS=/dispatcher/config.py -ENTRYPOINT [ "/usr/bin/tini-static", "--", "gunicorn", "-w", "4", "--access-logfile=-", "main:app" ] +RUN CGO_ENABLED=0 GOOS=linux go build -o /dispatcher ./... + +# Run the tests in the container +FROM build-stage AS run-test-stage +RUN go test -v ./... + +# Deploy the application binary into a lean image +FROM gcr.io/distroless/base-debian12 AS build-release-stage + +WORKDIR / + +COPY --from=build-stage /dispatcher /dispatcher + +USER nonroot:nonroot +ENV GIN_MODE=release + +ENTRYPOINT ["/dispatcher"] \ No newline at end of file diff --git a/cmd/dispatcher/config.go b/cmd/dispatcher/config.go new file mode 100644 index 0000000..42e3164 --- /dev/null +++ b/cmd/dispatcher/config.go @@ -0,0 +1,36 @@ +package main + +import ( + "github.com/inhies/go-bytesize" + "gopkg.in/yaml.v3" + "os" +) + +type Config struct { + Listen string `yaml:"listen"` + PromAddr string `yaml:"prometheus_address"` + Targets []ServerSpec `yaml:"targets"` +} + +type ServerSpec struct { + Id string `yaml:"id"` + Url string `yaml:"url"` + MinimumFreeSpace bytesize.ByteSize `yaml:"free_space_min"` + JobName string `yaml:"prom_job_name"` +} + +func NewConfigFromFile(fn string) (Config, error) { + var c Config + yamlFile, err := os.ReadFile(fn) + if err != nil { + return c, err + } + err = yaml.Unmarshal(yamlFile, &c) + if err != nil { + return c, err + } + + log.Infof("Loaded config: %v", c) + + return c, nil +} diff --git a/cmd/dispatcher/main.go b/cmd/dispatcher/main.go new file mode 100644 index 0000000..df8cb49 --- /dev/null +++ b/cmd/dispatcher/main.go @@ -0,0 +1,80 @@ +package main + +import ( + "github.com/Depado/ginprom" + "github.com/gin-gonic/gin" + "github.com/mroth/weightedrand/v2" + "github.com/sirupsen/logrus" + ginlogrus "github.com/toorop/gin-logrus" + "net/http" + "strconv" +) + +var log = logrus.New() + +func assignTarget(c *gin.Context) { + targets := state.GetTargets() + + string_size_hint := c.Query("SIZE_HINT") + size_hint, err := strconv.ParseInt(string_size_hint, 10, 64) + if err != nil { + c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{ + "reason": "Invalid size hint", + }) + return + } + + var choices []weightedrand.Choice[ServerSpec, int] + + for _, target := range targets { + expectedFreeSpace := target.FreeSpace - size_hint + if expectedFreeSpace > int64(target.Target.MinimumFreeSpace) && target.Weight > 0 { + choices = append(choices, weightedrand.NewChoice(target.Target, target.Weight)) + } + } + + if len(choices) == 0 { + c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{ + "reason": "No more targets available", + }) + } + + chooser, _ := weightedrand.NewChooser(choices...) + result := chooser.Pick() + + c.JSON(http.StatusOK, gin.H{ + "url": result.Url, + }) +} + +func setupRouter() *gin.Engine { + r := gin.New() + r.Use(ginlogrus.Logger(log), gin.Recovery()) + p := ginprom.New( + ginprom.Engine(r), + ginprom.Subsystem("gin"), + ginprom.Path("/metrics"), + ) + r.Use(p.Instrument()) + + r.GET("/ping", func(c *gin.Context) { + c.JSON(http.StatusOK, gin.H{ + "message": "pong", + }) + }) + + r.GET("/offload_target", assignTarget) + + return r +} + +func main() { + initState() + + go state.UpdateThread() + + router := setupRouter() + + err := router.Run(state.GetListenAddr()) + log.WithError(err).Fatal("Failed to run router") +} diff --git a/cmd/dispatcher/state.go b/cmd/dispatcher/state.go new file mode 100644 index 0000000..a623471 --- /dev/null +++ b/cmd/dispatcher/state.go @@ -0,0 +1,233 @@ +package main + +import ( + "context" + "fmt" + "github.com/prometheus/client_golang/api" + v1 "github.com/prometheus/client_golang/api/prometheus/v1" + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" + "github.com/prometheus/common/model" + "sync" + "time" +) + +type State struct { + config Config + weights map[string]int + spaces map[string]int64 + inflights map[string]int64 + lock sync.RWMutex +} + +type TargetState struct { + Target ServerSpec + Weight int + FreeSpace int64 + Inflight int64 +} + +var state *State + +func initState() { + c, err := NewConfigFromFile("config.yaml") + if err != nil { + log.WithError(err).Fatal("Unable to read config") + } + + state = &State{ + config: c, + } +} + +func (state *State) GetListenAddr() string { + state.lock.RLock() + defer state.lock.RUnlock() + return state.config.Listen +} + +func (state *State) getPromAddr() string { + state.lock.RLock() + defer state.lock.RUnlock() + return state.config.PromAddr +} + +func (state *State) MakeClient() (v1.API, error) { + addr := state.getPromAddr() + log.WithField("prometheus", addr).Info("Making new prom client...") + client, err := api.NewClient(api.Config{ + Address: addr, + }) + + if err != nil { + return nil, err + } + + c := v1.NewAPI(client) + + return c, nil +} + +func (state *State) GetTargets() []TargetState { + state.lock.RLock() + defer state.lock.RUnlock() + + var states []TargetState + + for _, targetSpec := range state.config.Targets { + w := 0 + + if val, ok := state.weights[targetSpec.Id]; ok { + w = val + } + + space := int64(0) + + if val, ok := state.spaces[targetSpec.Id]; ok { + space = val + } + + inflights := int64(0) + + if val, ok := state.inflights[targetSpec.Id]; ok { + inflights = val + } + + states = append(states, TargetState{ + Target: targetSpec, + Weight: w, + FreeSpace: space, + Inflight: inflights, + }) + } + + return states +} + +var ( + weightsGauge = promauto.NewGaugeVec(prometheus.GaugeOpts{ + Name: "offload_dispatcher_target_weights", + Help: "Weights of the targets in selection", + }, []string{"target"}) +) + +func (state *State) updateTargets(targets []TargetState) { + + newWeights := make(map[string]int) + newInflights := make(map[string]int64) + newSpaces := make(map[string]int64) + + for _, t := range targets { + newWeights[t.Target.Id] = t.Weight + newInflights[t.Target.Id] = t.Inflight + newSpaces[t.Target.Id] = t.FreeSpace + weightsGauge.WithLabelValues(t.Target.Id).Set(float64(t.Weight)) + } + + state.lock.Lock() + defer state.lock.Unlock() + + state.weights = newWeights + state.inflights = newInflights + state.spaces = newSpaces +} + +func (state *State) UpdateThread() { + log.Info("Starting update thread...") + v1api, err := state.MakeClient() + if err != nil { + log.WithError(err).Fatal("Unable to make prom client") + } + for { + log.Info("Starting update run...") + ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second) + + targets := state.GetTargets() + + for i, target := range targets { + { + result, warnings, err := v1api.Query(ctx, fmt.Sprintf("minio_cluster_capacity_usable_free_bytes{job=\"%s\"}", target.Target.JobName), time.Now(), v1.WithTimeout(5*time.Second)) + if err != nil { + log.WithField("target", target.Target.Id).WithError(err).Error("Error querying Prometheus for free space") + continue + } + if len(warnings) > 0 { + log.WithField("target", target.Target.Id).Warnf("Free space warnings: %v\n", warnings) + } + + resultVec := result.(model.Vector) + if resultVec.Len() > 0 { + resultVal := resultVec[0] + log.WithField("target", target.Target.Id).Debugf("Computed free space: %v", resultVal.Value) + targets[i].FreeSpace = int64(resultVal.Value) + } + } + + { + result, warnings, err := v1api.Query(ctx, fmt.Sprintf("sum(minio_s3_requests_inflight_total{job=\"%s\"})", target.Target.JobName), time.Now(), v1.WithTimeout(5*time.Second)) + if err != nil { + log.WithField("target", target.Target.Id).WithError(err).Error("Error querying Prometheus for inflights") + continue + } + if len(warnings) > 0 { + log.WithField("target", target.Target.Id).Warnf("Inflight warnings: %v\n", warnings) + } + + resultVec := result.(model.Vector) + if resultVec.Len() > 0 { + resultVal := resultVec[0] + log.WithField("target", target.Target.Id).Debugf("Computed inflight: %v", resultVal.Value) + targets[i].Inflight = int64(resultVal.Value) + } + } + + } + + cancel() + + inflightsSum := int64(0) + inflightsCount := 0 + for _, t := range targets { + inflightsSum = inflightsSum + t.Inflight + inflightsCount = inflightsCount + 1 + } + averageInflights := float64(inflightsSum) / float64(inflightsCount) + log.Debugf("Average inflights: %v", averageInflights) + + anyNonZero := false + for i, t := range targets { + if t.FreeSpace > int64(t.Target.MinimumFreeSpace) { + targets[i].Weight = int(int64(averageInflights) - int64(t.Inflight)) + + // Clamp to 0 + if targets[i].Weight < 0 { + targets[i].Weight = 0 + } + + // Record if we found anything nonzero + if targets[i].Weight > 0 { + anyNonZero = true + } + } else { + targets[i].Weight = 0 + } + + } + // Failsafe, if nobody is eligible, everybody is (that has space) + if !anyNonZero { + for i, t := range targets { + if t.FreeSpace > int64(t.Target.MinimumFreeSpace) { + targets[i].Weight = 1 + } + } + } + + for _, t := range targets { + log.WithField("target", t.Target.Id).Debugf("Computed weight: %v", t.Weight) + } + + state.updateTargets(targets) + + time.Sleep(15 * time.Second) + } +} diff --git a/compose-dev.yaml b/compose-dev.yaml deleted file mode 100644 index 25f3be5..0000000 --- a/compose-dev.yaml +++ /dev/null @@ -1,12 +0,0 @@ -version: "3.7" -services: - dispatcher: - build: - context: . - volumes: - - './config.py:/dispatcher/config.py:ro' - ports: - - '127.0.0.1:8000:8000' - command: - - '-b' - - '0.0.0.0' diff --git a/config.example.py b/config.example.py deleted file mode 100644 index d9768b5..0000000 --- a/config.example.py +++ /dev/null @@ -1,22 +0,0 @@ -PROMETHEUS = { - "url": "http://127.0.0.1:9090", - "disable_ssl": True, -} - -# URL FORMATS: -# -# Backend # TLS # Bucket/item # URL Format -########################################### -# Minio S3 # N # Y # minio+http://user:password@host[:port] -# Minio S3 # Y # Y # minio+https://user:password@host[:port] - - -TARGETS = [ - { - "url": "minio+https://user:password@minio", - "free_space": { - "query": "minio_cluster_capacity_usable_free_bytes{job=\"somejob\"}", - "minimum": (1024 * 1024 * 1024 * 500), # 500 gigabytes - } - }, -] diff --git a/config.example.yaml b/config.example.yaml new file mode 100644 index 0000000..e483064 --- /dev/null +++ b/config.example.yaml @@ -0,0 +1,8 @@ +--- +listen: "127.0.0.1:5000" +prometheus_address: "http://localhost:9090" +targets: + - id: "some-target" + prom_job_name: "some-job" + url: "minio+https://user:password@host" + free_space_min: "500gb" diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..34fd4c4 --- /dev/null +++ b/go.mod @@ -0,0 +1,50 @@ +module dispatcher + +go 1.21 + +require ( + github.com/Depado/ginprom v1.7.11 + github.com/gin-gonic/gin v1.9.1 + github.com/inhies/go-bytesize v0.0.0-20220417184213-4913239db9cf + github.com/mroth/weightedrand/v2 v2.1.0 + github.com/prometheus/client_golang v1.16.0 + github.com/prometheus/common v0.42.0 + github.com/sirupsen/logrus v1.9.3 + github.com/toorop/gin-logrus v0.0.0-20210225092905-2c785434f26f + gopkg.in/yaml.v3 v3.0.1 +) + +require ( + github.com/beorn7/perks v1.0.1 // indirect + github.com/bytedance/sonic v1.9.1 // indirect + github.com/cespare/xxhash/v2 v2.2.0 // indirect + github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect + github.com/gabriel-vasile/mimetype v1.4.2 // indirect + github.com/gin-contrib/sse v0.1.0 // indirect + github.com/go-playground/locales v0.14.1 // indirect + github.com/go-playground/universal-translator v0.18.1 // indirect + github.com/go-playground/validator/v10 v10.14.0 // indirect + github.com/goccy/go-json v0.10.2 // indirect + github.com/golang/protobuf v1.5.3 // indirect + github.com/json-iterator/go v1.1.12 // indirect + github.com/klauspost/cpuid/v2 v2.2.4 // indirect + github.com/kr/text v0.2.0 // indirect + github.com/leodido/go-urn v1.2.4 // indirect + github.com/mattn/go-isatty v0.0.19 // indirect + github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/pelletier/go-toml/v2 v2.0.8 // indirect + github.com/prometheus/client_model v0.3.0 // indirect + github.com/prometheus/procfs v0.10.1 // indirect + github.com/rogpeppe/go-internal v1.11.0 // indirect + github.com/twitchyliquid64/golang-asm v0.15.1 // indirect + github.com/ugorji/go/codec v1.2.11 // indirect + golang.org/x/arch v0.3.0 // indirect + golang.org/x/crypto v0.9.0 // indirect + golang.org/x/net v0.10.0 // indirect + golang.org/x/oauth2 v0.7.0 // indirect + golang.org/x/sys v0.8.0 // indirect + golang.org/x/text v0.9.0 // indirect + google.golang.org/protobuf v1.30.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..968abea --- /dev/null +++ b/go.sum @@ -0,0 +1,137 @@ +github.com/Depado/ginprom v1.7.11 h1:qOhxW/NJZkNkkG4TQrzAZklX8SUTjTfLA73zIUNIpww= +github.com/Depado/ginprom v1.7.11/go.mod h1:49mxL3NTQwDrhpDbY4V1mAIB3us9B+b2hP1+ph+Sla8= +github.com/appleboy/gofight/v2 v2.1.2 h1:VOy3jow4vIK8BRQJoC/I9muxyYlJ2yb9ht2hZoS3rf4= +github.com/appleboy/gofight/v2 v2.1.2/go.mod h1:frW+U1QZEdDgixycTj4CygQ48yLTUhplt43+Wczp3rw= +github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= +github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= +github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM= +github.com/bytedance/sonic v1.9.1 h1:6iJ6NqdoxCDr6mbY8h18oSO+cShGSMRGCEo7F2h0x8s= +github.com/bytedance/sonic v1.9.1/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U= +github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= +github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY= +github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 h1:qSGYFH7+jGhDF8vLC+iwCD4WpbV1EBDSzWkJODFLams= +github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk= +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU= +github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA= +github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= +github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= +github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg= +github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SUcPTeU= +github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= +github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= +github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= +github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= +github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= +github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= +github.com/go-playground/validator/v10 v10.14.0 h1:vgvQWe3XCz3gIeFDm/HnTIbj6UGmg/+t63MyGU2n5js= +github.com/go-playground/validator/v10 v10.14.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU= +github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= +github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.5/go.mod h1:6O5/vntMXwX2lRkT1hjjk0nAC1IDOTvTlVgjlRvqsdk= +github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= +github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg= +github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/inhies/go-bytesize v0.0.0-20220417184213-4913239db9cf h1:FtEj8sfIcaaBfAKrE1Cwb61YDtYq9JxChK1c7AKce7s= +github.com/inhies/go-bytesize v0.0.0-20220417184213-4913239db9cf/go.mod h1:yrqSXGoD/4EKfF26AOGzscPOgTTJcyAwM2rpixWT+t4= +github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2EA= +github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4= +github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= +github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= +github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= +github.com/klauspost/cpuid/v2 v2.2.4 h1:acbojRNwl3o09bUq+yDCtZFc1aiwaAAxtcn8YkZXnvk= +github.com/klauspost/cpuid/v2 v2.2.4/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8tyFgg4nqhuY= +github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q= +github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4= +github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= +github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo= +github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= +github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= +github.com/mroth/weightedrand/v2 v2.1.0 h1:o1ascnB1CIVzsqlfArQQjeMy1U0NcIbBO5rfd5E/OeU= +github.com/mroth/weightedrand/v2 v2.1.0/go.mod h1:f2faGsfOGOwc1p94wzHKKZyTpcJUW7OJ/9U4yfiNAOU= +github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f h1:KUppIJq7/+SVif2QVs3tOP0zanoHgBEVAwHxUSIzRqU= +github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= +github.com/pelletier/go-toml/v2 v2.0.8 h1:0ctb6s9mE31h0/lhu+J6OPmVeDxJn+kYnJc2jZR9tGQ= +github.com/pelletier/go-toml/v2 v2.0.8/go.mod h1:vuYfssBdrU2XDZ9bYydBu6t+6a6PYNcZljzZR9VXg+4= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/prometheus/client_golang v1.16.0 h1:yk/hx9hDbrGHovbci4BY+pRMfSuuat626eFsHb7tmT8= +github.com/prometheus/client_golang v1.16.0/go.mod h1:Zsulrv/L9oM40tJ7T815tM89lFEugiJ9HzIqaAx4LKc= +github.com/prometheus/client_model v0.3.0 h1:UBgGFHqYdG/TPFD1B1ogZywDqEkwp3fBMvqdiQ7Xew4= +github.com/prometheus/client_model v0.3.0/go.mod h1:LDGWKZIo7rky3hgvBe+caln+Dr3dPggB5dvjtD7w9+w= +github.com/prometheus/common v0.42.0 h1:EKsfXEYo4JpWMHH5cg+KOUWeuJSov1Id8zGR8eeI1YM= +github.com/prometheus/common v0.42.0/go.mod h1:xBwqVerjNdUDjgODMpudtOMwlOwf2SaTr1yjz4b7Zbc= +github.com/prometheus/procfs v0.10.1 h1:kYK1Va/YMlutzCGazswoHKo//tZVlFpKYh+PymziUAg= +github.com/prometheus/procfs v0.10.1/go.mod h1:nwNm2aOCAYw8uTR/9bWRREkZFxAUcWzPHWJq+XBB/FM= +github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= +github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA= +github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= +github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY= +github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/toorop/gin-logrus v0.0.0-20210225092905-2c785434f26f h1:oqdnd6OGlOUu1InG37hWcCB3a+Jy3fwjylyVboaNMwY= +github.com/toorop/gin-logrus v0.0.0-20210225092905-2c785434f26f/go.mod h1:X3Dd1SB8Gt1V968NTzpKFjMM6O8ccta2NPC6MprOxZQ= +github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= +github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= +github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU= +github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= +golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= +golang.org/x/arch v0.3.0 h1:02VY4/ZcO/gBOH6PUaoiptASxtXU10jazRCP865E97k= +golang.org/x/arch v0.3.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= +golang.org/x/crypto v0.9.0 h1:LF6fAI+IutBocDJ2OT0Q1g8plpYljMZ4+lty+dsqw3g= +golang.org/x/crypto v0.9.0/go.mod h1:yrmDGqONDYtNj3tH8X9dzUun2m2lzPa9ngI6/RUPGR0= +golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M= +golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= +golang.org/x/oauth2 v0.7.0 h1:qe6s0zUXlPX80/dITx3440hWZ7GwMwgDDyrSGTPJG/g= +golang.org/x/oauth2 v0.7.0/go.mod h1:hPLQkd9LyjfXTiRohC/41GhcFqxisoUQ99sCUOHO9x4= +golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU= +golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE= +golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c= +google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc= +google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= +google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= +google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng= +google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4= diff --git a/main.py b/main.py deleted file mode 100644 index b467c03..0000000 --- a/main.py +++ /dev/null @@ -1,38 +0,0 @@ -import copy - -from flask import Flask, request -from prometheus_api_client import PrometheusConnect -import random - -app = Flask(__name__) -app.config.from_envvar('DISPATCHER_SETTINGS') - - -def get_prom_client(): - return PrometheusConnect(**app.config["PROMETHEUS"]) - - -@app.route("/") -def hello_world(): - return "Target offload dispatcher" - - -prom = get_prom_client() - - -@app.route("/offload_target") -def offload_target(): - expected_size = int(request.args.get("SIZE_HINT", 20*1024*1024*1024)) - weights = [x.get("weight", 100) for x in app.config["TARGETS"]] - target = random.choices(app.config["TARGETS"], weights=weights)[0] - app.logger.info(f"Considering target {target['url']}...") - result = prom.custom_query(query=target["free_space"]["query"]) - if len(result) != 1: - return "Unable to allocate target. Can't get space info.", 500 - result = result[0] - target_free_space = int(result['value'][1]) - expected_free_space = target_free_space - expected_size - app.logger.info(f"Available space: {target_free_space} Expected size: {expected_size}") - if expected_free_space < target["free_space"].get("minimum", 1024 * 1024 * 1024 * 500): - return "Unable to allocate target. Not enough space on selected machine.", 507 - return {"url": target["url"]} diff --git a/requirements.txt b/requirements.txt deleted file mode 100644 index 53038c7..0000000 --- a/requirements.txt +++ /dev/null @@ -1,3 +0,0 @@ -flask~=2.3.3 -gunicorn -prometheus-api-client