mirror of
https://github.com/matrix-org/dendrite.git
synced 2026-01-08 14:43:09 -06:00
Add config.DataUnit for specifying friendly cache sizes
This commit is contained in:
parent
7e9109510d
commit
6ff90fa0ee
|
|
@ -9,15 +9,16 @@ import (
|
||||||
"github.com/dgraph-io/ristretto"
|
"github.com/dgraph-io/ristretto"
|
||||||
"github.com/dgraph-io/ristretto/z"
|
"github.com/dgraph-io/ristretto/z"
|
||||||
"github.com/matrix-org/dendrite/roomserver/types"
|
"github.com/matrix-org/dendrite/roomserver/types"
|
||||||
|
"github.com/matrix-org/dendrite/setup/config"
|
||||||
"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(maxCost int64, enablePrometheus bool) *ristretto.Cache {
|
func MustCreateCache(maxCost config.DataUnit, enablePrometheus bool) *ristretto.Cache {
|
||||||
cache, err := ristretto.NewCache(&ristretto.Config{
|
cache, err := ristretto.NewCache(&ristretto.Config{
|
||||||
NumCounters: 1e5,
|
NumCounters: 1e5,
|
||||||
MaxCost: maxCost,
|
MaxCost: int64(maxCost),
|
||||||
BufferItems: 64,
|
BufferItems: 64,
|
||||||
Metrics: true,
|
Metrics: true,
|
||||||
KeyToHash: func(key interface{}) (uint64, uint64) {
|
KeyToHash: func(key interface{}) (uint64, uint64) {
|
||||||
|
|
@ -56,7 +57,7 @@ const (
|
||||||
lazyLoadingCache
|
lazyLoadingCache
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewRistrettoCache(maxCost int64, enablePrometheus bool) (*Caches, error) {
|
func NewRistrettoCache(maxCost config.DataUnit, enablePrometheus bool) (*Caches, error) {
|
||||||
cache := MustCreateCache(maxCost, enablePrometheus)
|
cache := MustCreateCache(maxCost, enablePrometheus)
|
||||||
return &Caches{
|
return &Caches{
|
||||||
RoomVersions: &RistrettoCachePartition[string, gomatrixserverlib.RoomVersion]{
|
RoomVersions: &RistrettoCachePartition[string, gomatrixserverlib.RoomVersion]{
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,8 @@ package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/matrix-org/gomatrixserverlib"
|
"github.com/matrix-org/gomatrixserverlib"
|
||||||
|
|
@ -174,7 +176,7 @@ func (c *ServerNotices) Defaults(generate bool) {
|
||||||
func (c *ServerNotices) Verify(errors *ConfigErrors, isMonolith bool) {}
|
func (c *ServerNotices) Verify(errors *ConfigErrors, isMonolith bool) {}
|
||||||
|
|
||||||
type Cache struct {
|
type Cache struct {
|
||||||
EstMaxSize int64 `yaml:"max_bytes_est"`
|
EstMaxSize DataUnit `yaml:"max_bytes_est"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Cache) Defaults(generate bool) {
|
func (c *Cache) Defaults(generate bool) {
|
||||||
|
|
@ -285,3 +287,28 @@ type PresenceOptions struct {
|
||||||
// Whether outbound presence events are allowed
|
// Whether outbound presence events are allowed
|
||||||
EnableOutbound bool `yaml:"enable_outbound"`
|
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
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,8 @@ package config
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"gopkg.in/yaml.v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestLoadConfigRelative(t *testing.T) {
|
func TestLoadConfigRelative(t *testing.T) {
|
||||||
|
|
@ -268,3 +270,22 @@ n0Xq64k7fc42HXJpF8CGBkSaIhtlzcruO+vqR80B9r62+D0V7VmHOnP135MT6noU
|
||||||
ANAf5kxmMsM0zlN2hkxl0H6o7wKlBSw3RI3cjfilXiMWRPJrzlc4
|
ANAf5kxmMsM0zlN2hkxl0H6o7wKlBSw3RI3cjfilXiMWRPJrzlc4
|
||||||
-----END CERTIFICATE-----
|
-----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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue