21846: applied select to context menu open
[arvados.git] / services / keep-balance / main.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package keepbalance
6
7 import (
8         "bytes"
9         "context"
10         "flag"
11         "fmt"
12         "io"
13         _ "net/http/pprof"
14
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"
22         _ "github.com/lib/pq"
23         "github.com/prometheus/client_golang/prometheus"
24 )
25
26 type command struct{}
27
28 var Command = command{}
29
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")
49
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 {
55                 return code
56         }
57
58         if !*deprCommitPulls || !*deprCommitTrash {
59                 fmt.Fprint(stderr,
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
63         }
64
65         // Drop our custom args that would be rejected by the generic
66         // service.Command
67         args = nil
68         dropFlag := map[string]bool{
69                 "once":                    true,
70                 "commit-pulls":            true,
71                 "commit-trash":            true,
72                 "commit-confirmed-fields": true,
73                 "dump":                    true,
74         }
75         flags.Visit(func(f *flag.Flag) {
76                 if !dropFlag[f.Name] {
77                         args = append(args, "-"+f.Name+"="+f.Value.String())
78                 }
79         })
80
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)"))
85                         }
86
87                         ac, err := arvados.NewClientFromConfig(cluster)
88                         ac.AuthToken = token
89                         if err != nil {
90                                 return service.ErrorHandler(ctx, cluster, fmt.Errorf("error initializing client from cluster config: %s", err))
91                         }
92
93                         db, err := sqlx.Open("postgres", cluster.PostgreSQL.Connection.String())
94                         if err != nil {
95                                 return service.ErrorHandler(ctx, cluster, fmt.Errorf("postgresql connection failed: %s", err))
96                         }
97                         if p := cluster.PostgreSQL.ConnectionPool; p > 0 {
98                                 db.SetMaxOpenConns(p)
99                         }
100                         err = db.Ping()
101                         if err != nil {
102                                 return service.ErrorHandler(ctx, cluster, fmt.Errorf("postgresql connection succeeded but ping failed: %s", err))
103                         }
104
105                         if options.Logger == nil {
106                                 options.Logger = ctxlog.FromContext(ctx)
107                         }
108
109                         srv := &Server{
110                                 Cluster:    cluster,
111                                 ArvClient:  ac,
112                                 RunOptions: options,
113                                 Metrics:    newMetrics(registry),
114                                 Logger:     options.Logger,
115                                 Dumper:     options.Dumper,
116                                 DB:         db,
117                         }
118                         srv.Handler = &health.Handler{
119                                 Token:  cluster.ManagementToken,
120                                 Prefix: "/_health/",
121                                 Routes: health.Routes{"ping": srv.CheckHealth},
122                         }
123
124                         go srv.run(ctx)
125                         return srv
126                 }).RunCommand(prog, args, stdin, stdout, stderr)
127 }