Repository navigation
lockstep v0.3.0 lets actors on different processes talk to each other. A PID can point at an actor on another node: messages are encoded with a codec and sent over TCP, and asks and death watch work across nodes. Package cluster adds gossip-based membership with NodeUp and NodeDown signals, and virtual actors (grains) that you address by kind and key instead of by PID. All of it runs in sim too, on the same protocol code, so a seed can partition or crash nodes while messages are in flight and replay the run exactly. The API only adds to v0.2.0, but two changes in behaviour can break existing code; see Breaking changes. The README and Distribution show each feature with an example.
Requirements
- Go 1.26 or later.
- faultline v0.1.2, pinned in
go.modas in v0.2.0. Onlylockstep/simimports it; a program that does not importsimnever links faultline. - tenure v0.2.0 (github.com/hmdsefi/tenure), new in this release. Only
lockstep/clusterimports it, and only itscorepackage, which uses nothing outside the standard library.
Modules and tags
This release tags two modules:
github.com/hmdsefi/lockstepatv0.3.0: the runtime,lockstep/sim,lockstep/persist,lockstep/persist/filestore,lockstep/dedup, and the newlockstep/codec,lockstep/remoteandlockstep/cluster.github.com/hmdsefi/lockstep/codec/protobufatcodec/protobuf/v0.3.0, new: a codec for protocol buffer messages, in its own module so that the root module does not requiregoogle.golang.org/protobuf.
The analysis module has not changed since analysis/v0.2.0 and is not tagged again: install lockstep-vet@v0.2.0. examples/raft, examples/orders and bench are modules of their own, built against the lockstep code in the same checkout. They are not tagged.
Breaking changes
The API only adds to v0.2.0. These two changes are in what existing code does.
Messages between simulated nodes are encoded
In v0.2.0, sim passed a message from one simulated node to another by reference. In v0.3.0, simulated nodes run the same remoting protocol as production, and their frames travel through faultline's network as datagrams. Every message that crosses nodes in a simulation is therefore encoded with the codec in sim.Options.Remote (JSON by default), and its type must be registered:
func init() { codec.Register[Ping]("orders.Ping") }A Send or Ask across nodes whose message type is not registered, or does not encode, returns an error that wraps codec.ErrNotRegistered or the codec's error, and sends nothing. The receiver gets a decoded copy, so a message that held a pointer, a channel or an interface value no longer arrives as the same value. Loss, delay and partitions now apply to the datagrams.
A crash and restart of a simulated node is now a peer restart, as in production: an Ask that is waiting for a reply from that node when it restarts fails with an error matching remote.ErrPeerRestarted, and a death watch of an actor there ends with that error. A test that asked a node right across its restart may need to retry on that error. The README's counter test shows this.
Because of this, a seed that a v0.2.0 test printed does not replay the same run in v0.3.0, and trace hashes differ. Run the test again to get a v0.3.0 seed.
Addresses and PIDs encode as text
Address, PID[M] and ReplyTo[R] now implement encoding.TextMarshaler and encoding.TextUnmarshaler, with forms such as lockstep://n1/user/orders#1a. encoding/json used to write a PID as {}, because its fields are unexported, so a PID in a message or in persisted state was lost. It now writes the text form and reads it back.
JSON written by v0.2.0 that holds {} where a PID or Address is, for example in a persist snapshot or event encoded with persist.JSON, no longer decodes: encoding/json reports that the value must be a string. If you have stored state like that, rewrite those fields before upgrading or decode them into a type without the methods.
Added
Codecs (lockstep/codec)
- A message that crosses nodes travels as a wire name and the bytes of its value.
codec.Register[T](name)maps a type to its wire name in the process's registry,codec.NewRegistryandcodec.RegisterInmake a private one, andNameOf,TypeOfandNameslook mappings up. The mapping is one to one and never changes; misuse panics. codec.JSONencodes withencoding/jsonand writes the same bytes for the same value every time.Registry.Checkreports, before anything is sent, the registered types that the codec cannot encode.lockstep/codec/protobuf(its own module) is a codec for types that implementproto.Message.
Remoting (lockstep/remote)
remote.Enable(sys, cfg)connects aSystembuilt bylockstep.Newto its peers over TCP, optionally with TLS. Sends to an address on another node go through one connection per pair of nodes, which keeps the order of messages from one sender to one receiver.- Connections open with a versioned handshake, are kept alive with heartbeats, and are redialed with a backoff. Peers come from
Config.PeersorConfig.Resolve. Each peer has a bounded outgoing queue, and frames overConfig.MaxFrameare refused withErrFrameTooLarge. lockstep.Askworks across nodes. A failure on the other node comes back as a*remote.ErrorwhoseCodeunwraps to the matching lockstep error, such asErrMailboxFullorErrTerminated.- Death watch works across nodes: the watcher gets
Terminatedwhen the remote actor stops, and when its node restarts, with a failure matchingremote.ErrPeerRestarted. A lost connection does not end a watch; when the connection comes back to the same process, the watch carries on. remote.Lookup[M](ctx, sys, node, path)finds an actor on another node by its path.- Remoting reports its counters and gauges through the
WithMetricsinterface. - In simulation,
remote.Enablein a node's setup replaces that node's protocol settings and ignores the network fields.
Membership (lockstep/cluster)
cluster.Join(sys, cfg)makes aSystema member of a cluster, with a membership actor at/system/cluster. Members probe each other, ask other members to probe a node they cannot reach, suspect a node that answers neither, and declare it dead when nobody refutes the suspicion in time. News travels on the probe traffic, and nodes exchange their member tables now and then so that partitions heal.- A member is its node name plus the ID of the process that runs it, so a restarted process is a new member under the old name.
Membership.Viewreturns an immutable snapshot of the members, in node-name order, with their states:Alive,Suspect,DeadorLeft. - Actors that call
Membership.Subscribegetlockstep.NodeUpandlockstep.NodeDownsignals. A watch of an actor on a node that membership declared down ends withTerminatedat once, without waiting for remoting. Membership.Leaveleaves the cluster on purpose, so the other members mark the nodeLeftinstead of waiting to detect a failure.
Virtual actors (lockstep/cluster)
- A grain is an actor addressed by a kind and a key, for example
("account", "alice"), that exists on demand. A node declares the kinds it hosts incluster.Config.Kindswithcluster.NewKind(name, factory, opts...). cluster.GrainOf[M](m, kind, key)returns aGrain[M];Grain.Sendandcluster.Asksend to it. The owner of a key is chosen by rendezvous hashing over the up members that host its kind (cluster.Owner,cluster.Score), so every node computes the same owner from the same view, and a change of members moves few keys.- A host actor per kind activates a key on its first message, buffers messages while it does, and passivates the activation after
WithIdleTimeoutwithout messages. A message that reaches a node that is not the owner is forwarded, up toConfig.MaxHopstimes. - When members change, the new owner of a key asks the nodes that may still hold it, the previous owner and the runner-up, to stop their activations, and starts its own after they answer or after
WithHandoffTimeout. Once the nodes' views agree and every host has applied its view, no key is in the active set of two nodes.Membership.Host(kind)reports a host's active and stopping keys.
Simulation
sim.Options.Remotesets the remoting settings of every simulated node, including its codec and registry, which are checked once before the first seed.- Simulation tests cover remoting under partitions, loss, restarts and pauses, membership's detection, accuracy and convergence, and grains across crashes, leaves and handovers, including the single-activation property.
Changed
- An
Askfrom outside an actor makes three fewer allocations, a mailbox entry carries its addresses as pointers, and a turn takes each message of the turn in one critical section. examples/raftregisters its cross-node message types.
Known limitations
- Single activation holds only once the nodes' views agree and every host has applied its view. During a partition, or while a change is still spreading, two nodes can each activate the same key. An activation that a change passivated may still be stopping, and after a handoff timeout it can run beside the new one. Use grains for work that tolerates that, or make their effects idempotent.
- A handoff can reorder one sender's messages to a grain: a message that reached the new owner first is delivered before one the old owner forwards later. Messages from one sender keep their order while the owner does not change and nothing is dropped.
- Passivation stops an activation with
lockstep.Stop. Messages still waiting in its mailbox when it stops, during an ownership change or a handover, become dead letters and are not forwarded to the new owner. - A grain message that reaches a host whose mailbox is full is lost without a dead-letter record.
- An actor subscribed to membership that is suspended or restarting when a member changes state does not get that
NodeUporNodeDownlater. - A
Shutdownwhile the membership actor is restarting does not leave the cluster; the other members detect the node as failed instead. - After a node restarts, a message queued for a peer that the old process never reached can be delivered to that peer's next process.
codec/protobufcannot carrycluster's own messages, which are plain Go structs. A node that joins a cluster keepscodec.JSONfor remoting; grain messages can still use protocol buffers throughcluster.Config.Codec.- lockstep cannot preempt a turn. The watchdog reports a stuck turn and isolates its actor, but the turn keeps its worker.
- lockstep ships no tracing or metrics adapter. Tracing across nodes is planned for v0.4.0 (roadmap).
persist/filestorekeeps files on a local disk. Use onefilestore.Storeper directory.- An ask deadlock through a PID built with
PIDAtis not reported as a deadlock; the seed ends by the ask's timeout instead (#299). - Replay from a
schedule.jsonfile reproduces the same failure but not the same trace hash, as in v0.2.0. - The API can change before v1.0.