global singleton gen_server

Roberto Ostinelli ostinelli@REDACTED
Thu Dec 16 20:51:18 CET 2021


I was trying to use a more portable solution but this is indeed a
possibility... Thank you for bringing it up.

On Thu, Dec 16, 2021 at 8:18 PM A. G. Madi <viper7129@REDACTED> wrote:

> Maybe you can accomplish what you want using the distributed configuration
> options in the Kernel app.  I use this myself to make sure that certain
> gen_servers are only ever run on one node in my cluster.  Have a look at
> the config options for Kernel. I use the 'distributed' option with the
> 'sync_nodes_*' options.
>
> https://www.erlang.org/docs/21/man/kernel_app.html#configuration
>
>
> On Thu, Dec 16, 2021 at 13:09 Vyacheslav Levytskyy <v.levytskyy@REDACTED>
> wrote:
>
>> I have similar problem, and didn't find an ultimate solution yet. My
>> current approach is to register the singleton gen_server using {via,
>> my_global_module, my_regname(...)} rather than {global, ?MODULE}. In
>> "my_global_module" I control how a conflict is resolved between running
>> server and a new one. In the "resolve" callback of global:register_name I'm
>> able to query for each of gen_server's version and decide which of two
>> gen_server lives and which is to shut down. This helps also in case of
>> rolling updates.
>>
>> Best regards,
>> Vyacheslav
>> On 16.12.2021 19:34, Roberto Ostinelli wrote:
>>
>> Not necessarily. By the time your first gen_server start you might
>> already have more than one node in your cluster.
>> I'm ok having other nodes joining later on, I'm trying to avoid a clash
>> where multiple nodes execute the "cluster init" code
>>
>>
>> On Thu, Dec 16, 2021 at 3:48 PM Roger Lipscombe <roger@REDACTED>
>> wrote:
>>
>>> Define "cluster start". In normal operation, there's nothing stopping
>>> another node from joining the cluster at some arbitrary later time,
>>> even days later. The first node to come up is a degenerate, single
>>> node cluster, no?
>>>
>>> On Thu, 16 Dec 2021 at 11:00, Roberto Ostinelli <ostinelli@REDACTED>
>>> wrote:
>>> >
>>> > Dear all,
>>> > I'm trying to run an operation only once on cluster start but there's
>>> clearly something that I'm missing in the global module (see other related
>>> question here [1]).
>>> >
>>> > Therefore this time I'm trying to have a single global gen_server
>>> which I start directly from application start/2 callback with:
>>> > gen_server:start_link({global, ?MODULE}, ?MODULE, [], Options).
>>> >
>>> > Nodes are already connected at that point (I can see them with
>>> nodes()), however for some reason this registration works on all the nodes
>>> and I find myself with multiple gen_server registered globally.
>>> >
>>> > What is the proper way to register a global singleton gen_server at
>>> cluster level on application start? mY need is to run an operation only
>>> _once_ a cluster boots up.
>>> >
>>> > [1]
>>> http://erlang.org/pipermail/erlang-questions/2021-December/101781.html
>>>
>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20211216/e5fcb31b/attachment.htm>


More information about the erlang-questions mailing list