Documentation
Deployments
The same modules composed two ways: every service on one node, or split between a gateway, GPU nodes and a sandbox.
One program
cmd/local composes every service and needs no configuration: with no placement, every service runs on this node, called local.
// Command local runs the whole runtime as one program, the way a developer
// runs it: one node, every service on it. It composes the same modules that
// cmd/gateway, cmd/gpu and cmd/sandbox split between their nodes.
package main
import (
"github.com/floatdrop/grpcproc/examples/guide/internal/conversations"
"github.com/floatdrop/grpcproc/examples/guide/internal/models"
"github.com/floatdrop/grpcproc/examples/guide/internal/platform"
"github.com/floatdrop/grpcproc/examples/guide/internal/tools"
"github.com/floatdrop/grpcproc/examples/guide/internal/web"
)
func main() {
platform.Run(models.Module, tools.Module, conversations.Module, web.Module)
}A test starts cmd/local's composition, has a conversation, and draws what the node runs from the processes' parents: the root, a supervisor per service, the conversation with its topic under it, and the follower the test subscribed, as the web front does for a browser.
local
├── groups/default groups
├── root supervisor
│ ├── models-sup supervisor
│ │ └── models scheduler
│ ├── tools-sup supervisor
│ │ └── tools runner
│ └── conversations supervisor
│ └── conversation/1 conversation
│ └── conversation/1/events pubsub.topic
└── · followerA call from the conversation to the scheduler never leaves the program. A local send appends to the process's mailbox, and the message is not encoded.
git clone https://github.com/floatdrop/grpcproc && cd grpcproc/examples
go build -o bin/ ./guide/cmd/...
bin/local &
sleep 1
curl -N localhost:8080/conversations/1/events & # follow the conversation
curl -d 'what is 6 * 7?' localhost:8080/conversations/1
kill %1 %2The front also serves a chat page at localhost:8080, which follows a conversation and shows the tokens as they come.
A program per node
The distributed deployment splits the same modules between three entry points: the gateway faces browsers and holds the conversations, a GPU node runs the model, and the sandbox runs the tools. Each main is one line.
// Command gateway is the node that faces browsers: the web front and the
// conversations. Its configuration places the tools on another node; the
// conversations find the models in their group.
package main
import (
"github.com/floatdrop/grpcproc/examples/guide/internal/conversations"
"github.com/floatdrop/grpcproc/examples/guide/internal/platform"
"github.com/floatdrop/grpcproc/examples/guide/internal/web"
)
func main() { platform.Run(conversations.Module, web.Module) }// Command gpu is a node with a GPU: it runs the model. A deployment runs as
// many as it has, each under its own name.
package main
import (
"github.com/floatdrop/grpcproc/examples/guide/internal/models"
"github.com/floatdrop/grpcproc/examples/guide/internal/platform"
)
func main() { platform.Run(models.Module) }// Command sandbox is the node the tools run on, apart from the others: the
// other nodes are told not to trust it, and it offers them its runner and
// nothing else.
package main
import (
"github.com/floatdrop/grpcproc/examples/guide/internal/platform"
"github.com/floatdrop/grpcproc/examples/guide/internal/tools"
)
func main() { platform.Run(tools.Module) }The gateway's placement puts the tools on sandbox, so a conversation's runner names that node. The GPU nodes are not placed: each one's scheduler joins the models group as it starts, and the conversations find them in it, as below. A second GPU is the same program started under another name. The other nodes are told the gateway's address because a reply travels back on the replier's own link, and a generation publishes its tokens to a topic there. A placement naming a node not among the peers is refused at start.
Each node's tree holds only the services its entry point composed, and, where a service joins or reads a group, the node's groups scope, with a relay to each node it asks. This is the cluster in the middle of an answer: the conversation on the gateway, and its generation in a slot of gpu-1.
gateway
├── groups/default groups
│ ├── · groups relay
│ └── · groups relay
├── root supervisor
│ └── conversations supervisor
│ └── conversation/1 conversation
│ └── conversation/1/events pubsub.topic
└── · follower
gpu-1
├── groups/default groups
│ ├── · groups relay
│ └── · groups relay
└── root supervisor
└── models-sup supervisor
└── models scheduler
└── · generation
gpu-2
├── groups/default groups
│ ├── · groups relay
│ └── · groups relay
└── root supervisor
└── models-sup supervisor
└── models scheduler
sandbox
└── root supervisor
└── tools-sup supervisor
└── tools runnerEXPORT and UNTRUSTED say what each node lets its peers ask, as the platform reads them: a GPU node offers its scheduler and its groups scope, the sandbox its runner, and the gateway and the GPU nodes take no requests from the sandbox.
export PEERS=gateway=127.0.0.1:9101,gpu-1=127.0.0.1:9102,gpu-2=127.0.0.1:9103,sandbox=127.0.0.1:9104
NODE=gpu-1 LISTEN=127.0.0.1:9102 EXPORT=models UNTRUSTED=sandbox bin/gpu &
NODE=gpu-2 LISTEN=127.0.0.1:9103 EXPORT=models UNTRUSTED=sandbox bin/gpu &
NODE=sandbox LISTEN=127.0.0.1:9104 EXPORT=tools bin/sandbox &
NODE=gateway LISTEN=127.0.0.1:9101 UNTRUSTED=sandbox \
PLACEMENT=tools=sandbox bin/gateway &
sleep 1
curl -N localhost:8080/conversations/1/events &
curl -d 'what is 6 * 7?' localhost:8080/conversations/1Stop gpu-1 with kill %1 and the next answer comes from gpu-2; stop it while an answer streams, and the answer starts over there. A stopped GPU node leaves the gateway's copy of the models group, and is asked nothing more; restarted, its scope says hello to the gateway's, its scheduler joins the group again, and it takes generations from then on.
Finding the GPU nodes
The nodes that run a model are a process group. The platform starts the node's groups scope for the services that need one, the models, which join, and the conversations, which read: the sandbox runs none. A scope asks the nodes the node knows of, but those it does not trust, which may ask it nothing anyway.
// newScope is the node's part of the runtime's process groups: a service's
// processes join a group, and other services find them in it, on any node.
// The scope asks the nodes the node knows of, from etcd or the
// configuration, but those it does not trust, which may ask it nothing.
func newScope(cfg Config, n *grpcproc.Node) (*groups.Scope, error) {
trusted := func(node string) bool { return !slices.Contains(cfg.Untrusted, node) }
var peers []string
for _, node := range slices.Sorted(maps.Keys(cfg.Peers)) {
if trusted(node) {
peers = append(peers, node)
}
}
return groups.Start(n, groups.Config{
Scope: scope,
Peers: peers,
Select: func(m grpcproc.Member) bool { return trusted(m.Name) },
})
}A conversation takes the group's nodes as it starts each answer, in the order it tries them: those it knew, as it left them, so a node that failed stays at the back, then any that joined since. With none known yet, as for a gateway that has just started, it waits as if every one were busy.
// nodes is the nodes that run a model, as the models group has them now, in
// the order the conversation tries them: those it knew, as it left them,
// then any that joined since. A node that left the group is tried no more.
func (c *conversation) nodes() []string {
var now []string // ordered by node, as Members is
for _, pid := range c.scope.Members(modelsv1.Group) {
now = append(now, pid.Node)
}
now = slices.Compact(now)
left := func(n string) bool { return !slices.Contains(now, n) }
kept := slices.DeleteFunc(slices.Clone(c.models), left)
joined := func(n string) bool { return slices.Contains(kept, n) }
return append(kept, slices.DeleteFunc(now, joined)...)
}With etcd
With a static list, a node is still told every other node's address. With ETCD, the nodes find each other in etcd instead: grpcproc/etcd registers each node's name and LISTEN address under a lease as it starts, resolves the others from there, and reports them coming and going, and PEERS is not read. The platform takes the node's resolver, registrar and membership from it; the services do not change. The etcd guide has the rest.
// cluster is how the node finds the others: the peers the configuration
// names, or, with ETCD, the nodes registered there, which this one joins as
// it starts and leaves as it stops.
type cluster struct {
resolver grpcproc.Resolver
registrar grpcproc.Registrar // nil with a static list
membership grpcproc.Membership // nil with a static list
client *clientv3.Client // etcd's, closed after the node stops
}
func newCluster(cfg Config) (*cluster, error) {
if len(cfg.Etcd) == 0 {
return &cluster{resolver: grpcproc.StaticResolver(cfg.Peers)}, nil
}
client, err := clientv3.New(clientv3.Config{
Endpoints: cfg.Etcd,
DialTimeout: 5 * time.Second,
})
if err != nil {
return nil, err
}
c := grpcprocetcd.New(client, Prefix)
return &cluster{resolver: c, registrar: c, membership: c, client: client}, nil
}The gateway is told nothing of the GPU nodes now. Its scope learns of each from etcd and asks it for its members, so a GPU node started later is asked as soon as it registers, and one that stops leaves the group with its scheduler.
etcd --data-dir /tmp/agents-etcd & # or any etcd you have
export ETCD=127.0.0.1:2379
NODE=gpu-1 LISTEN=127.0.0.1:9102 EXPORT=models UNTRUSTED=sandbox bin/gpu &
NODE=gpu-2 LISTEN=127.0.0.1:9103 EXPORT=models UNTRUSTED=sandbox bin/gpu &
NODE=sandbox LISTEN=127.0.0.1:9104 EXPORT=tools bin/sandbox &
NODE=gateway LISTEN=127.0.0.1:9101 UNTRUSTED=sandbox \
PLACEMENT=tools=sandbox bin/gateway &
sleep 1
curl -d 'what is 6 * 7?' localhost:8080/conversations/1
# one GPU node more, which the gateway finds in etcd
NODE=gpu-3 LISTEN=127.0.0.1:9105 EXPORT=models UNTRUSTED=sandbox bin/gpu &A test runs the same four programs against an etcd in the test process, starts a third GPU node once the first answer is in, and stops the first:
the models group, as the gateway has it: gpu-1, gpu-2
> what is 6 * 7?
Let me calculate that.
[tool] calc 6 * 7
[ran] 42
The answer is 42.
[turn 1 done]
gpu-3 starts: gpu-1, gpu-2, gpu-3
gpu-1 stops: gpu-2, gpu-3