Documentation
Process groups
Named sets of processes on any node, after Erlang’s pg: joined on their own node, listed from every node’s copy, and watched for joins and leaves.
grpcproc/groups is optional, and built on the public API only. The code is from examples/groups.
Groups and scopes
A group is a name that many processes hold at once: the workers that resize photos, the nodes of a shard that serve rooms. Any process on any node lists its members and sends to the ones it picks. A global name has one holder, and a pub/sub topic keeps its subscribers to itself; a group is the list.
Each node that takes part runs a scope: a process registered as groups/<scope>, which keeps a copy of every node's members of every group. Nodes whose scopes share a name share their groups, and a node may run several scopes, one per purpose.
workers, err := groups.Start(node, groups.Config{Scope: "jobs"})A scope finds the other nodes that run it through its config:
| Config | The nodes it asks for their members |
|---|---|
Peers | These, from the start, whatever Membership reports. |
Membership | Each node it reports up, and it forgets each one it reports down, unless Peers names it. Unset, the node's own Config.Membership. |
Select | Of the nodes Membership reports up, only those it takes: by their metadata, say, so that a scope of one shard does not dial the whole cluster. |
Retry | How long after a node is lost it is asked again, doubling with each loss in a row up to 32 times as long. Default 1s. |
A node that asks this one is answered whatever these say. With neither Peers nor a Membership, a scope knows only the nodes that ask it.
Joining and leaving
A process joins a group on its own node's scope, from the Node or a Process, as for Addr.Call. It stays in the group until it leaves it or exits: the scope monitors it. The example's workers join as they start:
// worker resizes the photos it is called with. It joins the group "resize"
// on its own node's scope, and is in it until it exits.
func worker(scope *groups.Scope, n int) func(*grpcproc.Process[*wrapperspb.StringValue]) error {
return func(p *grpcproc.Process[*wrapperspb.StringValue]) error {
if err := scope.Join(p.Context(), p, "resize", p.PID()); err != nil {
return err
}
for {
m, err := p.Receive()
if err != nil {
return err
}
done := fmt.Sprintf("%s resized by worker %d on %s", m.Body.GetValue(), n, p.Node().Name())
_ = m.Reply(wrapperspb.String(done), nil)
}
}
}| Call | What it does |
|---|---|
scope.Join(ctx, from, group, pids…) | Adds processes of the scope’s node to the group, each once however often joined. When it returns, this node’s copy has them; the other nodes hear of it after. |
scope.Leave(ctx, from, group, pids…) | Takes them out of it. A process that is not in it is left alone. |
Both refuse a process of another node with ErrNotLocal, and a group's name that is empty, not valid UTF-8 or longer than 1 KiB with ErrGroup; Join refuses a process that does not run with ErrNoProc. A refusal changes nothing.
Finding members
scope.Members(group) lists the members on every node, ordered by node, incarnation and id. scope.Local(group) lists this node's, and scope.Groups() the groups with members. All three read the node's copy under a lock, from any goroutine, without a message. The example's dispatch node runs no worker, and sends each worker it finds a photo:
// The dispatch node finds them in its own copy of the group, and sends
// each a photo.
resize := scopes["dispatch"]
await(resize, 4)
members := resize.Members("resize")
fmt.Printf("workers: %d, on %s\n", len(members), nodesOf(members))
for i, pid := range members {
worker := grpcproc.AddrOf[*wrapperspb.StringValue](pid)
photo := wrapperspb.String(fmt.Sprintf("photo-%d.jpg", i+1))
done, err := worker.Call[*wrapperspb.StringValue](ctx, nodes["dispatch"], photo)
check(err)
fmt.Println(done.GetValue())
}The copy is eventually consistent. This node's own joins are in it when Join returns, and another node's once that node's message arrives. A member that exits or a node that goes away may still be listed for a moment, so a call to a member can fail as any call can, with ErrNoProc or ErrNoConnection: pick another and try again.
Watching a group
scope.Watch(ctx, p, group) has the scope send p a *groupsv1.Changed for each change to the group: who joined, and who left. Watch.Members are the members when the watch began, and the changes follow from there. p must be a process of the scope's node, and its mailbox must accept *groupsv1.Changed.
// watcher prints the first change to the group "resize", then stops
// watching. Its mailbox takes any message, *groupsv1.Changed among them.
func watcher(scope *groups.Scope, watching, done chan<- struct{}) func(*grpcproc.Process[proto.Message]) error {
return func(p *grpcproc.Process[proto.Message]) error {
defer close(done)
w, err := scope.Watch(p.Context(), p, "resize")
if err != nil {
return err
}
close(watching)
members := len(w.Members)
for {
m, err := p.Receive()
if err != nil {
return err
}
if c, ok := m.Body.(*groupsv1.Changed); ok {
members += len(c.GetJoined()) - len(c.GetLeft())
for _, pid := range c.GetLeft() {
fmt.Printf("watcher: a worker on %s left, %d remain\n", pid.GetNode(), members)
}
w.Cancel(p)
return nil
}
}
}
}| When | What the watcher sees |
|---|---|
| A member joins or leaves, or exits, on any node | A Changed with it. |
| A node can no longer be reached, or its scope ends | A Changed with that node's members as left. |
w.Cancel(p) | No more changes for that watch. What is already in the mailbox stays. |
| The scope ends | The Down with w.Ref. |
Each Watch is ended by its own Cancel. A process that holds several watches of one group still gets each change once.
Across nodes
A scope reaches each other node's scope through a relay, a process of its own labelled groups relay, so that a send that waits for a dial to an unreachable node holds up the relay and not the scope. The relay says hello to the other scope, watches the scope that answers, and carries this node's members to it: the whole list once, then each change. What one node says to another goes over one link in order, so no change overtakes the list it changes.
| What happens to another node | Its members on this node |
|---|---|
| Its scope answers | Its list, then its changes. |
| It runs no such scope, or its scope ends | Dropped. It is absent. |
| It cannot be reached | Dropped. It is unreachable. |
| Membership reports it gone | Dropped, and the node forgotten, unless Peers names it. |
An absent or unreachable node is asked again after Retry, doubling with each loss in a row, and at once when it says hello or Membership reports a newer incarnation of it. News of an incarnation older than one a node is known to have is ignored, as the core ignores it.
Example
Three nodes in one binary, as in the quick start: two workers on each of east and west, and a dispatch node that finds them and sends each a photo. A watcher on dispatch sees a worker exit on west; then east stops, and its workers leave the group with it.
// A watcher hears of a worker that exits.
watching, watched := make(chan struct{}), make(chan struct{})
_, err := nodes["dispatch"].Spawn(watcher(resize, watching, watched))
check(err)
<-watching
check(nodes["west"].Exit(ctx, crashy, "crash"))
<-watched
// A node that stops takes its workers with it.
check(nodes["east"].Stop(ctx))
await(resize, 1)
left := resize.Members("resize")
fmt.Printf("after east stopped: %d, on %s\n", len(left), nodesOf(left))workers: 4, on east, east, west, west
photo-1.jpg resized by worker 1 on east
photo-2.jpg resized by worker 2 on east
photo-3.jpg resized by worker 1 on west
photo-4.jpg resized by worker 2 on west
watcher: a worker on west left, 3 remain
after east stopped: 1, on westHow far it scales
A scope is a full mesh, as Erlang's pg is: every node holds a relay to every other node of the scope and a copy of every member, and sends each change to every other node. groups/scale_test.go measured it in one process over in-memory connections, with ten members a node:
| Nodes in the scope | Memory a node | Every node syncing at once, CPU in all | One change, CPU in all |
|---|---|---|---|
| 25 | 0.21 MB | 79 ms | 0.1 ms |
| 50 | 0.38 MB | 254 ms | 0.2 ms |
| 100 | 0.78 MB | 1.04 s | 0.5 ms |
| 200 | 1.57 MB | 4.7 s | 1.1 ms |
That is about 8 KB a node of the scope, and about 125 bytes a membership, on every node; a change costs each node that receives it about 5.5 µs. What grows with the square of the nodes is churn: if each of 900 nodes made ten changes a second, each node would take about 9,000 a second, some 5% of a core. Keep a big scope to roles that change slowly, and use Select and a scope per purpose to keep each scope to the nodes that need it. The links between the nodes cost more than the scope does.
Limits
- A group is a directory of live processes, not a store. A scope that restarts has no members until its processes join again.
- Membership is eventually consistent: two nodes may list a group differently for a moment. A choice that must be the same everywhere, one leader, one owner, is leader election or a global name.
- Every join and leave goes to every node of the scope. A group per room or per order would send each one's opening and closing to all of them: rooms are better found by a global name.
Inspecting
A scope answers a groupsv1.Query through grpcproc.WithQuery, which a read-only Inspector serves: its groups by prefix, with each one's members on every node and on this one, one group's members, and the other nodes it knows, by state. grpcprocctl reads it with grpcprocctl groups, the MCP server with process_groups and group_members, and the web UI on its Groups page, each as the copy of the node it is pointed at has the groups.