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:
authorPatrick Steinhardt <psteinhardt@gitlab.com>2021-01-08 11:06:47 +0300
committerPatrick Steinhardt <psteinhardt@gitlab.com>2021-01-14 14:55:15 +0300
commit77976bd7a6e388869dc813440f6e03a3fae287a7 (patch)
tree2a178a3b371c1969be94c79ed921f47d38ed0263
parentd62e7caac3ed51870b530ca34138907886dc90af (diff)
cache: Use locator to resolve storage locations
The cache LeaseKeyer needs to be able to resolve storage locations, which is currently done via the global `config.Config` variable. This commit converts it to instead use the locator, which is now available starting with the preceding commit.
-rw-r--r--internal/cache/cachedb_test.go4
-rw-r--r--internal/cache/keyer.go55
2 files changed, 30 insertions, 29 deletions
diff --git a/internal/cache/cachedb_test.go b/internal/cache/cachedb_test.go
index 0af3adfcb..4d7814828 100644
--- a/internal/cache/cachedb_test.go
+++ b/internal/cache/cachedb_test.go
@@ -149,13 +149,13 @@ func injectTempStorage(t testing.TB) (string, testhelper.Cleanup) {
}
func TestLoserCount(t *testing.T) {
- db := cache.NewStreamDB(cache.NewLeaseKeyer(config.NewLocator(config.Config)))
-
// the test can be contaminate by other tests using the cache, so a
// dedicated storage location should be used
storageName, storageCleanup := injectTempStorage(t)
defer storageCleanup()
+ db := cache.NewStreamDB(cache.NewLeaseKeyer(config.NewLocator(config.Config)))
+
req := &gitalypb.InfoRefsRequest{
Repository: &gitalypb.Repository{
RelativePath: "test",
diff --git a/internal/cache/keyer.go b/internal/cache/keyer.go
index b6515b959..9aeb2e5e9 100644
--- a/internal/cache/keyer.go
+++ b/internal/cache/keyer.go
@@ -16,7 +16,6 @@ import (
"github.com/golang/protobuf/proto"
"github.com/google/uuid"
"github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus/ctxlogrus"
- "gitlab.com/gitlab-org/gitaly/internal/gitaly/config"
"gitlab.com/gitlab-org/gitaly/internal/metadata/featureflag"
"gitlab.com/gitlab-org/gitaly/internal/safe"
"gitlab.com/gitlab-org/gitaly/internal/storage"
@@ -68,12 +67,13 @@ func NewLeaseKeyer(locator storage.Locator) LeaseKeyer {
type lease struct {
pendingPath string
repo *gitalypb.Repository
+ keyer LeaseKeyer
}
// EndLease will end the lease by removing the pending lease file and updating
// the key file with the current lease ID.
func (l lease) EndLease(ctx context.Context) error {
- _, err := updateLatest(ctx, l.repo)
+ _, err := l.keyer.updateLatest(ctx, l.repo)
if err != nil {
return err
}
@@ -88,8 +88,8 @@ func (l lease) EndLease(ctx context.Context) error {
return nil
}
-func updateLatest(ctx context.Context, repo *gitalypb.Repository) (string, error) {
- repoStatePath, err := getRepoStatePath(repo)
+func (keyer LeaseKeyer) updateLatest(ctx context.Context, repo *gitalypb.Repository) (string, error) {
+ repoStatePath, err := keyer.getRepoStatePath(repo)
if err != nil {
return "", err
}
@@ -134,8 +134,8 @@ type LeaseEnder interface {
// This is typically used when modifying the repo, since the cache is not
// stable until after the modification is complete. A lease object will be
// returned that allows the caller to signal the end of the lease.
-func (LeaseKeyer) StartLease(repo *gitalypb.Repository) (LeaseEnder, error) {
- pendingPath, err := newPendingLease(repo)
+func (keyer LeaseKeyer) StartLease(repo *gitalypb.Repository) (LeaseEnder, error) {
+ pendingPath, err := keyer.newPendingLease(repo)
if err != nil {
return lease{}, err
}
@@ -143,6 +143,7 @@ func (LeaseKeyer) StartLease(repo *gitalypb.Repository) (LeaseEnder, error) {
return lease{
pendingPath: pendingPath,
repo: repo,
+ keyer: keyer,
}, nil
}
@@ -152,13 +153,13 @@ const staleAge = time.Hour
// KeyPath will attempt to return the unique keypath for a request in the
// specified repo for the current generation. The context must contain the gRPC
// method in its values.
-func (LeaseKeyer) KeyPath(ctx context.Context, repo *gitalypb.Repository, req proto.Message) (string, error) {
- pending, err := currentLeases(repo)
+func (keyer LeaseKeyer) KeyPath(ctx context.Context, repo *gitalypb.Repository, req proto.Message) (string, error) {
+ pending, err := keyer.currentLeases(repo)
if err != nil {
return "", err
}
- repoStatePath, err := getRepoStatePath(repo)
+ repoStatePath, err := keyer.getRepoStatePath(repo)
if err != nil {
return "", err
}
@@ -181,7 +182,7 @@ func (LeaseKeyer) KeyPath(ctx context.Context, repo *gitalypb.Repository, req pr
return "", countErr(ErrPendingExists)
}
- genID, err := currentGenID(ctx, repo)
+ genID, err := keyer.currentGenID(ctx, repo)
if err != nil {
return "", err
}
@@ -191,7 +192,7 @@ func (LeaseKeyer) KeyPath(ctx context.Context, repo *gitalypb.Repository, req pr
return "", err
}
- cDir, err := cacheDir(repo)
+ cDir, err := keyer.cacheDir(repo)
if err != nil {
return "", err
}
@@ -206,8 +207,8 @@ func radixPath(root, key string) (string, error) {
return filepath.Join(root, key[0:2], key[2:]), nil
}
-func newPendingLease(repo *gitalypb.Repository) (string, error) {
- repoStatePath, err := getRepoStatePath(repo)
+func (keyer LeaseKeyer) newPendingLease(repo *gitalypb.Repository) (string, error) {
+ repoStatePath, err := keyer.getRepoStatePath(repo)
if err != nil {
return "", err
}
@@ -236,37 +237,37 @@ func newPendingLease(repo *gitalypb.Repository) (string, error) {
}
// cacheDir is $STORAGE/+gitaly/cache
-func cacheDir(repo *gitalypb.Repository) (string, error) {
- s, ok := config.Config.Storage(repo.StorageName)
- if !ok {
+func (keyer LeaseKeyer) cacheDir(repo *gitalypb.Repository) (string, error) {
+ storagePath, err := keyer.locator.GetStorageByName(repo.StorageName)
+ if err != nil {
return "", fmt.Errorf("storage not found for %v", repo)
}
- return tempdir.CacheDir(s), nil
+ return tempdir.AppendCacheDir(storagePath), nil
}
-func getRepoStatePath(repo *gitalypb.Repository) (string, error) {
- s, ok := config.Config.Storage(repo.StorageName)
- if !ok {
+func (keyer LeaseKeyer) getRepoStatePath(repo *gitalypb.Repository) (string, error) {
+ storagePath, err := keyer.locator.GetStorageByName(repo.StorageName)
+ if err != nil {
return "", fmt.Errorf("getRepoStatePath: storage not found for %v", repo)
}
- stateDir := tempdir.StateDir(s)
+ stateDir := tempdir.AppendStateDir(storagePath)
relativePath := repo.GetRelativePath()
if len(relativePath) == 0 {
return "", fmt.Errorf("getRepoStatePath: relative path missing from %+v", repo)
}
- if _, err := storage.ValidateRelativePath(s.Path, relativePath); err != nil {
+ if _, err := storage.ValidateRelativePath(storagePath, relativePath); err != nil {
return "", fmt.Errorf("getRepoStatePath: %w", err)
}
return filepath.Join(stateDir, relativePath), nil
}
-func currentLeases(repo *gitalypb.Repository) ([]os.FileInfo, error) {
- repoStatePath, err := getRepoStatePath(repo)
+func (keyer LeaseKeyer) currentLeases(repo *gitalypb.Repository) ([]os.FileInfo, error) {
+ repoStatePath, err := keyer.getRepoStatePath(repo)
if err != nil {
return nil, err
}
@@ -285,8 +286,8 @@ func currentLeases(repo *gitalypb.Repository) ([]os.FileInfo, error) {
return pendings, nil
}
-func currentGenID(ctx context.Context, repo *gitalypb.Repository) (string, error) {
- repoStatePath, err := getRepoStatePath(repo)
+func (keyer LeaseKeyer) currentGenID(ctx context.Context, repo *gitalypb.Repository) (string, error) {
+ repoStatePath, err := keyer.getRepoStatePath(repo)
if err != nil {
return "", err
}
@@ -295,7 +296,7 @@ func currentGenID(ctx context.Context, repo *gitalypb.Repository) (string, error
switch {
case os.IsNotExist(err):
// latest file doesn't exist, so create one
- return updateLatest(ctx, repo)
+ return keyer.updateLatest(ctx, repo)
case err == nil:
return string(latestBytes), nil
default: