From 21714672bdf80a5d2a29404620d5b0e1be9ef622 Mon Sep 17 00:00:00 2001 From: Stefan Majer Date: Fri, 25 Aug 2023 07:43:36 +0200 Subject: [PATCH] Errgroup --- .../database/meilisearch/meilisearch.go | 26 ++++++++++++++----- go.mod | 2 +- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/cmd/internal/database/meilisearch/meilisearch.go b/cmd/internal/database/meilisearch/meilisearch.go index bad764d..64d996a 100644 --- a/cmd/internal/database/meilisearch/meilisearch.go +++ b/cmd/internal/database/meilisearch/meilisearch.go @@ -1,6 +1,7 @@ package meilisearch import ( + "context" "errors" "fmt" "io/fs" @@ -15,6 +16,7 @@ import ( "github.com/Masterminds/semver/v3" "github.com/avast/retry-go/v4" "github.com/meilisearch/meilisearch-go" + "golang.org/x/sync/errgroup" "github.com/metal-stack/backup-restore-sidecar/cmd/internal/constants" "github.com/metal-stack/backup-restore-sidecar/cmd/internal/utils" @@ -169,8 +171,12 @@ func (db *Meilisearch) Upgrade() error { return fmt.Errorf("unable to rename dbdir: %w", err) } - var cmd *exec.Cmd - go func() { + var ( + cmd *exec.Cmd + g, _ = errgroup.WithContext(context.Background()) + ) + + g.Go(func() error { args := []string{"--import-dump", path.Join(db.dumpdir, latestStableDump), "--master-key", db.apikey} db.log.Infow("execute meilisearch", "args", args) @@ -179,10 +185,14 @@ func (db *Meilisearch) Upgrade() error { cmd.Stderr = os.Stderr err = cmd.Run() if err != nil { - db.log.Errorw("unable import latest dump, skipping upgrade", "error", err) + return fmt.Errorf("unable import latest dump, skipping upgrade %w", err) } - }() + db.log.Info("import of dump finished") + return nil + }) + // TODO big databases might take longer, not sure if 100 attempts are enough + // must check how long it take max with backoff ? err = retry.Do(func() error { v, err := db.client.Version() if err != nil { @@ -190,10 +200,14 @@ func (db *Meilisearch) Upgrade() error { } db.log.Infow("meilisearch started after upgrade, killing it", "version", v) return cmd.Process.Signal(syscall.SIGTERM) - }) + }, retry.Attempts(100)) if err != nil { return err } + err = g.Wait() + if err != nil { + return fmt.Errorf("database upgrade failed with %w", err) + } db.log.Infow("upgrade done and new data in place", "took", time.Since(start)) return nil @@ -216,7 +230,6 @@ func (db *Meilisearch) moveDumpsToBackupDir() error { dst := path.Join(constants.BackupDir, d.Name()) src := basepath - db.log.Infow("move dump", "from", src, "to", dst) latestStableDst := path.Join(db.dumpdir, latestStableDump) db.log.Infow("create latest dump", "from", src, "to", latestStableDst) @@ -225,6 +238,7 @@ func (db *Meilisearch) moveDumpsToBackupDir() error { return fmt.Errorf("unable create latest stable dump: %w", err) } + db.log.Infow("move dump", "from", src, "to", dst) copy := exec.Command("mv", "-v", src, dst) copy.Stdout = os.Stdout copy.Stderr = os.Stderr diff --git a/go.mod b/go.mod index 0b7d4aa..36b0e2b 100644 --- a/go.mod +++ b/go.mod @@ -17,6 +17,7 @@ require ( github.com/spf13/cobra v1.7.0 github.com/spf13/viper v1.16.0 go.uber.org/zap v1.25.0 + golang.org/x/sync v0.3.0 google.golang.org/api v0.138.0 google.golang.org/grpc v1.57.0 google.golang.org/protobuf v1.31.0 @@ -73,7 +74,6 @@ require ( golang.org/x/crypto v0.12.0 // indirect golang.org/x/net v0.14.0 // indirect golang.org/x/oauth2 v0.11.0 // indirect - golang.org/x/sync v0.3.0 // indirect golang.org/x/sys v0.11.0 // indirect golang.org/x/text v0.12.0 // indirect golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect