一个Erlang gen_server在另一个节点上如何启动一个gen_server? [英] How does an Erlang gen_server start_link a gen_server on another node?

查看:299
本文介绍了一个Erlang gen_server在另一个节点上如何启动一个gen_server?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Erlang应用程序,有一点太资源匮乏,留在一个节点上。我正在使gen_servers从一个进程移动到另一个进程 - 这是相对容易的。我处于最后一个障碍:获取创建这些gen_servers的工厂进程,以在远程节点上而不是本地进程生成它们。 start_link的默认行为显然只在本地启动,但我看不到任何改变的选项。



看起来我将不得不有创意的解决方案,并想看看有没有人已经实现了这样的事情取得成功。 IOW,推荐的解决方案是什么?



编辑



我在看通过调用触发的呼叫链:

  gen_server:start_link(?Module,Args,[])

gen_server:start_link / 3:

 code> start_link(Mod,Args,Options) - > 
gen:start(?MODULE,link,Mod,Args,Options)。

gen:start / 5:

  start(GenMod,LinkP,Mod,Args,Options) - > 
do_spawn(GenMod,LinkP,Mod,Args,Options)。

gen:do_spawn / 5:

  do_spawn(GenMod,link,Mod,Args,Options) - > 
Time = timeout(Options),
proc_lib:start_link(?MODULE,init_it,
[GenMod,self(),self(),Mod,Args,Options],
Time,
spawn_opts(Options));

proc_lib:start_link / 5:



<$ p当is_atom(M),is_atom(F),is_list(A) - >时,$ p> start_link(M,F,A,Timeout,SpawnOpts)
Pid =?MODULE:spawn_opt(M,F,A,ensure_link(SpawnOpts)),
sync_wait(Pid,Timeout)。

哪些最终让我们有趣的一点。有一个spawn_opt / 4匹配:

  spawn_opt(M,F,A,Opts)when is_atom(M),is_atom(F),is_list (A) - > 
...
...

但是,有一个对我实际有用的:

  spawn_opt(Node,M,F,A,Opts)when is_atom(M),is_atom(F),is_list(A) - > 
...
...

令我想起这是不暴露我意识到,一个粗心大意的程序员可能会尝试gen_server:start_link一个恰好在火星上运行的erlang节点上的一个进程,阻塞了半小时的呼叫,但肯定是程序员的监视。我真的坚持修改OTP或撰写某种特殊解决方案吗?

解决方案

我们不 start_link 直接在远程节点上的服务器。为了获得良好的程序结构和简单性,我们在远程节点上启动单独的应用程序,并将远程进程的创建委托给在远程应用程序中运行的某个进程。



由于链接到进程主要是为了监督或监视,我们更喜欢与本地主管进行链接而不是远程进程。如果您需要任何远程进程的活动状态,我建议 erlang:monitor erlang:demonitor



典型的分布式设置:

  Node1 
+ --------------- + Node2
| App1 | + --------------- +
|主管1 | Proc创建请求| App2 |
|过程| -----------------------> |主管2 |
| ...... | | |
| ...... | | |创建儿童
| ...... |监视器| V
| ...... | -----------------------> |过程|
+ --------------- + | ...... |
+ --------------- +


I have an Erlang application that is getting a little too resource-hungry to stay on one node. I'm in the process of making gen_servers move from one process to another - which turns out to be relatively easy. I'm at the last hurdle: getting the factory process that creates these gen_servers to spawn them on the remote node instead of the local one. The default behavior of start_link is clearly to start locally only, but I don't see any option to change that.

It would seem that I'm going to have to be inventive with the solution and wanted to see if anyone out there had already implemented something like this with any success. IOW, what's the recommended solution?

EDIT

I'm looking at the chain of calls that are triggered by calling:

gen_server:start_link(?Module, Args, [])

gen_server:start_link/3:

start_link(Mod, Args, Options) ->
    gen:start(?MODULE, link, Mod, Args, Options).

gen:start/5:

start(GenMod, LinkP, Mod, Args, Options) ->
    do_spawn(GenMod, LinkP, Mod, Args, Options).

gen:do_spawn/5:

do_spawn(GenMod, link, Mod, Args, Options) ->
    Time = timeout(Options),
    proc_lib:start_link(?MODULE, init_it,
                        [GenMod, self(), self(), Mod, Args, Options], 
                        Time,
                        spawn_opts(Options));

proc_lib:start_link/5:

start_link(M,F,A,Timeout,SpawnOpts) when is_atom(M), is_atom(F), is_list(A) ->
    Pid = ?MODULE:spawn_opt(M, F, A, ensure_link(SpawnOpts)),
    sync_wait(Pid, Timeout).

Which finally gets us to the interesting bit. There is a spawn_opt/4 that matches:

spawn_opt(M, F, A, Opts) when is_atom(M), is_atom(F), is_list(A) ->
    ...
    ...

BUT, there is one that would actually be useful to me:

spawn_opt(Node, M, F, A, Opts) when is_atom(M), is_atom(F), is_list(A) ->
    ...
    ...

It boggles my mind that this isn't exposed. I realize that there is a risk that a careless programmer might try to gen_server:start_link a process on a erlang node that happens to be running on Mars, blocking the call for half an hour, but surely, that's the programmers' lookout. Am I really stuck with modifying OTP or writing some sort of ad-hoc solution?

解决方案

We don't start_link a server on the remote node directly. For a good program structure and simplicity, we start a separate application on the remote node, and delegate the creation of remote processes to a certain process running in the remote application.

Since linking to a process is mainly for the purpose of supervising or monitoring, we prefer doing the linking with local supervisors instead of remote processes. If you need the aliveness status of any remote process, I recommend erlang:monitor and erlang:demonitor.

A typical distributed set-up:

Node1
+---------------+                          Node2
| App1          |                          +---------------+
|   Supervisor1 |  Proc Creation Request   | App2          |
|     Processes | -----------------------> |   Supervisor2 |
|     ......    |                          |      |
|     ......    |                          |      | Create Children
|     ......    |       Monitor            |      V
|     ......    | -----------------------> |     Processes |
+---------------+                          |     ......    |
                                           +---------------+

这篇关于一个Erlang gen_server在另一个节点上如何启动一个gen_server?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆