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/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 // Done implements service.Handler.
57 func (srv *Server) Done() <-chan struct{} {
61 func (srv *Server) run() {
63 if srv.RunOptions.Once {
64 _, err = srv.runOnce()
66 err = srv.runForever(nil)
76 func (srv *Server) runOnce() (*Balancer, error) {
81 LostBlocksFile: srv.Cluster.Collections.BlobMissingReport,
84 srv.RunOptions, err = bal.Run(srv.ArvClient, srv.Cluster, srv.RunOptions)
88 // RunForever runs forever, or (for testing purposes) until the given
89 // stop channel is ready to receive.
90 func (srv *Server) runForever(stop <-chan interface{}) error {
93 ticker := time.NewTicker(time.Duration(srv.Cluster.Collections.BalancePeriod))
95 // The unbuffered channel here means we only hear SIGUSR1 if
96 // it arrives while we're waiting in select{}.
97 sigUSR1 := make(chan os.Signal)
98 signal.Notify(sigUSR1, syscall.SIGUSR1)
100 logger.Printf("starting up: will scan every %v and on SIGUSR1", srv.Cluster.Collections.BalancePeriod)
103 if !srv.RunOptions.CommitPulls && !srv.RunOptions.CommitTrash {
104 logger.Print("WARNING: Will scan periodically, but no changes will be committed.")
105 logger.Print("======= Consider using -commit-pulls and -commit-trash flags.")
108 _, err := srv.runOnce()
110 logger.Print("run failed: ", err)
112 logger.Print("run succeeded")
120 logger.Print("timer went off")
122 logger.Print("received SIGUSR1, resetting timer")
123 // Reset the timer so we don't start the N+1st
124 // run too soon after the Nth run is triggered
127 ticker = time.NewTicker(time.Duration(srv.Cluster.Collections.BalancePeriod))
129 logger.Print("starting next run")