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