Documentation
etcd
Discovery and membership on etcd: nodes register under a lease, peers resolve them from it, and an expired lease drops the links to a dead node.
grpcproc/etcd implements Resolver, Registrar and Membership (Discovery and membership) on one etcd client. It is a separate module.
Install and wire
go get github.com/floatdrop/grpcproc/etcdimport grpcprocetcd "github.com/floatdrop/grpcproc/etcd"
cluster := grpcprocetcd.New(etcdClient, "/grpcproc/prod")
node, err := grpcproc.NewNode(grpcproc.Config{
Name: "orders-1",
Advertise: "10.0.0.5:9000", // this node's gRPC server, as peers reach it
Metadata: map[string]string{"version": buildVersion},
Resolver: cluster,
Registrar: cluster,
Membership: cluster,
Names: cluster.Names(), // global names, optional
Admit: grpcproc.AdmitTLS(nil),
})
err = node.Start(ctx) // registers; Stop withdrawsNew takes a *clientv3.Client and a prefix; every key the cluster writes is under it, so one etcd serves several clusters under different prefixes. Advertise is required here: it is what the node is registered with, and what peers dial. Give it the address the server listens on as other hosts see it, not 0.0.0.0 or localhost.
One Cluster value serves every node in a program, and whatever reaches nodes through node.Dial: the Inspector forwards to the others through the same registry with no option of its own. A leader elector with no Membership of its own follows the node's.
Options
| Option | What it sets |
|---|---|
WithTTL(d) | The lease TTL: how long a node that stops answering stays registered. Rounded up to whole seconds. Default 10s. |
WithRetry(d) | The pause before registering again after a lost lease, and before watching again after a broken watch. Default 1s. |
WithLogger(l) | The logger. Default slog.Default(). |
The TTL is the detection time for a node that died silently: its peers learn of it once the lease expires, so a short TTL means a faster verdict and more keepalive traffic to etcd. A node that loses its lease while it is still running registers again after the retry interval, so a TTL shorter than an etcd hiccup makes nodes flap in and out of the cluster, and their peers' links with them.
What it does in etcd
| grpcproc role | In etcd |
|---|---|
Registrar | One key per node, <prefix>/nodes/<name>, holding the name, the incarnation and the address, attached to a lease the node keeps alive. If the lease is lost, because etcd was unreachable for longer than the TTL, the node registers again every retry interval until it succeeds, or until it finds a newer incarnation registered. Stop revokes the lease, which removes the key at once. |
Resolver | Reads the key, and answers ErrNotRegistered for a node absent from etcd. |
Membership | Lists the keys, reporting every node up, then watches the prefix: a put is a member up, a delete a member down, with the incarnation taken from the previous value. If the watch breaks, through a compaction or an etcd restart, it lists again and reports what changed in between. |
Restarts
A node that registers a name already present replaces it: a restarted node supersedes its previous incarnation, whose lease may not have expired yet. Its peers see the new incarnation come up and drop their links to the old one, with the cause "restarted as incarnation N".
An older incarnation never replaces a newer one's record, which a compare-and-swap settles. Register fails with ErrSuperseded while a newer one holds the key, and an instance that was replaced, and comes back to etcd after losing its lease, stops registering when it finds a newer incarnation registered, rather than take the name back. Peers that have seen the newer one refuse the old one's links too, as Nodes and links explains. Incarnations must therefore grow with each start: with the default, the start time, the hosts' clocks must agree to within the time between two starts of a node.
Leases and keepalive
grpcproc notices a lost link by itself, as fast as gRPC keepalive allows, or never, for a node that died without closing its connections where keepalive is not configured. Its lease ends after the TTL regardless: every watching node then drops its links, monitors across them fire Down{noconnection}, and pending calls fail with "left the cluster". The lease is the verdict the whole cluster shares; a broken link is one node's view.
Use both. Keepalive is faster and needs no round trip to etcd; set it in the node's DialOptions and on the server, as Running in production shows. The lease catches what keepalive misses, and it is what removes a dead node from the addresses peers resolve.
Global names
cluster.Names() keeps the installation's global names: one key per name, <prefix>/names/<name>, holding its holder's PID, under the lease of the holder's node, so a node that dies takes its names with it as its peers drop their links to it. A claim is a transaction that creates the key; a release deletes it if its create revision is still the claim's, which is also the fencing token. Every node of a program shares one copy of the names, listed a page at a time when the first starts and kept by a watch, so a send to a global name never waits on etcd.
A node's claims are lost once its lease has not been kept alive for three quarters of the TTL: their holders end with name lost before etcd can let the lease end and another node claim the names. Claims made with KeepOnLoss keep their holders running, and are made again together once the node has registered again: a key that outlived the old lease moves to the new one with its revision, and a name another process took meanwhile ends the holder with name conflict.
Operating it
- Detection time is the TTL. A node that dies silently is declared gone when its lease expires; a node that stops cleanly is gone at once, since
Stoprevokes the lease, after its processes'Down{shutdown}notices have reached its peers. - An etcd outage. Links stay up while etcd is unreachable; only registration and the watch pause, and both resume after the retry interval. A lease may expire meanwhile, in which case the node registers again when it can, and its peers see it leave and come back.
- Clocks. The default incarnation is the start time, and a node whose clock is behind the last instance's cannot supersede it. Hosts that run the same node name need clocks that agree to within a restart, or a
Config.Incarnationtaken from something that grows: a deployment counter, an etcd revision. - Names. A node name is a key under the prefix, so it is unique per cluster. Two instances started with the same name are two incarnations of one node, and the newer wins.
The module's tests run against an embedded etcd, with no server to install; etcd_test.go shows the set-up.