14714: Simplifies server startup and removes globals
authorEric Biagiotti <ebiagiotti@veritasgenetics.com>
Mon, 30 Sep 2019 16:26:20 +0000 (12:26 -0400)
committerEric Biagiotti <ebiagiotti@veritasgenetics.com>
Mon, 30 Sep 2019 16:26:20 +0000 (12:26 -0400)
Arvados-DCO-1.1-Signed-off-by: Eric Biagiotti <ebiagiotti@veritasgenetics.com>

services/keep-balance/balance.go
services/keep-balance/balance_test.go
services/keep-balance/main.go
services/keep-balance/server.go

index a887c3c691e375917c94fbde51c671dbfa3ae243..3471d50fe356157ff85b762d5d650303ac6930b8 100644 (file)
@@ -446,7 +446,7 @@ func (bal *Balancer) addCollection(coll arvados.Collection) error {
        if coll.ReplicationDesired != nil {
                repl = *coll.ReplicationDesired
        }
-       debugf("%v: %d block x%d", coll.UUID, len(blkids), repl)
+       bal.Logger.Debugf("%v: %d block x%d", coll.UUID, len(blkids), repl)
        // Pass pdh to IncreaseDesired only if LostBlocksFile is being
        // written -- otherwise it's just a waste of memory.
        pdh := ""
@@ -563,7 +563,7 @@ type balanceResult struct {
 // balanceBlock compares current state to desired state for a single
 // block, and makes the appropriate ChangeSet calls.
 func (bal *Balancer) balanceBlock(blkid arvados.SizedDigest, blk *BlockState) balanceResult {
-       debugf("balanceBlock: %v %+v", blkid, blk)
+       bal.Logger.Debugf("balanceBlock: %v %+v", blkid, blk)
 
        type slot struct {
                mnt  *KeepMount // never nil
index e372d37841a7b095cc659216bb11b87b7bc793dd..6cffa8ded4dbad6975225949e871852e5ca2d50e 100644 (file)
@@ -13,6 +13,7 @@ import (
        "time"
 
        "git.curoverse.com/arvados.git/sdk/go/arvados"
+       "git.curoverse.com/arvados.git/sdk/go/ctxlog"
        check "gopkg.in/check.v1"
 )
 
@@ -69,6 +70,7 @@ func (bal *balancerSuite) SetUpSuite(c *check.C) {
        }
 
        bal.signatureTTL = 3600
+       bal.Logger = ctxlog.TestLogger(c)
 }
 
 func (bal *balancerSuite) SetUpTest(c *check.C) {
index 4379ae535e85f580f63d0e6f040084996cb5edf9..cf844ab050043c1662c3cf5cd62ef9ef238a6789 100644 (file)
@@ -9,7 +9,6 @@ import (
        "flag"
        "fmt"
        "io"
-       "log"
        "os"
 
        "git.curoverse.com/arvados.git/lib/config"
@@ -20,46 +19,6 @@ import (
        "github.com/sirupsen/logrus"
 )
 
-var (
-       version = "dev"
-       debugf  = func(string, ...interface{}) {}
-       command = service.Command(arvados.ServiceNameKeepbalance, newHandler)
-       options RunOptions
-)
-
-func newHandler(ctx context.Context, cluster *arvados.Cluster, token string, registry *prometheus.Registry) service.Handler {
-       if !options.Once && cluster.Collections.BalancePeriod == arvados.Duration(0) {
-               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)"))
-                       "If using the legacy keep-balance.yml config, RunPeriod is the equivalant of Collections.BalancePeriod."))
-       }
-
-       ac, err := arvados.NewClientFromConfig(cluster)
-       ac.AuthToken = token
-       if err != nil {
-               return service.ErrorHandler(ctx, cluster, fmt.Errorf("error initializing client from cluster config: %s", err))
-       }
-
-       if cluster.SystemLogs.LogLevel == "debug" {
-               debugf = log.Printf
-       }
-
-       if options.Logger == nil {
-               options.Logger = ctxlog.FromContext(ctx)
-       }
-
-       srv := &Server{
-               Cluster:    cluster,
-               ArvClient:  ac,
-               RunOptions: options,
-               Metrics:    newMetrics(registry),
-               Logger:     options.Logger,
-               Dumper:     options.Dumper,
-       }
-
-       srv.Start()
-       return srv
-}
-
 func main() {
        os.Exit(runCommand(os.Args[0], os.Args[1:], os.Stdin, os.Stdout, os.Stderr))
 }
@@ -67,6 +26,7 @@ func main() {
 func runCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
        logger := ctxlog.FromContext(context.Background())
 
+       var options RunOptions
        flags := flag.NewFlagSet(prog, flag.ExitOnError)
        flags.BoolVar(&options.Once, "once", false,
                "balance once and then exit")
@@ -98,5 +58,32 @@ func runCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.W
                }
        })
 
-       return command.RunCommand(prog, args, stdin, stdout, stderr)
+       return service.Command(arvados.ServiceNameKeepbalance,
+               func(ctx context.Context, cluster *arvados.Cluster, token string, registry *prometheus.Registry) service.Handler {
+                       if !options.Once && cluster.Collections.BalancePeriod == arvados.Duration(0) {
+                               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)"))
+                       }
+
+                       ac, err := arvados.NewClientFromConfig(cluster)
+                       ac.AuthToken = token
+                       if err != nil {
+                               return service.ErrorHandler(ctx, cluster, fmt.Errorf("error initializing client from cluster config: %s", err))
+                       }
+
+                       if options.Logger == nil {
+                               options.Logger = ctxlog.FromContext(ctx)
+                       }
+
+                       srv := &Server{
+                               Cluster:    cluster,
+                               ArvClient:  ac,
+                               RunOptions: options,
+                               Metrics:    newMetrics(registry),
+                               Logger:     options.Logger,
+                               Dumper:     options.Dumper,
+                       }
+
+                       go srv.run()
+                       return srv
+               }).RunCommand(prog, args, stdin, stdout, stderr)
 }
index 3e665a30d52c5310912164c38164182536b695d9..9f192d6355cacc791e3ac2f71cf3afc66da2cadc 100644 (file)
@@ -8,7 +8,6 @@ import (
        "net/http"
        "os"
        "os/signal"
-       "sync"
        "syscall"
        "time"
 
@@ -47,7 +46,6 @@ type Server struct {
        Metrics    *metrics
 
        httpHandler http.Handler
-       setupOnce   sync.Once
 
        Logger logrus.FieldLogger
        Dumper logrus.FieldLogger
@@ -63,12 +61,6 @@ func (srv *Server) CheckHealth() error {
        return nil
 }
 
-// Start sets up and runs the balancer.
-func (srv *Server) Start() {
-       srv.setupOnce.Do(srv.setup)
-       go srv.run()
-}
-
 func (srv *Server) setup() {
        if srv.Cluster.ManagementToken == "" {
                srv.httpHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {