gpt4 book ai didi

erlang - 如何处理 Poolboy 中的超时?

转载 作者:行者123 更新时间:2023-12-02 04:13:14 31 4
gpt4 key购买 nike

我有一个耗时较长的迁移问题,我希望并行运行(它可以并行运行)。实际上,迁移就是获取数据库中的所有记录,并对每条记录执行耗时且耗时的操作。

有时个别记录迁移会挂起,所以我会花 10 分钟来完成。如果迁移未完成,我希望它能够正常关闭,没有任何异常(见下文)

我也在使用poolboy erlang 包来并行化实现,因为迁移不仅消耗时间,还消耗资源。问题是我不知道当超时发生并且代码将要中断时如何处理错误。我的监督树是:

defmodule MyReelty.Repo.Migrations.MoveVideosFromVimeoToB2 do
use Ecto.Migration

alias MyReelty.Repo
alias MyReelty.Repo.Migrations.MoveVideosFromVimeoToB2.Migrator

# parallel nature of migration force us to disable transaction
@disable_ddl_transaction true

@migrator_waiting_time 10 * 60 * 1000 # timeout
@poolboy_waiting_time @migrator_waiting_time + 10 * 1000 # give a time for graceful shutdown

@pool_name :migrator
@pool_size 3
@pool_config [
{ :name, { :local, @pool_name }},
{ :worker_module, Migrator },
{ :size, @pool_size },
{ :max_overflow, 0 },
{ :strategy, :fifo }
]

def up do
children = [
:poolboy.child_spec(@pool_name, @pool_config)
]
opts = [strategy: :one_for_one, name: MyReelty.Supervisor]
Supervisor.start_link(children, opts)

rows = Review |> Repo.all

IO.puts "Total amount of reviews is: #{length(rows)}"

parallel_migrations(rows)
end

def parallel_migrations(rows) do
Enum.map(rows, fn(row) ->
pooled_migration(@pool_name, row)
end)
end

def pooled_migration(pool, x) do
:poolboy.transaction(
pool,
(fn(pid) -> Migrator.move(pid, { x, @migrator_waiting_time }) end),
@poolboy_waiting_time
)
end

defmodule Migrator do
alias MyReelty.Repo
alias MyReelty.Review

use GenServer

def start_link(_) do
GenServer.start_link(__MODULE__, nil, [])
end

def move(server, { params, waiting_time }) do
GenServer.call(server, { :move, params }, waiting_time)
end

def handle_call({ :move, result }, _from, state) do
big_time_and_resource_consuming_task_here
{:reply, %{}, state}
end
end
end

如果数据库中某些记录的迁移需要超过 10 分钟的问题我有这种异常:

20:18:16.917 [error] Task #PID<0.282.0> started from #PID<0.70.0> terminating
** (stop) exited in: GenServer.call(#PID<0.278.0>, {:move, [2, "/videos/164064419", "w 35th st Springfield United States Illinois 60020"]}, 60000)
** (EXIT) time out
(elixir) lib/gen_server.ex:604: GenServer.call/3
(poolboy) src/poolboy.erl:76: :poolboy.transaction/3
(elixir) lib/task/supervised.ex:94: Task.Supervised.do_apply/2
(elixir) lib/task/supervised.ex:45: Task.Supervised.reply/5
(stdlib) proc_lib.erl:247: :proc_lib.init_p_do_apply/3
Function: #Function<5.53617785/0 in MyReelty.Repo.Migrations.MoveVideosFromVimeoToB2.parallel_migrations/1>
Args: []

20:18:16.918 [error] GenServer MyReelty.Repo terminating
** (stop) exited in: GenServer.call(#PID<0.278.0>, {:move, [2, "/videos/164064419", "w 35th st Springfield United States Illinois 60020"]}, 60000)
** (EXIT) time out
Last message: {:EXIT, #PID<0.70.0>, {:timeout, {GenServer, :call, [#PID<0.278.0>, {:move, [2, "/videos/164064419", "w 35th st Springfield United States Illinois 60020"]}, 60000]}}}
State: {:state, {:local, MyReelty.Repo}, :one_for_one, [{:child, #PID<0.231.0>, DBConnection.Poolboy, {:poolboy, :start_link, [[name: {:local, MyReelty.Repo.Pool}, strategy: :fifo, size: 1, max_overflow: 0, worker_module: DBConnection.Poolboy.Worker], {Postgrex.Protocol, [types: true, username: "adik", types: true, name: MyReelty.Repo.Pool, otp_app: :my_reelty, repo: MyReelty.Repo, adapter: Ecto.Adapters.Postgres, database: "my_reelty_dev", hostname: "localhost", extensions: [{Geo.PostGIS.Extension, [library: Geo]}, {Ecto.Adapters.Postgres.DateTime, []}, {Postgrex.Extensions.JSON, [library: Poison]}], pool_size: 1, pool_timeout: 5000, timeout: 15000, adapter: Ecto.Adapters.Postgres, database: "my_dev", hostname: "localhost", pool_size: 10, pool: DBConnection.Poolboy, port: 5432]}]}, :permanent, 5000, :worker, [:poolboy]}], :undefined, 3, 5, [], 0, Ecto.Repo.Supervisor, {MyReelty.Repo, :my_reelty, Ecto.Adapters.Postgres, [otp_app: :my_reelty, repo: MyReelty.Repo, adapter: Ecto.Adapters.Postgres, database: "my_reelty_dev", hostname: "localhost", extensions: [{Geo.PostGIS.Extension, [library: Geo]}], pool_size: 1]}}

我尝试将 terminate/2handle_info/2 插入到 Migrator 并使用它,但我什至还没有达到这个目的要调用的函数。如何处理超时并防止它们破坏我的迁移?

已更新

我使用了@johlo的提示,但我仍然超时。我的职能是:

def init(_) do
Process.flag(:trap_exit, true)
{:ok, %{}}
end

最佳答案

Migrator.move/2(即 GenServer.call)函数超时时,整个 MoveVideosFromVimeoToB2 进程将会崩溃,因为进行 GenServer 调用的实际进程。

这里的解决方案是捕获pooled_migration中匿名函数的超时,类似于(我对Elixir语法不太熟悉,所以它可能无法编译,但你应该明白):

def pooled_migration(pool, x) do
:poolboy.transaction(
pool,
(fn(pid) ->
try do
Migrator.move(pid, { x, @migrator_waiting_time })
catch
:exit, reason ->
# Ignore error, log it or something else
:ok
end
end),
@poolboy_waiting_time
)
end

并不是 Migrator 进程超时,而是 GenServerMigrator 的调用超时,我们需要尝试-捕获那个。

另请注意,Migrator 进程并未被终止,它仍在运行,请参阅 GenServer call documentation 中的超时 部分。 .

更新:正如@asiniy在评论中提到的,@poolboy_waiting_time应该设置为:infinity,这样poolboy.transaction函数就不会抛出超时错误当等待空闲的 Migrator 工作进程时。由于Migrator最终会退出,所以这是安全的。

关于erlang - 如何处理 Poolboy 中的超时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38664319/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com