From bf18f7d5ad12ec0ec8910477bb7fbe49c3eda720 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Thu, 16 Jun 2022 16:43:49 +0100 Subject: [PATCH] Configurable cache sizees --- cmd/resolve-state/main.go | 2 +- federationapi/federationapi_keys_test.go | 2 +- internal/caching/caches.go | 14 -------------- internal/caching/impl_ristretto.go | 6 +++--- roomserver/internal/input/input_test.go | 2 +- setup/base/base.go | 2 +- setup/config/config_global.go | 19 +++++++++++++++++++ 7 files changed, 26 insertions(+), 21 deletions(-) diff --git a/cmd/resolve-state/main.go b/cmd/resolve-state/main.go index dccbc08a8..f26e81114 100644 --- a/cmd/resolve-state/main.go +++ b/cmd/resolve-state/main.go @@ -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) } diff --git a/federationapi/federationapi_keys_test.go b/federationapi/federationapi_keys_test.go index 036c1eebe..ea6ce1f8e 100644 --- a/federationapi/federationapi_keys_test.go +++ b/federationapi/federationapi_keys_test.go @@ -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()) } diff --git a/internal/caching/caches.go b/internal/caching/caches.go index 4e0bf9662..1a5133790 100644 --- a/internal/caching/caches.go +++ b/internal/caching/caches.go @@ -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) diff --git a/internal/caching/impl_ristretto.go b/internal/caching/impl_ristretto.go index 48c2f9880..9284397df 100644 --- a/internal/caching/impl_ristretto.go +++ b/internal/caching/impl_ristretto.go @@ -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]{ diff --git a/roomserver/internal/input/input_test.go b/roomserver/internal/input/input_test.go index 8b9f063e6..494fda354 100644 --- a/roomserver/internal/input/input_test.go +++ b/roomserver/internal/input/input_test.go @@ -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) } diff --git a/setup/base/base.go b/setup/base/base.go index 5cc90b46c..a02ea3d7f 100644 --- a/setup/base/base.go +++ b/setup/base/base.go @@ -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") } diff --git a/setup/config/config_global.go b/setup/config/config_global.go index 9d4c1485e..1b2c04611 100644 --- a/setup/config/config_global.go +++ b/setup/config/config_global.go @@ -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