Question

I would like to know what is the suggested way (if any) to move a gen_server/gen_fsm from erlang node A to erlang node B preserving its internal state.

Was it helpful?

Solution

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, i.e. 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!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top