Skip to content

Commit

Permalink
Add cache warmup driver to OCS
Browse files Browse the repository at this point in the history
  • Loading branch information
ishank011 committed Apr 26, 2021
1 parent 7d05e96 commit 203ba5d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 12 deletions.
24 changes: 13 additions & 11 deletions internal/http/services/owncloud/ocs/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,19 @@ import (

// Config holds the config options that need to be passed down to all ocs handlers
type Config struct {
Prefix string `mapstructure:"prefix"`
Config data.ConfigData `mapstructure:"config"`
Capabilities data.CapabilitiesData `mapstructure:"capabilities"`
GatewaySvc string `mapstructure:"gatewaysvc"`
DefaultUploadProtocol string `mapstructure:"default_upload_protocol"`
UserAgentChunkingMap map[string]string `mapstructure:"user_agent_chunking_map"`
SharePrefix string `mapstructure:"share_prefix"`
HomeNamespace string `mapstructure:"home_namespace"`
AdditionalInfoAttribute string `mapstructure:"additional_info_attribute"`
ResourceInfoCacheSize int `mapstructure:"resource_info_cache_size"`
ResourceInfoCacheTTL int `mapstructure:"resource_info_cache_ttl"`
Prefix string `mapstructure:"prefix"`
Config data.ConfigData `mapstructure:"config"`
Capabilities data.CapabilitiesData `mapstructure:"capabilities"`
GatewaySvc string `mapstructure:"gatewaysvc"`
DefaultUploadProtocol string `mapstructure:"default_upload_protocol"`
UserAgentChunkingMap map[string]string `mapstructure:"user_agent_chunking_map"`
SharePrefix string `mapstructure:"share_prefix"`
HomeNamespace string `mapstructure:"home_namespace"`
AdditionalInfoAttribute string `mapstructure:"additional_info_attribute"`
CacheWarmupDriver string `mapstructure:"cache_warmup_driver"`
CacheWarmupDrivers map[string]map[string]interface{} `mapstructure:"cache_warmup_drivers"`
ResourceInfoCacheSize int `mapstructure:"resource_info_cache_size"`
ResourceInfoCacheTTL int `mapstructure:"resource_info_cache_ttl"`
}

// Init sets sane defaults
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ import (
"github.com/cs3org/reva/pkg/appctx"
"github.com/cs3org/reva/pkg/rgrpc/todo/pool"
"github.com/cs3org/reva/pkg/rhttp/router"
"github.com/cs3org/reva/pkg/share/cache"
"github.com/cs3org/reva/pkg/share/cache/registry"
"github.com/pkg/errors"
)

Expand All @@ -72,6 +74,13 @@ type userIdentifiers struct {
Mail string
}

func getCacheWarmupManager(c *config.Config) (cache.Warmup, error) {
if f, ok := registry.NewFuncs[c.CacheWarmupDriver]; ok {
return f(c.CacheWarmupDrivers[c.CacheWarmupDriver])
}
return nil, fmt.Errorf("driver not found: %s", c.CacheWarmupDriver)
}

// Init initializes this and any contained handlers
func (h *Handler) Init(c *config.Config) error {
h.gatewayAddr = c.GatewaySvc
Expand All @@ -87,9 +96,25 @@ func (h *Handler) Init(c *config.Config) error {

h.resourceInfoCache = gcache.New(c.ResourceInfoCacheSize).LFU().Build()

if h.resourceInfoCacheTTL > 0 {
cwm, _ := getCacheWarmupManager(c)
go h.startCacheWarmup(cwm)
}

return nil
}

func (h *Handler) startCacheWarmup(c cache.Warmup) {
infos, err := c.GetResourceInfos()
if err != nil {
return
}
for _, r := range infos {
key := wrapResourceID(r.Id)
_ = h.resourceInfoCache.SetWithExpire(key, r, time.Second*h.resourceInfoCacheTTL)
}
}

func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log := appctx.GetLogger(r.Context())

Expand Down
2 changes: 1 addition & 1 deletion pkg/share/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
)

// FS is the interface to implement cache warmup strategies.
// Warmup is the interface to implement cache warmup strategies.
type Warmup interface {
GetResourceInfos() ([]*provider.ResourceInfo, error)
}

0 comments on commit 203ba5d

Please sign in to comment.