[erlang-questions] pg2 vs gproc?

547 views
Skip to first unread message

Motiejus Jakštys

unread,
May 26, 2012, 4:38:17 PM5/26/12
to erlang-q...@erlang.org
Hi,

For term() to pid() mapping I've always used gproc. For the same thing
my coworker always used pg2.

I am making a comparison now. There are some features in gproc which are
not in pg2:

* Await for registration
* Per-process properties
* Aggregate counters
* QLC

Whereas pg2 "natively" supports mapping one key to several processes
(which can be easily achieved with QLC in gproc).

Any more differences between them?

Thanks,
Motiejus
_______________________________________________
erlang-questions mailing list
erlang-q...@erlang.org
http://erlang.org/mailman/listinfo/erlang-questions

Motiejus Jakštys

unread,
Jun 6, 2012, 8:10:37 AM6/6/12
to erlang-q...@erlang.org
Bump

On Sat, May 26, 2012 at 10:38 PM, Motiejus Jakštys
<desir...@gmail.com> wrote:
> Hi,
>
> For term() to pid() mapping I've always used gproc. For the same thing
> my coworker always used pg2.
>
> I am making a comparison now. There are some features in gproc which are
> not in pg2:
>
> * Await for registration
> * Per-process properties
> * Aggregate counters
> * QLC
>
> Whereas pg2 "natively" supports mapping one key to several processes
> (which can be easily achieved with QLC in gproc).
>
> Any more differences between them?

--
Motiejus Jakštys

Loïc Hoguin

unread,
Jun 6, 2012, 8:27:48 AM6/6/12
to Motiejus Jakštys, erlang-q...@erlang.org
On 05/26/2012 10:38 PM, Motiejus Jakštys wrote:
> Hi,

Hello,

> For term() to pid() mapping I've always used gproc. For the same thing
> my coworker always used pg2.
>
> I am making a comparison now. There are some features in gproc which are
> not in pg2:
>
> * Await for registration
> * Per-process properties
> * Aggregate counters
> * QLC
>
> Whereas pg2 "natively" supports mapping one key to several processes
> (which can be easily achieved with QLC in gproc).

Unless I misunderstand, gproc's properties are exactly this, mapping one
key to several processes.

--
Loïc Hoguin
Erlang Cowboy
Nine Nines

Eric Moritz

unread,
Jun 6, 2012, 8:50:53 AM6/6/12
to Loïc Hoguin, Erlang Questions

Yes, if you register a property key, you can retrieve all the procs that have that property set.  For instance a fanout pub/sub can be accomplished doing:

subscribe(Channel) ->
    gproc:reg({p, l, {subscribers,
                               Channel}}).

publish(Channel, Msg) ->
    gproc:send({p, l, {subscribers,
                                  Channel}},
                         Msg).

If a process calls publish/2, the message will be sent to every process that called subscribe/1.

I'm on my phone so it's a bit hard to look up the details, if you want to get a list of pids that called subscribe, look at the code of gproc:send/2.  I think the function is called lookup_pids/1 but I may be wrong.

The etorrent project uses gproc.  That's a good read if you want to see how to use gproc.  He uses property keys for each torrent file process as well as using await to delay initialization of gen_servers that depend on other gen_servers.

Eric Moritz.

Ulf Wiger

unread,
Jun 6, 2012, 9:33:54 AM6/6/12
to Eric Moritz, Erlang Questions

Yes, this is correct.

Gproc supports a few different types of register entries:

- names, which are unique (can be any term)
- properties, which can be held by multiple processes simultaneously
- counters, which are properties with update_counter() semantics
- aggregate counters, which maintain the sum of all counters with
  the same name as the aggregate counter.

Properties are very useful for pub-sub, but also for highlighting various characteristics of the process. In my initial presentation of gproc, I had integrated it into OTP and modified the behaviors to register a {p,l,{behaviour,B}} property. This way, you could easily query the system for a certain type of behavior, and drill further to inspect other things.

(This had the unfortunate side-effect that many thought you had to have a patched OTP to use gproc. Next time, I will try to be smarter).

Thus, if you inspect a process, either with gproc:i(), or gproc:info(Pid [, gproc]), you can easily tell from its registered names and properties quite a bit about the process.

BTW, I've been thinking about adding a function, gproc:bcast(Key, Msg), which would be similar to:

rpc:abcast(gproc, send, [Key, Msg])

but with more predictable sequencing behavior, most likely making use of a gproc_bcast server to make sure all messages go the same way.

Wild cheers and enthusiasm may intice me to do it sooner rather than later…. ;-)

BR,
Ulf W
Ulf Wiger, Co-founder & Developer Advocate, Feuerlabs Inc.



Loïc Hoguin

unread,
Jun 6, 2012, 9:37:57 AM6/6/12
to Ulf Wiger, Erlang Questions
On 06/06/2012 03:33 PM, Ulf Wiger wrote:
> BTW, I've been thinking about adding a function, gproc:bcast(Key, Msg),
> which would be similar to:
>
> rpc:abcast(gproc, send, [Key, Msg])
>
> but with more predictable sequencing behavior, most likely making use of
> a gproc_bcast server to make sure all messages go the same way.
>
> Wild cheers and enthusiasm may intice me to do it sooner rather than
> later…. ;-)

Wild cheers and enthusiasm!

Mahesh Paolini-Subramanya

unread,
Jun 6, 2012, 10:38:59 AM6/6/12
to Erlang Questions
http://www.youtube.com/watch?v=R8yLKEj1WhQ&feature=related

Seriously tho'.  If, and when you get around to it, some of us would be ridiculously grateful, and would happily by you as much beer as you could possibly drink. Or Negronis.  Whichever :-)

That Tall Bald Indian Guy...
Blog   |   Twitter   |   Google+

Michael Truog

unread,
Jun 6, 2012, 12:44:59 PM6/6/12
to Motiejus Jakštys, erlang-q...@erlang.org
The other differences are mentioned in previous email threads. pg2 replicates all name lookup information in a way that doesn't require consistency (it does use ets though, if you don't want to use ets, there is an example here https://raw.github.com/okeuday/CloudI/master/src/lib/cloudi/src/list_pg.erl). gproc uses gen_leader so it maintains a central name lookup authority. So, pg2 is AP from the CAP theorem, while gproc is CA. That means gproc has issues with partition tolerance, similar to mnesia. To me, gproc is a method by which you are able to introduce global variables into your Erlang program. This helps beginners that are not working on large codebases, but it is generally only for Erlang that is limited to a single node. An implementation of gen_leader behavior that is suppose to have better partition tolerance is here https://github.com/ngmoco/gl_async_bully , but I don't know the details. I don't see leader election as necessary in Erlang, though it is a
fun intellectual exercise.

On 06/06/2012 05:10 AM, Motiejus Jakštys wrote:
> Bump
>
> On Sat, May 26, 2012 at 10:38 PM, Motiejus Jakštys
> <desir...@gmail.com> wrote:
>> Hi,
>>
>> For term() to pid() mapping I've always used gproc. For the same thing
>> my coworker always used pg2.
>>
>> I am making a comparison now. There are some features in gproc which are
>> not in pg2:
>>
>> * Await for registration
>> * Per-process properties
>> * Aggregate counters
>> * QLC
>>
>> Whereas pg2 "natively" supports mapping one key to several processes
>> (which can be easily achieved with QLC in gproc).
>>
>> Any more differences between them?

Ulf Wiger

unread,
Jun 6, 2012, 1:03:01 PM6/6/12
to Michael Truog, erlang-q...@erlang.org

This is true. I prefer to recommend people to use gproc as a mainly local registry. Of course, just as with the built-in registry, using it doesn't limit your system to running on one node. It's just that the registry is local to each node. ;-)

The newest versions of gen_leader are supposedly more tolerant to netsplits, but no facility to merge the user-level data exists (esp. not in gproc). In this regard, mnesia is much better off, since there are ways to deal with netsplits and recover to a consistent state (even though those ways may not appeal to everyone).

Of course, as long as this issue remains, one should at least think twice before relying on global registration in gproc. It can still work, as long as there are system-level recovery measures after netsplits that also fix gproc (e.g. the system detects the problem, identifies a consistent island, and restarts all other nodes).

BR,
Ulf W

On 6 Jun 2012, at 18:44, Michael Truog wrote:

> The other differences are mentioned in previous email threads. pg2 replicates all name lookup information in a way that doesn't require consistency (it does use ets though, if you don't want to use ets, there is an example herehttps://raw.github.com/okeuday/CloudI/master/src/lib/cloudi/src/list_pg.erl). gproc uses gen_leader so it maintains a central name lookup authority. So, pg2 is AP from the CAP theorem, while gproc is CA. That means gproc has issues with partition tolerance, similar to mnesia. To me, gproc is a method by which you are able to introduce global variables into your Erlang program. This helps beginners that are not working on large codebases, but it is generally only for Erlang that is limited to a single node. An implementation of gen_leader behavior that is suppose to have better partition tolerance is here https://github.com/ngmoco/gl_async_bully , but I don't know the details. I don't see leader election as necessary in Erlang, though it is a
> fun intellectual exercise.

Ulf Wiger, Co-founder & Developer Advocate, Feuerlabs Inc.
http://feuerlabs.com



Ulf Wiger

unread,
Jun 6, 2012, 1:55:20 PM6/6/12
to Mahesh Paolini-Subramanya, Erlang Questions

How to refuse such an offer?  :)

Well, a first version can be tested now.


BR,
Ulf W

OvermindDL1

unread,
Jun 6, 2012, 8:49:55 PM6/6/12
to Ulf Wiger, Erlang Questions
On Wed, Jun 6, 2012 at 11:55 AM, Ulf Wiger <u...@feuerlabs.com> wrote:
>
> How to refuse such an offer?  :)
>
> Well, a first version can be tested now.
>
> https://github.com/esl/gproc/blob/master/doc/gproc.md#bcast-2

Awesome, cannot wait to try it out. :)

Reply all
Reply to author
Forward
0 new messages