-
Notifications
You must be signed in to change notification settings - Fork 812
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: SungJin1212 <tjdwls1201@gmail.com>
- Loading branch information
1 parent
fbe118b
commit 975b5c3
Showing
8 changed files
with
179 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package tsdb | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/go-kit/log" | ||
"github.com/pkg/errors" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/thanos-io/thanos/pkg/cache" | ||
"github.com/thanos-io/thanos/pkg/model" | ||
|
||
"github.com/VictoriaMetrics/fastcache" | ||
) | ||
|
||
type InMemoryChunkCache struct { | ||
cache *cache.InMemoryCache | ||
fcache *fastcache.Cache | ||
name string | ||
} | ||
|
||
func newInMemoryChunkCache(name string, cfg InMemoryChunkCacheConfig, logger log.Logger, reg prometheus.Registerer) (*InMemoryChunkCache, error) { | ||
maxCacheSize := model.Bytes(cfg.MaxSizeBytes) | ||
|
||
// Calculate the max item size. | ||
maxItemSize := defaultMaxItemSize | ||
if maxItemSize > maxCacheSize { | ||
maxItemSize = maxCacheSize | ||
} | ||
|
||
config := cache.InMemoryCacheConfig{ | ||
MaxSize: maxCacheSize, | ||
MaxItemSize: maxItemSize, | ||
} | ||
|
||
inMemoryCache, err := cache.NewInMemoryCacheWithConfig(name, logger, reg, config) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "create in-memory chunk cache") | ||
} | ||
|
||
fcache := fastcache.New(int(config.MaxSize)) | ||
|
||
inMemoryChunkCache := &InMemoryChunkCache{ | ||
cache: inMemoryCache, | ||
fcache: fcache, | ||
name: name, | ||
} | ||
|
||
return inMemoryChunkCache, nil | ||
} | ||
|
||
func (c *InMemoryChunkCache) Store(data map[string][]byte, ttl time.Duration) { | ||
c.cache.Store(data, ttl) | ||
} | ||
|
||
// Fetch fetches multiple keys and returns a map containing cache hits | ||
// In case of error, it logs and return an empty cache hits map. | ||
func (c *InMemoryChunkCache) Fetch(ctx context.Context, keys []string) map[string][]byte { | ||
return c.cache.Fetch(ctx, keys) | ||
} | ||
|
||
func (c *InMemoryChunkCache) Name() string { | ||
return c.name | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package tsdb | ||
|
||
import ( | ||
"flag" | ||
|
||
"github.com/alecthomas/units" | ||
) | ||
|
||
type InMemoryChunkCacheConfig struct { | ||
MaxSizeBytes uint64 `yaml:"max_size_bytes"` | ||
} | ||
|
||
func (cfg *InMemoryChunkCacheConfig) RegisterFlagsWithPrefix(f *flag.FlagSet, prefix string) { | ||
f.Uint64Var(&cfg.MaxSizeBytes, prefix+"max-size-bytes", uint64(1*units.Gibibyte), "Maximum size in bytes of in-memory chunk cache used to speed up chunk lookups (shared between all tenants).") | ||
} |