Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Cai <jcai@gitlab.com>2022-03-16 20:06:00 +0300
committerJohn Cai <jcai@gitlab.com>2022-03-25 15:37:24 +0300
commit0b7fecb1980a8fe4bfa23f7943ccf9ce970c801a (patch)
tree8e369de66ec52c0bc3c776020793081503a4732e
parent00295c2340ed863044284d9e83b56072ef246b3b (diff)
maintenance: Move StartWorkers from server to maintenance package
StartWorkers currently lives in the server package because it required a gRPC server to be available. Now that maintenance doesn't need an rpc call, it doesn't need to be tied to the gRPC package anymore. Hence, we can move this function into the maintenance package.
-rw-r--r--cmd/gitaly/main.go2
-rw-r--r--internal/gitaly/maintenance/optimize.go46
-rw-r--r--internal/gitaly/server/server_factory.go45
3 files changed, 45 insertions, 48 deletions
diff --git a/cmd/gitaly/main.go b/cmd/gitaly/main.go
index 5b732b3a6..4bd8e234b 100644
--- a/cmd/gitaly/main.go
+++ b/cmd/gitaly/main.go
@@ -318,7 +318,7 @@ func run(cfg config.Cfg) error {
return fmt.Errorf("unable to start the bootstrap: %v", err)
}
- shutdownWorkers, err := gitalyServerFactory.StartWorkers(
+ shutdownWorkers, err := maintenance.StartWorkers(
ctx,
glog.Default(),
maintenance.DailyOptimizationWorker(cfg, maintenance.OptimizerFunc(func(ctx context.Context, repo repository.GitRepo) error {
diff --git a/internal/gitaly/maintenance/optimize.go b/internal/gitaly/maintenance/optimize.go
index 1f8695a95..33a939162 100644
--- a/internal/gitaly/maintenance/optimize.go
+++ b/internal/gitaly/maintenance/optimize.go
@@ -3,6 +3,7 @@ package maintenance
import (
"context"
"errors"
+ "fmt"
"math/rand"
"os"
"path/filepath"
@@ -13,7 +14,6 @@ import (
"github.com/sirupsen/logrus"
"gitlab.com/gitlab-org/gitaly/v14/internal/git/repository"
"gitlab.com/gitlab-org/gitaly/v14/internal/gitaly/config"
- "gitlab.com/gitlab-org/gitaly/v14/internal/gitaly/server"
"gitlab.com/gitlab-org/gitaly/v14/internal/gitaly/storage"
"gitlab.com/gitlab-org/gitaly/v14/internal/helper"
"gitlab.com/gitlab-org/gitaly/v14/proto/go/gitalypb"
@@ -31,6 +31,48 @@ func init() {
prometheus.MustRegister(repoOptimizationHistogram)
}
+// WorkerFunc is a function that does a unit of work meant to run in the background
+type WorkerFunc func(context.Context, logrus.FieldLogger) error
+
+// StartWorkers will start any auxiliary background workers that are allowed
+// to fail without stopping the rest of the server.
+func StartWorkers(
+ ctx context.Context,
+ l logrus.FieldLogger,
+ workers ...WorkerFunc,
+) (func(), error) {
+ errQ := make(chan error)
+
+ ctx, cancel := context.WithCancel(ctx)
+
+ for _, worker := range workers {
+ worker := worker
+ go func() {
+ errQ <- worker(ctx, l)
+ }()
+ }
+
+ shutdown := func() {
+ cancel()
+
+ // give the worker 5 seconds to shutdown gracefully
+ timeout := 5 * time.Second
+
+ var err error
+ select {
+ case err = <-errQ:
+ break
+ case <-time.After(timeout):
+ err = fmt.Errorf("timed out after %s", timeout)
+ }
+ if err != nil && err != context.Canceled {
+ l.WithError(err).Error("maintenance worker shutdown")
+ }
+ }
+
+ return shutdown, nil
+}
+
func shuffledStoragesCopy(randSrc *rand.Rand, storages []config.Storage) []config.Storage {
shuffled := make([]config.Storage, len(storages))
copy(shuffled, storages)
@@ -52,7 +94,7 @@ func (o OptimizerFunc) OptimizeRepository(ctx context.Context, repo repository.G
}
// DailyOptimizationWorker creates a worker that runs repository maintenance daily
-func DailyOptimizationWorker(cfg config.Cfg, optimizer Optimizer) server.WorkerFunc {
+func DailyOptimizationWorker(cfg config.Cfg, optimizer Optimizer) WorkerFunc {
return func(ctx context.Context, l logrus.FieldLogger) error {
return NewDailyWorker().StartDaily(
ctx,
diff --git a/internal/gitaly/server/server_factory.go b/internal/gitaly/server/server_factory.go
index ff3405c86..defea0c3b 100644
--- a/internal/gitaly/server/server_factory.go
+++ b/internal/gitaly/server/server_factory.go
@@ -1,10 +1,7 @@
package server
import (
- "context"
- "fmt"
"sync"
- "time"
"github.com/sirupsen/logrus"
"gitlab.com/gitlab-org/gitaly/v14/internal/backchannel"
@@ -43,48 +40,6 @@ func NewGitalyServerFactory(
}
}
-// WorkerFunc is a function that does a unit of work meant to run in the background
-type WorkerFunc func(context.Context, logrus.FieldLogger) error
-
-// StartWorkers will start any auxiliary background workers that are allowed
-// to fail without stopping the rest of the server.
-func (s *GitalyServerFactory) StartWorkers(
- ctx context.Context,
- l logrus.FieldLogger,
- workers ...WorkerFunc,
-) (func(), error) {
- errQ := make(chan error)
-
- ctx, cancel := context.WithCancel(ctx)
-
- for _, worker := range workers {
- worker := worker
- go func() {
- errQ <- worker(ctx, l)
- }()
- }
-
- shutdown := func() {
- cancel()
-
- // give the worker 5 seconds to shutdown gracefully
- timeout := 5 * time.Second
-
- var err error
- select {
- case err = <-errQ:
- break
- case <-time.After(timeout):
- err = fmt.Errorf("timed out after %s", timeout)
- }
- if err != nil && err != context.Canceled {
- l.WithError(err).Error("maintenance worker shutdown")
- }
- }
-
- return shutdown, nil
-}
-
// Stop immediately stops all servers created by the GitalyServerFactory.
func (s *GitalyServerFactory) Stop() {
for _, servers := range [][]*grpc.Server{