[erlang-questions] preserving state through a gen_server restart

Dmitry Kolesnikov <>
Sat Oct 20 13:57:14 CEST 2012


Hello Ivan,

I had a similar issue that you have. 
I've done a small library https://github.com/fogfish/pts
The library is similar to gproc but it also it brings data owner/consumer interaction. E.g. you can build key/val storage over my_lamb processes. 

The usage is very simple, please see pts_cache.erl for example. You have to hook pts with my_lamb after that process can be resolved via pns:whereis(N, Uid)

%%
%%
start_link(Ns, Uid) ->
  gen_server:start_link(?MODULE, [Ns, Uid], []).
  
init([Ns, Uid]) ->
   pns:register(Ns, Uid),
   {ok, #srv{ns=Ns, uid=Uid}}. 

terminate(_Reason, #srv{ns=Ns, uid=Uid}) ->
   pns:unregister(Ns, Uid),
   ok.

Of course library does not preserve state ove crash but it keep {Ns, Uid} mapping to process is consistent manner even if process crashes. BTW, if you start to use either pts or gproc then my_lambs process becomes unnecessary overhead.

- Dmitry


On Oct 19, 2012, at 6:03 PM, Ivan Uemlianin wrote:

> Dear All
> 
> I have an erlang/OTP application consisting mostly of gen_servers.  One repeated pattern is having a gateway/aggregate gen_server process that provides access (and other management) to individual gen_server processes, e.g.:
> 
>              my_sup
>                 |
>              my_lambs
>             /        \      \
>        my_lamb     my_lamb    my_lamb    ...
> 
> my_lambs' state variable holds a dictionary {LambName: LambPid} and among other functions it allows me to call lambs by name instead of my Pid.
> 
> If my_lambs terminates normally --- e.g., if the whole application is closing down --- its terminate/s function can call my:lamb:stop for each of the processes in its care.
> 
> However, if my_lambs terminates abnormally, and is restarted by my_sup, I think I'd like to preserve its state variable --- at least the Pids of the my_lamb processes --- so that it can be passed on to the new version of my_lambs.
> 
> I'd like to avoid losing all of the my_lamb processes, if the my_lambs process crashes.
> 
> Can state be passed to the supervisor, to be used when it restarts its child?
> 
> Could I park the state variable in some kind of holding process, so that the new my_lambs can retrieve it?
> 
> Or is there another way of doing this?
> 
> With thanks and best wishes
> 
> Ivan
> 
> 
> 
> -- 
> ============================================================
> Ivan A. Uemlianin PhD
> Llaisdy
> Speech Technology Research and Development
> 
>                    
>                     www.llaisdy.com
>                         llaisdy.wordpress.com
>              github.com/llaisdy
>                     www.linkedin.com/in/ivanuemlianin
> 
>              "hilaritas excessum habere nequit"
>                 (Spinoza, Ethica, IV, XLII)
> ============================================================
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20121020/576f85f5/attachment.html>


More information about the erlang-questions mailing list