12 "git.curoverse.com/arvados.git/sdk/go/arvados"
13 "git.curoverse.com/arvados.git/sdk/go/config"
16 // Config specifies site configuration, like API credentials and the
17 // choice of which servers are to be balanced.
19 // Config is loaded from a JSON config file (see usage()).
21 // Arvados API endpoint and credentials.
24 // List of service types (e.g., "disk") to balance.
25 KeepServiceTypes []string
27 KeepServiceList arvados.KeepServiceList
30 RunPeriod arvados.Duration
32 // Number of collections to request in each API call
33 CollectionBatchSize int
35 // Max collections to buffer in memory (bigger values consume
36 // more memory, but can reduce store-and-forward latency when
41 // RunOptions controls runtime behavior. The flags/options that belong
42 // here are the ones that are useful for interactive use. For example,
43 // "CommitTrash" is a runtime option rather than a config item because
44 // it invokes a troubleshooting feature rather than expressing how
45 // balancing is meant to be done at a given site.
47 // RunOptions fields are controlled by command line flags.
48 type RunOptions struct {
55 // SafeRendezvousState from the most recent balance operation,
56 // or "" if unknown. If this changes from one run to the next,
57 // we need to watch out for races. See
58 // (*Balancer)ClearTrashLists.
59 SafeRendezvousState string
62 var debugf = func(string, ...interface{}) {}
66 var runOptions RunOptions
68 configPath := flag.String("config", "",
69 "`path` of JSON or YAML configuration file")
70 serviceListPath := flag.String("config.KeepServiceList", "",
71 "`path` of JSON or YAML file with list of keep services to balance, as given by \"arv keep_service list\" "+
72 "(default: config[\"KeepServiceList\"], or if none given, get all available services and filter by config[\"KeepServiceTypes\"])")
73 flag.BoolVar(&runOptions.Once, "once", false,
74 "balance once and then exit")
75 flag.BoolVar(&runOptions.CommitPulls, "commit-pulls", false,
76 "send pull requests (make more replicas of blocks that are underreplicated or are not in optimal rendezvous probe order)")
77 flag.BoolVar(&runOptions.CommitTrash, "commit-trash", false,
78 "send trash requests (delete unreferenced old blocks, and excess replicas of overreplicated blocks)")
79 dumpFlag := flag.Bool("dump", false, "dump details for each block to stdout")
80 debugFlag := flag.Bool("debug", false, "enable debug messages")
84 if *configPath == "" {
85 log.Fatal("You must specify a config file (see `keep-balance -help`)")
87 mustReadConfig(&config, *configPath)
88 if *serviceListPath != "" {
89 mustReadConfig(&config.KeepServiceList, *serviceListPath)
94 if j, err := json.Marshal(config); err != nil {
97 log.Printf("config is %s", j)
101 runOptions.Dumper = log.New(os.Stdout, "", log.LstdFlags)
103 err := CheckConfig(config, runOptions)
106 } else if runOptions.Once {
107 _, err = (&Balancer{}).Run(config, runOptions)
109 err = RunForever(config, runOptions, nil)
116 func mustReadConfig(dst interface{}, path string) {
117 if err := config.LoadFile(dst, path); err != nil {
122 // RunForever runs forever, or (for testing purposes) until the given
123 // stop channel is ready to receive.
124 func RunForever(config Config, runOptions RunOptions, stop <-chan interface{}) error {
125 if runOptions.Logger == nil {
126 runOptions.Logger = log.New(os.Stderr, "", log.LstdFlags)
128 logger := runOptions.Logger
130 ticker := time.NewTicker(time.Duration(config.RunPeriod))
132 // The unbuffered channel here means we only hear SIGUSR1 if
133 // it arrives while we're waiting in select{}.
134 sigUSR1 := make(chan os.Signal)
135 signal.Notify(sigUSR1, syscall.SIGUSR1)
137 logger.Printf("starting up: will scan every %v and on SIGUSR1", config.RunPeriod)
140 if !runOptions.CommitPulls && !runOptions.CommitTrash {
141 logger.Print("WARNING: Will scan periodically, but no changes will be committed.")
142 logger.Print("======= Consider using -commit-pulls and -commit-trash flags.")
147 runOptions, err = bal.Run(config, runOptions)
149 logger.Print("run failed: ", err)
151 logger.Print("run succeeded")
159 logger.Print("timer went off")
161 logger.Print("received SIGUSR1, resetting timer")
162 // Reset the timer so we don't start the N+1st
163 // run too soon after the Nth run is triggered
166 ticker = time.NewTicker(time.Duration(config.RunPeriod))
168 logger.Print("starting next run")