Skip to content

Commit

Permalink
rework cache configuration
Browse files Browse the repository at this point in the history
Signed-off-by: jkoberg <jkoberg@owncloud.com>
  • Loading branch information
kobergj committed Dec 13, 2023
1 parent e760e9e commit fbe0bb7
Show file tree
Hide file tree
Showing 19 changed files with 103 additions and 129 deletions.
6 changes: 6 additions & 0 deletions changelog/unreleased/rework-cache-configuration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Enhancement: Rework cache configuration

Reworks configuration of the cache package allowing easier configuration. Also adds a new config value allow to
not persist cache entries (nats only)

https://github.com/cs3org/reva/pull/4406
65 changes: 25 additions & 40 deletions internal/grpc/services/gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"fmt"
"net/url"
"strings"
"time"

gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1"
"github.com/cs3org/reva/v2/pkg/errtypes"
Expand Down Expand Up @@ -64,26 +63,14 @@ type config struct {
TokenManager string `mapstructure:"token_manager"`
// ShareFolder is the location where to create shares in the recipient's storage provider.
// FIXME get rid of ShareFolder, there are findByPath calls in the ocmshareporvider.go and usershareprovider.go
ShareFolder string `mapstructure:"share_folder"`
DataTransfersFolder string `mapstructure:"data_transfers_folder"`
TokenManagers map[string]map[string]interface{} `mapstructure:"token_managers"`
AllowedUserAgents map[string][]string `mapstructure:"allowed_user_agents"` // map[path][]user-agent
StatCacheStore string `mapstructure:"stat_cache_store"`
StatCacheNodes []string `mapstructure:"stat_cache_nodes"`
StatCacheDatabase string `mapstructure:"stat_cache_database"`
StatCacheTTL int `mapstructure:"stat_cache_ttl"`
StatCacheSize int `mapstructure:"stat_cache_size"`
CreateHomeCacheStore string `mapstructure:"create_home_cache_store"`
CreateHomeCacheNodes []string `mapstructure:"create_home_cache_nodes"`
CreateHomeCacheDatabase string `mapstructure:"create_home_cache_database"`
CreateHomeCacheTTL int `mapstructure:"create_home_cache_ttl"`
CreateHomeCacheSize int `mapstructure:"create_home_cache_size"`
ProviderCacheStore string `mapstructure:"provider_cache_store"`
ProviderCacheNodes []string `mapstructure:"provider_cache_nodes"`
ProviderCacheDatabase string `mapstructure:"provider_cache_database"`
ProviderCacheTTL int `mapstructure:"provider_cache_ttl"`
ProviderCacheSize int `mapstructure:"provider_cache_size"`
UseCommonSpaceRootShareLogic bool `mapstructure:"use_common_space_root_share_logic"`
ShareFolder string `mapstructure:"share_folder"`
DataTransfersFolder string `mapstructure:"data_transfers_folder"`
TokenManagers map[string]map[string]interface{} `mapstructure:"token_managers"`
AllowedUserAgents map[string][]string `mapstructure:"allowed_user_agents"` // map[path][]user-agent
StatCacheConfig cache.Config `mapstructure:"stat_cache_config"`
CreatePersonalSpaceCacheConfig cache.Config `mapstructure:"create_personal_space_cache_config"`
ProviderCacheConfig cache.Config `mapstructure:"provider_cache_config"`
UseCommonSpaceRootShareLogic bool `mapstructure:"use_common_space_root_share_logic"`
}

// sets defaults
Expand Down Expand Up @@ -130,28 +117,28 @@ func (c *config) init() {
}

// caching needs to be explicitly enabled
if c.StatCacheStore == "" {
c.StatCacheStore = "noop"
if c.StatCacheConfig.Store == "" {
c.StatCacheConfig.Store = "noop"
}

if c.StatCacheDatabase == "" {
c.StatCacheDatabase = "reva"
if c.StatCacheConfig.Database == "" {
c.StatCacheConfig.Database = "reva"
}

if c.ProviderCacheStore == "" {
c.ProviderCacheStore = "noop"
if c.ProviderCacheConfig.Store == "" {
c.ProviderCacheConfig.Store = "noop"
}

if c.ProviderCacheDatabase == "" {
c.ProviderCacheDatabase = "reva"
if c.ProviderCacheConfig.Database == "" {
c.ProviderCacheConfig.Database = "reva"
}

if c.CreateHomeCacheStore == "" {
c.CreateHomeCacheStore = "noop"
if c.CreatePersonalSpaceCacheConfig.Store == "" {
c.CreatePersonalSpaceCacheConfig.Store = "noop"
}

if c.CreateHomeCacheDatabase == "" {
c.CreateHomeCacheDatabase = "reva"
if c.CreatePersonalSpaceCacheConfig.Database == "" {
c.CreatePersonalSpaceCacheConfig.Database = "reva"
}
}

Expand All @@ -161,14 +148,13 @@ type svc struct {
tokenmgr token.Manager
statCache cache.StatCache
providerCache cache.ProviderCache
createHomeCache cache.CreateHomeCache
createPersonalSpaceCache cache.CreatePersonalSpaceCache
}

// New creates a new gateway svc that acts as a proxy for any grpc operation.
// The gateway is responsible for high-level controls: rate-limiting, coordination between svcs
// like sharing and storage acls, asynchronous transactions, ...
func New(m map[string]interface{}, ss *grpc.Server) (rgrpc.Service, error) {
func New(m map[string]interface{}, _ *grpc.Server) (rgrpc.Service, error) {
c, err := parseConfig(m)
if err != nil {
return nil, err
Expand All @@ -191,10 +177,9 @@ func New(m map[string]interface{}, ss *grpc.Server) (rgrpc.Service, error) {
c: c,
dataGatewayURL: *u,
tokenmgr: tokenManager,
statCache: cache.GetStatCache(c.StatCacheStore, c.StatCacheNodes, c.StatCacheDatabase, "stat", time.Duration(c.StatCacheTTL)*time.Second, c.StatCacheSize),
providerCache: cache.GetProviderCache(c.ProviderCacheStore, c.ProviderCacheNodes, c.ProviderCacheDatabase, "provider", time.Duration(c.ProviderCacheTTL)*time.Second, c.ProviderCacheSize),
createHomeCache: cache.GetCreateHomeCache(c.CreateHomeCacheStore, c.CreateHomeCacheNodes, c.CreateHomeCacheDatabase, "createHome", time.Duration(c.CreateHomeCacheTTL)*time.Second, c.CreateHomeCacheSize),
createPersonalSpaceCache: cache.GetCreatePersonalSpaceCache(c.CreateHomeCacheStore, c.CreateHomeCacheNodes, c.CreateHomeCacheDatabase, "createPersonalSpace", time.Duration(c.CreateHomeCacheTTL)*time.Second, c.CreateHomeCacheSize),
statCache: cache.GetStatCache(c.StatCacheConfig),
providerCache: cache.GetProviderCache(c.ProviderCacheConfig),
createPersonalSpaceCache: cache.GetCreatePersonalSpaceCache(c.CreatePersonalSpaceCacheConfig),
}

return s, nil
Expand All @@ -207,7 +192,7 @@ func (s *svc) Register(ss *grpc.Server) {
func (s *svc) Close() error {
s.statCache.Close()
s.providerCache.Close()
s.createHomeCache.Close()
s.createPersonalSpaceCache.Close()
return nil
}

Expand Down
1 change: 0 additions & 1 deletion internal/grpc/services/gateway/storageprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1107,7 +1107,6 @@ func (s *svc) getStorageProviderClient(_ context.Context, p *registry.ProviderIn
return &cachedAPIClient{
c: c,
statCache: s.statCache,
createHomeCache: s.createHomeCache,
createPersonalSpaceCache: s.createPersonalSpaceCache,
}, nil
}
Expand Down
7 changes: 3 additions & 4 deletions internal/grpc/services/gateway/storageprovidercache.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ func (c *cachedRegistryClient) GetHome(ctx context.Context, in *registry.GetHome
type cachedAPIClient struct {
c provider.ProviderAPIClient
statCache cache.StatCache
createHomeCache cache.CreateHomeCache
createPersonalSpaceCache cache.CreatePersonalSpaceCache
}

Expand Down Expand Up @@ -121,10 +120,10 @@ func (c *cachedAPIClient) Stat(ctx context.Context, in *provider.StatRequest, op

// CreateHome caches calls to CreateHome locally - anyways they only need to be called once per user
func (c *cachedAPIClient) CreateHome(ctx context.Context, in *provider.CreateHomeRequest, opts ...grpc.CallOption) (*provider.CreateHomeResponse, error) {
key := c.createHomeCache.GetKey(ctxpkg.ContextMustGetUser(ctx).GetId())
key := c.createPersonalSpaceCache.GetKey(ctxpkg.ContextMustGetUser(ctx).GetId())
if key != "" {
s := &provider.CreateHomeResponse{}
if err := c.createHomeCache.PullFromCache(key, s); err == nil {
if err := c.createPersonalSpaceCache.PullFromCache(key, s); err == nil {
return s, nil
}
}
Expand All @@ -137,7 +136,7 @@ func (c *cachedAPIClient) CreateHome(ctx context.Context, in *provider.CreateHom
case key == "":
return resp, nil
default:
return resp, c.createHomeCache.PushToCache(key, resp)
return resp, c.createPersonalSpaceCache.PushToCache(key, resp)
}
}

Expand Down
8 changes: 2 additions & 6 deletions internal/http/services/owncloud/ocs/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package config
import (
"github.com/cs3org/reva/v2/internal/http/services/owncloud/ocs/data"
"github.com/cs3org/reva/v2/pkg/sharedconf"
"github.com/cs3org/reva/v2/pkg/storage/cache"
)

// Config holds the config options that need to be passed down to all ocs handlers
Expand All @@ -37,12 +38,7 @@ type Config struct {
AdditionalInfoAttribute string `mapstructure:"additional_info_attribute"`
CacheWarmupDriver string `mapstructure:"cache_warmup_driver"`
CacheWarmupDrivers map[string]map[string]interface{} `mapstructure:"cache_warmup_drivers"`
StatCacheStore string `mapstructure:"stat_cache_store"`
StatCacheNodes []string `mapstructure:"stat_cache_nodes"`
StatCacheDatabase string `mapstructure:"stat_cache_database"`
StatCacheTable string `mapstructure:"stat_cache_table"`
StatCacheTTL int `mapstructure:"stat_cache_ttl"`
StatCacheSize int `mapstructure:"stat_cache_size"`
StatCacheConfig cache.Config `mapstructure:"stat_cache_config"`
UserIdentifierCacheTTL int `mapstructure:"user_identifier_cache_ttl"`
MachineAuthAPIKey string `mapstructure:"machine_auth_apikey"`
SkipUpdatingExistingSharesMountpoints bool `mapstructure:"skip_updating_existing_shares_mountpoint"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func (h *Handler) Init(c *config.Config) error {
h.publicPasswordEnforced = publicPwdEnforced(c)
h.passwordValidator = passwordPolicies(c)

h.statCache = cache.GetStatCache(c.StatCacheStore, c.StatCacheNodes, c.StatCacheDatabase, "stat", time.Duration(c.StatCacheTTL)*time.Second, c.StatCacheSize)
h.statCache = cache.GetStatCache(c.StatCacheConfig)
if c.CacheWarmupDriver != "" {
cwm, err := getCacheWarmupManager(c)
if err != nil {
Expand Down
5 changes: 2 additions & 3 deletions internal/http/services/owncloud/ocs/ocs.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package ocs

import (
"net/http"
"time"

"github.com/cs3org/reva/v2/internal/http/services/owncloud/ocs/config"
"github.com/cs3org/reva/v2/internal/http/services/owncloud/ocs/handlers/apps/sharing/sharees"
Expand Down Expand Up @@ -67,9 +66,9 @@ func New(m map[string]interface{}, log *zerolog.Logger) (global.Service, error)
return nil, err
}

if conf.CacheWarmupDriver == "first-request" && conf.StatCacheStore != "noop" {
if conf.CacheWarmupDriver == "first-request" && conf.StatCacheConfig.Store != "noop" {
s.warmupCacheTracker = ttlcache.NewCache()
_ = s.warmupCacheTracker.SetTTL(time.Second * time.Duration(conf.StatCacheTTL))
_ = s.warmupCacheTracker.SetTTL(conf.StatCacheConfig.TTL)
}

return s, nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/rhttp/datatx/manager/simple/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func New(m map[string]interface{}, publisher events.Publisher) (datatx.DataTX, e
return &manager{
conf: c,
publisher: publisher,
statCache: cache.GetStatCache(c.Store, c.Nodes, c.Database, c.Table, time.Duration(c.TTL)*time.Second, c.Size),
statCache: cache.GetStatCache(*c),
}, nil
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/rhttp/datatx/manager/spaces/spaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func New(m map[string]interface{}, publisher events.Publisher) (datatx.DataTX, e
return &manager{
conf: c,
publisher: publisher,
statCache: cache.GetStatCache(c.Store, c.Nodes, c.Database, c.Table, time.Duration(c.TTL)*time.Second, c.Size),
statCache: cache.GetStatCache(*c),
}, nil
}

Expand Down
3 changes: 1 addition & 2 deletions pkg/rhttp/datatx/manager/tus/tus.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"log"
"net/http"
"path"
"time"

"github.com/pkg/errors"
tusd "github.com/tus/tusd/pkg/handler"
Expand Down Expand Up @@ -71,7 +70,7 @@ func New(m map[string]interface{}, publisher events.Publisher) (datatx.DataTX, e
return &manager{
conf: c,
publisher: publisher,
statCache: cache.GetStatCache(c.Store, c.Nodes, c.Database, c.Table, time.Duration(c.TTL)*time.Second, c.Size),
statCache: cache.GetStatCache(*c),
}, nil
}

Expand Down
59 changes: 30 additions & 29 deletions pkg/storage/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ var (

// Config contains the configuring for a cache
type Config struct {
Store string `mapstructure:"cache_store"`
Nodes []string `mapstructure:"cache_nodes"`
Database string `mapstructure:"cache_database"`
Table string `mapstructure:"cache_table"`
TTL int `mapstructure:"cache_ttl"`
Size int `mapstructure:"cache_size"`
DisablePersistence bool `mapstructure:"cache_disable_persistence"`
Store string `mapstructure:"cache_store"`
Nodes []string `mapstructure:"cache_nodes"`
Database string `mapstructure:"cache_database"`
Table string `mapstructure:"cache_table"`
TTL time.Duration `mapstructure:"cache_ttl"`
Size int `mapstructure:"cache_size"`
DisablePersistence bool `mapstructure:"cache_disable_persistence"`
}

// Cache handles key value operations on caches
Expand Down Expand Up @@ -101,65 +101,65 @@ type FileMetadataCache interface {

// GetStatCache will return an existing StatCache for the given store, nodes, database and table
// If it does not exist yet it will be created, different TTLs are ignored
func GetStatCache(cacheStore string, cacheNodes []string, database, table string, ttl time.Duration, size int) StatCache {
func GetStatCache(cfg Config) StatCache {
mutex.Lock()
defer mutex.Unlock()

key := strings.Join(append(append([]string{cacheStore}, cacheNodes...), database, table), ":")
key := strings.Join(append(append([]string{cfg.Store}, cfg.Nodes...), cfg.Database, cfg.Table), ":")
if statCaches[key] == nil {
statCaches[key] = NewStatCache(cacheStore, cacheNodes, database, table, ttl, size)
statCaches[key] = NewStatCache(cfg)
}
return statCaches[key]
}

// GetProviderCache will return an existing ProviderCache for the given store, nodes, database and table
// If it does not exist yet it will be created, different TTLs are ignored
func GetProviderCache(cacheStore string, cacheNodes []string, database, table string, ttl time.Duration, size int) ProviderCache {
func GetProviderCache(cfg Config) ProviderCache {
mutex.Lock()
defer mutex.Unlock()

key := strings.Join(append(append([]string{cacheStore}, cacheNodes...), database, table), ":")
key := strings.Join(append(append([]string{cfg.Store}, cfg.Nodes...), cfg.Database, cfg.Table), ":")
if providerCaches[key] == nil {
providerCaches[key] = NewProviderCache(cacheStore, cacheNodes, database, table, ttl, size)
providerCaches[key] = NewProviderCache(cfg)
}
return providerCaches[key]
}

// GetCreateHomeCache will return an existing CreateHomeCache for the given store, nodes, database and table
// If it does not exist yet it will be created, different TTLs are ignored
func GetCreateHomeCache(cacheStore string, cacheNodes []string, database, table string, ttl time.Duration, size int) CreateHomeCache {
func GetCreateHomeCache(cfg Config) CreateHomeCache {
mutex.Lock()
defer mutex.Unlock()

key := strings.Join(append(append([]string{cacheStore}, cacheNodes...), database, table), ":")
key := strings.Join(append(append([]string{cfg.Store}, cfg.Nodes...), cfg.Database, cfg.Table), ":")
if createHomeCaches[key] == nil {
createHomeCaches[key] = NewCreateHomeCache(cacheStore, cacheNodes, database, table, ttl, size)
createHomeCaches[key] = NewCreateHomeCache(cfg)
}
return createHomeCaches[key]
}

// GetCreatePersonalSpaceCache will return an existing CreatePersonalSpaceCache for the given store, nodes, database and table
// If it does not exist yet it will be created, different TTLs are ignored
func GetCreatePersonalSpaceCache(cacheStore string, cacheNodes []string, database, table string, ttl time.Duration, size int) CreatePersonalSpaceCache {
func GetCreatePersonalSpaceCache(cfg Config) CreatePersonalSpaceCache {
mutex.Lock()
defer mutex.Unlock()

key := strings.Join(append(append([]string{cacheStore}, cacheNodes...), database, table), ":")
key := strings.Join(append(append([]string{cfg.Store}, cfg.Nodes...), cfg.Database, cfg.Table), ":")
if createPersonalSpaceCaches[key] == nil {
createPersonalSpaceCaches[key] = NewCreatePersonalSpaceCache(cacheStore, cacheNodes, database, table, ttl, size)
createPersonalSpaceCaches[key] = NewCreatePersonalSpaceCache(cfg)
}
return createPersonalSpaceCaches[key]
}

// GetFileMetadataCache will return an existing GetFileMetadataCache for the given store, nodes, database and table
// If it does not exist yet it will be created, different TTLs are ignored
func GetFileMetadataCache(cacheStore string, cacheNodes []string, database, table string, ttl time.Duration, size int) FileMetadataCache {
func GetFileMetadataCache(cfg Config) FileMetadataCache {
mutex.Lock()
defer mutex.Unlock()

key := strings.Join(append(append([]string{cacheStore}, cacheNodes...), database, table), ":")
key := strings.Join(append(append([]string{cfg.Store}, cfg.Nodes...), cfg.Database, cfg.Table), ":")
if fileMetadataCaches[key] == nil {
fileMetadataCaches[key] = NewFileMetadataCache(cacheStore, cacheNodes, database, table, ttl, size)
fileMetadataCaches[key] = NewFileMetadataCache(cfg)
}
return fileMetadataCaches[key]
}
Expand Down Expand Up @@ -231,13 +231,14 @@ func (cache cacheStore) Close() error {
return cache.s.Close()
}

func getStore(storeType string, nodes []string, database, table string, ttl time.Duration, size int) microstore.Store {
func getStore(cfg Config) microstore.Store {
return store.Create(
store.Store(storeType),
microstore.Nodes(nodes...),
microstore.Database(database),
microstore.Table(table),
store.TTL(ttl),
store.Size(size),
store.Store(cfg.Store),
microstore.Nodes(cfg.Nodes...),
microstore.Database(cfg.Database),
microstore.Table(cfg.Table),
store.TTL(cfg.TTL),
store.Size(cfg.Size),
store.DisablePersistence(cfg.DisablePersistence),
)
}
Loading

0 comments on commit fbe0bb7

Please sign in to comment.