1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
17 "git.curoverse.com/arvados.git/sdk/go/arvados"
18 "git.curoverse.com/arvados.git/sdk/go/config"
19 arvadosVersion "git.curoverse.com/arvados.git/sdk/go/version"
22 const defaultConfigPath = "/etc/arvados/keep-balance/keep-balance.yml"
24 // Config specifies site configuration, like API credentials and the
25 // choice of which servers are to be balanced.
27 // Config is loaded from a JSON config file (see usage()).
29 // Arvados API endpoint and credentials.
32 // List of service types (e.g., "disk") to balance.
33 KeepServiceTypes []string
35 KeepServiceList arvados.KeepServiceList
38 RunPeriod arvados.Duration
40 // Number of collections to request in each API call
41 CollectionBatchSize int
43 // Max collections to buffer in memory (bigger values consume
44 // more memory, but can reduce store-and-forward latency when
49 // RunOptions controls runtime behavior. The flags/options that belong
50 // here are the ones that are useful for interactive use. For example,
51 // "CommitTrash" is a runtime option rather than a config item because
52 // it invokes a troubleshooting feature rather than expressing how
53 // balancing is meant to be done at a given site.
55 // RunOptions fields are controlled by command line flags.
56 type RunOptions struct {
63 // SafeRendezvousState from the most recent balance operation,
64 // or "" if unknown. If this changes from one run to the next,
65 // we need to watch out for races. See
66 // (*Balancer)ClearTrashLists.
67 SafeRendezvousState string
70 var debugf = func(string, ...interface{}) {}
74 var runOptions RunOptions
76 configPath := flag.String("config", defaultConfigPath,
77 "`path` of JSON or YAML configuration file")
78 serviceListPath := flag.String("config.KeepServiceList", "",
79 "`path` of JSON or YAML file with list of keep services to balance, as given by \"arv keep_service list\" "+
80 "(default: config[\"KeepServiceList\"], or if none given, get all available services and filter by config[\"KeepServiceTypes\"])")
81 flag.BoolVar(&runOptions.Once, "once", false,
82 "balance once and then exit")
83 flag.BoolVar(&runOptions.CommitPulls, "commit-pulls", false,
84 "send pull requests (make more replicas of blocks that are underreplicated or are not in optimal rendezvous probe order)")
85 flag.BoolVar(&runOptions.CommitTrash, "commit-trash", false,
86 "send trash requests (delete unreferenced old blocks, and excess replicas of overreplicated blocks)")
87 dumpConfig := flag.Bool("dump-config", false, "write current configuration to stdout and exit")
88 dumpFlag := flag.Bool("dump", false, "dump details for each block to stdout")
89 debugFlag := flag.Bool("debug", false, "enable debug messages")
90 getVersion := flag.Bool("version", false, "Print version information and exit.")
94 // Print version information if requested
96 fmt.Printf("Version: %s\n", arvadosVersion.GetVersion())
100 mustReadConfig(&cfg, *configPath)
101 if *serviceListPath != "" {
102 mustReadConfig(&cfg.KeepServiceList, *serviceListPath)
106 log.Fatal(config.DumpAndExit(cfg))
109 log.Printf("keep-balance %q started", arvadosVersion.GetVersion())
113 if j, err := json.Marshal(cfg); err != nil {
116 log.Printf("config is %s", j)
120 runOptions.Dumper = log.New(os.Stdout, "", log.LstdFlags)
122 err := CheckConfig(cfg, runOptions)
125 } else if runOptions.Once {
126 _, err = (&Balancer{}).Run(cfg, runOptions)
128 err = RunForever(cfg, runOptions, nil)
135 func mustReadConfig(dst interface{}, path string) {
136 if err := config.LoadFile(dst, path); err != nil {
141 // RunForever runs forever, or (for testing purposes) until the given
142 // stop channel is ready to receive.
143 func RunForever(config Config, runOptions RunOptions, stop <-chan interface{}) error {
144 if runOptions.Logger == nil {
145 runOptions.Logger = log.New(os.Stderr, "", log.LstdFlags)
147 logger := runOptions.Logger
149 ticker := time.NewTicker(time.Duration(config.RunPeriod))
151 // The unbuffered channel here means we only hear SIGUSR1 if
152 // it arrives while we're waiting in select{}.
153 sigUSR1 := make(chan os.Signal)
154 signal.Notify(sigUSR1, syscall.SIGUSR1)
156 logger.Printf("starting up: will scan every %v and on SIGUSR1", config.RunPeriod)
159 if !runOptions.CommitPulls && !runOptions.CommitTrash {
160 logger.Print("WARNING: Will scan periodically, but no changes will be committed.")
161 logger.Print("======= Consider using -commit-pulls and -commit-trash flags.")
166 runOptions, err = bal.Run(config, runOptions)
168 logger.Print("run failed: ", err)
170 logger.Print("run succeeded")
178 logger.Print("timer went off")
180 logger.Print("received SIGUSR1, resetting timer")
181 // Reset the timer so we don't start the N+1st
182 // run too soon after the Nth run is triggered
185 ticker = time.NewTicker(time.Duration(config.RunPeriod))
187 logger.Print("starting next run")