Use separate caches for hopefully better hash distribution

This commit is contained in:
Neil Alexander 2022-06-15 09:36:33 +01:00
parent b7a908305e
commit 07ce5b185f
No known key found for this signature in database
GPG key ID: A02A2019A2BB0944

View file

@ -13,72 +13,68 @@ import (
"github.com/prometheus/client_golang/prometheus/promauto" "github.com/prometheus/client_golang/prometheus/promauto"
) )
func NewRistrettoCache(maxCost CacheSize, enablePrometheus bool) (*Caches, error) { func MustCreateCache(name string, maxCost CacheSize, enablePrometheus bool) *ristretto.Cache {
cache, err := ristretto.NewCache(&ristretto.Config{ cache, err := ristretto.NewCache(&ristretto.Config{
NumCounters: 1e7, NumCounters: 1e6,
MaxCost: int64(maxCost), MaxCost: int64(maxCost),
BufferItems: 64, BufferItems: 64,
Metrics: true, Metrics: true,
}) })
if err != nil { if err != nil {
return nil, err panic(err)
} }
promauto.NewGaugeFunc(prometheus.GaugeOpts{ promauto.NewGaugeFunc(prometheus.GaugeOpts{
Namespace: "dendrite", Namespace: "dendrite",
Subsystem: "caching_ristretto", Subsystem: "caching_ristretto",
Name: "ratio", Name: name + "_ratio",
}, func() float64 { }, func() float64 {
return float64(cache.Metrics.Ratio()) return float64(cache.Metrics.Ratio())
}) })
promauto.NewGaugeFunc(prometheus.GaugeOpts{ promauto.NewGaugeFunc(prometheus.GaugeOpts{
Namespace: "dendrite", Namespace: "dendrite",
Subsystem: "caching_ristretto", Subsystem: "caching_ristretto",
Name: "cost", Name: name + "_cost",
}, func() float64 { }, func() float64 {
return float64(cache.Metrics.CostAdded() - cache.Metrics.CostEvicted()) return float64(cache.Metrics.CostAdded() - cache.Metrics.CostEvicted())
}) })
return cache
}
func NewRistrettoCache(maxCost CacheSize, enablePrometheus bool) (*Caches, error) {
return &Caches{ return &Caches{
RoomVersions: &RistrettoCachePartition[string, gomatrixserverlib.RoomVersion]{ RoomVersions: &RistrettoCachePartition[string, gomatrixserverlib.RoomVersion]{
cache: cache, cache: MustCreateCache("room_versions", 1*MB, enablePrometheus),
Name: "room_versions",
}, },
ServerKeys: &RistrettoCachePartition[string, gomatrixserverlib.PublicKeyLookupResult]{ ServerKeys: &RistrettoCachePartition[string, gomatrixserverlib.PublicKeyLookupResult]{
cache: cache, cache: MustCreateCache("server_keys", 32*MB, enablePrometheus),
Name: "server_keys",
Mutable: true, Mutable: true,
}, },
RoomServerRoomIDs: &RistrettoCachePartition[int64, string]{ RoomServerRoomIDs: &RistrettoCachePartition[int64, string]{
cache: cache, cache: MustCreateCache("room_ids", 1*MB, enablePrometheus),
Name: "room_ids",
}, },
RoomServerEvents: &RistrettoCachePartition[int64, *gomatrixserverlib.Event]{ RoomServerEvents: &RistrettoCachePartition[int64, *gomatrixserverlib.Event]{
cache: cache, cache: MustCreateCache("room_events", 1*GB, enablePrometheus),
Name: "room_events",
}, },
RoomInfos: &RistrettoCachePartition[string, types.RoomInfo]{ RoomInfos: &RistrettoCachePartition[string, types.RoomInfo]{
cache: cache, cache: MustCreateCache("room_infos", 16*MB, enablePrometheus),
Name: "room_infos",
Mutable: true, Mutable: true,
MaxAge: time.Minute * 5, MaxAge: time.Minute * 5,
}, },
FederationPDUs: &RistrettoCachePartition[int64, *gomatrixserverlib.HeaderedEvent]{ FederationPDUs: &RistrettoCachePartition[int64, *gomatrixserverlib.HeaderedEvent]{
cache: cache, cache: MustCreateCache("federation_pdus", 128*MB, enablePrometheus),
Name: "federation_events_pdu",
Mutable: true, Mutable: true,
}, },
FederationEDUs: &RistrettoCachePartition[int64, *gomatrixserverlib.EDU]{ FederationEDUs: &RistrettoCachePartition[int64, *gomatrixserverlib.EDU]{
cache: cache, cache: MustCreateCache("federation_edus", 128*MB, enablePrometheus),
Name: "federation_events_edu",
Mutable: true, Mutable: true,
}, },
SpaceSummaryRooms: &RistrettoCachePartition[string, gomatrixserverlib.MSC2946SpacesResponse]{ SpaceSummaryRooms: &RistrettoCachePartition[string, gomatrixserverlib.MSC2946SpacesResponse]{
cache: cache, cache: MustCreateCache("space_summary_rooms", 128, enablePrometheus), // TODO: not costed
Name: "space_summary_rooms",
Mutable: true, Mutable: true,
}, },
LazyLoading: &RistrettoCachePartition[string, any]{ // TODO: type LazyLoading: &RistrettoCachePartition[string, any]{ // TODO: type
cache: cache, cache: MustCreateCache("lazy_loading", 256, enablePrometheus), // TODO: not costed
Name: "lazy_loading",
Mutable: true, Mutable: true,
}, },
}, nil }, nil
@ -86,16 +82,14 @@ func NewRistrettoCache(maxCost CacheSize, enablePrometheus bool) (*Caches, error
type RistrettoCachePartition[K keyable, V any] struct { type RistrettoCachePartition[K keyable, V any] struct {
cache *ristretto.Cache cache *ristretto.Cache
Name string
Mutable bool Mutable bool
MaxAge time.Duration MaxAge time.Duration
} }
func (c *RistrettoCachePartition[K, V]) Set(key K, value V) { func (c *RistrettoCachePartition[K, V]) Set(key K, value V) {
strkey := fmt.Sprintf("%v\000%s", key, c.Name)
if !c.Mutable { if !c.Mutable {
if v, ok := c.cache.Get(strkey); ok && v != nil && !reflect.DeepEqual(v, value) { if v, ok := c.cache.Get(key); ok && v != nil && !reflect.DeepEqual(v, value) {
panic(fmt.Sprintf("invalid use of immutable cache tries to change value of %v from %v to %v", strkey, v, value)) panic(fmt.Sprintf("invalid use of immutable cache tries to change value of %v from %v to %v", key, v, value))
} }
} }
var cost int64 var cost int64
@ -106,20 +100,18 @@ func (c *RistrettoCachePartition[K, V]) Set(key K, value V) {
} else { } else {
cost = int64(unsafe.Sizeof(value)) cost = int64(unsafe.Sizeof(value))
} }
c.cache.SetWithTTL(strkey, value, cost, c.MaxAge) c.cache.SetWithTTL(key, value, cost, c.MaxAge)
} }
func (c *RistrettoCachePartition[K, V]) Unset(key K) { func (c *RistrettoCachePartition[K, V]) Unset(key K) {
strkey := fmt.Sprintf("%s_%v", c.Name, key)
if !c.Mutable { if !c.Mutable {
panic(fmt.Sprintf("invalid use of immutable cache tries to unset value of %v", strkey)) panic(fmt.Sprintf("invalid use of immutable cache tries to unset value of %v", key))
} }
c.cache.Del(strkey) c.cache.Del(key)
} }
func (c *RistrettoCachePartition[K, V]) Get(key K) (value V, ok bool) { func (c *RistrettoCachePartition[K, V]) Get(key K) (value V, ok bool) {
strkey := fmt.Sprintf("%s_%v", c.Name, key) v, ok := c.cache.Get(key)
v, ok := c.cache.Get(strkey)
if !ok || v == nil { if !ok || v == nil {
var empty V var empty V
return empty, false return empty, false