18308: Merge branch 'main' into 18308-enable-collection-versioning
[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 main
6
7 import (
8         "context"
9         "flag"
10         "fmt"
11         "io"
12         "net/http"
13         _ "net/http/pprof"
14         "os"
15
16         "git.arvados.org/arvados.git/lib/cmd"
17         "git.arvados.org/arvados.git/lib/config"
18         "git.arvados.org/arvados.git/lib/service"
19         "git.arvados.org/arvados.git/sdk/go/arvados"
20         "git.arvados.org/arvados.git/sdk/go/ctxlog"
21         "git.arvados.org/arvados.git/sdk/go/health"
22         "github.com/jmoiron/sqlx"
23         _ "github.com/lib/pq"
24         "github.com/prometheus/client_golang/prometheus"
25         "github.com/sirupsen/logrus"
26 )
27
28 func main() {
29         os.Exit(runCommand(os.Args[0], os.Args[1:], os.Stdin, os.Stdout, os.Stderr))
30 }
31
32 func runCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
33         logger := ctxlog.FromContext(context.Background())
34
35         var options RunOptions
36         flags := flag.NewFlagSet(prog, flag.ContinueOnError)
37         flags.BoolVar(&options.Once, "once", false,
38                 "balance once and then exit")
39         flags.BoolVar(&options.CommitPulls, "commit-pulls", false,
40                 "send pull requests (make more replicas of blocks that are underreplicated or are not in optimal rendezvous probe order)")
41         flags.BoolVar(&options.CommitTrash, "commit-trash", false,
42                 "send trash requests (delete unreferenced old blocks, and excess replicas of overreplicated blocks)")
43         flags.BoolVar(&options.CommitConfirmedFields, "commit-confirmed-fields", true,
44                 "update collection fields (replicas_confirmed, storage_classes_confirmed, etc.)")
45         dumpFlag := flags.Bool("dump", false, "dump details for each block to stdout")
46         pprofAddr := flags.String("pprof", "", "serve Go profile data at `[addr]:port`")
47         // "show version" is implemented by service.Command, so we
48         // don't need the var here -- we just need the -version flag
49         // to pass flags.Parse().
50         flags.Bool("version", false, "Write version information to stdout and exit 0")
51
52         if *pprofAddr != "" {
53                 go func() {
54                         logrus.Println(http.ListenAndServe(*pprofAddr, nil))
55                 }()
56         }
57
58         loader := config.NewLoader(os.Stdin, logger)
59         loader.SetupFlags(flags)
60
61         munged := loader.MungeLegacyConfigArgs(logger, args, "-legacy-keepbalance-config")
62         if ok, code := cmd.ParseFlags(flags, prog, munged, "", stderr); !ok {
63                 return code
64         }
65
66         if *dumpFlag {
67                 dumper := logrus.New()
68                 dumper.Out = os.Stdout
69                 dumper.Formatter = &logrus.TextFormatter{}
70                 options.Dumper = dumper
71         }
72
73         // Drop our custom args that would be rejected by the generic
74         // service.Command
75         args = nil
76         dropFlag := map[string]bool{
77                 "once":                    true,
78                 "commit-pulls":            true,
79                 "commit-trash":            true,
80                 "commit-confirmed-fields": true,
81                 "dump":                    true,
82         }
83         flags.Visit(func(f *flag.Flag) {
84                 if !dropFlag[f.Name] {
85                         args = append(args, "-"+f.Name+"="+f.Value.String())
86                 }
87         })
88
89         return service.Command(arvados.ServiceNameKeepbalance,
90                 func(ctx context.Context, cluster *arvados.Cluster, token string, registry *prometheus.Registry) service.Handler {
91                         if !options.Once && cluster.Collections.BalancePeriod == arvados.Duration(0) {
92                                 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)"))
93                         }
94
95                         ac, err := arvados.NewClientFromConfig(cluster)
96                         ac.AuthToken = token
97                         if err != nil {
98                                 return service.ErrorHandler(ctx, cluster, fmt.Errorf("error initializing client from cluster config: %s", err))
99                         }
100
101                         db, err := sqlx.Open("postgres", cluster.PostgreSQL.Connection.String())
102                         if err != nil {
103                                 return service.ErrorHandler(ctx, cluster, fmt.Errorf("postgresql connection failed: %s", err))
104                         }
105                         if p := cluster.PostgreSQL.ConnectionPool; p > 0 {
106                                 db.SetMaxOpenConns(p)
107                         }
108                         err = db.Ping()
109                         if err != nil {
110                                 return service.ErrorHandler(ctx, cluster, fmt.Errorf("postgresql connection succeeded but ping failed: %s", err))
111                         }
112
113                         if options.Logger == nil {
114                                 options.Logger = ctxlog.FromContext(ctx)
115                         }
116
117                         srv := &Server{
118                                 Cluster:    cluster,
119                                 ArvClient:  ac,
120                                 RunOptions: options,
121                                 Metrics:    newMetrics(registry),
122                                 Logger:     options.Logger,
123                                 Dumper:     options.Dumper,
124                                 DB:         db,
125                         }
126                         srv.Handler = &health.Handler{
127                                 Token:  cluster.ManagementToken,
128                                 Prefix: "/_health/",
129                                 Routes: health.Routes{"ping": srv.CheckHealth},
130                         }
131
132                         go srv.run()
133                         return srv
134                 }).RunCommand(prog, args, stdin, stdout, stderr)
135 }