From d76fc2a451689bfb8db4411e7c6575a2df664623 Mon Sep 17 00:00:00 2001 From: jkoberg Date: Wed, 13 Dec 2023 13:05:31 +0100 Subject: [PATCH 1/2] add option to not persist natsjs stores Signed-off-by: jkoberg --- pkg/storage/cache/cache.go | 13 +++++++------ pkg/storage/utils/decomposedfs/decomposedfs.go | 1 + pkg/store/options.go | 14 ++++++++++++++ pkg/store/store.go | 7 +++++++ 4 files changed, 29 insertions(+), 6 deletions(-) diff --git a/pkg/storage/cache/cache.go b/pkg/storage/cache/cache.go index af4b0ffdce..516c65c738 100644 --- a/pkg/storage/cache/cache.go +++ b/pkg/storage/cache/cache.go @@ -44,12 +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"` + 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"` } // Cache handles key value operations on caches diff --git a/pkg/storage/utils/decomposedfs/decomposedfs.go b/pkg/storage/utils/decomposedfs/decomposedfs.go index 5c341c7357..8ac7799473 100644 --- a/pkg/storage/utils/decomposedfs/decomposedfs.go +++ b/pkg/storage/utils/decomposedfs/decomposedfs.go @@ -143,6 +143,7 @@ func NewDefault(m map[string]interface{}, bs tree.Blobstore, es events.Stream) ( microstore.Nodes(o.IDCache.Nodes...), microstore.Database(o.IDCache.Database), microstore.Table(o.IDCache.Table), + store.DisablePersistance(o.IDCache.DisablePersistence), )) permissionsSelector, err := pool.PermissionsSelector(o.PermissionsSVC, pool.WithTLSMode(o.PermTLSMode)) diff --git a/pkg/store/options.go b/pkg/store/options.go index 5866d6f09a..c6f3c9fee9 100644 --- a/pkg/store/options.go +++ b/pkg/store/options.go @@ -75,3 +75,17 @@ func TTL(val time.Duration) store.Option { o.Context = context.WithValue(o.Context, ttlContextKey{}, val) } } + +type disablePersistanceContextKey struct{} + +// DisablePersistance disables the persistence of the store by instructing it to use memory only. +// Only supported by the `natsjs` and `natsjskv` implementations. +func DisablePersistance(val bool) store.Option { + return func(o *store.Options) { + if o.Context == nil { + o.Context = context.Background() + } + + o.Context = context.WithValue(o.Context, disablePersistanceContextKey{}, val) + } +} diff --git a/pkg/store/store.go b/pkg/store/store.go index c3f66d376f..67ef00dd2b 100644 --- a/pkg/store/store.go +++ b/pkg/store/store.go @@ -119,6 +119,9 @@ func Create(opts ...microstore.Option) microstore.Store { return *ocMemStore case TypeNatsJS: ttl, _ := options.Context.Value(ttlContextKey{}).(time.Duration) + if mem, _ := options.Context.Value(disablePersistanceContextKey{}).(bool); mem { + opts = append(opts, natsjs.DefaultMemory()) + } // TODO nats needs a DefaultTTL option as it does not support per Write TTL ... // FIXME nats has restrictions on the key, we cannot use slashes AFAICT // host, port, clusterid @@ -132,6 +135,10 @@ func Create(opts ...microstore.Option) microstore.Store { case TypeNatsJSKV: // NOTE: nats needs a DefaultTTL option as it does not support per Write TTL ... ttl, _ := options.Context.Value(ttlContextKey{}).(time.Duration) + if mem, _ := options.Context.Value(disablePersistanceContextKey{}).(bool); mem { + opts = append(opts, natsjskv.DefaultMemory()) + } + natsOptions := nats.GetDefaultOptions() natsOptions.Name = "TODO" // we can pass in the service name to allow identifying the client, but that requires adding a custom context option return natsjskv.NewStore( From 2516d234cdc06098f8ce8a68252494f66aea73f7 Mon Sep 17 00:00:00 2001 From: jkoberg Date: Wed, 13 Dec 2023 16:03:15 +0100 Subject: [PATCH 2/2] rework cache configuration Signed-off-by: jkoberg --- .../unreleased/rework-cache-configuration.md | 6 ++ internal/grpc/services/gateway/gateway.go | 65 +++++++------------ .../grpc/services/gateway/storageprovider.go | 1 - .../services/gateway/storageprovidercache.go | 7 +- .../services/owncloud/ocs/config/config.go | 8 +-- .../handlers/apps/sharing/shares/shares.go | 2 +- .../apps/sharing/shares/shares_test.go | 6 +- internal/http/services/owncloud/ocs/ocs.go | 5 +- pkg/rhttp/datatx/manager/simple/simple.go | 2 +- pkg/rhttp/datatx/manager/spaces/spaces.go | 2 +- pkg/rhttp/datatx/manager/tus/tus.go | 3 +- pkg/storage/cache/cache.go | 59 ++++++++--------- pkg/storage/cache/createhome.go | 11 ++-- pkg/storage/cache/createpersonalspace.go | 12 ++-- pkg/storage/cache/filemetadata.go | 14 ++-- pkg/storage/cache/provider.go | 11 ++-- pkg/storage/cache/stat.go | 11 ++-- .../utils/decomposedfs/decomposedfs.go | 6 +- .../metadata/messagepack_backend.go | 3 +- pkg/store/options.go | 4 +- 20 files changed, 106 insertions(+), 132 deletions(-) create mode 100644 changelog/unreleased/rework-cache-configuration.md diff --git a/changelog/unreleased/rework-cache-configuration.md b/changelog/unreleased/rework-cache-configuration.md new file mode 100644 index 0000000000..66e99fd60c --- /dev/null +++ b/changelog/unreleased/rework-cache-configuration.md @@ -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 diff --git a/internal/grpc/services/gateway/gateway.go b/internal/grpc/services/gateway/gateway.go index 2dc2d44da5..b8e6533da9 100644 --- a/internal/grpc/services/gateway/gateway.go +++ b/internal/grpc/services/gateway/gateway.go @@ -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" @@ -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 @@ -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" } } @@ -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 @@ -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 @@ -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 } diff --git a/internal/grpc/services/gateway/storageprovider.go b/internal/grpc/services/gateway/storageprovider.go index a4ad2af393..5347f4bacd 100644 --- a/internal/grpc/services/gateway/storageprovider.go +++ b/internal/grpc/services/gateway/storageprovider.go @@ -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 } diff --git a/internal/grpc/services/gateway/storageprovidercache.go b/internal/grpc/services/gateway/storageprovidercache.go index 806018d643..8dd5d720b5 100644 --- a/internal/grpc/services/gateway/storageprovidercache.go +++ b/internal/grpc/services/gateway/storageprovidercache.go @@ -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 } @@ -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 } } @@ -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) } } diff --git a/internal/http/services/owncloud/ocs/config/config.go b/internal/http/services/owncloud/ocs/config/config.go index 09c5baf43d..554a9eeb3c 100644 --- a/internal/http/services/owncloud/ocs/config/config.go +++ b/internal/http/services/owncloud/ocs/config/config.go @@ -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 @@ -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"` diff --git a/internal/http/services/owncloud/ocs/handlers/apps/sharing/shares/shares.go b/internal/http/services/owncloud/ocs/handlers/apps/sharing/shares/shares.go index a2552b69ea..22265a857e 100644 --- a/internal/http/services/owncloud/ocs/handlers/apps/sharing/shares/shares.go +++ b/internal/http/services/owncloud/ocs/handlers/apps/sharing/shares/shares.go @@ -150,7 +150,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 { diff --git a/internal/http/services/owncloud/ocs/handlers/apps/sharing/shares/shares_test.go b/internal/http/services/owncloud/ocs/handlers/apps/sharing/shares/shares_test.go index b207433be1..cc8c83ce5b 100644 --- a/internal/http/services/owncloud/ocs/handlers/apps/sharing/shares/shares_test.go +++ b/internal/http/services/owncloud/ocs/handlers/apps/sharing/shares/shares_test.go @@ -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 @@ -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{ @@ -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{ diff --git a/internal/http/services/owncloud/ocs/ocs.go b/internal/http/services/owncloud/ocs/ocs.go index 2982e4dfa2..c71da5d9b6 100644 --- a/internal/http/services/owncloud/ocs/ocs.go +++ b/internal/http/services/owncloud/ocs/ocs.go @@ -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" @@ -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 diff --git a/pkg/rhttp/datatx/manager/simple/simple.go b/pkg/rhttp/datatx/manager/simple/simple.go index b5ac5b924b..a89ce71ecd 100644 --- a/pkg/rhttp/datatx/manager/simple/simple.go +++ b/pkg/rhttp/datatx/manager/simple/simple.go @@ -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 } diff --git a/pkg/rhttp/datatx/manager/spaces/spaces.go b/pkg/rhttp/datatx/manager/spaces/spaces.go index 6e2ef03709..d39b12baa5 100644 --- a/pkg/rhttp/datatx/manager/spaces/spaces.go +++ b/pkg/rhttp/datatx/manager/spaces/spaces.go @@ -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 } diff --git a/pkg/rhttp/datatx/manager/tus/tus.go b/pkg/rhttp/datatx/manager/tus/tus.go index a46bd17739..fbad5e5bb8 100644 --- a/pkg/rhttp/datatx/manager/tus/tus.go +++ b/pkg/rhttp/datatx/manager/tus/tus.go @@ -23,7 +23,6 @@ import ( "log" "net/http" "path" - "time" "github.com/pkg/errors" tusd "github.com/tus/tusd/pkg/handler" @@ -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 } diff --git a/pkg/storage/cache/cache.go b/pkg/storage/cache/cache.go index 516c65c738..c02d246242 100644 --- a/pkg/storage/cache/cache.go +++ b/pkg/storage/cache/cache.go @@ -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 @@ -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] } @@ -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), ) } diff --git a/pkg/storage/cache/createhome.go b/pkg/storage/cache/createhome.go index 8a39c68a58..3b3c67835d 100644 --- a/pkg/storage/cache/createhome.go +++ b/pkg/storage/cache/createhome.go @@ -20,7 +20,6 @@ package cache import ( "strings" - "time" userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1" @@ -32,12 +31,12 @@ type createHomeCache struct { } // NewCreateHomeCache creates a new CreateHomeCache -func NewCreateHomeCache(store string, nodes []string, database, table string, ttl time.Duration, size int) CreateHomeCache { +func NewCreateHomeCache(cfg Config) CreateHomeCache { c := &createHomeCache{} - c.s = getStore(store, nodes, database, table, ttl, size) - c.database = database - c.table = table - c.ttl = ttl + c.s = getStore(cfg) + c.database = cfg.Database + c.table = cfg.Table + c.ttl = cfg.TTL return c } diff --git a/pkg/storage/cache/createpersonalspace.go b/pkg/storage/cache/createpersonalspace.go index fa6f9d11e9..b7422b504e 100644 --- a/pkg/storage/cache/createpersonalspace.go +++ b/pkg/storage/cache/createpersonalspace.go @@ -19,8 +19,6 @@ package cache import ( - "time" - userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" ) @@ -30,12 +28,12 @@ type createPersonalSpaceCache struct { } // NewCreatePersonalSpaceCache creates a new CreatePersonalSpaceCache -func NewCreatePersonalSpaceCache(store string, nodes []string, database, table string, ttl time.Duration, size int) CreatePersonalSpaceCache { +func NewCreatePersonalSpaceCache(cfg Config) CreatePersonalSpaceCache { c := &createPersonalSpaceCache{} - c.s = getStore(store, nodes, database, table, ttl, size) - c.database = database - c.table = table - c.ttl = ttl + c.s = getStore(cfg) + c.database = cfg.Database + c.table = cfg.Table + c.ttl = cfg.TTL return c } diff --git a/pkg/storage/cache/filemetadata.go b/pkg/storage/cache/filemetadata.go index 41053b4f28..987e451df0 100644 --- a/pkg/storage/cache/filemetadata.go +++ b/pkg/storage/cache/filemetadata.go @@ -18,21 +18,17 @@ package cache -import ( - "time" -) - type fileMetadataCache struct { cacheStore } // NewFileMetadataCache creates a new FileMetadataCache -func NewFileMetadataCache(store string, nodes []string, database, table string, ttl time.Duration, size int) FileMetadataCache { +func NewFileMetadataCache(cfg Config) FileMetadataCache { c := &fileMetadataCache{} - c.s = getStore(store, nodes, database, table, ttl, size) - c.database = database - c.table = table - c.ttl = ttl + c.s = getStore(cfg) + c.database = cfg.Database + c.table = cfg.Table + c.ttl = cfg.TTL return c } diff --git a/pkg/storage/cache/provider.go b/pkg/storage/cache/provider.go index 23b8a3f824..306498c103 100644 --- a/pkg/storage/cache/provider.go +++ b/pkg/storage/cache/provider.go @@ -20,7 +20,6 @@ package cache import ( "sync" - "time" userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1" @@ -33,12 +32,12 @@ type providerCache struct { } // NewProviderCache creates a new ProviderCache -func NewProviderCache(store string, nodes []string, database, table string, ttl time.Duration, size int) ProviderCache { +func NewProviderCache(cfg Config) ProviderCache { c := &providerCache{} - c.s = getStore(store, nodes, database, table, ttl, size) - c.database = database - c.table = table - c.ttl = ttl + c.s = getStore(cfg) + c.database = cfg.Database + c.table = cfg.Table + c.ttl = cfg.TTL return c } diff --git a/pkg/storage/cache/stat.go b/pkg/storage/cache/stat.go index bb39cf9395..19045e3625 100644 --- a/pkg/storage/cache/stat.go +++ b/pkg/storage/cache/stat.go @@ -22,7 +22,6 @@ import ( "context" "strings" "sync" - "time" userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1" @@ -40,12 +39,12 @@ func init() { } // NewStatCache creates a new StatCache -func NewStatCache(store string, nodes []string, database, table string, ttl time.Duration, size int) StatCache { +func NewStatCache(cfg Config) StatCache { c := statCache{} - c.s = getStore(store, nodes, database, table, ttl, size) - c.database = database - c.table = table - c.ttl = ttl + c.s = getStore(cfg) + c.database = cfg.Database + c.table = cfg.Table + c.ttl = cfg.TTL return &c } diff --git a/pkg/storage/utils/decomposedfs/decomposedfs.go b/pkg/storage/utils/decomposedfs/decomposedfs.go index 8ac7799473..60662ee814 100644 --- a/pkg/storage/utils/decomposedfs/decomposedfs.go +++ b/pkg/storage/utils/decomposedfs/decomposedfs.go @@ -138,12 +138,12 @@ func NewDefault(m map[string]interface{}, bs tree.Blobstore, es events.Stream) ( tp := tree.New(lu, bs, o, store.Create( store.Store(o.IDCache.Store), - store.TTL(time.Duration(o.IDCache.TTL)*time.Second), + store.TTL(o.IDCache.TTL), store.Size(o.IDCache.Size), microstore.Nodes(o.IDCache.Nodes...), microstore.Database(o.IDCache.Database), microstore.Table(o.IDCache.Table), - store.DisablePersistance(o.IDCache.DisablePersistence), + store.DisablePersistence(o.IDCache.DisablePersistence), )) permissionsSelector, err := pool.PermissionsSelector(o.PermissionsSVC, pool.WithTLSMode(o.PermTLSMode)) @@ -204,7 +204,7 @@ func New(o *options.Options, lu *lookup.Lookup, p Permissions, tp Tree, es event p: p, chunkHandler: chunking.NewChunkHandler(filepath.Join(o.Root, "uploads")), stream: es, - cache: cache.GetStatCache(o.StatCache.Store, o.StatCache.Nodes, o.StatCache.Database, "stat", time.Duration(o.StatCache.TTL)*time.Second, o.StatCache.Size), + cache: cache.GetStatCache(o.StatCache), UserCache: ttlcache.NewCache(), userSpaceIndex: userSpaceIndex, groupSpaceIndex: groupSpaceIndex, diff --git a/pkg/storage/utils/decomposedfs/metadata/messagepack_backend.go b/pkg/storage/utils/decomposedfs/metadata/messagepack_backend.go index 92caf0cf52..ab7c7e8fdb 100644 --- a/pkg/storage/utils/decomposedfs/metadata/messagepack_backend.go +++ b/pkg/storage/utils/decomposedfs/metadata/messagepack_backend.go @@ -27,7 +27,6 @@ import ( "path/filepath" "strconv" "strings" - "time" "github.com/cs3org/reva/v2/pkg/storage/cache" "github.com/google/renameio/v2" @@ -53,7 +52,7 @@ type readWriteCloseSeekTruncater interface { func NewMessagePackBackend(rootPath string, o cache.Config) MessagePackBackend { return MessagePackBackend{ rootPath: filepath.Clean(rootPath), - metaCache: cache.GetFileMetadataCache(o.Store, o.Nodes, o.Database, "filemetadata:", time.Duration(o.TTL)*time.Second, o.Size), + metaCache: cache.GetFileMetadataCache(o), } } diff --git a/pkg/store/options.go b/pkg/store/options.go index c6f3c9fee9..c4e177e546 100644 --- a/pkg/store/options.go +++ b/pkg/store/options.go @@ -78,9 +78,9 @@ func TTL(val time.Duration) store.Option { type disablePersistanceContextKey struct{} -// DisablePersistance disables the persistence of the store by instructing it to use memory only. +// DisablePersistence disables the persistence of the store by instructing it to use memory only. // Only supported by the `natsjs` and `natsjskv` implementations. -func DisablePersistance(val bool) store.Option { +func DisablePersistence(val bool) store.Option { return func(o *store.Options) { if o.Context == nil { o.Context = context.Background()