diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2023-05-04 14:07:49 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2023-05-09 18:23:25 +0300 |
commit | 8cad2e1ab385d0209c6cb8593267dcfd15bb83e2 (patch) | |
tree | 244f518c3d85cff9e3d20ee1d9a7e8804fae2d70 | |
parent | 7c442aac5295e4a913e469188b82ed96c358306b (diff) |
datastore: Rename generic `cacheValue` type
The cache for consistent storages uses a `cacheValue` type to represent
cached information. This name is too generic to be useful as it only
tells the reader that it has something to do with caching, but it does
not give any hint at what kind of information is actually getting
cached.
Rename the type to `cachedReplicaInfo` to further clarify its intent.
-rw-r--r-- | internal/praefect/datastore/storage_provider.go | 35 |
1 files changed, 18 insertions, 17 deletions
diff --git a/internal/praefect/datastore/storage_provider.go b/internal/praefect/datastore/storage_provider.go index 6d8770dbb..5bc2f51bf 100644 --- a/internal/praefect/datastore/storage_provider.go +++ b/internal/praefect/datastore/storage_provider.go @@ -24,12 +24,18 @@ type ConsistentStoragesGetter interface { // errNotExistingVirtualStorage indicates that the requested virtual storage can't be found or not configured. var errNotExistingVirtualStorage = errors.New("virtual storage does not exist") +type cachedReplicaInfo struct { + replicaPath string + storages *datastructure.Set[string] +} + // CachingConsistentStoragesGetter is a ConsistentStoragesGetter that caches up to date storages by repository. // Each virtual storage has it's own cache that invalidates entries based on notifications. type CachingConsistentStoragesGetter struct { csg ConsistentStoragesGetter - // caches is per virtual storage cache. It is initialized once on construction. - caches map[string]*lru.Cache[string, cacheValue] + // caches is per virtual storage cache. The caches themselves contain information about + // replicas keyed by their respective relative paths. + caches map[string]*lru.Cache[string, cachedReplicaInfo] // access is access method to use: 0 - without caching; 1 - with caching. access int32 // syncer allows to sync retrieval operations to omit unnecessary runs. @@ -43,7 +49,7 @@ type CachingConsistentStoragesGetter struct { func NewCachingConsistentStoragesGetter(logger logrus.FieldLogger, csg ConsistentStoragesGetter, virtualStorages []string) (*CachingConsistentStoragesGetter, error) { cached := &CachingConsistentStoragesGetter{ csg: csg, - caches: make(map[string]*lru.Cache[string, cacheValue], len(virtualStorages)), + caches: make(map[string]*lru.Cache[string, cachedReplicaInfo], len(virtualStorages)), syncer: syncer{inflight: map[string]chan struct{}{}}, callbackLogger: logger.WithField("component", "caching_storage_provider"), cacheAccessTotal: prometheus.NewCounterVec( @@ -57,7 +63,7 @@ func NewCachingConsistentStoragesGetter(logger logrus.FieldLogger, csg Consisten for _, virtualStorage := range virtualStorages { virtualStorage := virtualStorage - cache, err := lru.NewWithEvict(2<<20, func(key string, value cacheValue) { + cache, err := lru.NewWithEvict(2<<20, func(key string, value cachedReplicaInfo) { cached.cacheAccessTotal.WithLabelValues(virtualStorage, "evict").Inc() }) if err != nil { @@ -134,12 +140,12 @@ func (c *CachingConsistentStoragesGetter) cacheMiss(ctx context.Context, virtual return c.csg.GetConsistentStorages(ctx, virtualStorage, relativePath) } -func (c *CachingConsistentStoragesGetter) tryCache(virtualStorage, relativePath string) (func(), *lru.Cache[string, cacheValue], cacheValue, bool) { +func (c *CachingConsistentStoragesGetter) tryCache(virtualStorage, relativePath string) (func(), *lru.Cache[string, cachedReplicaInfo], cachedReplicaInfo, bool) { populateDone := func() {} // should be called AFTER any cache population is done cache, found := c.caches[virtualStorage] if !found { - return populateDone, nil, cacheValue{}, false + return populateDone, nil, cachedReplicaInfo{}, false } if storages, found := cache.Get(relativePath); found { @@ -153,7 +159,7 @@ func (c *CachingConsistentStoragesGetter) tryCache(virtualStorage, relativePath return populateDone, cache, storages, true } - return populateDone, cache, cacheValue{}, false + return populateDone, cache, cachedReplicaInfo{}, false } func (c *CachingConsistentStoragesGetter) isCacheEnabled() bool { @@ -162,34 +168,29 @@ func (c *CachingConsistentStoragesGetter) isCacheEnabled() bool { // GetConsistentStorages returns the replica path and the set of up to date storages for the given repository keyed by virtual storage and relative path. func (c *CachingConsistentStoragesGetter) GetConsistentStorages(ctx context.Context, virtualStorage, relativePath string) (string, *datastructure.Set[string], error) { - var cache *lru.Cache[string, cacheValue] + var cache *lru.Cache[string, cachedReplicaInfo] if c.isCacheEnabled() { - var value cacheValue + var info cachedReplicaInfo var ok bool var populationDone func() - populationDone, cache, value, ok = c.tryCache(virtualStorage, relativePath) + populationDone, cache, info, ok = c.tryCache(virtualStorage, relativePath) defer populationDone() if ok { c.cacheAccessTotal.WithLabelValues(virtualStorage, "hit").Inc() - return value.replicaPath, value.storages, nil + return info.replicaPath, info.storages, nil } } replicaPath, storages, err := c.cacheMiss(ctx, virtualStorage, relativePath) if err == nil && cache != nil { - cache.Add(relativePath, cacheValue{replicaPath: replicaPath, storages: storages}) + cache.Add(relativePath, cachedReplicaInfo{replicaPath: replicaPath, storages: storages}) c.cacheAccessTotal.WithLabelValues(virtualStorage, "populate").Inc() } return replicaPath, storages, err } -type cacheValue struct { - replicaPath string - storages *datastructure.Set[string] -} - // syncer allows to sync access to a particular key. type syncer struct { // inflight contains set of keys already acquired for sync. |