1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
14 "git.curoverse.com/arvados.git/sdk/go/arvados"
15 "github.com/sirupsen/logrus"
18 // RunOptions controls runtime behavior. The flags/options that belong
19 // here are the ones that are useful for interactive use. For example,
20 // "CommitTrash" is a runtime option rather than a config item because
21 // it invokes a troubleshooting feature rather than expressing how
22 // balancing is meant to be done at a given site.
24 // RunOptions fields are controlled by command line flags.
25 type RunOptions struct {
29 Logger logrus.FieldLogger
30 Dumper logrus.FieldLogger
32 // SafeRendezvousState from the most recent balance operation,
33 // or "" if unknown. If this changes from one run to the next,
34 // we need to watch out for races. See
35 // (*Balancer)ClearTrashLists.
36 SafeRendezvousState string
42 Cluster *arvados.Cluster
43 ArvClient *arvados.Client
47 Logger logrus.FieldLogger
48 Dumper logrus.FieldLogger
51 // CheckHealth implements service.Handler.
52 func (srv *Server) CheckHealth() error {
56 func (srv *Server) run() {
58 if srv.RunOptions.Once {
59 _, err = srv.runOnce()
61 err = srv.runForever(nil)
71 func (srv *Server) runOnce() (*Balancer, error) {
76 LostBlocksFile: srv.Cluster.Collections.BlobMissingReport,
79 srv.RunOptions, err = bal.Run(srv.ArvClient, srv.Cluster, srv.RunOptions)
83 // RunForever runs forever, or (for testing purposes) until the given
84 // stop channel is ready to receive.
85 func (srv *Server) runForever(stop <-chan interface{}) error {
88 ticker := time.NewTicker(time.Duration(srv.Cluster.Collections.BalancePeriod))
90 // The unbuffered channel here means we only hear SIGUSR1 if
91 // it arrives while we're waiting in select{}.
92 sigUSR1 := make(chan os.Signal)
93 signal.Notify(sigUSR1, syscall.SIGUSR1)
95 logger.Printf("starting up: will scan every %v and on SIGUSR1", srv.Cluster.Collections.BalancePeriod)
98 if !srv.RunOptions.CommitPulls && !srv.RunOptions.CommitTrash {
99 logger.Print("WARNING: Will scan periodically, but no changes will be committed.")
100 logger.Print("======= Consider using -commit-pulls and -commit-trash flags.")
103 _, err := srv.runOnce()
105 logger.Print("run failed: ", err)
107 logger.Print("run succeeded")
115 logger.Print("timer went off")
117 logger.Print("received SIGUSR1, resetting timer")
118 // Reset the timer so we don't start the N+1st
119 // run too soon after the Nth run is triggered
122 ticker = time.NewTicker(time.Duration(srv.Cluster.Collections.BalancePeriod))
124 logger.Print("starting next run")