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 CommitConfirmedFields bool
31 Logger logrus.FieldLogger
32 Dumper logrus.FieldLogger
34 // SafeRendezvousState from the most recent balance operation,
35 // or "" if unknown. If this changes from one run to the next,
36 // we need to watch out for races. See
37 // (*Balancer)ClearTrashLists.
38 SafeRendezvousState string
44 Cluster *arvados.Cluster
45 ArvClient *arvados.Client
49 Logger logrus.FieldLogger
50 Dumper logrus.FieldLogger
55 // CheckHealth implements service.Handler.
56 func (srv *Server) CheckHealth() error {
60 // Done implements service.Handler.
61 func (srv *Server) Done() <-chan struct{} {
65 func (srv *Server) run() {
67 if srv.RunOptions.Once {
68 _, err = srv.runOnce()
70 err = srv.runForever(nil)
80 func (srv *Server) runOnce() (*Balancer, error) {
86 LostBlocksFile: srv.Cluster.Collections.BlobMissingReport,
89 srv.RunOptions, err = bal.Run(srv.ArvClient, srv.Cluster, srv.RunOptions)
93 // RunForever runs forever, or (for testing purposes) until the given
94 // stop channel is ready to receive.
95 func (srv *Server) runForever(stop <-chan interface{}) error {
98 ticker := time.NewTicker(time.Duration(srv.Cluster.Collections.BalancePeriod))
100 // The unbuffered channel here means we only hear SIGUSR1 if
101 // it arrives while we're waiting in select{}.
102 sigUSR1 := make(chan os.Signal)
103 signal.Notify(sigUSR1, syscall.SIGUSR1)
105 logger.Printf("starting up: will scan every %v and on SIGUSR1", srv.Cluster.Collections.BalancePeriod)
108 if !srv.RunOptions.CommitPulls && !srv.RunOptions.CommitTrash {
109 logger.Print("WARNING: Will scan periodically, but no changes will be committed.")
110 logger.Print("======= Consider using -commit-pulls and -commit-trash flags.")
113 _, err := srv.runOnce()
115 logger.Print("run failed: ", err)
117 logger.Print("run succeeded")
125 logger.Print("timer went off")
127 logger.Print("received SIGUSR1, resetting timer")
128 // Reset the timer so we don't start the N+1st
129 // run too soon after the Nth run is triggered
132 ticker = time.NewTicker(time.Duration(srv.Cluster.Collections.BalancePeriod))
134 logger.Print("starting next run")