Add config.DataUnit for specifying friendly cache sizes

This commit is contained in:
Neil Alexander 2022-06-20 11:28:45 +01:00
parent 7e9109510d
commit 6ff90fa0ee
No known key found for this signature in database
GPG key ID: A02A2019A2BB0944
3 changed files with 53 additions and 4 deletions

View file

@ -9,15 +9,16 @@ import (
"github.com/dgraph-io/ristretto"
"github.com/dgraph-io/ristretto/z"
"github.com/matrix-org/dendrite/roomserver/types"
"github.com/matrix-org/dendrite/setup/config"
"github.com/matrix-org/gomatrixserverlib"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
func MustCreateCache(maxCost int64, enablePrometheus bool) *ristretto.Cache {
func MustCreateCache(maxCost config.DataUnit, enablePrometheus bool) *ristretto.Cache {
cache, err := ristretto.NewCache(&ristretto.Config{
NumCounters: 1e5,
MaxCost: maxCost,
MaxCost: int64(maxCost),
BufferItems: 64,
Metrics: true,
KeyToHash: func(key interface{}) (uint64, uint64) {
@ -56,7 +57,7 @@ const (
lazyLoadingCache
)
func NewRistrettoCache(maxCost int64, enablePrometheus bool) (*Caches, error) {
func NewRistrettoCache(maxCost config.DataUnit, enablePrometheus bool) (*Caches, error) {
cache := MustCreateCache(maxCost, enablePrometheus)
return &Caches{
RoomVersions: &RistrettoCachePartition[string, gomatrixserverlib.RoomVersion]{

View file

@ -2,6 +2,8 @@ package config
import (
"math/rand"
"strconv"
"strings"
"time"
"github.com/matrix-org/gomatrixserverlib"
@ -174,7 +176,7 @@ func (c *ServerNotices) Defaults(generate bool) {
func (c *ServerNotices) Verify(errors *ConfigErrors, isMonolith bool) {}
type Cache struct {
EstMaxSize int64 `yaml:"max_bytes_est"`
EstMaxSize DataUnit `yaml:"max_bytes_est"`
}
func (c *Cache) Defaults(generate bool) {
@ -285,3 +287,28 @@ type PresenceOptions struct {
// Whether outbound presence events are allowed
EnableOutbound bool `yaml:"enable_outbound"`
}
type DataUnit int64
func (d *DataUnit) UnmarshalText(text []byte) error {
var magnitude float64
s := strings.ToLower(string(text))
switch {
case strings.HasSuffix(s, "tb"):
s, magnitude = s[:len(s)-2], 1024*1024*1024*1024
case strings.HasSuffix(s, "gb"):
s, magnitude = s[:len(s)-2], 1024*1024*1024
case strings.HasSuffix(s, "mb"):
s, magnitude = s[:len(s)-2], 1024*1024
case strings.HasSuffix(s, "kb"):
s, magnitude = s[:len(s)-2], 1024
default:
magnitude = 1
}
v, err := strconv.ParseFloat(s, 64)
if err != nil {
return err
}
*d = DataUnit(v * magnitude)
return nil
}

View file

@ -17,6 +17,8 @@ package config
import (
"fmt"
"testing"
"gopkg.in/yaml.v2"
)
func TestLoadConfigRelative(t *testing.T) {
@ -268,3 +270,22 @@ n0Xq64k7fc42HXJpF8CGBkSaIhtlzcruO+vqR80B9r62+D0V7VmHOnP135MT6noU
ANAf5kxmMsM0zlN2hkxl0H6o7wKlBSw3RI3cjfilXiMWRPJrzlc4
-----END CERTIFICATE-----
`
func TestUnmarshalDataUnit(t *testing.T) {
target := struct {
Got DataUnit `yaml:"value"`
}{}
for input, expect := range map[string]DataUnit{
"value: 0.6tb": 659706976665,
"value: 1.2gb": 1288490188,
"value: 256mb": 268435456,
"value: 128kb": 131072,
"value: 128": 128,
} {
if err := yaml.Unmarshal([]byte(input), &target); err != nil {
t.Fatal(err)
} else if target.Got != expect {
t.Fatalf("expected value %d but got %d", expect, target.Got)
}
}
}