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 5d2db13
Show file tree
Hide file tree
Showing 20 changed files with 106 additions and 132 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
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ var _ = Describe("The ocs API", func() {

c := &config.Config{}
c.GatewaySvc = "gatewaysvc"
c.StatCacheDatabase = strconv.FormatInt(rand.Int63(), 10) // Use a fresh database for each test
c.StatCacheConfig.Database = strconv.FormatInt(rand.Int63(), 10) // Use a fresh database for each test
c.Init()
h.InitWithGetter(c, func() (gateway.GatewayAPIClient, error) {
return gatewayClient, nil
Expand Down Expand Up @@ -483,7 +483,7 @@ var _ = Describe("The ocs API", func() {

c := &config.Config{}
c.GatewaySvc = "gatewaysvc"
c.StatCacheDatabase = strconv.FormatInt(rand.Int63(), 10) // Use a fresh database for each test
c.StatCacheConfig.Database = strconv.FormatInt(rand.Int63(), 10) // Use a fresh database for each test
// this is equivalent of the ocis OCIS_SHARING_PUBLIC_WRITEABLE_SHARE_MUST_HAVE_PASSWORD=true
c.Capabilities = cdata.CapabilitiesData{
Capabilities: &cdata.Capabilities{FilesSharing: &cdata.CapabilitiesFilesSharing{Public: &cdata.CapabilitiesFilesSharingPublic{
Expand Down Expand Up @@ -624,7 +624,7 @@ var _ = Describe("The ocs API", func() {

c := &config.Config{}
c.GatewaySvc = "gatewaysvc"
c.StatCacheDatabase = strconv.FormatInt(rand.Int63(), 10) // Use a fresh database for each test
c.StatCacheConfig.Database = strconv.FormatInt(rand.Int63(), 10) // Use a fresh database for each test
// this is equivalent of the ocis OCIS_SHARING_PUBLIC_WRITEABLE_SHARE_MUST_HAVE_PASSWORD=true
c.Capabilities = cdata.CapabilitiesData{
Capabilities: &cdata.Capabilities{FilesSharing: &cdata.CapabilitiesFilesSharing{Public: &cdata.CapabilitiesFilesSharingPublic{
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

0 comments on commit 5d2db13

Please sign in to comment.