Documentation
Discovery and membership
The three interfaces through which a node finds its peers, announces itself, and learns who is alive.
The core ships a static Resolver; grpcproc/etcd implements all three interfaces on etcd leases.
The interfaces
type Resolver interface {
Resolve(ctx context.Context, node string) (addr string, err error)
}
type Member struct {
Name string
Incarnation uint64
Addr string // where peers dial it: its Config.Advertise
Metadata map[string]string // its Config.Metadata: version, zone
}
type Registrar interface {
Register(ctx context.Context, self Member) (withdraw func(context.Context) error, err error)
}
type Membership interface {
Watch(ctx context.Context) (<-chan MemberEvent, error)
}
type MemberEvent struct {
Member Member
Up bool
}| Interface | What it answers |
|---|---|
Resolver | Where does a peer with this name listen? Called before a dial, bounded by Config.DialTimeout. |
Registrar | Publishes this node on Start, keeps it published, and withdraws it on Stop. |
Membership | Who is alive? A stream of members joining and leaving, from Start until Stop. |
Resolver is required; the other two are optional. A node with only a resolver works: it dials peers as it needs them and notices a lost one through its links. The other two are for clusters whose nodes come and go, and for failures the links cannot see.
Resolver
StaticResolver is a map from node name to address, and ResolverFunc adapts a function. Either is enough for a fixed set of programs whose addresses are in their configuration. The tutorial's platform builds one from what the program is told, or, told an etcd, takes grpcproc/etcd's resolver, registrar and membership instead:
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
}Resolve runs once per dial, not per message: a node keeps its stream to a peer open, and asks again only when it has to dial again. It must return once its ctx is done, since that ctx is what bounds a dial. An address a resolver returns is whatever the node's DialOptions can dial: a host and port, or a target a gRPC name resolver understands.
Registrar and metadata
Config.Advertise is the address peers dial to reach this node's gRPC server. The core does not use it to listen, since the server is the application's; it reports it in NodeInfo and hands it to the Registrar as the member's Addr. Peers then resolve this node to it.
Start registers after it has begun watching membership, so that no event is missed between the two, and Stop withdraws last, after the node's processes have exited and their Down notices have reached its peers: peers that saw the node leave the cluster first would report noconnection instead of shutdown. An error from Register must leave the node unpublished, because Start may call it again.
Config.Metadata goes with the member: the application's version, its zone, whatever choosing a node for work decides by. grpcproc reads none of it, and it is fixed for the node's life, so a new version is a new start. A Membership that carries it brings it to every node, where node.Members() lists the members it reports up, each with its metadata, this node included:
node, _ := grpcproc.NewNode(grpcproc.Config{
Name: "orders-3",
Metadata: map[string]string{"version": buildVersion, "zone": "eu-1"},
// Resolver, Registrar, Membership, Admit …
})
// Later: start the child on a node of this zone that runs the new version.
for _, m := range node.Members() {
if m.Metadata["zone"] == "eu-1" && m.Metadata["version"] == buildVersion {
// actor.StartChildFrom(ctx, node, grpcproc.Name{Node: m.Name, Name: "workers"}, "worker", arg)
}
}NodeInfo.Metadata and the Inspector show a node's own, so a rollout can be followed node by node, and the Inspector's GetNode lists the members a node knows, so a tool finds the nodes no link leads to yet. Metadata is what a node says about itself, not what it has proven: authorization decides by credentials, in Config.Admit.
Membership
A node notices a lost link by itself, as fast as gRPC keepalive allows. That signal is local: a peer that dies without closing its connections is noticed only when keepalive gives up, or never if keepalive is not configured, and a broken link does not say whether the peer is gone or only unreachable from here.
Membership is the cluster-wide verdict on top of that. With etcd, it is the peer's lease expiring: every node watching the cluster learns of it at once, whatever its own link was doing. Erlang has the same split, between the net_kernel tick that guards one connection and an external registry that decides who is in.
When the node's watch reports a change, the node acts on it:
- a member down, for the incarnation the node has streams to, drops those streams: monitors across them fire
Down{noconnection}, links end their processes, and pending calls fail withErrNoConnectionand the cause, "left the cluster"; - a member up as a newer incarnation than the one the node has streams to drops them the same way, with the cause "restarted as incarnation N", and the next send dials the new instance;
- a member up as an older incarnation than the node has seen is ignored, as a stream from it would be refused; see incarnations;
- a member up, unless it is an older incarnation than the node has seen, ends any dial backoff for that peer.
A down with incarnation zero means whichever incarnation it was; an up with zero, one not known. The node forgets the newest incarnation it saw of a peer when membership reports that one gone, which is what lets an instance with an older incarnation, or a clock behind the last one's, join again.
etcd
grpcproc/etcd implements the three on one Cluster value: a key per node under a lease the node keeps alive, resolved by reading the key, and membership by listing the keys and then watching the prefix. It is a separate module, and a deployment that wants another registry implements the same three interfaces. The tutorial's platform takes its resolver, registrar and membership from the configuration, and the services see only the node.