1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
15 "git.arvados.org/arvados.git/lib/cmd"
16 "git.arvados.org/arvados.git/lib/config"
17 "git.arvados.org/arvados.git/lib/service"
18 "git.arvados.org/arvados.git/sdk/go/arvados"
19 "git.arvados.org/arvados.git/sdk/go/ctxlog"
20 "git.arvados.org/arvados.git/sdk/go/health"
21 "github.com/jmoiron/sqlx"
23 "github.com/prometheus/client_golang/prometheus"
28 var Command = command{}
30 func (command) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
31 var options RunOptions
32 flags := flag.NewFlagSet(prog, flag.ContinueOnError)
33 flags.BoolVar(&options.Once, "once", false,
34 "balance once and then exit")
35 deprCommitPulls := flags.Bool("commit-pulls", true,
36 "send pull requests (must be true -- configure Collections.BalancePullLimit = 0 to disable.)")
37 deprCommitTrash := flags.Bool("commit-trash", true,
38 "send trash requests (must be true -- configure Collections.BalanceTrashLimit = 0 to disable.)")
39 flags.BoolVar(&options.CommitConfirmedFields, "commit-confirmed-fields", true,
40 "update collection fields (replicas_confirmed, storage_classes_confirmed, etc.)")
41 flags.StringVar(&options.ChunkPrefix, "chunk-prefix", "",
42 "operate only on blocks with the given prefix (experimental, see https://dev.arvados.org/issues/19923)")
43 // These options are implemented by service.Command, so we
44 // don't need the vars here -- we just need the flags
45 // to pass flags.Parse().
46 flags.Bool("dump", false, "dump details for each block to stdout")
47 flags.String("pprof", "", "serve Go profile data at `[addr]:port`")
48 flags.Bool("version", false, "Write version information to stdout and exit 0")
50 logger := ctxlog.New(stderr, "json", "info")
51 loader := config.NewLoader(&bytes.Buffer{}, logger)
52 loader.SetupFlags(flags)
53 munged := loader.MungeLegacyConfigArgs(logger, args, "-legacy-keepbalance-config")
54 if ok, code := cmd.ParseFlags(flags, prog, munged, "", stderr); !ok {
58 if !*deprCommitPulls || !*deprCommitTrash {
60 "Usage error: the -commit-pulls or -commit-trash command line flags are no longer supported.\n",
61 "Use Collections.BalancePullLimit and Collections.BalanceTrashLimit instead.\n")
62 return cmd.EXIT_INVALIDARGUMENT
65 // Drop our custom args that would be rejected by the generic
68 dropFlag := map[string]bool{
72 "commit-confirmed-fields": true,
75 flags.Visit(func(f *flag.Flag) {
76 if !dropFlag[f.Name] {
77 args = append(args, "-"+f.Name+"="+f.Value.String())
81 return service.Command(arvados.ServiceNameKeepbalance,
82 func(ctx context.Context, cluster *arvados.Cluster, token string, registry *prometheus.Registry) service.Handler {
83 if !options.Once && cluster.Collections.BalancePeriod == arvados.Duration(0) {
84 return service.ErrorHandler(ctx, cluster, fmt.Errorf("cannot start service: Collections.BalancePeriod is zero (if you want to run once and then exit, use the -once flag)"))
87 ac, err := arvados.NewClientFromConfig(cluster)
90 return service.ErrorHandler(ctx, cluster, fmt.Errorf("error initializing client from cluster config: %s", err))
93 db, err := sqlx.Open("postgres", cluster.PostgreSQL.Connection.String())
95 return service.ErrorHandler(ctx, cluster, fmt.Errorf("postgresql connection failed: %s", err))
97 if p := cluster.PostgreSQL.ConnectionPool; p > 0 {
102 return service.ErrorHandler(ctx, cluster, fmt.Errorf("postgresql connection succeeded but ping failed: %s", err))
105 if options.Logger == nil {
106 options.Logger = ctxlog.FromContext(ctx)
113 Metrics: newMetrics(registry),
114 Logger: options.Logger,
115 Dumper: options.Dumper,
118 srv.Handler = &health.Handler{
119 Token: cluster.ManagementToken,
121 Routes: health.Routes{"ping": srv.CheckHealth},
126 }).RunCommand(prog, args, stdin, stdout, stderr)