1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
16 "git.curoverse.com/arvados.git/sdk/go/arvados"
17 "git.curoverse.com/arvados.git/sdk/go/config"
20 const defaultConfigPath = "/etc/arvados/keep-balance/keep-balance.yml"
22 // Config specifies site configuration, like API credentials and the
23 // choice of which servers are to be balanced.
25 // Config is loaded from a JSON config file (see usage()).
27 // Arvados API endpoint and credentials.
30 // List of service types (e.g., "disk") to balance.
31 KeepServiceTypes []string
33 KeepServiceList arvados.KeepServiceList
36 RunPeriod arvados.Duration
38 // Number of collections to request in each API call
39 CollectionBatchSize int
41 // Max collections to buffer in memory (bigger values consume
42 // more memory, but can reduce store-and-forward latency when
47 // RunOptions controls runtime behavior. The flags/options that belong
48 // here are the ones that are useful for interactive use. For example,
49 // "CommitTrash" is a runtime option rather than a config item because
50 // it invokes a troubleshooting feature rather than expressing how
51 // balancing is meant to be done at a given site.
53 // RunOptions fields are controlled by command line flags.
54 type RunOptions struct {
61 // SafeRendezvousState from the most recent balance operation,
62 // or "" if unknown. If this changes from one run to the next,
63 // we need to watch out for races. See
64 // (*Balancer)ClearTrashLists.
65 SafeRendezvousState string
68 var debugf = func(string, ...interface{}) {}
72 var runOptions RunOptions
74 configPath := flag.String("config", defaultConfigPath,
75 "`path` of JSON or YAML configuration file")
76 serviceListPath := flag.String("config.KeepServiceList", "",
77 "`path` of JSON or YAML file with list of keep services to balance, as given by \"arv keep_service list\" "+
78 "(default: config[\"KeepServiceList\"], or if none given, get all available services and filter by config[\"KeepServiceTypes\"])")
79 flag.BoolVar(&runOptions.Once, "once", false,
80 "balance once and then exit")
81 flag.BoolVar(&runOptions.CommitPulls, "commit-pulls", false,
82 "send pull requests (make more replicas of blocks that are underreplicated or are not in optimal rendezvous probe order)")
83 flag.BoolVar(&runOptions.CommitTrash, "commit-trash", false,
84 "send trash requests (delete unreferenced old blocks, and excess replicas of overreplicated blocks)")
85 dumpConfig := flag.Bool("dump-config", false, "write current configuration to stdout and exit")
86 dumpFlag := flag.Bool("dump", false, "dump details for each block to stdout")
87 debugFlag := flag.Bool("debug", false, "enable debug messages")
91 mustReadConfig(&cfg, *configPath)
92 if *serviceListPath != "" {
93 mustReadConfig(&cfg.KeepServiceList, *serviceListPath)
97 log.Fatal(config.DumpAndExit(cfg))
102 if j, err := json.Marshal(cfg); err != nil {
105 log.Printf("config is %s", j)
109 runOptions.Dumper = log.New(os.Stdout, "", log.LstdFlags)
111 err := CheckConfig(cfg, runOptions)
114 } else if runOptions.Once {
115 _, err = (&Balancer{}).Run(cfg, runOptions)
117 err = RunForever(cfg, runOptions, nil)
124 func mustReadConfig(dst interface{}, path string) {
125 if err := config.LoadFile(dst, path); err != nil {
130 // RunForever runs forever, or (for testing purposes) until the given
131 // stop channel is ready to receive.
132 func RunForever(config Config, runOptions RunOptions, stop <-chan interface{}) error {
133 if runOptions.Logger == nil {
134 runOptions.Logger = log.New(os.Stderr, "", log.LstdFlags)
136 logger := runOptions.Logger
138 ticker := time.NewTicker(time.Duration(config.RunPeriod))
140 // The unbuffered channel here means we only hear SIGUSR1 if
141 // it arrives while we're waiting in select{}.
142 sigUSR1 := make(chan os.Signal)
143 signal.Notify(sigUSR1, syscall.SIGUSR1)
145 logger.Printf("starting up: will scan every %v and on SIGUSR1", config.RunPeriod)
148 if !runOptions.CommitPulls && !runOptions.CommitTrash {
149 logger.Print("WARNING: Will scan periodically, but no changes will be committed.")
150 logger.Print("======= Consider using -commit-pulls and -commit-trash flags.")
155 runOptions, err = bal.Run(config, runOptions)
157 logger.Print("run failed: ", err)
159 logger.Print("run succeeded")
167 logger.Print("timer went off")
169 logger.Print("received SIGUSR1, resetting timer")
170 // Reset the timer so we don't start the N+1st
171 // run too soon after the Nth run is triggered
174 ticker = time.NewTicker(time.Duration(config.RunPeriod))
176 logger.Print("starting next run")