From 11f4f6196fae0c9194ce71c87a2a5a2e38b50cbb Mon Sep 17 00:00:00 2001 From: Andriy Biletsky Date: Fri, 15 Nov 2024 00:48:43 +0700 Subject: [PATCH] Fix cache attachment --- api/routes.go | 2 +- app/proxy/proxy.go | 7 +++---- app/publish/publish.go | 3 +-- app/publish/tus.go | 3 +-- app/query/cache.go | 16 +++++++++++----- app/query/caller.go | 1 + apps/lbrytv/config/config.go | 2 +- readme.md | 2 +- 8 files changed, 20 insertions(+), 16 deletions(-) diff --git a/api/routes.go b/api/routes.go index bca1a563..585a823f 100644 --- a/api/routes.go +++ b/api/routes.go @@ -209,7 +209,7 @@ func defaultMiddlewares(oauthAuther auth.Authenticator, legacyProvider auth.Prov panic(err) } cache := query.NewQueryCache(store) - logger.Log().Infof("cache configured: master=%s", config.GetSturdyCacheMaster()) + logger.Log().Infof("cache configured: master=%s, replicas=%s", config.GetSturdyCacheMaster(), config.GetSturdyCacheReplicas()) defaultHeaders := []string{ wallet.LegacyTokenHeader, wallet.AuthorizationHeader, "X-Requested-With", "Content-Type", "Accept", diff --git a/app/proxy/proxy.go b/app/proxy/proxy.go index 1a1dab08..b583513f 100644 --- a/app/proxy/proxy.go +++ b/app/proxy/proxy.go @@ -10,13 +10,12 @@ import ( "context" "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "strings" "github.com/OdyseeTeam/odysee-api/app/auth" "github.com/OdyseeTeam/odysee-api/app/query" - "github.com/OdyseeTeam/odysee-api/app/query/cache" "github.com/OdyseeTeam/odysee-api/app/sdkrouter" "github.com/OdyseeTeam/odysee-api/internal/audit" "github.com/OdyseeTeam/odysee-api/internal/errors" @@ -72,7 +71,7 @@ func Handle(w http.ResponseWriter, r *http.Request) { return } - body, err := ioutil.ReadAll(r.Body) + body, err := io.ReadAll(r.Body) if err != nil { w.WriteHeader(http.StatusBadRequest) writeResponse(w, rpcerrors.NewJSONParseError(errors.Err("error reading request body")).JSON()) @@ -130,7 +129,7 @@ func Handle(w http.ResponseWriter, r *http.Request) { return nil, nil }, "") - if cache.HasCache(r) { + if query.HasCache(r) { c.Cache = query.CacheFromRequest(r) } diff --git a/app/publish/publish.go b/app/publish/publish.go index 96171127..f1ec85e4 100644 --- a/app/publish/publish.go +++ b/app/publish/publish.go @@ -14,7 +14,6 @@ import ( "github.com/OdyseeTeam/odysee-api/app/auth" "github.com/OdyseeTeam/odysee-api/app/proxy" "github.com/OdyseeTeam/odysee-api/app/query" - "github.com/OdyseeTeam/odysee-api/app/query/cache" "github.com/OdyseeTeam/odysee-api/app/sdkrouter" "github.com/OdyseeTeam/odysee-api/internal/errors" "github.com/OdyseeTeam/odysee-api/internal/metrics" @@ -161,7 +160,7 @@ retry: }() var qCache *query.QueryCache - if cache.HasCache(r) { + if query.HasCache(r) { qCache = query.CacheFromRequest(r) } diff --git a/app/publish/tus.go b/app/publish/tus.go index a0f9ad5a..cb90a3d4 100644 --- a/app/publish/tus.go +++ b/app/publish/tus.go @@ -12,7 +12,6 @@ import ( "github.com/OdyseeTeam/odysee-api/app/auth" "github.com/OdyseeTeam/odysee-api/app/proxy" "github.com/OdyseeTeam/odysee-api/app/query" - "github.com/OdyseeTeam/odysee-api/app/query/cache" "github.com/OdyseeTeam/odysee-api/app/sdkrouter" "github.com/OdyseeTeam/odysee-api/app/wallet" "github.com/OdyseeTeam/odysee-api/internal/errors" @@ -265,7 +264,7 @@ func (h TusHandler) Notify(w http.ResponseWriter, r *http.Request) { // upload is completed, notify lbrynet server var qCache *query.QueryCache - if cache.HasCache(r) { + if query.HasCache(r) { qCache = query.CacheFromRequest(r) } diff --git a/app/query/cache.go b/app/query/cache.go index 5fadbaca..91621323 100644 --- a/app/query/cache.go +++ b/app/query/cache.go @@ -43,6 +43,7 @@ func NewQueryCache(store cache.CacheInterface[any]) *QueryCache { } func (c *QueryCache) Retrieve(query *Query, getter func() (any, error)) (*CachedResponse, error) { + log := logger.Log() cacheReq := CacheRequest{ Method: query.Method(), Params: query.Params(), @@ -59,10 +60,12 @@ func (c *QueryCache) Retrieve(query *Query, getter func() (any, error)) (*Cached } metrics.SturdyQueryCacheHitCount.WithLabelValues(cacheReq.Method).Inc() if getter == nil { + log.Warnf("nil getter provided for %s", query.Method()) metrics.SturdyQueryCacheErrorCount.WithLabelValues(cacheReq.Method).Inc() return nil, errors.New("cache miss with no object getter provided") } + log.Infof("cold cache retrieval for %s", query.Method()) obj, err, _ := c.singleflight.Do(cacheReq.GetCacheKey(), getter) if err != nil { metrics.SturdyQueryCacheErrorCount.WithLabelValues(cacheReq.Method).Inc() @@ -144,17 +147,20 @@ func (r *CachedResponse) UnmarshalBinary(data []byte) error { return json.Unmarshal(data, r) } -func preflightCacheHook(c *Caller, ctx context.Context) (*jsonrpc.RPCResponse, error) { - if c.Cache == nil { +func preflightCacheHook(caller *Caller, ctx context.Context) (*jsonrpc.RPCResponse, error) { + log := logger.Log() + if caller.Cache == nil { + log.Warn("no cache present on caller") return nil, nil } query := QueryFromContext(ctx) - cachedResp, err := c.Cache.Retrieve(query, func() (any, error) { - return c.SendQuery(ctx, query) + cachedResp, err := caller.Cache.Retrieve(query, func() (any, error) { + log.Debugf("cache miss, calling %s", query.Method()) + return caller.SendQuery(ctx, query) }) if err != nil { return nil, rpcerrors.NewSDKError(err) } - logger.Log().Debugf("FFS") + log.Debugf("cache hit for %s", query.Method()) return cachedResp.RPCResponse(query.Request.ID), nil } diff --git a/app/query/caller.go b/app/query/caller.go index 5eee72bc..4f039ee2 100644 --- a/app/query/caller.go +++ b/app/query/caller.go @@ -198,6 +198,7 @@ func (c *Caller) call(ctx context.Context, req *jsonrpc.RPCRequest) (*jsonrpc.RP return nil, rpcerrors.NewSDKError(err) } if res != nil { + logger.Log().Infof("got %s response from %s hook", q.Method(), hook.name) return res, nil } } diff --git a/apps/lbrytv/config/config.go b/apps/lbrytv/config/config.go index d256f535..405f30ea 100644 --- a/apps/lbrytv/config/config.go +++ b/apps/lbrytv/config/config.go @@ -90,7 +90,7 @@ func GetSturdyCacheReplicas() []string { } func GetSturdyCachePassword() string { - return Config.Viper.GetString("sturdycache.replicas") + return Config.Viper.GetString("sturdycache.password") } // GetDatabase returns postgresql database server connection config. diff --git a/readme.md b/readme.md index 823a76ed..72b891f3 100644 --- a/readme.md +++ b/readme.md @@ -50,7 +50,7 @@ Make sure you have recent enough Docker and `docker compose` installed. This will pull and launch SDK and postgres images, which Odysee API requires to operate. -`docker compose -f docker-compose.yml -f docker compose.app.yml up -d` +`docker compose -f docker-compose.yml -f docker-compose.app.yml up -d` *Note: if you're running a LBRY desktop app or lbrynet instance, you will have to either shut it down or change ports*