diff --git a/cmd/gitness/wire_gen.go b/cmd/gitness/wire_gen.go index fbfc94776f..fb8fa414db 100644 --- a/cmd/gitness/wire_gen.go +++ b/cmd/gitness/wire_gen.go @@ -228,12 +228,13 @@ func initSystem(ctx context.Context, config *types.Config) (*server.System, erro registryRepository := database2.ProvideRegistryDao(db, mediaTypesRepository) evictor2 := cache2.ProvideEvictorRegistryCore(pubSub) registryIDCache := cache2.ProvideRegistryIDCache(ctx, registryRepository, evictor2) + registryUUIDCache := cache2.ProvideRegistryUUIDCache(ctx, registryRepository, evictor2) registryRootRefCache := cache2.ProvideRegRootRefCache(ctx, registryRepository, evictor2) upstreamProxyConfigRepository := database2.ProvideUpstreamDao(db, registryRepository, spaceFinder) evictor3 := cache2.ProvideEvictorUpstreamProxy(pubSub) upstreamProxyRegistryIDCache := cache2.ProvideUpstreamProxyRegistryIDCache(ctx, upstreamProxyConfigRepository, evictor3) upstreamProxyFinder := refcache2.ProvideUpstreamProxyFinder(upstreamProxyConfigRepository, upstreamProxyRegistryIDCache, evictor3) - registryFinder := refcache2.ProvideRegistryFinder(registryRepository, registryIDCache, registryRootRefCache, evictor2, spaceFinder, upstreamProxyFinder) + registryFinder := refcache2.ProvideRegistryFinder(registryRepository, registryIDCache, registryUUIDCache, registryRootRefCache, evictor2, spaceFinder, upstreamProxyFinder) publicaccessService := publicaccess.ProvidePublicAccess(config, publicAccessStore, spaceFinder, repoFinder, registryFinder) authorizer := authz.ProvideAuthorizer(permissionCache, spaceFinder, publicaccessService) principalUIDTransformation := store.ProvidePrincipalUIDTransformation() diff --git a/registry/app/api/controller/mocks/registry_finder.go b/registry/app/api/controller/mocks/registry_finder.go index e00abae316..bb07aaf5de 100644 --- a/registry/app/api/controller/mocks/registry_finder.go +++ b/registry/app/api/controller/mocks/registry_finder.go @@ -5,6 +5,7 @@ package mocks import ( context "context" + uuid "github.com/google/uuid" mock "github.com/stretchr/testify/mock" types "github.com/harness/gitness/registry/types" @@ -130,6 +131,65 @@ func (_c *RegistryFinder_FindByID_Call) RunAndReturn(run func(context.Context, i return _c } +// FindByUUID provides a mock function with given fields: ctx, repoUUID +func (_m *RegistryFinder) FindByUUID(ctx context.Context, repoUUID uuid.UUID) (*types.Registry, error) { + ret := _m.Called(ctx, repoUUID) + + if len(ret) == 0 { + panic("no return value specified for FindByUUID") + } + + var r0 *types.Registry + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, uuid.UUID) (*types.Registry, error)); ok { + return rf(ctx, repoUUID) + } + if rf, ok := ret.Get(0).(func(context.Context, uuid.UUID) *types.Registry); ok { + r0 = rf(ctx, repoUUID) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*types.Registry) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, uuid.UUID) error); ok { + r1 = rf(ctx, repoUUID) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// RegistryFinder_FindByUUID_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'FindByUUID' +type RegistryFinder_FindByUUID_Call struct { + *mock.Call +} + +// FindByUUID is a helper method to define mock.On call +// - ctx context.Context +// - repoUUID uuid.UUID +func (_e *RegistryFinder_Expecter) FindByUUID(ctx interface{}, repoUUID interface{}) *RegistryFinder_FindByUUID_Call { + return &RegistryFinder_FindByUUID_Call{Call: _e.mock.On("FindByUUID", ctx, repoUUID)} +} + +func (_c *RegistryFinder_FindByUUID_Call) Run(run func(ctx context.Context, repoUUID uuid.UUID)) *RegistryFinder_FindByUUID_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(uuid.UUID)) + }) + return _c +} + +func (_c *RegistryFinder_FindByUUID_Call) Return(_a0 *types.Registry, _a1 error) *RegistryFinder_FindByUUID_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *RegistryFinder_FindByUUID_Call) RunAndReturn(run func(context.Context, uuid.UUID) (*types.Registry, error)) *RegistryFinder_FindByUUID_Call { + _c.Call.Return(run) + return _c +} + // FindByRootParentID provides a mock function with given fields: ctx, rootParentID, regIdentifier func (_m *RegistryFinder) FindByRootParentID(ctx context.Context, rootParentID int64, regIdentifier string) (*types.Registry, error) { ret := _m.Called(ctx, rootParentID, regIdentifier) diff --git a/registry/app/services/refcache/reg_finder.go b/registry/app/services/refcache/reg_finder.go index cea3c42530..729630b8b3 100644 --- a/registry/app/services/refcache/reg_finder.go +++ b/registry/app/services/refcache/reg_finder.go @@ -22,11 +22,14 @@ import ( "github.com/harness/gitness/app/store/cache" "github.com/harness/gitness/registry/app/store" "github.com/harness/gitness/registry/types" + + "github.com/google/uuid" ) type RegistryFinder interface { MarkChanged(ctx context.Context, reg *types.Registry) FindByID(ctx context.Context, repoID int64) (*types.Registry, error) + FindByUUID(ctx context.Context, repoUUID uuid.UUID) (*types.Registry, error) FindByRootRef(ctx context.Context, rootParentRef string, regIdentifier string) ( *types.Registry, error, @@ -42,6 +45,7 @@ type RegistryFinder interface { type registryFinder struct { inner store.RegistryRepository regIDCache store.RegistryIDCache + regUUIDCache store.RegistryUUIDCache regRootRefCache store.RegistryRootRefCache spaceFinder refcache.SpaceFinder evictor cache.Evictor[*types.Registry] @@ -51,6 +55,7 @@ type registryFinder struct { func NewRegistryFinder( registryRepository store.RegistryRepository, regIDCache store.RegistryIDCache, + regUUIDCache store.RegistryUUIDCache, regRootRefCache store.RegistryRootRefCache, evictor cache.Evictor[*types.Registry], spaceFinder refcache.SpaceFinder, @@ -59,6 +64,7 @@ func NewRegistryFinder( return registryFinder{ inner: registryRepository, regIDCache: regIDCache, + regUUIDCache: regUUIDCache, regRootRefCache: regRootRefCache, evictor: evictor, spaceFinder: spaceFinder, @@ -82,6 +88,10 @@ func (r registryFinder) FindByID(ctx context.Context, repoID int64) (*types.Regi return r.regIDCache.Get(ctx, repoID) } +func (r registryFinder) FindByUUID(ctx context.Context, repoUUID uuid.UUID) (*types.Registry, error) { + return r.regUUIDCache.Get(ctx, repoUUID.String()) +} + func (r registryFinder) FindByRootRef(ctx context.Context, rootParentRef string, regIdentifier string) ( *types.Registry, error, diff --git a/registry/app/services/refcache/wire.go b/registry/app/services/refcache/wire.go index 7a400adbfa..b76db15dbc 100644 --- a/registry/app/services/refcache/wire.go +++ b/registry/app/services/refcache/wire.go @@ -26,12 +26,21 @@ import ( func ProvideRegistryFinder( registryRepository store.RegistryRepository, regIDCache store.RegistryIDCache, + regUUIDCache store.RegistryUUIDCache, regRootRefCache store.RegistryRootRefCache, evictor cache.Evictor[*types.Registry], spaceFinder refcache.SpaceFinder, upstreamProxyFinder UpstreamProxyFinder, ) RegistryFinder { - return NewRegistryFinder(registryRepository, regIDCache, regRootRefCache, evictor, spaceFinder, upstreamProxyFinder) + return NewRegistryFinder( + registryRepository, + regIDCache, + regUUIDCache, + regRootRefCache, + evictor, + spaceFinder, + upstreamProxyFinder, + ) } func ProvideUpstreamProxyFinder( diff --git a/registry/app/store/cache.go b/registry/app/store/cache.go index b22c0a6160..b744467c71 100644 --- a/registry/app/store/cache.go +++ b/registry/app/store/cache.go @@ -22,5 +22,6 @@ import ( type ( RegistryIDCache cache.Cache[int64, *types.Registry] RegistryRootRefCache cache.Cache[types.RegistryRootRefCacheKey, int64] + RegistryUUIDCache cache.Cache[string, *types.Registry] UpstreamProxyRegistryIDCache cache.Cache[int64, *types.UpstreamProxy] ) diff --git a/registry/app/store/cache/reg_uuid.go b/registry/app/store/cache/reg_uuid.go new file mode 100644 index 0000000000..108cf9b885 --- /dev/null +++ b/registry/app/store/cache/reg_uuid.go @@ -0,0 +1,55 @@ +// Copyright 2023 Harness, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cache + +import ( + "context" + "fmt" + "time" + + cache2 "github.com/harness/gitness/app/store/cache" + "github.com/harness/gitness/cache" + "github.com/harness/gitness/registry/app/store" + "github.com/harness/gitness/registry/types" +) + +func NewRegistryUUIDCache( + appCtx context.Context, + regSource store.RegistryRepository, + evictorRepo cache2.Evictor[*types.Registry], + dur time.Duration, +) store.RegistryUUIDCache { + c := cache.New[string, *types.Registry](registryUUIDCacheGetter{regSource: regSource}, dur) + + evictorRepo.Subscribe(appCtx, func(repoCore *types.Registry) error { + c.Evict(appCtx, repoCore.UUID) + return nil + }) + + return c +} + +type registryUUIDCacheGetter struct { + regSource store.RegistryRepository +} + +func (c registryUUIDCacheGetter) Find(ctx context.Context, repoUUID string) (*types.Registry, error) { + repo, err := c.regSource.GetByUUID(ctx, repoUUID) + if err != nil { + return nil, fmt.Errorf("failed to find repo by UUID: %w", err) + } + + return repo, nil +} diff --git a/registry/app/store/cache/wire.go b/registry/app/store/cache/wire.go index a01832befc..3330a1a9cb 100644 --- a/registry/app/store/cache/wire.go +++ b/registry/app/store/cache/wire.go @@ -61,6 +61,14 @@ func ProvideRegRootRefCache( return NewRegistryRootRefCache(appCtx, regSource, evictorRepo, registryCacheDuration) } +func ProvideRegistryUUIDCache( + appCtx context.Context, + regSource store.RegistryRepository, + evictorRepo cache.Evictor[*types.Registry], +) store.RegistryUUIDCache { + return NewRegistryUUIDCache(appCtx, regSource, evictorRepo, registryCacheDuration) +} + func ProvideUpstreamProxyRegistryIDCache( appCtx context.Context, upstreamProxySource store.UpstreamProxyConfigRepository, @@ -74,5 +82,6 @@ var WireSet = wire.NewSet( ProvideEvictorUpstreamProxy, ProvideRegRootRefCache, ProvideRegistryIDCache, + ProvideRegistryUUIDCache, ProvideUpstreamProxyRegistryIDCache, )