【问题标题】:Elixir, Phoenix and Ecto : When database is down, requests to serve cached data become really slow (from 100ms to 3 seconds)Elixir、Phoenix 和 Ecto:当数据库关闭时,提供缓存数据的请求变得非常慢(从 100 毫秒到 3 秒)
【发布时间】:2021-07-27 23:25:12
【问题描述】:

我用 --no-html 和 --database mysql 创建了一个 phoenix 项目

我们在工作中使用的版本是:

Erlang/OTP 21 [erts-10.2] [source] [64-bit] [smp:12:12] [ds:12:12:10] [async-threads:1] [hipe]

Elixir 1.8.0 (compiled with Erlang/OTP 20)

我正在做一个项目,它充当状态页面工具 (hund.io) 和我们自己的 API 和报告工具之间的代理,所以我需要一个数据库来存储服务名称 + 请求的 URL,并且,因为我需要一点与数据库 Cachex (https://github.com/whitfin/cachex) 分离以将我的数据放入缓存中

当数据被缓存时,使用邮递员请求我的包装器的 api 大约需要 100 毫秒才能得到响应,并且 phoenix 的日志显示类似[info] Sent 200 in 1ms 但是,我曾经关闭数据库,邮递员最多需要 3 秒才能得到响应,凤凰日志仍然会说这种事情[info] Sent 200 in 1ms

我确信包装器在这两种情况下都使用缓存数据:

我的代码:

def show(conn, %{"service_name" => service_name}) do
    Logger.debug("Top of show func")

    case Cachex.get(:proxies, service_name) do
      {:ok, nil} ->
        Logger.debug("Service #{service_name} not found in cache")
        proxy = Health.get_proxy_by_name!(service_name)
        Logger.debug("Service #{service_name} found in db")
        Cachex.put(:proxies, service_name, proxy)
        proxy_with_health = Checker.call_api(proxy)

        render(conn, "show.json", proxy_health: proxy_with_health)

      {:ok, proxy} ->
        Logger.debug("Found service #{service_name} in cache")
        proxy_with_health = Checker.call_api(proxy)
        render(conn, "show.json", proxy_health: proxy_with_health)
    end
  end

日志:

[info] GET /api/proxies_health/docto
[debug] Processing with StatusWeb.ProxyHealthController.show/2
  Parameters: %{"service_name" => "docto"}
  Pipelines: [:api]
[debug] Top of show func
[debug] Found service docto in cache

对于路由器部分:

 pipeline :api do
    plug :accepts, ["json"]
  end

  scope "/api", StatusWeb do
    pipe_through :api

    resources "/proxies", ProxyController, except: [:new, :edit]
    get "/proxies_health", ProxyHealthController, :index
    get "/proxies_health/:service_name", ProxyHealthController, :show
  end

我在日志中也有两个错误:

[error] MyXQL.Connection (#PID<0.373.0>) failed to connect: ** (DBConnection.ConnectionError) connection refused

这似乎是“正常的”,因为应用程序想要重新连接到数据库。 而这个,就在处理请求之前(至少根据日志)

[error] Could not create schema migrations table. This error usually happens due to the following:

  * The database does not exist
  * The "schema_migrations" table, which Ecto uses for managing
    migrations, was defined by another library
  * There is a deadlock while migrating (such as using concurrent
    indexes with a migration_lock)

To fix the first issue, run "mix ecto.create".

To address the second, you can run "mix ecto.drop" followed by
"mix ecto.create". Alternatively you may configure Ecto to use
another table and/or repository for managing migrations:

    config :status, Status.Repo,
      migration_source: "some_other_table_for_schema_migrations",
      migration_repo: AnotherRepoForSchemaMigrations

The full error report is shown below.

[error] GenServer #PID<0.620.0> terminating
** (DBConnection.ConnectionError) connection refused
    (db_connection) lib/db_connection/connection.ex:100: DBConnection.Connection.connect/2
    (connection) lib/connection.ex:622: Connection.enter_connect/5
    (stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
Last message: nil
State: MyXQL.Connection

我在查看 How do I detect database connection issues from Elixir Ecto? 时尝试放入我的 Status.Repo 文件 (repo.ex)

backoff_type: :stop

但这对我的问题没有任何改变

【问题讨论】:

    标签: elixir ecto phoenix


    【解决方案1】:

    作为我的代码,在缓存中找到对象的路径中不(根本)涉及数据库,我们最终在不和谐中发现问题来自端点中的插件,该插件应该只存在于开发环境(MyAppWeb/endpoint.ex)

       if code_reloading? do
          plug Phoenix.CodeReloader
          plug Phoenix.Ecto.CheckRepoStatus, otp_app: :status
      end
    

    这是一个插件,可以在每次请求时检查数据库是否已启动、迁移是否已运行等 等待 Ecto 的回答,在 genserver 中超时,意识到调用导致了延迟!

    解决方法是简单地注释该行

              plug Phoenix.Ecto.CheckRepoStatus, otp_app: :status
    

    【讨论】:

      猜你喜欢
      • 2018-07-07
      • 1970-01-01
      • 2014-11-05
      • 2012-06-08
      • 2017-04-15
      • 2013-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多