1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
14 "git.arvados.org/arvados.git/sdk/go/arvados"
15 "github.com/jmoiron/sqlx"
16 "github.com/sirupsen/logrus"
19 // RunOptions controls runtime behavior. The flags/options that belong
20 // here are the ones that are useful for interactive use. For example,
21 // "CommitTrash" is a runtime option rather than a config item because
22 // it invokes a troubleshooting feature rather than expressing how
23 // balancing is meant to be done at a given site.
25 // RunOptions fields are controlled by command line flags.
26 type RunOptions struct {
30 Logger logrus.FieldLogger
31 Dumper logrus.FieldLogger
33 // SafeRendezvousState from the most recent balance operation,
34 // or "" if unknown. If this changes from one run to the next,
35 // we need to watch out for races. See
36 // (*Balancer)ClearTrashLists.
37 SafeRendezvousState string
43 Cluster *arvados.Cluster
44 ArvClient *arvados.Client
48 Logger logrus.FieldLogger
49 Dumper logrus.FieldLogger
54 // CheckHealth implements service.Handler.
55 func (srv *Server) CheckHealth() error {
59 // Done implements service.Handler.
60 func (srv *Server) Done() <-chan struct{} {
64 func (srv *Server) run() {
66 if srv.RunOptions.Once {
67 _, err = srv.runOnce()
69 err = srv.runForever(nil)
79 func (srv *Server) runOnce() (*Balancer, error) {
85 LostBlocksFile: srv.Cluster.Collections.BlobMissingReport,
88 srv.RunOptions, err = bal.Run(srv.ArvClient, srv.Cluster, srv.RunOptions)
92 // RunForever runs forever, or (for testing purposes) until the given
93 // stop channel is ready to receive.
94 func (srv *Server) runForever(stop <-chan interface{}) error {
97 ticker := time.NewTicker(time.Duration(srv.Cluster.Collections.BalancePeriod))
99 // The unbuffered channel here means we only hear SIGUSR1 if
100 // it arrives while we're waiting in select{}.
101 sigUSR1 := make(chan os.Signal)
102 signal.Notify(sigUSR1, syscall.SIGUSR1)
104 logger.Printf("starting up: will scan every %v and on SIGUSR1", srv.Cluster.Collections.BalancePeriod)
107 if !srv.RunOptions.CommitPulls && !srv.RunOptions.CommitTrash {
108 logger.Print("WARNING: Will scan periodically, but no changes will be committed.")
109 logger.Print("======= Consider using -commit-pulls and -commit-trash flags.")
112 _, err := srv.runOnce()
114 logger.Print("run failed: ", err)
116 logger.Print("run succeeded")
124 logger.Print("timer went off")
126 logger.Print("received SIGUSR1, resetting timer")
127 // Reset the timer so we don't start the N+1st
128 // run too soon after the Nth run is triggered
131 ticker = time.NewTicker(time.Duration(srv.Cluster.Collections.BalancePeriod))
133 logger.Print("starting next run")