Configurable cache sizees

This commit is contained in:
Neil Alexander 2022-06-16 16:43:49 +01:00
parent 4aaed3aff3
commit bf18f7d5ad
No known key found for this signature in database
GPG key ID: A02A2019A2BB0944
7 changed files with 26 additions and 21 deletions

View file

@ -53,7 +53,7 @@ func main() {
fmt.Println("Fetching", len(snapshotNIDs), "snapshot NIDs")
cache, err := caching.NewRistrettoCache(128*caching.MB, true)
cache, err := caching.NewRistrettoCache(128*1024*1024, true)
if err != nil {
panic(err)
}

View file

@ -64,7 +64,7 @@ func TestMain(m *testing.M) {
}
// Create a new cache but don't enable prometheus!
s.cache, err = caching.NewRistrettoCache(8*caching.MB, false)
s.cache, err = caching.NewRistrettoCache(8*1024*1024, false)
if err != nil {
panic("can't create cache: " + err.Error())
}

View file

@ -1,8 +1,6 @@
package caching
import (
"time"
"github.com/matrix-org/dendrite/roomserver/types"
"github.com/matrix-org/gomatrixserverlib"
)
@ -38,15 +36,3 @@ type keyable interface {
type costable interface {
CacheCost() int
}
type CacheSize int64
const (
_ = iota
KB CacheSize = 1 << (10 * iota)
MB
GB
TB
)
const CacheNoMaxAge = time.Duration(0)

View file

@ -14,10 +14,10 @@ import (
"github.com/prometheus/client_golang/prometheus/promauto"
)
func MustCreateCache(maxCost CacheSize, enablePrometheus bool) *ristretto.Cache {
func MustCreateCache(maxCost int64, enablePrometheus bool) *ristretto.Cache {
cache, err := ristretto.NewCache(&ristretto.Config{
NumCounters: 1e5,
MaxCost: int64(maxCost),
MaxCost: maxCost,
BufferItems: 64,
Metrics: true,
KeyToHash: func(key interface{}) (uint64, uint64) {
@ -56,7 +56,7 @@ const (
lazyLoadingCache
)
func NewRistrettoCache(maxCost CacheSize, enablePrometheus bool) (*Caches, error) {
func NewRistrettoCache(maxCost int64, enablePrometheus bool) (*Caches, error) {
cache := MustCreateCache(maxCost, enablePrometheus)
return &Caches{
RoomVersions: &RistrettoCachePartition[string, gomatrixserverlib.RoomVersion]{

View file

@ -48,7 +48,7 @@ func TestSingleTransactionOnInput(t *testing.T) {
Kind: api.KindOutlier, // don't panic if we generate an output event
Event: event.Headered(gomatrixserverlib.RoomVersionV6),
}
cache, err := caching.NewRistrettoCache(8*caching.MB, false)
cache, err := caching.NewRistrettoCache(8*1024*1024, false)
if err != nil {
t.Fatal(err)
}

View file

@ -161,7 +161,7 @@ func NewBaseDendrite(cfg *config.Dendrite, componentName string, options ...Base
}
}
cache, err := caching.NewRistrettoCache(1*caching.GB, enableMetrics)
cache, err := caching.NewRistrettoCache(cfg.Global.Caches.EstMaxSize, enableMetrics)
if err != nil {
logrus.WithError(err).Warnf("Failed to create cache")
}

View file

@ -73,6 +73,9 @@ type Global struct {
// ReportStats configures opt-in anonymous stats reporting.
ReportStats ReportStats `yaml:"report_stats"`
// Configuration for the caches.
Caches Caches `yaml:"caches"`
}
func (c *Global) Defaults(generate bool) {
@ -90,6 +93,7 @@ func (c *Global) Defaults(generate bool) {
c.Sentry.Defaults()
c.ServerNotices.Defaults(generate)
c.ReportStats.Defaults()
c.Caches.Defaults(generate)
}
func (c *Global) Verify(configErrs *ConfigErrors, isMonolith bool) {
@ -102,6 +106,7 @@ func (c *Global) Verify(configErrs *ConfigErrors, isMonolith bool) {
c.DNSCache.Verify(configErrs, isMonolith)
c.ServerNotices.Verify(configErrs, isMonolith)
c.ReportStats.Verify(configErrs, isMonolith)
c.Caches.Verify(configErrs, isMonolith)
}
type OldVerifyKeys struct {
@ -168,6 +173,20 @@ func (c *ServerNotices) Defaults(generate bool) {
func (c *ServerNotices) Verify(errors *ConfigErrors, isMonolith bool) {}
type Caches struct {
Enabled bool `yaml:"enabled"`
EstMaxSize int64 `yaml:"max_bytes_est"`
}
func (c *Caches) Defaults(generate bool) {
c.Enabled = true
c.EstMaxSize = 1024 * 1024 * 1024 // 1GB
}
func (c *Caches) Verify(errors *ConfigErrors, isMonolith bool) {
checkPositive(errors, "max_bytes_est", int64(c.EstMaxSize))
}
// ReportStats configures opt-in anonymous stats reporting.
type ReportStats struct {
// Enabled configures anonymous usage stats of the server