简体   繁体   中英

how to transfer gen_server/gen_fsm from node to node

我想知道将gen_server / gen_fsm从erlang节点A移到erlang节点B并保留其内部状态的建议方法(如果有)。

AFAIK there's no way to transfer a process between erlang nodes and I can think about many reasons to forbid this, between the others you may mess with internal nodes memory, simply consider a process which holds data (other than in the internal 'State' loop parameter) in process dictionary (process heap), binary (different garbage collection method).

One workaround could be to provide the gen_fsm/gen_server with a method that can spawn a new process recreating at the same time the internal state of the server/state machine. I think it's more difficult to say that to implement, you could simply use two start functions:

  • one that initializes the behaviour (like I think you're doing right now)
  • one that takes also a node and start through remote method call the server on that node and initializes state (by init/1 function or in an explicite way by sending a message, ie the state of the server)

But I must say that I see two main problems here:

  • Synchronization: one needs to make sure the process: start server on remote node -> set remote server state -> kill current local server is atomic
  • Coherence: other processes referring to local one must switch their reference to the remote one

The former could be resolved in many ways (my two cents: explicit message passing between local and remote server - overhead but bulletproof considering Erlang runtime system), the latter could be resolved with monitor/links and exit return values (the remote server pid) or in a more elegant way with a publish/subscribe model with a gen_event process.

I hope you find this useful to resolve your issue and ask anything if you need!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM