mirror of
https://github.com/matrix-org/dendrite.git
synced 2026-01-08 14:43:09 -06:00
Use a shared cache again
This commit is contained in:
parent
849dfdfc1f
commit
e0466c7777
|
|
@ -7,18 +7,22 @@ import (
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
"github.com/dgraph-io/ristretto"
|
"github.com/dgraph-io/ristretto"
|
||||||
|
"github.com/dgraph-io/ristretto/z"
|
||||||
"github.com/matrix-org/dendrite/roomserver/types"
|
"github.com/matrix-org/dendrite/roomserver/types"
|
||||||
"github.com/matrix-org/gomatrixserverlib"
|
"github.com/matrix-org/gomatrixserverlib"
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
"github.com/prometheus/client_golang/prometheus/promauto"
|
"github.com/prometheus/client_golang/prometheus/promauto"
|
||||||
)
|
)
|
||||||
|
|
||||||
func MustCreateCache(name string, maxCost CacheSize, enablePrometheus bool) *ristretto.Cache {
|
func MustCreateCache(maxCost CacheSize, enablePrometheus bool) *ristretto.Cache {
|
||||||
cache, err := ristretto.NewCache(&ristretto.Config{
|
cache, err := ristretto.NewCache(&ristretto.Config{
|
||||||
NumCounters: 1e6,
|
NumCounters: 1e6,
|
||||||
MaxCost: int64(maxCost),
|
MaxCost: int64(maxCost),
|
||||||
BufferItems: 64,
|
BufferItems: 64,
|
||||||
Metrics: true,
|
Metrics: true,
|
||||||
|
KeyToHash: func(key interface{}) (uint64, uint64) {
|
||||||
|
return z.KeyToHash(key)
|
||||||
|
},
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
|
@ -26,62 +30,83 @@ func MustCreateCache(name string, maxCost CacheSize, enablePrometheus bool) *ris
|
||||||
promauto.NewGaugeFunc(prometheus.GaugeOpts{
|
promauto.NewGaugeFunc(prometheus.GaugeOpts{
|
||||||
Namespace: "dendrite",
|
Namespace: "dendrite",
|
||||||
Subsystem: "caching_ristretto",
|
Subsystem: "caching_ristretto",
|
||||||
Name: name + "_ratio",
|
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: name + "_cost",
|
Name: "cost",
|
||||||
}, func() float64 {
|
}, func() float64 {
|
||||||
return float64(cache.Metrics.CostAdded() - cache.Metrics.CostEvicted())
|
return float64(cache.Metrics.CostAdded() - cache.Metrics.CostEvicted())
|
||||||
})
|
})
|
||||||
return cache
|
return cache
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRistrettoCache(maxCost CacheSize, enablePrometheus bool) (*Caches, error) {
|
const (
|
||||||
|
roomVersionsCache byte = iota + 1
|
||||||
|
serverKeysCache
|
||||||
|
roomIDsCache
|
||||||
|
roomEventsCache
|
||||||
|
roomInfosCache
|
||||||
|
federationPDUsCache
|
||||||
|
federationEDUsCache
|
||||||
|
spaceSummaryRoomsCache
|
||||||
|
lazyLoadingCache
|
||||||
|
)
|
||||||
|
|
||||||
|
func NewRistrettoCache(maxCost CacheSize, enablePrometheus bool) (*Caches, error) {
|
||||||
|
cache := MustCreateCache(maxCost, enablePrometheus)
|
||||||
return &Caches{
|
return &Caches{
|
||||||
RoomVersions: &RistrettoCachePartition[string, gomatrixserverlib.RoomVersion]{
|
RoomVersions: &RistrettoCachePartition[string, gomatrixserverlib.RoomVersion]{
|
||||||
cache: MustCreateCache("room_versions", 1*MB, enablePrometheus),
|
cache: cache,
|
||||||
|
Prefix: roomVersionsCache,
|
||||||
MaxAge: time.Hour,
|
MaxAge: time.Hour,
|
||||||
},
|
},
|
||||||
ServerKeys: &RistrettoCachePartition[string, gomatrixserverlib.PublicKeyLookupResult]{
|
ServerKeys: &RistrettoCachePartition[string, gomatrixserverlib.PublicKeyLookupResult]{
|
||||||
cache: MustCreateCache("server_keys", 32*MB, enablePrometheus),
|
cache: cache,
|
||||||
|
Prefix: serverKeysCache,
|
||||||
Mutable: true,
|
Mutable: true,
|
||||||
MaxAge: time.Hour,
|
MaxAge: time.Hour,
|
||||||
},
|
},
|
||||||
RoomServerRoomIDs: &RistrettoCachePartition[int64, string]{
|
RoomServerRoomIDs: &RistrettoCachePartition[int64, string]{
|
||||||
cache: MustCreateCache("room_ids", 1*MB, enablePrometheus),
|
cache: cache,
|
||||||
|
Prefix: roomIDsCache,
|
||||||
MaxAge: time.Hour,
|
MaxAge: time.Hour,
|
||||||
},
|
},
|
||||||
RoomServerEvents: &RistrettoCachePartition[int64, *gomatrixserverlib.Event]{
|
RoomServerEvents: &RistrettoCachePartition[int64, *gomatrixserverlib.Event]{
|
||||||
cache: MustCreateCache("room_events", 1*GB, enablePrometheus),
|
cache: cache,
|
||||||
|
Prefix: roomEventsCache,
|
||||||
MaxAge: time.Hour,
|
MaxAge: time.Hour,
|
||||||
},
|
},
|
||||||
RoomInfos: &RistrettoCachePartition[string, types.RoomInfo]{
|
RoomInfos: &RistrettoCachePartition[string, types.RoomInfo]{
|
||||||
cache: MustCreateCache("room_infos", 16*MB, enablePrometheus),
|
cache: cache,
|
||||||
|
Prefix: roomInfosCache,
|
||||||
Mutable: true,
|
Mutable: true,
|
||||||
MaxAge: time.Hour,
|
MaxAge: time.Hour,
|
||||||
},
|
},
|
||||||
FederationPDUs: &RistrettoCachePartition[int64, *gomatrixserverlib.HeaderedEvent]{
|
FederationPDUs: &RistrettoCachePartition[int64, *gomatrixserverlib.HeaderedEvent]{
|
||||||
cache: MustCreateCache("federation_pdus", 128*MB, enablePrometheus),
|
cache: cache,
|
||||||
|
Prefix: federationPDUsCache,
|
||||||
Mutable: true,
|
Mutable: true,
|
||||||
MaxAge: time.Hour / 2,
|
MaxAge: time.Hour / 2,
|
||||||
},
|
},
|
||||||
FederationEDUs: &RistrettoCachePartition[int64, *gomatrixserverlib.EDU]{
|
FederationEDUs: &RistrettoCachePartition[int64, *gomatrixserverlib.EDU]{
|
||||||
cache: MustCreateCache("federation_edus", 128*MB, enablePrometheus),
|
cache: cache,
|
||||||
|
Prefix: federationEDUsCache,
|
||||||
Mutable: true,
|
Mutable: true,
|
||||||
MaxAge: time.Hour / 2,
|
MaxAge: time.Hour / 2,
|
||||||
},
|
},
|
||||||
SpaceSummaryRooms: &RistrettoCachePartition[string, gomatrixserverlib.MSC2946SpacesResponse]{
|
SpaceSummaryRooms: &RistrettoCachePartition[string, gomatrixserverlib.MSC2946SpacesResponse]{
|
||||||
cache: MustCreateCache("space_summary_rooms", 128, enablePrometheus), // TODO: not costed
|
cache: cache,
|
||||||
|
Prefix: spaceSummaryRoomsCache,
|
||||||
Mutable: true,
|
Mutable: true,
|
||||||
MaxAge: time.Hour,
|
MaxAge: time.Hour,
|
||||||
},
|
},
|
||||||
LazyLoading: &RistrettoCachePartition[string, any]{ // TODO: type
|
LazyLoading: &RistrettoCachePartition[string, any]{ // TODO: type
|
||||||
cache: MustCreateCache("lazy_loading", 256, enablePrometheus), // TODO: not costed
|
cache: cache,
|
||||||
|
Prefix: lazyLoadingCache,
|
||||||
Mutable: true,
|
Mutable: true,
|
||||||
MaxAge: time.Hour,
|
MaxAge: time.Hour,
|
||||||
},
|
},
|
||||||
|
|
@ -90,13 +115,15 @@ 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
|
||||||
|
Prefix byte
|
||||||
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) {
|
||||||
|
bkey := fmt.Sprintf("%c%v", c.Prefix, key)
|
||||||
if !c.Mutable {
|
if !c.Mutable {
|
||||||
if v, ok := c.cache.Get(key); ok && v != nil && !reflect.DeepEqual(v, value) {
|
if v, ok := c.cache.Get(bkey); 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))
|
panic(fmt.Sprintf("invalid use of immutable cache tries to change value of %v from %v to %v", key, v, value))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -108,18 +135,20 @@ 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(key, value, cost, c.MaxAge)
|
c.cache.SetWithTTL(bkey, value, cost, c.MaxAge)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *RistrettoCachePartition[K, V]) Unset(key K) {
|
func (c *RistrettoCachePartition[K, V]) Unset(key K) {
|
||||||
|
bkey := fmt.Sprintf("%c%v", c.Prefix, key)
|
||||||
if !c.Mutable {
|
if !c.Mutable {
|
||||||
panic(fmt.Sprintf("invalid use of immutable cache tries to unset value of %v", key))
|
panic(fmt.Sprintf("invalid use of immutable cache tries to unset value of %v", key))
|
||||||
}
|
}
|
||||||
c.cache.Del(key)
|
c.cache.Del(bkey)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *RistrettoCachePartition[K, V]) Get(key K) (value V, ok bool) {
|
func (c *RistrettoCachePartition[K, V]) Get(key K) (value V, ok bool) {
|
||||||
v, ok := c.cache.Get(key)
|
bkey := fmt.Sprintf("%c%v", c.Prefix, key)
|
||||||
|
v, ok := c.cache.Get(bkey)
|
||||||
if !ok || v == nil {
|
if !ok || v == nil {
|
||||||
var empty V
|
var empty V
|
||||||
return empty, false
|
return empty, false
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue