Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions site/src/content/guides-grpcprocctl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,14 @@ orders order-17 charging stuck 5 1 2026-10-05T11:49:2
<C>--node</C>, <C>--prefix</C>, <C>--limit</C>.
</>
],
[
<C>groups [group]</C>,
<>
A <Ext href={file('groups/README.md')}>process groups</Ext> scope's groups, with their members on every node and
on this one, and its other nodes and their states; or one group's members: <C>--node</C>,{' '}
<C>--scope</C>, <C>--prefix</C>, <C>--limit</C>.
</>
],
[
<C>watch</C>,
<>
Expand Down
4 changes: 3 additions & 1 deletion site/src/content/guides-mcp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ export const guidesMcp: Doc = {
[<C>cron_jobs</C>, 'Cron processes and their jobs, on a node or all of them.'],
[<C>saga_runs</C>, 'Saga runs by saga and status, a page at a time.'],
[<C>saga_run</C>, 'One saga run: the error that stopped it, its waiting signals, and its data when the engine shows it.'],
[<C>global_names</C>, 'Who holds a global name, or every name with a prefix, such as room:.']
[<C>global_names</C>, 'Who holds a global name, or every name with a prefix, such as room:.'],
[<C>process_groups</C>, 'A grpcproc/groups scope’s groups and its other nodes, as one node’s copy has them.'],
[<C>group_members</C>, 'One grpcproc/groups group’s members on every node, by pid.']
]}
/>
<p>
Expand Down
11 changes: 10 additions & 1 deletion site/src/content/guides-web.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Code } from '../code.tsx';
import { Screenshot } from '../components/Figure.tsx';
import { A, C, Table } from '../components/prose.tsx';
import { A, C, Ext, Table } from '../components/prose.tsx';
import { file } from '../config.ts';
import type { Doc } from './types.ts';

export const guidesWeb: Doc = {
Expand Down Expand Up @@ -59,6 +60,14 @@ grpcprocctl --plaintext --addr 10.0.0.5:9000 web # http://localhost:9911`}</Co
Who holds each <A to="concepts/addressing/#global">global name</A>.
</>
],
[
'Groups',
<>
The <Ext href={file('groups/README.md')}>process groups</Ext> scopes the node runs, as its copy has them: each
group's members on every node and on this one, a group's members once picked, and the
other nodes of the scope with their states.
</>
],
['Supervision', 'Each process under the one that started it, coloured by state, mailbox or activity.'],
['Events', 'Spawns, exits with their reasons, links and dead letters, streamed as they happen.'],
[
Expand Down
10 changes: 8 additions & 2 deletions tools/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ monitors: 2
| `ps` | processes: `--node`, `--name`, `--label`, `--state`, `--min-mailbox`, `--sort pid\|mailbox\|received\|sent\|busy`, `--limit` |
| `inspect <pid\|name>` | one process, with what it says about itself: `--node`, `--wait` |
| `names [name]` | [global names](https://floatdrop.github.io/grpcproc/concepts/addressing/#global): who holds one, or a list: `--node`, `--prefix`, `--limit` |
| `groups [group]` | a [grpcproc/groups](../groups/README.md) scope's groups, with their members on every node and on this one, and the scope's other nodes and their states; or one group's members: `--node`, `--scope`, `--prefix`, `--limit` |
| `watch` | stream spawns, exits, links, dead letters: `--node`, `--kind`, `--count` |
| `exit <pid\|name> [reason]` | ask a process to exit |
| `loglevel <pid\|name> <level>` | change one process's log level |
Expand All @@ -94,7 +95,7 @@ what its MCP server reports.

A pid is written as grpcproc prints it, `<node.incarnation.id>`; a name is
looked up on `--node`, by default the node serving the Inspector.
`--json` before `node`, `nodes`, `ps`, `inspect`, `names`, `watch`, `leader`, `cron` or `saga` prints the same
`--json` before `node`, `nodes`, `ps`, `inspect`, `names`, `groups`, `watch`, `leader`, `cron` or `saga` prints the same
data as JSON: one indented value, or for `watch` one compact event per line,
so `grpcprocctl --json watch | jq` sees events as they happen. The objects
are those the MCP tools return, which wrap lists in an object of their own.
Expand Down Expand Up @@ -218,6 +219,10 @@ through the same Inspector and refreshes every second:
and how electors are named, asking 32 nodes at a time; a node that
cannot be asked is listed with why. A cron process spawned with a label
of its own is shown by `grpcprocctl cron --node`, not here.
- **Groups**: the grpcproc/groups scopes the node runs, as its copy has
them: each group's members on every node and on this one, by prefix, a
group's members once picked, and the other nodes of the scope with their
states (synced, syncing, absent, unreachable).
- **Sagas**: the grpcproc/saga engines reachable from here and the sagas
each runs, and a saga's runs a page at a time, by status, as
`grpcprocctl saga runs` lists them. A run opens beside them, as `saga
Expand All @@ -243,7 +248,7 @@ so a page elsewhere cannot reach it through the browser; a change must come
from the page itself. On any other address, put it behind something that
authenticates. The API behind the page returns the objects `--json`
prints, `/api/nodes` with when the walk ended: `/api/nodes`, `/api/node`, `/api/processes`, `/api/process`,
`/api/crons`, `/api/elections`, `/api/sagas`, `/api/saga/runs`,
`/api/names`, `/api/groups`, `/api/group`, `/api/crons`, `/api/elections`, `/api/sagas`, `/api/saga/runs`,
`/api/saga/run`, and `/api/events` as server-sent events.

## For an AI agent
Expand All @@ -267,6 +272,7 @@ or a busy process means) and offers:
| `saga_runs` | saga runs by saga and status, a page at a time |
| `saga_run` | one saga run: its state, error and waiting signals, and its data when the engine shows it |
| `global_names` | who holds a global name, or every name with a prefix |
| `process_groups`, `group_members` | a groups scope's groups and other nodes, as one node's copy has them; one group's members |
| `exit_process`, `set_log_level`, `move_leader`, `cordon_node`, `uncordon_node`, `enable_cron_job`, `disable_cron_job`, `remove_cron_job`, `resume_saga_run` | only with `--allow-writes` |

So "orders are slow since the deploy" becomes: list processes by mailbox,
Expand Down
67 changes: 66 additions & 1 deletion tools/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ Commands:
ps processes (--node, --name, --label, --state, --min-mailbox, --sort, --limit)
inspect <pid|name> one process, and what it says about itself (--node, --wait)
names [name] global names: who holds one, or a list (--node, --prefix, --limit)
groups [group] grpcproc/groups: a scope's groups and peers, or one group's members (--node, --scope, --prefix, --limit)
watch stream events (--node, --kind, --count)
exit <pid|name> [reason] ask a process to exit (--node)
loglevel <pid|name> <level> set a process's log level (--node)
Expand Down Expand Up @@ -252,7 +253,7 @@ func init() {
commands = map[string]command{
"node": cmdNode, "nodes": cmdNodes, "ps": cmdPS, "inspect": cmdInspect, "watch": cmdWatch,
"exit": cmdExit, "loglevel": cmdLogLevel, "dot": cmdDot, "mcp": cmdMCP, "leader": cmdLeader, "cron": cmdCron, "saga": cmdSaga,
"web": cmdWeb, "names": cmdNames,
"web": cmdWeb, "names": cmdNames, "groups": cmdGroups,
}
}

Expand Down Expand Up @@ -428,6 +429,70 @@ func cmdNames(ctx context.Context, a *app, args []string) error {
return a.table("NAME\tPID", rows)
}

func cmdGroups(ctx context.Context, a *app, args []string) error {
var node, scope, prefix string
var limit int
fs, err := a.flags("groups", args, func(fs *flag.FlagSet) {
fs.StringVar(&node, "node", "", "node whose copy of the groups to read, by default the one serving the Inspector")
fs.StringVar(&scope, "scope", "", "scope to read, by default the one named default, or the node's first")
fs.StringVar(&prefix, "prefix", "", "only groups whose names start with this")
fs.IntVar(&limit, "limit", 0, "show at most this many groups, or members (0: 1000)")
})
if err != nil {
return err
}
ctx, cancel := a.request(ctx)
defer cancel()
switch fs.NArg() {
case 0:
case 1:
m, err := a.client.GroupMembers(ctx, node, scope, fs.Arg(0), limit)
if err != nil {
return err
}
if a.json {
return a.printJSON(m)
}
rows := make([][]string, 0, len(m.Members))
for _, pid := range m.Members {
rows = append(rows, []string{pid})
}
return a.omitted(a.table("PID", rows), m.Omitted, "members")
default:
return usageError{"want at most one group"}
}
v, err := a.client.Groups(ctx, node, scope, prefix, limit)
if err != nil {
return err
}
if a.json {
return a.printJSON(v)
}
fmt.Fprintf(a.env.Stdout, "scope %s on %s (it runs %s)\n\n", v.Scope, v.Node, strings.Join(v.Scopes, ", "))
rows := make([][]string, 0, len(v.Groups))
for _, g := range v.Groups {
rows = append(rows, []string{g.Name, strconv.Itoa(g.Members), strconv.Itoa(g.Local), strconv.Itoa(g.Nodes)})
}
err = a.omitted(a.table("GROUP\tMEMBERS\tLOCAL\tNODES", rows), v.Omitted, "groups")
if err == nil {
rows = rows[:0]
for _, p := range v.Peers {
rows = append(rows, []string{p.Node, p.State, strconv.Itoa(p.Members), p.Scope})
}
fmt.Fprintln(a.env.Stdout)
err = a.table("PEER\tSTATE\tMEMBERS\tSCOPE", rows)
}
return err
}

// omitted says how many rows a limit left out, after a table that printed.
func (a *app) omitted(err error, n int, what string) error {
if err == nil && n > 0 {
fmt.Fprintf(a.env.Stdout, "… %d more %s past --limit\n", n, what)
}
return err
}

func cmdPS(ctx context.Context, a *app, args []string) error {
var node, sortBy string
var limit int
Expand Down
2 changes: 1 addition & 1 deletion tools/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ func TestMCPCommand(t *testing.T) {
t.Fatalf("mcp reports version %q", v)
}
tools, err := session.ListTools(t.Context(), nil)
if err != nil || len(tools.Tools) != 19 {
if err != nil || len(tools.Tools) != 21 {
t.Fatalf("%v %v", tools, err)
}
_ = session.Close()
Expand Down
37 changes: 37 additions & 0 deletions tools/cli/groups_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package cli_test

import (
"strings"
"testing"

"github.com/floatdrop/grpcproc/tools/internal/testcluster"
)

func TestGroups(t *testing.T) {
f := testcluster.Start(t, "c")
f.Groups(t)
has(t, ok(t, run(t, f, "groups")),
"scope default on a (it runs broken, default, rooms)",
"GROUP MEMBERS LOCAL NODES", "chat 2 1 2", "echo 1 0 1",
"PEER STATE MEMBERS SCOPE", "b synced 2 <b.", "c absent 0")
has(t, ok(t, run(t, f, "groups", "--limit", "1")), "chat 2 1 2", "… 1 more groups past --limit")
has(t, ok(t, run(t, f, "groups", "--node", "b", "--prefix", "e")), "scope default on b", "echo 1 1 1")
has(t, ok(t, run(t, f, "groups", "chat")), "PID", f.Talker.String(), f.Echo.String())
has(t, ok(t, run(t, f, "groups", "--limit", "1", "chat")), f.Talker.String(), "… 1 more members past --limit")
has(t, ok(t, run(t, f, "--json", "groups")), `"scope": "default"`, `"name": "chat"`)
has(t, ok(t, run(t, f, "--json", "groups", "chat")), `"group": "chat"`, `"members": [`)
for _, tc := range []struct {
args []string
code int
want string
}{
{[]string{"groups", "a", "b"}, 2, "at most one group"},
{[]string{"groups", "--bogus"}, 2, ""},
{[]string{"groups", "--node", "c"}, 1, "no grpcproc/groups scope on c"},
{[]string{"groups", "--node", "c", "chat"}, 1, "no grpcproc/groups scope on c"},
} {
if r := run(t, f, tc.args...); r.code != tc.code || !strings.Contains(r.stderr, tc.want) {
t.Errorf("%v: %+v", tc.args, r)
}
}
}
151 changes: 151 additions & 0 deletions tools/client/groups.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
package client

import (
"cmp"
"context"
"errors"
"fmt"
"maps"
"slices"
"strings"

"google.golang.org/protobuf/types/known/anypb"

"github.com/floatdrop/grpcproc"
"github.com/floatdrop/grpcproc/groups"
groupsv1 "github.com/floatdrop/grpcproc/groups/proto/grpcproc/groups/v1"
inspectv1 "github.com/floatdrop/grpcproc/proto/grpcproc/inspect/v1"
)

// ErrNoGroups is the error of a node that runs no grpcproc/groups scope, or
// none of the name asked.
var ErrNoGroups = errors.New("no grpcproc/groups scope")

// GroupsView is a grpcproc/groups scope's groups, as one node's copy has
// them.
type GroupsView struct {
Node string `json:"node" jsonschema:"the node whose copy of the groups this is"`
Scope string `json:"scope"`
PID string `json:"pid" jsonschema:"the scope's process"`
Scopes []string `json:"scopes" jsonschema:"every scope the node runs"`
Groups []GroupView `json:"groups"`
Omitted int `json:"omitted,omitzero" jsonschema:"groups left out past the limit"`
Peers []GroupPeerView `json:"peers" jsonschema:"the other nodes the scope knows"`
}

// GroupView is one group's counts.
type GroupView struct {
Name string `json:"name"`
Members int `json:"members" jsonschema:"its members on every node"`
Local int `json:"local" jsonschema:"its members on the node whose copy this is"`
Nodes int `json:"nodes" jsonschema:"nodes with a member"`
}

// GroupPeerView is another node's scope, as this one knows it.
type GroupPeerView struct {
Node string `json:"node"`
State string `json:"state" jsonschema:"syncing: asked, its members not yet known; synced: its members are known; absent: it runs no such scope; unreachable: lost, and asked again later"`
Scope string `json:"scope,omitempty" jsonschema:"its scope's pid, once synced"`
Members int `json:"members" jsonschema:"its processes' memberships, in every group"`
}

// GroupMembersView is one group's members, as one node's copy has them.
type GroupMembersView struct {
Node string `json:"node"`
Scope string `json:"scope"`
Group string `json:"group"`
Members []string `json:"members" jsonschema:"their pids, ordered by node"`
Omitted int `json:"omitted,omitzero" jsonschema:"members left out past the limit"`
}

// Groups lists the groups of scope ("" for the default scope, or the
// node's first) whose names
// start with prefix, at most limit of them (1000 when 0), as node's copy
// has them; "" is the node serving the Inspector.
func (c *Client) Groups(ctx context.Context, node, scope, prefix string, limit int) (GroupsView, error) {
t, v, err := c.scope(ctx, node, scope)
if err != nil {
return GroupsView{}, err
}
r, err := c.askGroups(ctx, t, &groupsv1.Query{Prefix: prefix, Limit: clampLimit(limit)})
if err != nil {
return GroupsView{}, err
}
v.Groups, v.Peers, v.Omitted = []GroupView{}, []GroupPeerView{}, int(r.GetOmitted())
for _, g := range r.GetGroups() {
v.Groups = append(v.Groups, GroupView{Name: g.GetName(), Members: int(g.GetMembers()), Local: int(g.GetLocal()), Nodes: int(g.GetNodes())})
}
for _, p := range r.GetPeers() {
pv := GroupPeerView{Node: p.GetNode(), State: p.GetState(), Members: int(p.GetMembers())}
if p.GetScope() != nil {
pv.Scope = grpcproc.PIDFromProto(p.GetScope()).String()
}
v.Peers = append(v.Peers, pv)
}
return v, nil
}

// GroupMembers lists the members of group in scope, at most limit of them
// (1000 when 0), as node's copy has them.
func (c *Client) GroupMembers(ctx context.Context, node, scope, group string, limit int) (GroupMembersView, error) {
if group == "" {
return GroupMembersView{}, errors.New("a group is required")
}
t, v, err := c.scope(ctx, node, scope)
if err != nil {
return GroupMembersView{}, err
}
r, err := c.askGroups(ctx, t, &groupsv1.Query{Group: group, Limit: clampLimit(limit)})
if err != nil {
return GroupMembersView{}, err
}
out := GroupMembersView{Node: v.Node, Scope: v.Scope, Group: group, Members: []string{}, Omitted: int(r.GetOmitted())}
for _, p := range r.GetMembers() {
out.Members = append(out.Members, grpcproc.PIDFromProto(p).String())
}
return out, nil
}

func clampLimit(n int) uint32 { return uint32(min(max(n, 0), 1<<20)) }

// scope finds the scope named scope on node, or the default one, or its
// first, and the view of it to fill in.
func (c *Client) scope(ctx context.Context, node, scope string) (*inspectv1.Target, GroupsView, error) {
ps, err := c.Processes(ctx, node, Filter{Label: groups.Label})
if err != nil {
return nil, GroupsView{}, err
}
byScope := map[string]ProcessView{}
for _, p := range ps {
if s, ok := strings.CutPrefix(p.Name, groups.ScopeName("")); ok {
byScope[s] = p
}
}
scopes := slices.Sorted(maps.Keys(byScope))
if len(scopes) == 0 {
return nil, GroupsView{}, fmt.Errorf("%w on %s", ErrNoGroups, cmp.Or(node, "the node serving the Inspector"))
}
if scope == "" {
scope = scopes[0]
if _, ok := byScope[groups.DefaultScope]; ok {
scope = groups.DefaultScope
}
}
p, ok := byScope[scope]
if !ok {
return nil, GroupsView{}, fmt.Errorf("%w named %q on %s: it runs %s", ErrNoGroups, scope, cmp.Or(node, "the node serving the Inspector"), strings.Join(scopes, ", "))
}
t, _ := parseTarget(p.PID) // as the Inspector wrote it
return t, GroupsView{Node: t.GetPid().GetNode(), Scope: scope, PID: p.PID, Scopes: scopes}, nil
}

// askGroups puts q to the scope t, as an Inspector Query.
func (c *Client) askGroups(ctx context.Context, t *inspectv1.Target, q *groupsv1.Query) (*groupsv1.Report, error) {
body, _ := anypb.New(q) // a Query always marshals
resp, err := c.rpc.Query(ctx, &inspectv1.QueryRequest{Target: t, Body: body})
if err != nil {
return nil, err
}
r := &groupsv1.Report{}
return r, resp.GetBody().UnmarshalTo(r)
}
Loading
Loading