1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
15 "git.curoverse.com/arvados.git/lib/config"
16 "git.curoverse.com/arvados.git/lib/service"
17 "git.curoverse.com/arvados.git/sdk/go/arvados"
18 "git.curoverse.com/arvados.git/sdk/go/ctxlog"
19 "github.com/prometheus/client_golang/prometheus"
20 "github.com/sirupsen/logrus"
25 debugf = func(string, ...interface{}) {}
26 command = service.Command(arvados.ServiceNameKeepbalance, newHandler)
30 func newHandler(ctx context.Context, cluster *arvados.Cluster, token string, registry *prometheus.Registry) service.Handler {
31 if !options.Once && cluster.Collections.BalancePeriod == arvados.Duration(0) {
32 return service.ErrorHandler(ctx, cluster, fmt.Errorf("You must either run keep-balance with the -once flag, or set Collections.BalancePeriod in the config. "+
33 "If using the legacy keep-balance.yml config, RunPeriod is the equivalant of Collections.BalancePeriod."))
36 ac, err := arvados.NewClientFromConfig(cluster)
39 return service.ErrorHandler(ctx, cluster, fmt.Errorf("error initializing client from cluster config: %s", err))
42 if cluster.SystemLogs.LogLevel == "debug" {
46 if options.Logger == nil {
47 options.Logger = ctxlog.FromContext(ctx)
54 Metrics: newMetrics(registry),
55 Logger: options.Logger,
56 Dumper: options.Dumper,
64 os.Exit(runCommand(os.Args[0], os.Args[1:], os.Stdin, os.Stdout, os.Stderr))
67 func runCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
68 logger := ctxlog.FromContext(context.Background())
70 flags := flag.NewFlagSet(prog, flag.ExitOnError)
71 flags.BoolVar(&options.Once, "once", false,
72 "balance once and then exit")
73 flags.BoolVar(&options.CommitPulls, "commit-pulls", false,
74 "send pull requests (make more replicas of blocks that are underreplicated or are not in optimal rendezvous probe order)")
75 flags.BoolVar(&options.CommitTrash, "commit-trash", false,
76 "send trash requests (delete unreferenced old blocks, and excess replicas of overreplicated blocks)")
77 flags.Bool("version", false, "Write version information to stdout and exit 0")
78 dumpFlag := flags.Bool("dump", false, "dump details for each block to stdout")
80 loader := config.NewLoader(os.Stdin, logger)
81 loader.SetupFlags(flags)
83 munged := loader.MungeLegacyConfigArgs(logger, args, "-legacy-keepbalance-config")
87 dumper := logrus.New()
88 dumper.Out = os.Stdout
89 dumper.Formatter = &logrus.TextFormatter{}
90 options.Dumper = dumper
93 // Only pass along the version flag, which gets handled in RunCommand
95 flags.Visit(func(f *flag.Flag) {
96 if f.Name == "version" {
97 args = append(args, "-"+f.Name, f.Value.String())
101 return command.RunCommand(prog, args, stdin, stdout, stderr)