From d10616ddf2598052702aeb3dadbb05b8fd6a631b Mon Sep 17 00:00:00 2001 From: Andriy Biletsky Date: Fri, 15 Nov 2024 00:47:44 +0700 Subject: [PATCH 1/4] Tweak launcher script --- docker/launcher.sh | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/docker/launcher.sh b/docker/launcher.sh index c7551219..8a137568 100755 --- a/docker/launcher.sh +++ b/docker/launcher.sh @@ -1,5 +1,9 @@ #!/bin/sh -ssh-keygen -t rsa -f token_privkey.rsa -m pem +file="token_privkey.rsa" +if [ ! -f "$file" ]; then + ssh-keygen -t rsa -f "$file" -m pem +fi + ./oapi db_migrate_up ./oapi From 5389a0a29150acb777c93740670b0dd31e30d8ce Mon Sep 17 00:00:00 2001 From: Andriy Biletsky Date: Fri, 15 Nov 2024 00:48:12 +0700 Subject: [PATCH 2/4] Fix local config files --- docker-compose.app.yml | 5 +++-- docker/oapi.yml | 16 +++++++++++----- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/docker-compose.app.yml b/docker-compose.app.yml index b8bfafb4..4b7408a5 100644 --- a/docker-compose.app.yml +++ b/docker-compose.app.yml @@ -1,5 +1,3 @@ -version: "3.2" - services: oapi: image: odyseeteam/odysee-api:latest @@ -15,3 +13,6 @@ services: - postgres labels: com.centurylinklabs.watchtower.enable: true + +volumes: + storage: {} diff --git a/docker/oapi.yml b/docker/oapi.yml index 41120761..0bbea39f 100644 --- a/docker/oapi.yml +++ b/docker/oapi.yml @@ -25,9 +25,9 @@ StreamsV6: InternalAPIHost: https://api.lbry.com ProjectURL: https://lbry.tv -DatabaseDSN: postgres://lbrytv:lbrytv@postgres +DatabaseDSN: postgres://postgres:odyseeteam@postgres Database: - DBName: lbrytv + DBName: postgres Options: sslmode=disable OAuth: @@ -56,12 +56,18 @@ RPCTimeouts: transaction_list: 4m publish: 4m -RedisLocker: redis://:odyredis@localhost:6379/1 -RedisBus: redis://:odyredis@localhost:6379/2 +RedisLocker: redis://:odyredis@redis:6379/1 +RedisBus: redis://:odyredis@redis:6379/2 # AsynqueryRequestsConnURL is Redis database where asynquery will be listening for finalized uploads requests. # This corresponds to AsynqueryRequestsConnURL in forklift.yml config. -AsynqueryRequestsConnURL: redis://:odyredis@localhost:6379/3 +AsynqueryRequestsConnURL: redis://:odyredis@redis:6379/3 + +SturdyCache: + Master: redis:6379 + Replicas: + - redis:6379 + Password: odyredis ReflectorUpstream: DatabaseDSN: 'user:password@tcp(localhost:3306)/blobs' From f9048a82a6c002e8917d0076752ef3f8c6584a9d Mon Sep 17 00:00:00 2001 From: Andriy Biletsky Date: Fri, 15 Nov 2024 00:48:31 +0700 Subject: [PATCH 3/4] Disable V3 publish --- cmd/serve.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/cmd/serve.go b/cmd/serve.go index 35c0d26a..0c8e0cd1 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -4,9 +4,7 @@ import ( "fmt" "io/ioutil" "log" - "math/rand" "os" - "time" "github.com/OdyseeTeam/odysee-api/api" "github.com/OdyseeTeam/odysee-api/app/sdkrouter" @@ -22,13 +20,12 @@ var rootCmd = &cobra.Command{ Use: "oapi", Short: "backend server for Odysee frontend", Run: func(_ *cobra.Command, _ []string) { - rand.Seed(time.Now().UnixNano()) // always seed random! sdkRouter := sdkrouter.New(config.GetLbrynetServers()) go sdkRouter.WatchLoad() s := server.NewServer(config.GetAddress(), sdkRouter, &api.RoutesOptions{ EnableProfiling: config.GetProfiling(), - EnableV3Publish: true, + EnableV3Publish: false, }) err := s.Start() if err != nil { From a31703229f47a945cbaf9f7f80eeb08618fc5be1 Mon Sep 17 00:00:00 2001 From: Andriy Biletsky Date: Fri, 15 Nov 2024 00:48:43 +0700 Subject: [PATCH 4/4] 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..c9dbc2c9 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().Info("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*