Skip to content

Commit

Permalink
fixed issues reported by linter
Browse files Browse the repository at this point in the history
Signed-off-by: Vladyslav Diachenko <[email protected]>
  • Loading branch information
vlad-diachenko committed Dec 6, 2023
1 parent d549044 commit d6f618e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
18 changes: 9 additions & 9 deletions pkg/storage/chunk/cache/embeddedcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ type EmbeddedCache[K comparable, V any] struct {
memoryBytes prometheus.Gauge
}

type CacheEntry[K comparable, V any] struct {
type Entry[K comparable, V any] struct {
updated time.Time
Key K
Value V
Expand Down Expand Up @@ -91,7 +91,7 @@ func (cfg *EmbeddedCacheConfig) IsEnabled() bool {
return cfg.Enabled
}

type cacheEntrySizeCalculator[K comparable, V any] func(entry *CacheEntry[K, V]) uint64
type cacheEntrySizeCalculator[K comparable, V any] func(entry *Entry[K, V]) uint64

// NewEmbeddedCache returns a new initialised EmbeddedCache where the key is a string and the value is a slice of bytes.
func NewEmbeddedCache(name string, cfg EmbeddedCacheConfig, reg prometheus.Registerer, logger log.Logger, cacheType stats.CacheType) *EmbeddedCache[string, []byte] {
Expand Down Expand Up @@ -195,7 +195,7 @@ func (c *EmbeddedCache[K, V]) pruneExpiredItems(ttl time.Duration) {
defer c.lock.Unlock()

for k, v := range c.entries {
entry := v.Value.(*CacheEntry[K, V])
entry := v.Value.(*Entry[K, V])
if time.Since(entry.updated) > ttl {
c.remove(k, v, expiredReason)
}
Expand Down Expand Up @@ -248,7 +248,7 @@ func (c *EmbeddedCache[K, V]) GetCacheType() stats.CacheType {
}

func (c *EmbeddedCache[K, V]) remove(key K, element *list.Element, reason string) {
entry := c.lru.Remove(element).(*CacheEntry[K, V])
entry := c.lru.Remove(element).(*Entry[K, V])
delete(c.entries, key)
if c.onEntryRemoved != nil {
c.onEntryRemoved(entry.Key, entry.Value)
Expand All @@ -266,7 +266,7 @@ func (c *EmbeddedCache[K, V]) put(key K, value V) {
c.remove(key, element, replacedReason)
}

entry := &CacheEntry[K, V]{
entry := &Entry[K, V]{
updated: time.Now(),
Key: key,
Value: value,
Expand All @@ -289,7 +289,7 @@ func (c *EmbeddedCache[K, V]) put(key K, value V) {
if lastElement == nil {
break
}
entryToRemove := lastElement.Value.(*CacheEntry[K, V])
entryToRemove := lastElement.Value.(*Entry[K, V])
c.remove(entryToRemove.Key, lastElement, fullReason)
}

Expand All @@ -310,15 +310,15 @@ func (c *EmbeddedCache[K, V]) Get(_ context.Context, key K) (V, bool) {

element, ok := c.entries[key]
if ok {
entry := element.Value.(*CacheEntry[K, V])
entry := element.Value.(*Entry[K, V])
return entry.Value, true
}
var empty V
return empty, false
}

func sizeOf(item *CacheEntry[string, []byte]) uint64 {
return uint64(int(unsafe.Sizeof(*item)) + // size of CacheEntry
func sizeOf(item *Entry[string, []byte]) uint64 {
return uint64(int(unsafe.Sizeof(*item)) + // size of Entry
len(item.Key) + // size of Key
cap(item.Value) + // size of Value
elementSize + // size of the element in linked list
Expand Down
10 changes: 5 additions & 5 deletions pkg/storage/chunk/cache/embeddedcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ func TestEmbeddedCacheEviction(t *testing.T) {
// compute value size such that 10 entries account to exactly 1MB.
// adding one more entry to the cache would result in eviction when MaxSizeMB is configured to a value of 1.
// value cap = target size of each entry (0.1MB) - size of cache entry with empty value.
valueCap := (1e6 / cnt) - sizeOf(&CacheEntry[string, []byte]{
valueCap := (1e6 / cnt) - sizeOf(&Entry[string, []byte]{
Key: "00",
})

itemTemplate := &CacheEntry[string, []byte]{
itemTemplate := &Entry[string, []byte]{
Key: "00",
Value: make([]byte, 0, valueCap),
}
Expand Down Expand Up @@ -176,9 +176,9 @@ func TestEmbeddedCacheExpiry(t *testing.T) {
key1, key2, key3, key4 := "01", "02", "03", "04"
data1, data2, data3, data4 := genBytes(32), genBytes(64), genBytes(128), genBytes(32)

memorySz := sizeOf(&CacheEntry[string, []byte]{Key: key1, Value: data1}) +
sizeOf(&CacheEntry[string, []byte]{Key: key2, Value: data2}) +
sizeOf(&CacheEntry[string, []byte]{Key: key3, Value: data3})
memorySz := sizeOf(&Entry[string, []byte]{Key: key1, Value: data1}) +
sizeOf(&Entry[string, []byte]{Key: key2, Value: data2}) +
sizeOf(&Entry[string, []byte]{Key: key3, Value: data3})

cfg := EmbeddedCacheConfig{
MaxSizeItems: 3,
Expand Down
7 changes: 4 additions & 3 deletions pkg/storage/stores/shipper/bloomshipper/block_downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ func NewBlocksCache(config config.Config, reg prometheus.Registerer, logger log.
})
}

func calculateBlockDirectorySize(entry *cache.CacheEntry[string, *cachedBlock]) uint64 {
func calculateBlockDirectorySize(entry *cache.Entry[string, *cachedBlock]) uint64 {
value := entry.Value
bloomFileStats, _ := os.Lstat(path.Join(value.blockDirectory, v1.BloomFileName))
seriesFileStats, _ := os.Lstat(path.Join(value.blockDirectory, v1.SeriesFileName))
Expand Down Expand Up @@ -372,10 +372,11 @@ const defaultActiveQueriersCheckInterval = 100 * time.Millisecond
func (b *cachedBlock) removeDirectoryAsync() {
go func() {
timeout := time.After(b.removeDirectoryTimeout)
tick := time.Tick(b.activeQueriersCheckInterval)
ticker := time.NewTicker(b.activeQueriersCheckInterval)
defer ticker.Stop()
for {
select {
case <-tick:
case <-ticker.C:
if b.activeQueriers.Load() == 0 {
err := deleteFolder(b.blockDirectory)
if err == nil {
Expand Down

0 comments on commit d6f618e

Please sign in to comment.