diff --git a/cmd/resolve-state/main.go b/cmd/resolve-state/main.go index f26e81114..74d41c2aa 100644 --- a/cmd/resolve-state/main.go +++ b/cmd/resolve-state/main.go @@ -7,6 +7,7 @@ import ( "sort" "strconv" "strings" + "time" "github.com/matrix-org/dendrite/internal/caching" "github.com/matrix-org/dendrite/roomserver/state" @@ -53,7 +54,7 @@ func main() { fmt.Println("Fetching", len(snapshotNIDs), "snapshot NIDs") - cache, err := caching.NewRistrettoCache(128*1024*1024, true) + cache, err := caching.NewRistrettoCache(128*1024*1024, time.Hour, true) if err != nil { panic(err) } diff --git a/dendrite-sample.monolith.yaml b/dendrite-sample.monolith.yaml index d426b410a..88f259ef6 100644 --- a/dendrite-sample.monolith.yaml +++ b/dendrite-sample.monolith.yaml @@ -44,10 +44,18 @@ global: # Configuration for in-process caches, to help speed up processing transactions # and reducing load on the database. cache: - # The estimated maximum size for the global cache in bytes. Note that this is - # not a hard limit, nor is it a memory limit for the entire process. A cache that - # is too small may end up yielding little benefit. - max_bytes_est: 1073741824 + # The estimated maximum size for the global cache in bytes, or in terabytes, + # gigabytes, megabytes or kilobytes when the appropriate 'tb', 'gb', 'mb' or + # 'kb' suffix is specified. Note that this is not a hard limit, nor is it a + # memory limit for the entire process. A cache that is too small may ultimately + # provide little or no benefit. + max_bytes_est: 1gb + + # The maximum amount of time that a cache entry can live for in memory before + # it will be evicted and/or refreshed from the database. Lower values result in + # easier admission of new cache entries but may also increase database load in + # comparison to higher values, so adjust conservatively. + max_age: 1h # The server name to delegate server-server communications to, with optional port # e.g. localhost:443 diff --git a/dendrite-sample.polylith.yaml b/dendrite-sample.polylith.yaml index 9c4a348dd..b94d828ca 100644 --- a/dendrite-sample.polylith.yaml +++ b/dendrite-sample.polylith.yaml @@ -34,10 +34,18 @@ global: # Configuration for in-process caches, to help speed up processing transactions # and reducing load on the database. cache: - # The estimated maximum size for the global cache in bytes. Note that this is - # not a hard limit, nor is it a memory limit for the entire process. A cache that - # is too small may end up yielding little benefit. - max_bytes_est: 1073741824 + # The estimated maximum size for the global cache in bytes, or in terabytes, + # gigabytes, megabytes or kilobytes when the appropriate 'tb', 'gb', 'mb' or + # 'kb' suffix is specified. Note that this is not a hard limit, nor is it a + # memory limit for the entire process. A cache that is too small may ultimately + # provide little or no benefit. + max_bytes_est: 1gb + + # The maximum amount of time that a cache entry can live for in memory before + # it will be evicted and/or refreshed from the database. Lower values result in + # easier admission of new cache entries but may also increase database load in + # comparison to higher values, so adjust conservatively. + max_age: 1h # The server name to delegate server-server communications to, with optional port # e.g. localhost:443 diff --git a/federationapi/federationapi_keys_test.go b/federationapi/federationapi_keys_test.go index ea6ce1f8e..5860ff2d0 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*1024*1024, false) + s.cache, err = caching.NewRistrettoCache(8*1024*1024, time.Hour, false) if err != nil { panic("can't create cache: " + err.Error()) } diff --git a/internal/caching/impl_ristretto.go b/internal/caching/impl_ristretto.go index 3f63026ae..4c5e5a6ef 100644 --- a/internal/caching/impl_ristretto.go +++ b/internal/caching/impl_ristretto.go @@ -57,37 +57,37 @@ const ( lazyLoadingCache ) -func NewRistrettoCache(maxCost config.DataUnit, enablePrometheus bool) (*Caches, error) { +func NewRistrettoCache(maxCost config.DataUnit, maxAge time.Duration, enablePrometheus bool) (*Caches, error) { cache := MustCreateCache(maxCost, enablePrometheus) return &Caches{ RoomVersions: &RistrettoCachePartition[string, gomatrixserverlib.RoomVersion]{ cache: cache, Prefix: roomVersionsCache, - MaxAge: time.Hour, + MaxAge: maxAge, }, ServerKeys: &RistrettoCachePartition[string, gomatrixserverlib.PublicKeyLookupResult]{ cache: cache, Prefix: serverKeysCache, Mutable: true, - MaxAge: time.Hour, + MaxAge: maxAge, }, RoomServerRoomIDs: &RistrettoCachePartition[int64, string]{ cache: cache, Prefix: roomIDsCache, - MaxAge: time.Hour, + MaxAge: maxAge, }, RoomServerEvents: &RistrettoCostedCachePartition[int64, *gomatrixserverlib.Event]{ &RistrettoCachePartition[int64, *gomatrixserverlib.Event]{ cache: cache, Prefix: roomEventsCache, - MaxAge: time.Hour, + MaxAge: maxAge, }, }, RoomInfos: &RistrettoCachePartition[string, types.RoomInfo]{ cache: cache, Prefix: roomInfosCache, Mutable: true, - MaxAge: time.Hour, + MaxAge: maxAge, }, FederationPDUs: &RistrettoCostedCachePartition[int64, *gomatrixserverlib.HeaderedEvent]{ &RistrettoCachePartition[int64, *gomatrixserverlib.HeaderedEvent]{ @@ -109,13 +109,13 @@ func NewRistrettoCache(maxCost config.DataUnit, enablePrometheus bool) (*Caches, cache: cache, Prefix: spaceSummaryRoomsCache, Mutable: true, - MaxAge: time.Hour, + MaxAge: maxAge, }, LazyLoading: &RistrettoCachePartition[string, any]{ // TODO: type cache: cache, Prefix: lazyLoadingCache, Mutable: true, - MaxAge: time.Hour, + MaxAge: maxAge, }, }, nil } diff --git a/roomserver/internal/input/input_test.go b/roomserver/internal/input/input_test.go index 494fda354..9108f0ad9 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*1024*1024, false) + cache, err := caching.NewRistrettoCache(8*1024*1024, time.Hour, false) if err != nil { t.Fatal(err) } diff --git a/setup/base/base.go b/setup/base/base.go index 85698cedc..e1a9a4dc0 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(cfg.Global.Cache.EstMaxSize, enableMetrics) + cache, err := caching.NewRistrettoCache(cfg.Global.Cache.EstMaxSize, cfg.Global.Cache.MaxAge, 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 343a23eaf..525e36ed6 100644 --- a/setup/config/config_global.go +++ b/setup/config/config_global.go @@ -176,11 +176,13 @@ func (c *ServerNotices) Defaults(generate bool) { func (c *ServerNotices) Verify(errors *ConfigErrors, isMonolith bool) {} type Cache struct { - EstMaxSize DataUnit `yaml:"max_bytes_est"` + EstMaxSize DataUnit `yaml:"max_bytes_est"` + MaxAge time.Duration `yaml:"max_age"` } func (c *Cache) Defaults(generate bool) { c.EstMaxSize = 1024 * 1024 * 1024 // 1GB + c.MaxAge = time.Hour } func (c *Cache) Verify(errors *ConfigErrors, isMonolith bool) {