18947: Refactor keep-balance as arvados-server subcommand.
authorTom Clegg <tom@curii.com>
Thu, 28 Apr 2022 19:17:05 +0000 (15:17 -0400)
committerTom Clegg <tom@curii.com>
Mon, 2 May 2022 19:14:32 +0000 (15:14 -0400)
Arvados-DCO-1.1-Signed-off-by: Tom Clegg <tom@curii.com>

18 files changed:
build/run-tests.sh
cmd/arvados-server/cmd.go
sdk/python/tests/run_test_server.py
services/keep-balance/balance.go
services/keep-balance/balance_run_test.go
services/keep-balance/balance_test.go
services/keep-balance/block_state.go
services/keep-balance/block_state_test.go
services/keep-balance/change_set.go
services/keep-balance/change_set_test.go
services/keep-balance/collection.go
services/keep-balance/collection_test.go
services/keep-balance/integration_test.go
services/keep-balance/keep_service.go
services/keep-balance/main.go
services/keep-balance/main_test.go
services/keep-balance/metrics.go
services/keep-balance/server.go

index d517e7e2c4d921bbc91d8edd31f1ecfcee9bb881..0f996f77e927b4c4d6e0eae6458c4d80f3229be7 100755 (executable)
@@ -158,6 +158,8 @@ only_install=
 temp=
 temp_preserve=
 
 temp=
 temp_preserve=
 
+ignore_sigint=
+
 clear_temp() {
     if [[ -z "$temp" ]]; then
         # we did not even get as far as making a temp dir
 clear_temp() {
     if [[ -z "$temp" ]]; then
         # we did not even get as far as making a temp dir
@@ -473,6 +475,10 @@ stop_services() {
 }
 
 interrupt() {
 }
 
 interrupt() {
+    if [[ -n "$ignore_sigint" ]]; then
+        echo >&2 "ignored SIGINT"
+        return
+    fi
     failures+=("($(basename $0) interrupted)")
     exit_cleanly
 }
     failures+=("($(basename $0) interrupted)")
     exit_cleanly
 }
@@ -1216,6 +1222,7 @@ else
     setnextcmd
     HISTFILE="$WORKSPACE/tmp/.history"
     history -r
     setnextcmd
     HISTFILE="$WORKSPACE/tmp/.history"
     history -r
+    ignore_sigint=1
     while read -p 'What next? ' -e -i "$nextcmd" nextcmd; do
         history -s "$nextcmd"
         history -w
     while read -p 'What next? ' -e -i "$nextcmd" nextcmd; do
         history -s "$nextcmd"
         history -w
index e4bd39002aafaab29a5ed690fc09a3174f30effc..c9a91c872160cb74c10d098c4ae2f0f9c16ce667 100644 (file)
@@ -22,6 +22,7 @@ import (
        "git.arvados.org/arvados.git/lib/lsf"
        "git.arvados.org/arvados.git/lib/recovercollection"
        "git.arvados.org/arvados.git/services/githttpd"
        "git.arvados.org/arvados.git/lib/lsf"
        "git.arvados.org/arvados.git/lib/recovercollection"
        "git.arvados.org/arvados.git/services/githttpd"
+       keepbalance "git.arvados.org/arvados.git/services/keep-balance"
        keepweb "git.arvados.org/arvados.git/services/keep-web"
        "git.arvados.org/arvados.git/services/keepproxy"
        "git.arvados.org/arvados.git/services/keepstore"
        keepweb "git.arvados.org/arvados.git/services/keep-web"
        "git.arvados.org/arvados.git/services/keepproxy"
        "git.arvados.org/arvados.git/services/keepstore"
@@ -46,6 +47,7 @@ var (
                "git-httpd":          githttpd.Command,
                "install":            install.Command,
                "init":               install.InitCommand,
                "git-httpd":          githttpd.Command,
                "install":            install.Command,
                "init":               install.InitCommand,
+               "keep-balance":       keepbalance.Command,
                "keep-web":           keepweb.Command,
                "keepproxy":          keepproxy.Command,
                "keepstore":          keepstore.Command,
                "keep-web":           keepweb.Command,
                "keepproxy":          keepproxy.Command,
                "keepstore":          keepstore.Command,
index 74722b256e4e7c1dadb0148d3b507bcbc3b0d08f..6f591b74a5a20f51b1d87e798c07eebf83606f3f 100644 (file)
@@ -957,6 +957,9 @@ if __name__ == "__main__":
               format(args.action, actions),
               file=sys.stderr)
         sys.exit(1)
               format(args.action, actions),
               file=sys.stderr)
         sys.exit(1)
+    # Create a new process group so our child processes don't exit on
+    # ^C in run-tests.sh interactive mode.
+    os.setpgid(0, 0)
     if args.action == 'start':
         stop(force=('ARVADOS_TEST_API_HOST' not in os.environ))
         run(leave_running_atexit=True)
     if args.action == 'start':
         stop(force=('ARVADOS_TEST_API_HOST' not in os.environ))
         run(leave_running_atexit=True)
index eb6f580f435c56072ad381f984703e229c524f29..1dedb409a4a2de5c4f414959b024e291007d42b1 100644 (file)
@@ -2,7 +2,7 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-package main
+package keepbalance
 
 import (
        "bytes"
 
 import (
        "bytes"
index 0d1b6b5912d2820be5a40f1000871b02c1321e0e..2db7bea173c17dc41f6943b4fe579cbc7d15a24f 100644 (file)
@@ -2,7 +2,7 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-package main
+package keepbalance
 
 import (
        "bytes"
 
 import (
        "bytes"
index df04145b9dd15e3d1066b0a1d5a71c57ba27a5f8..6626609b5769f55bdb7d32385afffc443df8712c 100644 (file)
@@ -2,7 +2,7 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-package main
+package keepbalance
 
 import (
        "crypto/md5"
 
 import (
        "crypto/md5"
index e30b4ff7943d4c4a041ec71924000a48a856c4d4..07c9952f906d7e57dfca21d0007c01d96f42294b 100644 (file)
@@ -2,7 +2,7 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-package main
+package keepbalance
 
 import (
        "sync"
 
 import (
        "sync"
index aaf2c18e297c206b2aa8f3de54df6878b6c7235b..8a58be288ff1832a3799224510d04d5d581d2455 100644 (file)
@@ -2,7 +2,7 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-package main
+package keepbalance
 
 import (
        "time"
 
 import (
        "time"
index 85d03d409b681626bdee480f7b7c2260047385c2..8e0ba028acd801e182a9b475f47b658c86e250e1 100644 (file)
@@ -2,7 +2,7 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-package main
+package keepbalance
 
 import (
        "encoding/json"
 
 import (
        "encoding/json"
index baf7ab6dc45665b6fc4e7690da2085a6c7452a9c..5474d29fb57e2d64a67286382b1d53907afe3ae7 100644 (file)
@@ -2,7 +2,7 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-package main
+package keepbalance
 
 import (
        "encoding/json"
 
 import (
        "encoding/json"
index 1e1e51abe7ebcd55e5d0ed7ca55eae02a8b56504..ccb01bdd10c5fb7f777d868a2aeefa175d494ace 100644 (file)
@@ -2,7 +2,7 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-package main
+package keepbalance
 
 import (
        "context"
 
 import (
        "context"
index f749bad6ad1865a30670d0fe2978dfe8ebd2764c..ca67d1617d6fb110a6c90646f46ca8977c5569d8 100644 (file)
@@ -2,7 +2,7 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-package main
+package keepbalance
 
 import (
        "context"
 
 import (
        "context"
index a6cc3281042739191921dcc8cda04d3a4e938d59..3cfb5cdeda5039fb37f414f5cd0b095eea0e772d 100644 (file)
@@ -2,7 +2,7 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-package main
+package keepbalance
 
 import (
        "bytes"
 
 import (
        "bytes"
index 17f8418f622f992a7025db9b9214e60c5a39f2ca..f50fbfd946775e86fa667de2a89eddb1e728acc4 100644 (file)
@@ -2,7 +2,7 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-package main
+package keepbalance
 
 import (
        "context"
 
 import (
        "context"
index 8a95d389c8292f04c8479f0c21abdcf5c84932a3..f0b0df5bd331d6a97a2cdaab0a8d968cfdbfc550 100644 (file)
@@ -2,16 +2,15 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-package main
+package keepbalance
 
 import (
 
 import (
+       "bytes"
        "context"
        "flag"
        "fmt"
        "io"
        "context"
        "flag"
        "fmt"
        "io"
-       "net/http"
        _ "net/http/pprof"
        _ "net/http/pprof"
-       "os"
 
        "git.arvados.org/arvados.git/lib/cmd"
        "git.arvados.org/arvados.git/lib/config"
 
        "git.arvados.org/arvados.git/lib/cmd"
        "git.arvados.org/arvados.git/lib/config"
@@ -22,16 +21,13 @@ import (
        "github.com/jmoiron/sqlx"
        _ "github.com/lib/pq"
        "github.com/prometheus/client_golang/prometheus"
        "github.com/jmoiron/sqlx"
        _ "github.com/lib/pq"
        "github.com/prometheus/client_golang/prometheus"
-       "github.com/sirupsen/logrus"
 )
 
 )
 
-func main() {
-       os.Exit(runCommand(os.Args[0], os.Args[1:], os.Stdin, os.Stdout, os.Stderr))
-}
+type command struct{}
 
 
-func runCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
-       logger := ctxlog.FromContext(context.Background())
+var Command = command{}
 
 
+func (command) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
        var options RunOptions
        flags := flag.NewFlagSet(prog, flag.ContinueOnError)
        flags.BoolVar(&options.Once, "once", false,
        var options RunOptions
        flags := flag.NewFlagSet(prog, flag.ContinueOnError)
        flags.BoolVar(&options.Once, "once", false,
@@ -42,34 +38,21 @@ func runCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.W
                "send trash requests (delete unreferenced old blocks, and excess replicas of overreplicated blocks)")
        flags.BoolVar(&options.CommitConfirmedFields, "commit-confirmed-fields", true,
                "update collection fields (replicas_confirmed, storage_classes_confirmed, etc.)")
                "send trash requests (delete unreferenced old blocks, and excess replicas of overreplicated blocks)")
        flags.BoolVar(&options.CommitConfirmedFields, "commit-confirmed-fields", true,
                "update collection fields (replicas_confirmed, storage_classes_confirmed, etc.)")
-       dumpFlag := flags.Bool("dump", false, "dump details for each block to stdout")
-       pprofAddr := flags.String("pprof", "", "serve Go profile data at `[addr]:port`")
-       // "show version" is implemented by service.Command, so we
-       // don't need the var here -- we just need the -version flag
+       // These options are implemented by service.Command, so we
+       // don't need the vars here -- we just need the flags
        // to pass flags.Parse().
        // to pass flags.Parse().
+       flags.Bool("dump", false, "dump details for each block to stdout")
+       flags.String("pprof", "", "serve Go profile data at `[addr]:port`")
        flags.Bool("version", false, "Write version information to stdout and exit 0")
 
        flags.Bool("version", false, "Write version information to stdout and exit 0")
 
-       if *pprofAddr != "" {
-               go func() {
-                       logrus.Println(http.ListenAndServe(*pprofAddr, nil))
-               }()
-       }
-
-       loader := config.NewLoader(os.Stdin, logger)
+       logger := ctxlog.New(stderr, "json", "info")
+       loader := config.NewLoader(&bytes.Buffer{}, logger)
        loader.SetupFlags(flags)
        loader.SetupFlags(flags)
-
        munged := loader.MungeLegacyConfigArgs(logger, args, "-legacy-keepbalance-config")
        if ok, code := cmd.ParseFlags(flags, prog, munged, "", stderr); !ok {
                return code
        }
 
        munged := loader.MungeLegacyConfigArgs(logger, args, "-legacy-keepbalance-config")
        if ok, code := cmd.ParseFlags(flags, prog, munged, "", stderr); !ok {
                return code
        }
 
-       if *dumpFlag {
-               dumper := logrus.New()
-               dumper.Out = os.Stdout
-               dumper.Formatter = &logrus.TextFormatter{}
-               options.Dumper = dumper
-       }
-
        // Drop our custom args that would be rejected by the generic
        // service.Command
        args = nil
        // Drop our custom args that would be rejected by the generic
        // service.Command
        args = nil
index 820f35216637626d7e543fb5781b613294f2dd91..26002eca986c3d2c60c7c20e035e7b32b474c22c 100644 (file)
@@ -2,7 +2,7 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-package main
+package keepbalance
 
 import (
        "bytes"
 
 import (
        "bytes"
@@ -25,7 +25,7 @@ type mainSuite struct{}
 
 func (s *mainSuite) TestVersionFlag(c *check.C) {
        var stdout, stderr bytes.Buffer
 
 func (s *mainSuite) TestVersionFlag(c *check.C) {
        var stdout, stderr bytes.Buffer
-       runCommand("keep-balance", []string{"-version"}, nil, &stdout, &stderr)
+       Command.RunCommand("keep-balance", []string{"-version"}, nil, &stdout, &stderr)
        c.Check(stderr.String(), check.Equals, "")
        c.Log(stdout.String())
        c.Check(stdout.String(), check.Matches, `keep-balance.*\(go1.*\)\n`)
        c.Check(stderr.String(), check.Equals, "")
        c.Log(stdout.String())
        c.Check(stdout.String(), check.Matches, `keep-balance.*\(go1.*\)\n`)
@@ -51,7 +51,7 @@ func (s *mainSuite) TestHTTPServer(c *check.C) {
        c.Assert(err, check.IsNil)
 
        var stdout bytes.Buffer
        c.Assert(err, check.IsNil)
 
        var stdout bytes.Buffer
-       go runCommand("keep-balance", []string{"-config", "-"}, bytes.NewBuffer(config), &stdout, &stdout)
+       go Command.RunCommand("keep-balance", []string{"-config", "-"}, bytes.NewBuffer(config), &stdout, &stdout)
        done := make(chan struct{})
        go func() {
                defer close(done)
        done := make(chan struct{})
        go func() {
                defer close(done)
index ce1b1811cc69f28f3fad955a5525b35a666baf3a..4683b67b9860052d97d8fa77e92141ae29bdcef1 100644 (file)
@@ -2,7 +2,7 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-package main
+package keepbalance
 
 import (
        "fmt"
 
 import (
        "fmt"
index 5299b96c1caf2ac3aaa28c639e71d501ddbbd637..e485f5b2061f28134306d1d897b22cb62e4190e9 100644 (file)
@@ -2,7 +2,7 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-package main
+package keepbalance
 
 import (
        "net/http"
 
 import (
        "net/http"