1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
15 "git.curoverse.com/arvados.git/sdk/go/arvados"
16 "git.curoverse.com/arvados.git/sdk/go/auth"
17 "github.com/julienschmidt/httprouter"
18 "github.com/prometheus/client_golang/prometheus/promhttp"
19 "github.com/sirupsen/logrus"
22 // RunOptions controls runtime behavior. The flags/options that belong
23 // here are the ones that are useful for interactive use. For example,
24 // "CommitTrash" is a runtime option rather than a config item because
25 // it invokes a troubleshooting feature rather than expressing how
26 // balancing is meant to be done at a given site.
28 // RunOptions fields are controlled by command line flags.
29 type RunOptions struct {
33 Logger logrus.FieldLogger
34 Dumper logrus.FieldLogger
36 // SafeRendezvousState from the most recent balance operation,
37 // or "" if unknown. If this changes from one run to the next,
38 // we need to watch out for races. See
39 // (*Balancer)ClearTrashLists.
40 SafeRendezvousState string
44 Cluster *arvados.Cluster
45 ArvClient *arvados.Client
49 httpHandler http.Handler
52 Logger logrus.FieldLogger
53 Dumper logrus.FieldLogger
56 // ServeHTTP implements service.Handler.
57 func (srv *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
58 srv.httpHandler.ServeHTTP(w, r)
61 // CheckHealth implements service.Handler.
62 func (srv *Server) CheckHealth() error {
66 // Start sets up and runs the balancer.
67 func (srv *Server) Start() {
68 srv.setupOnce.Do(srv.setup)
72 func (srv *Server) setup() {
73 if srv.Cluster.ManagementToken == "" {
74 srv.httpHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
75 http.Error(w, "Management API authentication is not configured", http.StatusForbidden)
78 mux := httprouter.New()
79 metricsH := promhttp.HandlerFor(srv.Metrics.reg, promhttp.HandlerOpts{
82 mux.Handler("GET", "/metrics", metricsH)
83 mux.Handler("GET", "/metrics.json", metricsH)
84 srv.httpHandler = auth.RequireLiteralToken(srv.Cluster.ManagementToken, mux)
88 func (srv *Server) run() {
90 if srv.RunOptions.Once {
91 _, err = srv.runOnce()
93 err = srv.runForever(nil)
100 func (srv *Server) runOnce() (*Balancer, error) {
104 Metrics: srv.Metrics,
105 LostBlocksFile: srv.Cluster.Collections.BlobMissingReport,
108 srv.RunOptions, err = bal.Run(srv.ArvClient, srv.Cluster, srv.RunOptions)
112 // RunForever runs forever, or (for testing purposes) until the given
113 // stop channel is ready to receive.
114 func (srv *Server) runForever(stop <-chan interface{}) error {
117 ticker := time.NewTicker(time.Duration(srv.Cluster.Collections.BalancePeriod))
119 // The unbuffered channel here means we only hear SIGUSR1 if
120 // it arrives while we're waiting in select{}.
121 sigUSR1 := make(chan os.Signal)
122 signal.Notify(sigUSR1, syscall.SIGUSR1)
124 logger.Printf("starting up: will scan every %v and on SIGUSR1", srv.Cluster.Collections.BalancePeriod)
127 if !srv.RunOptions.CommitPulls && !srv.RunOptions.CommitTrash {
128 logger.Print("WARNING: Will scan periodically, but no changes will be committed.")
129 logger.Print("======= Consider using -commit-pulls and -commit-trash flags.")
132 _, err := srv.runOnce()
134 logger.Print("run failed: ", err)
136 logger.Print("run succeeded")
144 logger.Print("timer went off")
146 logger.Print("received SIGUSR1, resetting timer")
147 // Reset the timer so we don't start the N+1st
148 // run too soon after the Nth run is triggered
151 ticker = time.NewTicker(time.Duration(srv.Cluster.Collections.BalancePeriod))
153 logger.Print("starting next run")